9 #include "core/bsinc_defs.h"
10 #include "core/cubic_defs.h"
20 #if defined(__GNUC__) && !defined(__clang__) && !defined(__SSE__)
21 #pragma GCC target("sse")
26 constexpr uint BSincPhaseDiffBits{MixerFracBits - BSincPhaseBits};
27 constexpr uint BSincPhaseDiffOne{1 << BSincPhaseDiffBits};
28 constexpr uint BSincPhaseDiffMask{BSincPhaseDiffOne - 1u};
30 constexpr uint CubicPhaseDiffBits{MixerFracBits - CubicPhaseBits};
31 constexpr uint CubicPhaseDiffOne{1 << CubicPhaseDiffBits};
32 constexpr uint CubicPhaseDiffMask{CubicPhaseDiffOne - 1u};
34 #define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
36 inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const ConstHrirSpan Coeffs,
37 const float left, const float right)
39 const __m128 lrlr{_mm_setr_ps(left, right, left, right)};
41 ASSUME(IrSize >= MinIrLength);
42 /* This isn't technically correct to test alignment, but it's true for
43 * systems that support SSE, which is the only one that needs to know the
44 * alignment of Values (which alternates between 8- and 16-byte aligned).
46 if(!(reinterpret_cast<uintptr_t>(Values)&15))
48 for(size_t i{0};i < IrSize;i += 2)
50 const __m128 coeffs{_mm_load_ps(Coeffs[i].data())};
51 __m128 vals{_mm_load_ps(Values[i].data())};
52 vals = MLA4(vals, lrlr, coeffs);
53 _mm_store_ps(Values[i].data(), vals);
59 __m128 coeffs{_mm_load_ps(Coeffs[0].data())};
60 __m128 vals{_mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<__m64*>(Values[0].data()))};
61 imp0 = _mm_mul_ps(lrlr, coeffs);
62 vals = _mm_add_ps(imp0, vals);
63 _mm_storel_pi(reinterpret_cast<__m64*>(Values[0].data()), vals);
64 size_t td{((IrSize+1)>>1) - 1};
67 coeffs = _mm_load_ps(Coeffs[i+1].data());
68 vals = _mm_load_ps(Values[i].data());
69 imp1 = _mm_mul_ps(lrlr, coeffs);
70 imp0 = _mm_shuffle_ps(imp0, imp1, _MM_SHUFFLE(1, 0, 3, 2));
71 vals = _mm_add_ps(imp0, vals);
72 _mm_store_ps(Values[i].data(), vals);
76 vals = _mm_loadl_pi(vals, reinterpret_cast<__m64*>(Values[i].data()));
77 imp0 = _mm_movehl_ps(imp0, imp0);
78 vals = _mm_add_ps(imp0, vals);
79 _mm_storel_pi(reinterpret_cast<__m64*>(Values[i].data()), vals);
83 force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT dst,
84 float &CurrentGain, const float TargetGain, const float delta, const size_t min_len,
85 const size_t aligned_len, size_t Counter)
87 float gain{CurrentGain};
88 const float step{(TargetGain-gain) * delta};
91 if(!(std::abs(step) > std::numeric_limits<float>::epsilon()))
95 float step_count{0.0f};
96 /* Mix with applying gain steps in aligned multiples of 4. */
97 if(size_t todo{min_len >> 2})
99 const __m128 four4{_mm_set1_ps(4.0f)};
100 const __m128 step4{_mm_set1_ps(step)};
101 const __m128 gain4{_mm_set1_ps(gain)};
102 __m128 step_count4{_mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f)};
104 const __m128 val4{_mm_load_ps(&InSamples[pos])};
105 __m128 dry4{_mm_load_ps(&dst[pos])};
107 /* dry += val * (gain + step*step_count) */
108 dry4 = MLA4(dry4, val4, MLA4(gain4, step4, step_count4));
110 _mm_store_ps(&dst[pos], dry4);
111 step_count4 = _mm_add_ps(step_count4, four4);
114 /* NOTE: step_count4 now represents the next four counts after the
115 * last four mixed samples, so the lowest element represents the
116 * next step count to apply.
118 step_count = _mm_cvtss_f32(step_count4);
120 /* Mix with applying left over gain steps that aren't aligned multiples of 4. */
121 for(size_t leftover{min_len&3};leftover;++pos,--leftover)
123 dst[pos] += InSamples[pos] * (gain + step*step_count);
129 gain += step*step_count;
131 /* Mix until pos is aligned with 4 or the mix is done. */
132 for(size_t leftover{aligned_len&3};leftover;++pos,--leftover)
133 dst[pos] += InSamples[pos] * gain;
137 if(!(std::abs(gain) > GainSilenceThreshold))
139 if(size_t todo{(InSamples.size()-pos) >> 2})
141 const __m128 gain4{_mm_set1_ps(gain)};
143 const __m128 val4{_mm_load_ps(&InSamples[pos])};
144 __m128 dry4{_mm_load_ps(&dst[pos])};
145 dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
146 _mm_store_ps(&dst[pos], dry4);
150 for(size_t leftover{(InSamples.size()-pos)&3};leftover;++pos,--leftover)
151 dst[pos] += InSamples[pos] * gain;
157 void Resample_<CubicTag,SSETag>(const InterpState *state, const float *RESTRICT src, uint frac,
158 const uint increment, const al::span<float> dst)
160 ASSUME(frac < MixerFracOne);
162 const CubicCoefficients *RESTRICT filter = al::assume_aligned<16>(state->cubic.filter);
165 for(float &out_sample : dst)
167 const uint pi{frac >> CubicPhaseDiffBits};
168 const float pf{static_cast<float>(frac&CubicPhaseDiffMask) * (1.0f/CubicPhaseDiffOne)};
169 const __m128 pf4{_mm_set1_ps(pf)};
171 /* Apply the phase interpolated filter. */
173 /* f = fil + pf*phd */
174 const __m128 f4 = MLA4(_mm_load_ps(filter[pi].mCoeffs), pf4,
175 _mm_load_ps(filter[pi].mDeltas));
177 __m128 r4{_mm_mul_ps(f4, _mm_loadu_ps(src))};
179 r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
180 r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
181 out_sample = _mm_cvtss_f32(r4);
184 src += frac>>MixerFracBits;
185 frac &= MixerFracMask;
190 void Resample_<BSincTag,SSETag>(const InterpState *state, const float *RESTRICT src, uint frac,
191 const uint increment, const al::span<float> dst)
193 const float *const filter{state->bsinc.filter};
194 const __m128 sf4{_mm_set1_ps(state->bsinc.sf)};
195 const size_t m{state->bsinc.m};
197 ASSUME(frac < MixerFracOne);
199 src -= state->bsinc.l;
200 for(float &out_sample : dst)
202 // Calculate the phase index and factor.
203 const uint pi{frac >> BSincPhaseDiffBits};
204 const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)};
206 // Apply the scale and phase interpolated filter.
207 __m128 r4{_mm_setzero_ps()};
209 const __m128 pf4{_mm_set1_ps(pf)};
210 const float *RESTRICT fil{filter + m*pi*2};
211 const float *RESTRICT phd{fil + m};
212 const float *RESTRICT scd{fil + BSincPhaseCount*2*m};
213 const float *RESTRICT spd{scd + m};
218 /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
219 const __m128 f4 = MLA4(
220 MLA4(_mm_load_ps(&fil[j]), sf4, _mm_load_ps(&scd[j])),
221 pf4, MLA4(_mm_load_ps(&phd[j]), sf4, _mm_load_ps(&spd[j])));
223 r4 = MLA4(r4, f4, _mm_loadu_ps(&src[j]));
227 r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
228 r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
229 out_sample = _mm_cvtss_f32(r4);
232 src += frac>>MixerFracBits;
233 frac &= MixerFracMask;
238 void Resample_<FastBSincTag,SSETag>(const InterpState *state, const float *RESTRICT src, uint frac,
239 const uint increment, const al::span<float> dst)
241 const float *const filter{state->bsinc.filter};
242 const size_t m{state->bsinc.m};
244 ASSUME(frac < MixerFracOne);
246 src -= state->bsinc.l;
247 for(float &out_sample : dst)
249 // Calculate the phase index and factor.
250 const uint pi{frac >> BSincPhaseDiffBits};
251 const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)};
253 // Apply the phase interpolated filter.
254 __m128 r4{_mm_setzero_ps()};
256 const __m128 pf4{_mm_set1_ps(pf)};
257 const float *RESTRICT fil{filter + m*pi*2};
258 const float *RESTRICT phd{fil + m};
263 /* f = fil + pf*phd */
264 const __m128 f4 = MLA4(_mm_load_ps(&fil[j]), pf4, _mm_load_ps(&phd[j]));
266 r4 = MLA4(r4, f4, _mm_loadu_ps(&src[j]));
270 r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
271 r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
272 out_sample = _mm_cvtss_f32(r4);
275 src += frac>>MixerFracBits;
276 frac &= MixerFracMask;
282 void MixHrtf_<SSETag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
283 const MixHrtfFilter *hrtfparams, const size_t BufferSize)
284 { MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, BufferSize); }
287 void MixHrtfBlend_<SSETag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
288 const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize)
290 MixHrtfBlendBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, oldparams, newparams,
295 void MixDirectHrtf_<SSETag>(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
296 const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples,
297 float *TempBuf, HrtfChannelState *ChanState, const size_t IrSize, const size_t BufferSize)
299 MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, TempBuf, ChanState,
305 void Mix_<SSETag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
306 float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos)
308 const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
309 const auto min_len = minz(Counter, InSamples.size());
310 const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len;
312 for(FloatBufferLine &output : OutBuffer)
313 MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++,
314 *TargetGains++, delta, min_len, aligned_len, Counter);
318 void Mix_<SSETag>(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain,
319 const float TargetGain, const size_t Counter)
321 const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
322 const auto min_len = minz(Counter, InSamples.size());
323 const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len;
325 MixLine(InSamples, al::assume_aligned<16>(OutBuffer), CurrentGain, TargetGain, delta, min_len,
326 aligned_len, Counter);