Commit | Line | Data |
---|---|---|
3719602c PC |
1 | /* Copyright (C) 2010-2021 The RetroArch team |
2 | * | |
3 | * --------------------------------------------------------------------------------------- | |
4 | * The following license statement only applies to this file (s16_to_float.c). | |
5 | * --------------------------------------------------------------------------------------- | |
6 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
14 | * | |
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. | |
21 | */ | |
22 | #if defined(__SSE2__) | |
23 | #include <emmintrin.h> | |
24 | #elif defined(__ALTIVEC__) | |
25 | #include <altivec.h> | |
26 | #endif | |
27 | ||
28 | #include <boolean.h> | |
29 | #include <features/features_cpu.h> | |
30 | #include <audio/conversion/s16_to_float.h> | |
31 | ||
32 | #if (defined(__ARM_NEON__) || defined(HAVE_NEON)) | |
33 | static bool s16_to_float_neon_enabled = false; | |
34 | ||
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); | |
39 | #else | |
40 | #include <arm_neon.h> | |
41 | #endif | |
42 | ||
43 | void convert_s16_to_float(float *out, | |
44 | const int16_t *in, size_t samples, float gain) | |
45 | { | |
46 | unsigned i = 0; | |
47 | ||
48 | if (s16_to_float_neon_enabled) | |
49 | { | |
50 | #ifdef HAVE_ARM_NEON_ASM_OPTIMIZATIONS | |
51 | size_t aligned_samples = samples & ~7; | |
52 | if (aligned_samples) | |
53 | convert_s16_float_asm(out, in, aligned_samples, &gain); | |
54 | ||
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; | |
59 | i = 0; | |
60 | #else | |
61 | float gf = gain / (1 << 15); | |
62 | float32x4_t vgf = {gf, gf, gf, gf}; | |
63 | while (samples >= 8) | |
64 | { | |
65 | float32x4x2_t oreg; | |
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); | |
71 | vst2q_f32(out, oreg); | |
72 | in += 8; | |
73 | out += 8; | |
74 | samples -= 8; | |
75 | } | |
76 | #endif | |
77 | } | |
78 | ||
79 | gain /= 0x8000; | |
80 | ||
81 | for (; i < samples; i++) | |
82 | out[i] = (float)in[i] * gain; | |
83 | } | |
84 | ||
85 | void convert_s16_to_float_init_simd(void) | |
86 | { | |
87 | uint64_t cpu = cpu_features_get(); | |
88 | ||
89 | if (cpu & RETRO_SIMD_NEON) | |
90 | s16_to_float_neon_enabled = true; | |
91 | } | |
92 | #else | |
93 | void convert_s16_to_float(float *out, | |
94 | const int16_t *in, size_t samples, float gain) | |
95 | { | |
96 | unsigned i = 0; | |
97 | ||
98 | #if defined(__SSE2__) | |
99 | float fgain = gain / UINT32_C(0x80000000); | |
100 | __m128 factor = _mm_set1_ps(fgain); | |
101 | ||
102 | for (i = 0; i + 8 <= samples; i += 8, in += 8, out += 8) | |
103 | { | |
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); | |
109 | ||
110 | _mm_storeu_ps(out + 0, output_l); | |
111 | _mm_storeu_ps(out + 4, output_r); | |
112 | } | |
113 | ||
114 | samples = samples - i; | |
115 | i = 0; | |
116 | #elif defined(__ALTIVEC__) | |
117 | size_t samples_in = samples; | |
118 | ||
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) | |
122 | { | |
123 | const vector float gain_vec = { gain, gain , gain, gain }; | |
124 | const vector float zero_vec = { 0.0f, 0.0f, 0.0f, 0.0f}; | |
125 | ||
126 | for (i = 0; i + 8 <= samples; i += 8, in += 8, out += 8) | |
127 | { | |
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); | |
133 | ||
134 | vec_st(out_hi, 0, out); | |
135 | vec_st(out_lo, 16, out); | |
136 | } | |
137 | ||
138 | samples_in -= i; | |
139 | } | |
140 | ||
141 | samples = samples_in; | |
142 | i = 0; | |
143 | #endif | |
144 | ||
145 | gain /= 0x8000; | |
146 | ||
147 | #if defined(_MIPS_ARCH_ALLEGREX) | |
148 | #ifdef DEBUG | |
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); | |
153 | #endif | |
154 | ||
155 | __asm__ ( | |
156 | ".set push \n" | |
157 | ".set noreorder \n" | |
158 | "mtv %0, s200 \n" | |
159 | ".set pop \n" | |
160 | ::"r"(gain)); | |
161 | ||
162 | for (i = 0; i + 16 <= samples; i += 16) | |
163 | { | |
164 | __asm__ ( | |
165 | ".set push \n" | |
166 | ".set noreorder \n" | |
167 | ||
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" | |
176 | ||
177 | "vs2i.p c100, c100 \n" | |
178 | "vs2i.p c110, c110 \n" | |
179 | "vs2i.p c120, c120 \n" | |
180 | "vs2i.p c130, c130 \n" | |
181 | ||
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" | |
186 | ||
187 | "vmscl.q e100, e100, s200 \n" | |
188 | ||
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" | |
193 | ||
194 | ".set pop \n" | |
195 | :: "r"(in + i), "r"(out + i)); | |
196 | } | |
197 | #endif | |
198 | ||
199 | for (; i < samples; i++) | |
200 | out[i] = (float)in[i] * gain; | |
201 | } | |
202 | ||
203 | void convert_s16_to_float_init_simd(void) { } | |
204 | #endif | |
205 |