1 /* Copyright (C) 2010-2021 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (s16_to_float.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <emmintrin.h>
24 #elif defined(__ALTIVEC__)
29 #include <features/features_cpu.h>
30 #include <audio/conversion/s16_to_float.h>
32 #if (defined(__ARM_NEON__) || defined(HAVE_NEON))
33 static bool s16_to_float_neon_enabled = false;
35 #ifdef HAVE_ARM_NEON_ASM_OPTIMIZATIONS
36 /* Avoid potential hard-float/soft-float ABI issues. */
37 void convert_s16_float_asm(float *out, const int16_t *in,
38 size_t samples, const float *gain);
43 void convert_s16_to_float(float *out,
44 const int16_t *in, size_t samples, float gain)
48 if (s16_to_float_neon_enabled)
50 #ifdef HAVE_ARM_NEON_ASM_OPTIMIZATIONS
51 size_t aligned_samples = samples & ~7;
53 convert_s16_float_asm(out, in, aligned_samples, &gain);
55 /* Could do all conversion in ASM, but keep it simple for now. */
56 out += aligned_samples;
57 in += aligned_samples;
58 samples -= aligned_samples;
61 float gf = gain / (1 << 15);
62 float32x4_t vgf = {gf, gf, gf, gf};
66 int16x4x2_t inreg = vld2_s16(in);
67 int32x4_t p1 = vmovl_s16(inreg.val[0]);
68 int32x4_t p2 = vmovl_s16(inreg.val[1]);
69 oreg.val[0] = vmulq_f32(vcvtq_f32_s32(p1), vgf);
70 oreg.val[1] = vmulq_f32(vcvtq_f32_s32(p2), vgf);
81 for (; i < samples; i++)
82 out[i] = (float)in[i] * gain;
85 void convert_s16_to_float_init_simd(void)
87 uint64_t cpu = cpu_features_get();
89 if (cpu & RETRO_SIMD_NEON)
90 s16_to_float_neon_enabled = true;
93 void convert_s16_to_float(float *out,
94 const int16_t *in, size_t samples, float gain)
99 float fgain = gain / UINT32_C(0x80000000);
100 __m128 factor = _mm_set1_ps(fgain);
102 for (i = 0; i + 8 <= samples; i += 8, in += 8, out += 8)
104 __m128i input = _mm_loadu_si128((const __m128i *)in);
105 __m128i regs_l = _mm_unpacklo_epi16(_mm_setzero_si128(), input);
106 __m128i regs_r = _mm_unpackhi_epi16(_mm_setzero_si128(), input);
107 __m128 output_l = _mm_mul_ps(_mm_cvtepi32_ps(regs_l), factor);
108 __m128 output_r = _mm_mul_ps(_mm_cvtepi32_ps(regs_r), factor);
110 _mm_storeu_ps(out + 0, output_l);
111 _mm_storeu_ps(out + 4, output_r);
114 samples = samples - i;
116 #elif defined(__ALTIVEC__)
117 size_t samples_in = samples;
119 /* Unaligned loads/store is a bit expensive, so we
120 * optimize for the good path (very likely). */
121 if (((uintptr_t)out & 15) + ((uintptr_t)in & 15) == 0)
123 const vector float gain_vec = { gain, gain , gain, gain };
124 const vector float zero_vec = { 0.0f, 0.0f, 0.0f, 0.0f};
126 for (i = 0; i + 8 <= samples; i += 8, in += 8, out += 8)
128 vector signed short input = vec_ld(0, in);
129 vector signed int hi = vec_unpackh(input);
130 vector signed int lo = vec_unpackl(input);
131 vector float out_hi = vec_madd(vec_ctf(hi, 15), gain_vec, zero_vec);
132 vector float out_lo = vec_madd(vec_ctf(lo, 15), gain_vec, zero_vec);
134 vec_st(out_hi, 0, out);
135 vec_st(out_lo, 16, out);
141 samples = samples_in;
147 #if defined(_MIPS_ARCH_ALLEGREX)
149 /* Make sure the buffer is 16 byte aligned, this should be the
150 * default behaviour of malloc in the PSPSDK.
151 * Only the output buffer can be assumed to be 16-byte aligned. */
152 retro_assert(((uintptr_t)out & 0xf) == 0);
162 for (i = 0; i + 16 <= samples; i += 16)
168 "lv.s s100, 0(%0) \n"
169 "lv.s s101, 4(%0) \n"
170 "lv.s s110, 8(%0) \n"
171 "lv.s s111, 12(%0) \n"
172 "lv.s s120, 16(%0) \n"
173 "lv.s s121, 20(%0) \n"
174 "lv.s s130, 24(%0) \n"
175 "lv.s s131, 28(%0) \n"
177 "vs2i.p c100, c100 \n"
178 "vs2i.p c110, c110 \n"
179 "vs2i.p c120, c120 \n"
180 "vs2i.p c130, c130 \n"
182 "vi2f.q c100, c100, 16 \n"
183 "vi2f.q c110, c110, 16 \n"
184 "vi2f.q c120, c120, 16 \n"
185 "vi2f.q c130, c130, 16 \n"
187 "vmscl.q e100, e100, s200 \n"
189 "sv.q c100, 0(%1) \n"
190 "sv.q c110, 16(%1) \n"
191 "sv.q c120, 32(%1) \n"
192 "sv.q c130, 48(%1) \n"
195 :: "r"(in + i), "r"(out + i));
199 for (; i < samples; i++)
200 out[i] = (float)in[i] * gain;
203 void convert_s16_to_float_init_simd(void) { }