Fix NULL pointer dereference
[openal-soft.git] / Alc / mixer_neon.c
blob631e4f7cf5580e4d51572c1573564f1dbe856841
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_lerp_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_bsinc_Neon(const InterpState *state,
70 const ALfloat *restrict src, ALsizei frac, ALint increment,
71 ALfloat *restrict dst, ALsizei dstlen)
73 const ALfloat *const filter = state->bsinc.filter;
74 const float32x4_t sf4 = vdupq_n_f32(state->bsinc.sf);
75 const ALsizei m = state->bsinc.m;
76 const float32x4_t *fil, *scd, *phd, *spd;
77 ALsizei pi, i, j, offset;
78 float32x4_t r4;
79 ALfloat pf;
81 src += state->bsinc.l;
82 for(i = 0;i < dstlen;i++)
84 // Calculate the phase index and factor.
85 #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
86 pi = frac >> FRAC_PHASE_BITDIFF;
87 pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
88 #undef FRAC_PHASE_BITDIFF
90 offset = m*pi*4;
91 fil = ASSUME_ALIGNED(filter + offset, 16); offset += m;
92 scd = ASSUME_ALIGNED(filter + offset, 16); offset += m;
93 phd = ASSUME_ALIGNED(filter + offset, 16); offset += m;
94 spd = ASSUME_ALIGNED(filter + offset, 16);
96 // Apply the scale and phase interpolated filter.
97 r4 = vdupq_n_f32(0.0f);
99 const float32x4_t pf4 = vdupq_n_f32(pf);
100 for(j = 0;j < m;j+=4,fil++,scd++,phd++,spd++)
102 /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
103 const float32x4_t f4 = vmlaq_f32(
104 vmlaq_f32(*fil, sf4, *scd),
105 pf4, vmlaq_f32(*phd, sf4, *spd)
107 /* r += f*src */
108 r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[j]));
111 r4 = vaddq_f32(r4, vcombine_f32(vrev64_f32(vget_high_f32(r4)),
112 vrev64_f32(vget_low_f32(r4))));
113 dst[i] = vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0);
115 frac += increment;
116 src += frac>>FRACTIONBITS;
117 frac &= FRACTIONMASK;
119 return dst;
123 static inline void ApplyCoeffs(ALsizei Offset, ALfloat (*restrict Values)[2],
124 const ALsizei IrSize,
125 const ALfloat (*restrict Coeffs)[2],
126 ALfloat left, ALfloat right)
128 ALsizei c;
129 float32x4_t leftright4;
131 float32x2_t leftright2 = vdup_n_f32(0.0);
132 leftright2 = vset_lane_f32(left, leftright2, 0);
133 leftright2 = vset_lane_f32(right, leftright2, 1);
134 leftright4 = vcombine_f32(leftright2, leftright2);
136 Values = ASSUME_ALIGNED(Values, 16);
137 Coeffs = ASSUME_ALIGNED(Coeffs, 16);
138 for(c = 0;c < IrSize;c += 2)
140 const ALsizei o0 = (Offset+c)&HRIR_MASK;
141 const ALsizei o1 = (o0+1)&HRIR_MASK;
142 float32x4_t vals = vcombine_f32(vld1_f32((float32_t*)&Values[o0][0]),
143 vld1_f32((float32_t*)&Values[o1][0]));
144 float32x4_t coefs = vld1q_f32((float32_t*)&Coeffs[c][0]);
146 vals = vmlaq_f32(vals, coefs, leftright4);
148 vst1_f32((float32_t*)&Values[o0][0], vget_low_f32(vals));
149 vst1_f32((float32_t*)&Values[o1][0], vget_high_f32(vals));
153 #define MixHrtf MixHrtf_Neon
154 #define MixHrtfBlend MixHrtfBlend_Neon
155 #define MixDirectHrtf MixDirectHrtf_Neon
156 #include "mixer_inc.c"
157 #undef MixHrtf
160 void Mix_Neon(const ALfloat *data, ALsizei OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
161 ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
162 ALsizei BufferSize)
164 ALfloat gain, delta, step;
165 float32x4_t gain4;
166 ALsizei c;
168 data = ASSUME_ALIGNED(data, 16);
169 OutBuffer = ASSUME_ALIGNED(OutBuffer, 16);
171 delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
173 for(c = 0;c < OutChans;c++)
175 ALsizei pos = 0;
176 gain = CurrentGains[c];
177 step = (TargetGains[c] - gain) * delta;
178 if(fabsf(step) > FLT_EPSILON)
180 ALsizei minsize = mini(BufferSize, Counter);
181 /* Mix with applying gain steps in aligned multiples of 4. */
182 if(minsize-pos > 3)
184 float32x4_t step4;
185 gain4 = vsetq_lane_f32(gain, gain4, 0);
186 gain4 = vsetq_lane_f32(gain + step, gain4, 1);
187 gain4 = vsetq_lane_f32(gain + step + step, gain4, 2);
188 gain4 = vsetq_lane_f32(gain + step + step + step, gain4, 3);
189 step4 = vdupq_n_f32(step + step + step + step);
190 do {
191 const float32x4_t val4 = vld1q_f32(&data[pos]);
192 float32x4_t dry4 = vld1q_f32(&OutBuffer[c][OutPos+pos]);
193 dry4 = vmlaq_f32(dry4, val4, gain4);
194 gain4 = vaddq_f32(gain4, step4);
195 vst1q_f32(&OutBuffer[c][OutPos+pos], dry4);
196 pos += 4;
197 } while(minsize-pos > 3);
198 /* NOTE: gain4 now represents the next four gains after the
199 * last four mixed samples, so the lowest element represents
200 * the next gain to apply.
202 gain = vgetq_lane_f32(gain4, 0);
204 /* Mix with applying left over gain steps that aren't aligned multiples of 4. */
205 for(;pos < minsize;pos++)
207 OutBuffer[c][OutPos+pos] += data[pos]*gain;
208 gain += step;
210 if(pos == Counter)
211 gain = TargetGains[c];
212 CurrentGains[c] = gain;
214 /* Mix until pos is aligned with 4 or the mix is done. */
215 minsize = mini(BufferSize, (pos+3)&~3);
216 for(;pos < minsize;pos++)
217 OutBuffer[c][OutPos+pos] += data[pos]*gain;
220 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
221 continue;
222 gain4 = vdupq_n_f32(gain);
223 for(;BufferSize-pos > 3;pos += 4)
225 const float32x4_t val4 = vld1q_f32(&data[pos]);
226 float32x4_t dry4 = vld1q_f32(&OutBuffer[c][OutPos+pos]);
227 dry4 = vmlaq_f32(dry4, val4, gain4);
228 vst1q_f32(&OutBuffer[c][OutPos+pos], dry4);
230 for(;pos < BufferSize;pos++)
231 OutBuffer[c][OutPos+pos] += data[pos]*gain;
235 void MixRow_Neon(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
237 float32x4_t gain4;
238 ALsizei c;
240 data = ASSUME_ALIGNED(data, 16);
241 OutBuffer = ASSUME_ALIGNED(OutBuffer, 16);
243 for(c = 0;c < InChans;c++)
245 ALsizei pos = 0;
246 ALfloat gain = Gains[c];
247 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
248 continue;
250 gain4 = vdupq_n_f32(gain);
251 for(;BufferSize-pos > 3;pos += 4)
253 const float32x4_t val4 = vld1q_f32(&data[c][InPos+pos]);
254 float32x4_t dry4 = vld1q_f32(&OutBuffer[pos]);
255 dry4 = vmlaq_f32(dry4, val4, gain4);
256 vst1q_f32(&OutBuffer[pos], dry4);
258 for(;pos < BufferSize;pos++)
259 OutBuffer[pos] += data[c][InPos+pos]*gain;