Add some more 'restrict' keywords
[openal-soft.git] / Alc / mixer_c.c
blob6ef818c7832fd78104fc3b32d7d34764cb161c0c
1 #include "config.h"
3 #include <assert.h>
5 #include "alMain.h"
6 #include "alu.h"
7 #include "alSource.h"
8 #include "alAuxEffectSlot.h"
11 static inline ALfloat point32(const ALfloat *restrict vals, ALuint UNUSED(frac))
12 { return vals[0]; }
13 static inline ALfloat lerp32(const ALfloat *restrict vals, ALuint frac)
14 { return lerp(vals[0], vals[1], frac * (1.0f/FRACTIONONE)); }
15 static inline ALfloat fir4_32(const ALfloat *restrict vals, ALuint frac)
16 { return resample_fir4(vals[-1], vals[0], vals[1], vals[2], frac); }
17 static inline ALfloat fir8_32(const ALfloat *restrict vals, ALuint frac)
18 { return resample_fir8(vals[-3], vals[-2], vals[-1], vals[0], vals[1], vals[2], vals[3], vals[4], frac); }
21 const ALfloat *Resample_copy32_C(const BsincState* UNUSED(state), const ALfloat *restrict src, ALuint UNUSED(frac),
22 ALuint UNUSED(increment), ALfloat *restrict dst, ALuint numsamples)
24 #if defined(HAVE_SSE) || defined(HAVE_NEON)
25 /* Avoid copying the source data if it's aligned like the destination. */
26 if((((intptr_t)src)&15) == (((intptr_t)dst)&15))
27 return src;
28 #endif
29 memcpy(dst, src, numsamples*sizeof(ALfloat));
30 return dst;
33 #define DECL_TEMPLATE(Sampler) \
34 const ALfloat *Resample_##Sampler##_C(const BsincState* UNUSED(state), \
35 const ALfloat *restrict src, ALuint frac, ALuint increment, \
36 ALfloat *restrict dst, ALuint numsamples) \
37 { \
38 ALuint i; \
39 for(i = 0;i < numsamples;i++) \
40 { \
41 dst[i] = Sampler(src, frac); \
43 frac += increment; \
44 src += frac>>FRACTIONBITS; \
45 frac &= FRACTIONMASK; \
46 } \
47 return dst; \
50 DECL_TEMPLATE(point32)
51 DECL_TEMPLATE(lerp32)
52 DECL_TEMPLATE(fir4_32)
53 DECL_TEMPLATE(fir8_32)
55 #undef DECL_TEMPLATE
57 const ALfloat *Resample_bsinc32_C(const BsincState *state, const ALfloat *restrict src,
58 ALuint frac, ALuint increment, ALfloat *restrict dst,
59 ALuint dstlen)
61 const ALfloat *fil, *scd, *phd, *spd;
62 const ALfloat sf = state->sf;
63 const ALuint m = state->m;
64 const ALint l = state->l;
65 ALuint j_f, pi, i;
66 ALfloat pf, r;
67 ALint j_s;
69 for(i = 0;i < dstlen;i++)
71 // Calculate the phase index and factor.
72 #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
73 pi = frac >> FRAC_PHASE_BITDIFF;
74 pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
75 #undef FRAC_PHASE_BITDIFF
77 fil = state->coeffs[pi].filter;
78 scd = state->coeffs[pi].scDelta;
79 phd = state->coeffs[pi].phDelta;
80 spd = state->coeffs[pi].spDelta;
82 // Apply the scale and phase interpolated filter.
83 r = 0.0f;
84 for(j_f = 0,j_s = l;j_f < m;j_f++,j_s++)
85 r += (fil[j_f] + sf*scd[j_f] + pf*(phd[j_f] + sf*spd[j_f])) *
86 src[j_s];
87 dst[i] = r;
89 frac += increment;
90 src += frac>>FRACTIONBITS;
91 frac &= FRACTIONMASK;
93 return dst;
97 void ALfilterState_processC(ALfilterState *filter, ALfloat *restrict dst, const ALfloat *restrict src, ALuint numsamples)
99 ALuint i;
100 if(numsamples > 1)
102 dst[0] = filter->b0 * src[0] +
103 filter->b1 * filter->x[0] +
104 filter->b2 * filter->x[1] -
105 filter->a1 * filter->y[0] -
106 filter->a2 * filter->y[1];
107 dst[1] = filter->b0 * src[1] +
108 filter->b1 * src[0] +
109 filter->b2 * filter->x[0] -
110 filter->a1 * dst[0] -
111 filter->a2 * filter->y[0];
112 for(i = 2;i < numsamples;i++)
113 dst[i] = filter->b0 * src[i] +
114 filter->b1 * src[i-1] +
115 filter->b2 * src[i-2] -
116 filter->a1 * dst[i-1] -
117 filter->a2 * dst[i-2];
118 filter->x[0] = src[i-1];
119 filter->x[1] = src[i-2];
120 filter->y[0] = dst[i-1];
121 filter->y[1] = dst[i-2];
123 else if(numsamples == 1)
125 dst[0] = filter->b0 * src[0] +
126 filter->b1 * filter->x[0] +
127 filter->b2 * filter->x[1] -
128 filter->a1 * filter->y[0] -
129 filter->a2 * filter->y[1];
130 filter->x[1] = filter->x[0];
131 filter->x[0] = src[0];
132 filter->y[1] = filter->y[0];
133 filter->y[0] = dst[0];
138 static inline void ApplyCoeffsStep(ALuint Offset, ALfloat (*restrict Values)[2],
139 const ALuint IrSize,
140 ALfloat (*restrict Coeffs)[2],
141 const ALfloat (*restrict CoeffStep)[2],
142 ALfloat left, ALfloat right)
144 ALuint c;
145 for(c = 0;c < IrSize;c++)
147 const ALuint off = (Offset+c)&HRIR_MASK;
148 Values[off][0] += Coeffs[c][0] * left;
149 Values[off][1] += Coeffs[c][1] * right;
150 Coeffs[c][0] += CoeffStep[c][0];
151 Coeffs[c][1] += CoeffStep[c][1];
155 static inline void ApplyCoeffs(ALuint Offset, ALfloat (*restrict Values)[2],
156 const ALuint IrSize,
157 ALfloat (*restrict Coeffs)[2],
158 ALfloat left, ALfloat right)
160 ALuint c;
161 for(c = 0;c < IrSize;c++)
163 const ALuint off = (Offset+c)&HRIR_MASK;
164 Values[off][0] += Coeffs[c][0] * left;
165 Values[off][1] += Coeffs[c][1] * right;
169 #define MixHrtf MixHrtf_C
170 #define MixDirectHrtf MixDirectHrtf_C
171 #include "mixer_inc.c"
172 #undef MixHrtf
175 void Mix_C(const ALfloat *data, ALuint OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
176 ALfloat *CurrentGains, const ALfloat *TargetGains, ALuint Counter, ALuint OutPos,
177 ALuint BufferSize)
179 ALfloat gain, delta, step;
180 ALuint c;
182 delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
184 for(c = 0;c < OutChans;c++)
186 ALuint pos = 0;
187 gain = CurrentGains[c];
188 step = (TargetGains[c] - gain) * delta;
189 if(fabsf(step) > FLT_EPSILON)
191 ALuint minsize = minu(BufferSize, Counter);
192 for(;pos < minsize;pos++)
194 OutBuffer[c][OutPos+pos] += data[pos]*gain;
195 gain += step;
197 if(pos == Counter)
198 gain = TargetGains[c];
199 CurrentGains[c] = gain;
202 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
203 continue;
204 for(;pos < BufferSize;pos++)
205 OutBuffer[c][OutPos+pos] += data[pos]*gain;
209 /* Basically the inverse of the above. Rather than one input going to multiple
210 * outputs (each with its own gain), it's multiple inputs (each with its own
211 * gain) going to one output. This applies one row (vs one column) of a matrix
212 * transform. And as the matrices are more or less static once set up, no
213 * stepping is necessary.
215 void MixRow_C(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALuint InChans, ALuint InPos, ALuint BufferSize)
217 ALuint c, i;
219 for(c = 0;c < InChans;c++)
221 ALfloat gain = Gains[c];
222 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
223 continue;
225 for(i = 0;i < BufferSize;i++)
226 OutBuffer[i] += data[c][InPos+i] * gain;