Start a ALC_SOFT_loopback2 extension
[openal-soft.git] / Alc / mixer_neon.c
blob727c5c5582ff5d5f95ba988eb03c8d3896ac6800
1 #include "config.h"
3 #include <arm_neon.h>
5 #include "AL/al.h"
6 #include "AL/alc.h"
7 #include "alMain.h"
8 #include "alu.h"
9 #include "hrtf.h"
10 #include "mixer_defs.h"
13 const ALfloat *Resample_lerp32_Neon(const InterpState* UNUSED(state),
14 const ALfloat *restrict src, ALuint frac, ALint increment,
15 ALfloat *restrict dst, ALsizei numsamples)
17 const int32x4_t increment4 = vdupq_n_s32(increment*4);
18 const float32x4_t fracOne4 = vdupq_n_f32(1.0f/FRACTIONONE);
19 const uint32x4_t fracMask4 = vdupq_n_u32(FRACTIONMASK);
20 alignas(16) ALint pos_[4];
21 alignas(16) ALuint frac_[4];
22 int32x4_t pos4;
23 uint32x4_t frac4;
24 ALsizei i;
26 InitiatePositionArrays(frac, increment, frac_, pos_, 4);
28 frac4 = vld1q_u32(frac_);
29 pos4 = vld1q_s32(pos_);
31 for(i = 0;numsamples-i > 3;i += 4)
33 const float32x4_t val1 = (float32x4_t){src[pos_[0]], src[pos_[1]], src[pos_[2]], src[pos_[3]]};
34 const float32x4_t val2 = (float32x4_t){src[pos_[0]+1], src[pos_[1]+1], src[pos_[2]+1], src[pos_[3]+1]};
36 /* val1 + (val2-val1)*mu */
37 const float32x4_t r0 = vsubq_f32(val2, val1);
38 const float32x4_t mu = vmulq_f32(vcvtq_f32_u32(frac4), fracOne4);
39 const float32x4_t out = vmlaq_f32(val1, mu, r0);
41 vst1q_f32(&dst[i], out);
43 frac4 = vaddq_u32(frac4, (uint32x4_t)increment4);
44 pos4 = vaddq_s32(pos4, (int32x4_t)vshrq_n_u32(frac4, FRACTIONBITS));
45 frac4 = vandq_u32(frac4, fracMask4);
47 vst1q_s32(pos_, pos4);
50 if(i < numsamples)
52 /* NOTE: These four elements represent the position *after* the last
53 * four samples, so the lowest element is the next position to
54 * resample.
56 ALint pos = pos_[0];
57 frac = vgetq_lane_u32(frac4, 0);
58 do {
59 dst[i] = lerp(src[pos], src[pos+1], frac * (1.0f/FRACTIONONE));
61 frac += increment;
62 pos += frac>>FRACTIONBITS;
63 frac &= FRACTIONMASK;
64 } while(++i < numsamples);
66 return dst;
69 const ALfloat *Resample_fir4_32_Neon(const InterpState* UNUSED(state),
70 const ALfloat *restrict src, ALuint frac, ALint increment,
71 ALfloat *restrict dst, ALsizei numsamples)
73 const int32x4_t increment4 = vdupq_n_s32(increment*4);
74 const uint32x4_t fracMask4 = vdupq_n_u32(FRACTIONMASK);
75 alignas(16) ALint pos_[4];
76 alignas(16) ALuint frac_[4];
77 int32x4_t pos4;
78 uint32x4_t frac4;
79 ALsizei i;
81 InitiatePositionArrays(frac, increment, frac_, pos_, 4);
83 frac4 = vld1q_u32(frac_);
84 pos4 = vld1q_s32(pos_);
86 --src;
87 for(i = 0;numsamples-i > 3;i += 4)
89 const float32x4_t val0 = vld1q_f32(&src[pos_[0]]);
90 const float32x4_t val1 = vld1q_f32(&src[pos_[1]]);
91 const float32x4_t val2 = vld1q_f32(&src[pos_[2]]);
92 const float32x4_t val3 = vld1q_f32(&src[pos_[3]]);
93 float32x4_t k0 = vld1q_f32(ResampleCoeffs_FIR4[frac_[0]]);
94 float32x4_t k1 = vld1q_f32(ResampleCoeffs_FIR4[frac_[1]]);
95 float32x4_t k2 = vld1q_f32(ResampleCoeffs_FIR4[frac_[2]]);
96 float32x4_t k3 = vld1q_f32(ResampleCoeffs_FIR4[frac_[3]]);
97 float32x4_t out;
99 k0 = vmulq_f32(k0, val0);
100 k1 = vmulq_f32(k1, val1);
101 k2 = vmulq_f32(k2, val2);
102 k3 = vmulq_f32(k3, val3);
103 k0 = vcombine_f32(vpadd_f32(vget_low_f32(k0), vget_high_f32(k0)),
104 vpadd_f32(vget_low_f32(k1), vget_high_f32(k1)));
105 k2 = vcombine_f32(vpadd_f32(vget_low_f32(k2), vget_high_f32(k2)),
106 vpadd_f32(vget_low_f32(k3), vget_high_f32(k3)));
107 out = vcombine_f32(vpadd_f32(vget_low_f32(k0), vget_high_f32(k0)),
108 vpadd_f32(vget_low_f32(k2), vget_high_f32(k2)));
110 vst1q_f32(&dst[i], out);
112 frac4 = vaddq_u32(frac4, (uint32x4_t)increment4);
113 pos4 = vaddq_s32(pos4, (int32x4_t)vshrq_n_u32(frac4, FRACTIONBITS));
114 frac4 = vandq_u32(frac4, fracMask4);
116 vst1q_s32(pos_, pos4);
117 vst1q_u32(frac_, frac4);
120 if(i < numsamples)
122 /* NOTE: These four elements represent the position *after* the last
123 * four samples, so the lowest element is the next position to
124 * resample.
126 ALint pos = pos_[0];
127 frac = frac_[0];
128 do {
129 dst[i] = resample_fir4(src[pos], src[pos+1], src[pos+2], src[pos+3], frac);
131 frac += increment;
132 pos += frac>>FRACTIONBITS;
133 frac &= FRACTIONMASK;
134 } while(++i < numsamples);
136 return dst;
139 const ALfloat *Resample_bsinc32_Neon(const InterpState *state,
140 const ALfloat *restrict src, ALuint frac, ALint increment,
141 ALfloat *restrict dst, ALsizei dstlen)
143 const float32x4_t sf4 = vdupq_n_f32(state->bsinc.sf);
144 const ALsizei m = state->bsinc.m;
145 const ALfloat *fil, *scd, *phd, *spd;
146 ALsizei pi, i, j;
147 float32x4_t r4;
148 ALfloat pf;
150 src += state->bsinc.l;
151 for(i = 0;i < dstlen;i++)
153 // Calculate the phase index and factor.
154 #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
155 pi = frac >> FRAC_PHASE_BITDIFF;
156 pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
157 #undef FRAC_PHASE_BITDIFF
159 fil = ASSUME_ALIGNED(state->bsinc.coeffs[pi].filter, 16);
160 scd = ASSUME_ALIGNED(state->bsinc.coeffs[pi].scDelta, 16);
161 phd = ASSUME_ALIGNED(state->bsinc.coeffs[pi].phDelta, 16);
162 spd = ASSUME_ALIGNED(state->bsinc.coeffs[pi].spDelta, 16);
164 // Apply the scale and phase interpolated filter.
165 r4 = vdupq_n_f32(0.0f);
167 const float32x4_t pf4 = vdupq_n_f32(pf);
168 for(j = 0;j < m;j+=4)
170 /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
171 const float32x4_t f4 = vmlaq_f32(vmlaq_f32(vld1q_f32(&fil[j]),
172 sf4, vld1q_f32(&scd[j])),
173 pf4, vmlaq_f32(vld1q_f32(&phd[j]),
174 sf4, vld1q_f32(&spd[j])
177 /* r += f*src */
178 r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[j]));
181 r4 = vaddq_f32(r4, vcombine_f32(vrev64_f32(vget_high_f32(r4)),
182 vrev64_f32(vget_low_f32(r4))));
183 dst[i] = vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0);
185 frac += increment;
186 src += frac>>FRACTIONBITS;
187 frac &= FRACTIONMASK;
189 return dst;
193 static inline void ApplyCoeffsStep(ALsizei Offset, ALfloat (*restrict Values)[2],
194 const ALsizei IrSize,
195 ALfloat (*restrict Coeffs)[2],
196 const ALfloat (*restrict CoeffStep)[2],
197 ALfloat left, ALfloat right)
199 ALsizei c;
200 float32x4_t leftright4;
202 float32x2_t leftright2 = vdup_n_f32(0.0);
203 leftright2 = vset_lane_f32(left, leftright2, 0);
204 leftright2 = vset_lane_f32(right, leftright2, 1);
205 leftright4 = vcombine_f32(leftright2, leftright2);
207 Values = ASSUME_ALIGNED(Values, 16);
208 Coeffs = ASSUME_ALIGNED(Coeffs, 16);
209 CoeffStep = ASSUME_ALIGNED(CoeffStep, 16);
210 for(c = 0;c < IrSize;c += 2)
212 const ALsizei o0 = (Offset+c)&HRIR_MASK;
213 const ALsizei o1 = (o0+1)&HRIR_MASK;
214 float32x4_t vals = vcombine_f32(vld1_f32((float32_t*)&Values[o0][0]),
215 vld1_f32((float32_t*)&Values[o1][0]));
216 float32x4_t coefs = vld1q_f32((float32_t*)&Coeffs[c][0]);
217 float32x4_t deltas = vld1q_f32(&CoeffStep[c][0]);
219 vals = vmlaq_f32(vals, coefs, leftright4);
220 coefs = vaddq_f32(coefs, deltas);
222 vst1_f32((float32_t*)&Values[o0][0], vget_low_f32(vals));
223 vst1_f32((float32_t*)&Values[o1][0], vget_high_f32(vals));
224 vst1q_f32(&Coeffs[c][0], coefs);
228 static inline void ApplyCoeffs(ALsizei Offset, ALfloat (*restrict Values)[2],
229 const ALsizei IrSize,
230 ALfloat (*restrict Coeffs)[2],
231 ALfloat left, ALfloat right)
233 ALsizei c;
234 float32x4_t leftright4;
236 float32x2_t leftright2 = vdup_n_f32(0.0);
237 leftright2 = vset_lane_f32(left, leftright2, 0);
238 leftright2 = vset_lane_f32(right, leftright2, 1);
239 leftright4 = vcombine_f32(leftright2, leftright2);
241 Values = ASSUME_ALIGNED(Values, 16);
242 Coeffs = ASSUME_ALIGNED(Coeffs, 16);
243 for(c = 0;c < IrSize;c += 2)
245 const ALsizei o0 = (Offset+c)&HRIR_MASK;
246 const ALsizei o1 = (o0+1)&HRIR_MASK;
247 float32x4_t vals = vcombine_f32(vld1_f32((float32_t*)&Values[o0][0]),
248 vld1_f32((float32_t*)&Values[o1][0]));
249 float32x4_t coefs = vld1q_f32((float32_t*)&Coeffs[c][0]);
251 vals = vmlaq_f32(vals, coefs, leftright4);
253 vst1_f32((float32_t*)&Values[o0][0], vget_low_f32(vals));
254 vst1_f32((float32_t*)&Values[o1][0], vget_high_f32(vals));
258 #define MixHrtf MixHrtf_Neon
259 #define MixDirectHrtf MixDirectHrtf_Neon
260 #include "mixer_inc.c"
261 #undef MixHrtf
264 void Mix_Neon(const ALfloat *data, ALsizei OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
265 ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
266 ALsizei BufferSize)
268 ALfloat gain, delta, step;
269 float32x4_t gain4;
270 ALsizei c;
272 data = ASSUME_ALIGNED(data, 16);
273 OutBuffer = ASSUME_ALIGNED(OutBuffer, 16);
275 delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
277 for(c = 0;c < OutChans;c++)
279 ALsizei pos = 0;
280 gain = CurrentGains[c];
281 step = (TargetGains[c] - gain) * delta;
282 if(fabsf(step) > FLT_EPSILON)
284 ALsizei minsize = mini(BufferSize, Counter);
285 /* Mix with applying gain steps in aligned multiples of 4. */
286 if(minsize-pos > 3)
288 float32x4_t step4;
289 gain4 = vsetq_lane_f32(gain, gain4, 0);
290 gain4 = vsetq_lane_f32(gain + step, gain4, 1);
291 gain4 = vsetq_lane_f32(gain + step + step, gain4, 2);
292 gain4 = vsetq_lane_f32(gain + step + step + step, gain4, 3);
293 step4 = vdupq_n_f32(step + step + step + step);
294 do {
295 const float32x4_t val4 = vld1q_f32(&data[pos]);
296 float32x4_t dry4 = vld1q_f32(&OutBuffer[c][OutPos+pos]);
297 dry4 = vmlaq_f32(dry4, val4, gain4);
298 gain4 = vaddq_f32(gain4, step4);
299 vst1q_f32(&OutBuffer[c][OutPos+pos], dry4);
300 pos += 4;
301 } while(minsize-pos > 3);
302 /* NOTE: gain4 now represents the next four gains after the
303 * last four mixed samples, so the lowest element represents
304 * the next gain to apply.
306 gain = vgetq_lane_f32(gain4, 0);
308 /* Mix with applying left over gain steps that aren't aligned multiples of 4. */
309 for(;pos < minsize;pos++)
311 OutBuffer[c][OutPos+pos] += data[pos]*gain;
312 gain += step;
314 if(pos == Counter)
315 gain = TargetGains[c];
316 CurrentGains[c] = gain;
318 /* Mix until pos is aligned with 4 or the mix is done. */
319 minsize = mini(BufferSize, (pos+3)&~3);
320 for(;pos < minsize;pos++)
321 OutBuffer[c][OutPos+pos] += data[pos]*gain;
324 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
325 continue;
326 gain4 = vdupq_n_f32(gain);
327 for(;BufferSize-pos > 3;pos += 4)
329 const float32x4_t val4 = vld1q_f32(&data[pos]);
330 float32x4_t dry4 = vld1q_f32(&OutBuffer[c][OutPos+pos]);
331 dry4 = vmlaq_f32(dry4, val4, gain4);
332 vst1q_f32(&OutBuffer[c][OutPos+pos], dry4);
334 for(;pos < BufferSize;pos++)
335 OutBuffer[c][OutPos+pos] += data[pos]*gain;
339 void MixRow_Neon(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
341 float32x4_t gain4;
342 ALsizei c;
344 data = ASSUME_ALIGNED(data, 16);
345 OutBuffer = ASSUME_ALIGNED(OutBuffer, 16);
347 for(c = 0;c < InChans;c++)
349 ALsizei pos = 0;
350 ALfloat gain = Gains[c];
351 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
352 continue;
354 gain4 = vdupq_n_f32(gain);
355 for(;BufferSize-pos > 3;pos += 4)
357 const float32x4_t val4 = vld1q_f32(&data[c][InPos+pos]);
358 float32x4_t dry4 = vld1q_f32(&OutBuffer[pos]);
359 dry4 = vmlaq_f32(dry4, val4, gain4);
360 vst1q_f32(&OutBuffer[pos], dry4);
362 for(;pos < BufferSize;pos++)
363 OutBuffer[pos] += data[c][InPos+pos]*gain;