Simplify bsinc filter storage in the filter state
[openal-soft.git] / Alc / mixer_neon.c
blobdd7e4226b68ab473d61aaef310fb971310cd2a60
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, ALsizei 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 int32x4_t fracMask4 = vdupq_n_s32(FRACTIONMASK);
20 alignas(16) ALint pos_[4];
21 alignas(16) ALsizei frac_[4];
22 int32x4_t pos4;
23 int32x4_t frac4;
24 ALsizei i;
26 InitiatePositionArrays(frac, increment, frac_, pos_, 4);
28 frac4 = vld1q_s32(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_s32(frac4), fracOne4);
39 const float32x4_t out = vmlaq_f32(val1, mu, r0);
41 vst1q_f32(&dst[i], out);
43 frac4 = vaddq_s32(frac4, increment4);
44 pos4 = vaddq_s32(pos4, vshrq_n_s32(frac4, FRACTIONBITS));
45 frac4 = vandq_s32(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_s32(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, ALsizei frac, ALint increment,
71 ALfloat *restrict dst, ALsizei numsamples)
73 const int32x4_t increment4 = vdupq_n_s32(increment*4);
74 const int32x4_t fracMask4 = vdupq_n_s32(FRACTIONMASK);
75 alignas(16) ALint pos_[4];
76 alignas(16) ALsizei frac_[4];
77 int32x4_t pos4;
78 int32x4_t frac4;
79 ALsizei i;
81 InitiatePositionArrays(frac, increment, frac_, pos_, 4);
83 frac4 = vld1q_s32(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(sinc4Tab[frac_[0]]);
94 float32x4_t k1 = vld1q_f32(sinc4Tab[frac_[1]]);
95 float32x4_t k2 = vld1q_f32(sinc4Tab[frac_[2]]);
96 float32x4_t k3 = vld1q_f32(sinc4Tab[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_s32(frac4, increment4);
113 pos4 = vaddq_s32(pos4, vshrq_n_s32(frac4, FRACTIONBITS));
114 frac4 = vandq_s32(frac4, fracMask4);
116 vst1q_s32(pos_, pos4);
117 vst1q_s32(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, ALsizei frac, ALint increment,
141 ALfloat *restrict dst, ALsizei dstlen)
143 const ALfloat *filter = state->bsinc.filter;
144 const float32x4_t sf4 = vdupq_n_f32(state->bsinc.sf);
145 const ALsizei m = state->bsinc.m;
146 const ALfloat *fil, *scd, *phd, *spd;
147 ALsizei pi, i, j;
148 float32x4_t r4;
149 ALfloat pf;
151 src += state->bsinc.l;
152 for(i = 0;i < dstlen;i++)
154 // Calculate the phase index and factor.
155 #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
156 pi = frac >> FRAC_PHASE_BITDIFF;
157 pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
158 #undef FRAC_PHASE_BITDIFF
160 fil = ASSUME_ALIGNED(filter + m*pi*4, 16);
161 scd = ASSUME_ALIGNED(fil + m, 16);
162 phd = ASSUME_ALIGNED(scd + m, 16);
163 spd = ASSUME_ALIGNED(phd + m, 16);
165 // Apply the scale and phase interpolated filter.
166 r4 = vdupq_n_f32(0.0f);
168 const float32x4_t pf4 = vdupq_n_f32(pf);
169 for(j = 0;j < m;j+=4)
171 /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
172 const float32x4_t f4 = vmlaq_f32(vmlaq_f32(vld1q_f32(&fil[j]),
173 sf4, vld1q_f32(&scd[j])),
174 pf4, vmlaq_f32(vld1q_f32(&phd[j]),
175 sf4, vld1q_f32(&spd[j])
178 /* r += f*src */
179 r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[j]));
182 r4 = vaddq_f32(r4, vcombine_f32(vrev64_f32(vget_high_f32(r4)),
183 vrev64_f32(vget_low_f32(r4))));
184 dst[i] = vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0);
186 frac += increment;
187 src += frac>>FRACTIONBITS;
188 frac &= FRACTIONMASK;
190 return dst;
194 static inline void ApplyCoeffs(ALsizei Offset, ALfloat (*restrict Values)[2],
195 const ALsizei IrSize,
196 const ALfloat (*restrict Coeffs)[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 for(c = 0;c < IrSize;c += 2)
211 const ALsizei o0 = (Offset+c)&HRIR_MASK;
212 const ALsizei o1 = (o0+1)&HRIR_MASK;
213 float32x4_t vals = vcombine_f32(vld1_f32((float32_t*)&Values[o0][0]),
214 vld1_f32((float32_t*)&Values[o1][0]));
215 float32x4_t coefs = vld1q_f32((float32_t*)&Coeffs[c][0]);
217 vals = vmlaq_f32(vals, coefs, leftright4);
219 vst1_f32((float32_t*)&Values[o0][0], vget_low_f32(vals));
220 vst1_f32((float32_t*)&Values[o1][0], vget_high_f32(vals));
224 #define MixHrtf MixHrtf_Neon
225 #define MixHrtfBlend MixHrtfBlend_Neon
226 #define MixDirectHrtf MixDirectHrtf_Neon
227 #include "mixer_inc.c"
228 #undef MixHrtf
231 void Mix_Neon(const ALfloat *data, ALsizei OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
232 ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
233 ALsizei BufferSize)
235 ALfloat gain, delta, step;
236 float32x4_t gain4;
237 ALsizei c;
239 data = ASSUME_ALIGNED(data, 16);
240 OutBuffer = ASSUME_ALIGNED(OutBuffer, 16);
242 delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
244 for(c = 0;c < OutChans;c++)
246 ALsizei pos = 0;
247 gain = CurrentGains[c];
248 step = (TargetGains[c] - gain) * delta;
249 if(fabsf(step) > FLT_EPSILON)
251 ALsizei minsize = mini(BufferSize, Counter);
252 /* Mix with applying gain steps in aligned multiples of 4. */
253 if(minsize-pos > 3)
255 float32x4_t step4;
256 gain4 = vsetq_lane_f32(gain, gain4, 0);
257 gain4 = vsetq_lane_f32(gain + step, gain4, 1);
258 gain4 = vsetq_lane_f32(gain + step + step, gain4, 2);
259 gain4 = vsetq_lane_f32(gain + step + step + step, gain4, 3);
260 step4 = vdupq_n_f32(step + step + step + step);
261 do {
262 const float32x4_t val4 = vld1q_f32(&data[pos]);
263 float32x4_t dry4 = vld1q_f32(&OutBuffer[c][OutPos+pos]);
264 dry4 = vmlaq_f32(dry4, val4, gain4);
265 gain4 = vaddq_f32(gain4, step4);
266 vst1q_f32(&OutBuffer[c][OutPos+pos], dry4);
267 pos += 4;
268 } while(minsize-pos > 3);
269 /* NOTE: gain4 now represents the next four gains after the
270 * last four mixed samples, so the lowest element represents
271 * the next gain to apply.
273 gain = vgetq_lane_f32(gain4, 0);
275 /* Mix with applying left over gain steps that aren't aligned multiples of 4. */
276 for(;pos < minsize;pos++)
278 OutBuffer[c][OutPos+pos] += data[pos]*gain;
279 gain += step;
281 if(pos == Counter)
282 gain = TargetGains[c];
283 CurrentGains[c] = gain;
285 /* Mix until pos is aligned with 4 or the mix is done. */
286 minsize = mini(BufferSize, (pos+3)&~3);
287 for(;pos < minsize;pos++)
288 OutBuffer[c][OutPos+pos] += data[pos]*gain;
291 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
292 continue;
293 gain4 = vdupq_n_f32(gain);
294 for(;BufferSize-pos > 3;pos += 4)
296 const float32x4_t val4 = vld1q_f32(&data[pos]);
297 float32x4_t dry4 = vld1q_f32(&OutBuffer[c][OutPos+pos]);
298 dry4 = vmlaq_f32(dry4, val4, gain4);
299 vst1q_f32(&OutBuffer[c][OutPos+pos], dry4);
301 for(;pos < BufferSize;pos++)
302 OutBuffer[c][OutPos+pos] += data[pos]*gain;
306 void MixRow_Neon(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
308 float32x4_t gain4;
309 ALsizei c;
311 data = ASSUME_ALIGNED(data, 16);
312 OutBuffer = ASSUME_ALIGNED(OutBuffer, 16);
314 for(c = 0;c < InChans;c++)
316 ALsizei pos = 0;
317 ALfloat gain = Gains[c];
318 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
319 continue;
321 gain4 = vdupq_n_f32(gain);
322 for(;BufferSize-pos > 3;pos += 4)
324 const float32x4_t val4 = vld1q_f32(&data[c][InPos+pos]);
325 float32x4_t dry4 = vld1q_f32(&OutBuffer[pos]);
326 dry4 = vmlaq_f32(dry4, val4, gain4);
327 vst1q_f32(&OutBuffer[pos], dry4);
329 for(;pos < BufferSize;pos++)
330 OutBuffer[pos] += data[c][InPos+pos]*gain;