diff options
Diffstat (limited to 'core/mixer')
-rw-r--r-- | core/mixer/defs.h | 109 | ||||
-rw-r--r-- | core/mixer/hrtfbase.h | 129 | ||||
-rw-r--r-- | core/mixer/hrtfdefs.h | 53 | ||||
-rw-r--r-- | core/mixer/mixer_c.cpp | 218 | ||||
-rw-r--r-- | core/mixer/mixer_neon.cpp | 362 | ||||
-rw-r--r-- | core/mixer/mixer_sse.cpp | 327 | ||||
-rw-r--r-- | core/mixer/mixer_sse2.cpp | 90 | ||||
-rw-r--r-- | core/mixer/mixer_sse3.cpp | 0 | ||||
-rw-r--r-- | core/mixer/mixer_sse41.cpp | 95 |
9 files changed, 1383 insertions, 0 deletions
diff --git a/core/mixer/defs.h b/core/mixer/defs.h new file mode 100644 index 00000000..48daca9b --- /dev/null +++ b/core/mixer/defs.h @@ -0,0 +1,109 @@ +#ifndef CORE_MIXER_DEFS_H +#define CORE_MIXER_DEFS_H + +#include <array> +#include <stdlib.h> + +#include "alspan.h" +#include "core/bufferline.h" +#include "core/resampler_limits.h" + +struct CubicCoefficients; +struct HrtfChannelState; +struct HrtfFilter; +struct MixHrtfFilter; + +using uint = unsigned int; +using float2 = std::array<float,2>; + + +constexpr int MixerFracBits{16}; +constexpr int MixerFracOne{1 << MixerFracBits}; +constexpr int MixerFracMask{MixerFracOne - 1}; +constexpr int MixerFracHalf{MixerFracOne >> 1}; + +constexpr float GainSilenceThreshold{0.00001f}; /* -100dB */ + + +enum class Resampler : uint8_t { + Point, + Linear, + Cubic, + FastBSinc12, + BSinc12, + FastBSinc24, + BSinc24, + + Max = BSinc24 +}; + +/* Interpolator state. Kind of a misnomer since the interpolator itself is + * stateless. This just keeps it from having to recompute scale-related + * mappings for every sample. + */ +struct BsincState { + float sf; /* Scale interpolation factor. */ + uint m; /* Coefficient count. */ + uint l; /* Left coefficient offset. */ + /* Filter coefficients, followed by the phase, scale, and scale-phase + * delta coefficients. Starting at phase index 0, each subsequent phase + * index follows contiguously. + */ + const float *filter; +}; + +struct CubicState { + /* Filter coefficients, and coefficient deltas. Starting at phase index 0, + * each subsequent phase index follows contiguously. + */ + const CubicCoefficients *filter; +}; + +union InterpState { + CubicState cubic; + BsincState bsinc; +}; + +using ResamplerFunc = void(*)(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst); + +ResamplerFunc PrepareResampler(Resampler resampler, uint increment, InterpState *state); + + +template<typename TypeTag, typename InstTag> +void Resample_(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst); + +template<typename InstTag> +void Mix_(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer, + float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos); +template<typename InstTag> +void Mix_(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain, + const float TargetGain, const size_t Counter); + +template<typename InstTag> +void MixHrtf_(const float *InSamples, float2 *AccumSamples, const uint IrSize, + const MixHrtfFilter *hrtfparams, const size_t BufferSize); +template<typename InstTag> +void MixHrtfBlend_(const float *InSamples, float2 *AccumSamples, const uint IrSize, + const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize); +template<typename InstTag> +void MixDirectHrtf_(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut, + const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples, + float *TempBuf, HrtfChannelState *ChanState, const size_t IrSize, const size_t BufferSize); + +/* Vectorized resampler helpers */ +template<size_t N> +inline void InitPosArrays(uint frac, uint increment, uint (&frac_arr)[N], uint (&pos_arr)[N]) +{ + pos_arr[0] = 0; + frac_arr[0] = frac; + for(size_t i{1};i < N;i++) + { + const uint frac_tmp{frac_arr[i-1] + increment}; + pos_arr[i] = pos_arr[i-1] + (frac_tmp>>MixerFracBits); + frac_arr[i] = frac_tmp&MixerFracMask; + } +} + +#endif /* CORE_MIXER_DEFS_H */ diff --git a/core/mixer/hrtfbase.h b/core/mixer/hrtfbase.h new file mode 100644 index 00000000..36f88e49 --- /dev/null +++ b/core/mixer/hrtfbase.h @@ -0,0 +1,129 @@ +#ifndef CORE_MIXER_HRTFBASE_H +#define CORE_MIXER_HRTFBASE_H + +#include <algorithm> +#include <cmath> + +#include "almalloc.h" +#include "hrtfdefs.h" +#include "opthelpers.h" + + +using uint = unsigned int; + +using ApplyCoeffsT = void(&)(float2 *RESTRICT Values, const size_t irSize, + const ConstHrirSpan Coeffs, const float left, const float right); + +template<ApplyCoeffsT ApplyCoeffs> +inline void MixHrtfBase(const float *InSamples, float2 *RESTRICT AccumSamples, const size_t IrSize, + const MixHrtfFilter *hrtfparams, const size_t BufferSize) +{ + ASSUME(BufferSize > 0); + + const ConstHrirSpan Coeffs{hrtfparams->Coeffs}; + const float gainstep{hrtfparams->GainStep}; + const float gain{hrtfparams->Gain}; + + size_t ldelay{HrtfHistoryLength - hrtfparams->Delay[0]}; + size_t rdelay{HrtfHistoryLength - hrtfparams->Delay[1]}; + float stepcount{0.0f}; + for(size_t i{0u};i < BufferSize;++i) + { + const float g{gain + gainstep*stepcount}; + const float left{InSamples[ldelay++] * g}; + const float right{InSamples[rdelay++] * g}; + ApplyCoeffs(AccumSamples+i, IrSize, Coeffs, left, right); + + stepcount += 1.0f; + } +} + +template<ApplyCoeffsT ApplyCoeffs> +inline void MixHrtfBlendBase(const float *InSamples, float2 *RESTRICT AccumSamples, + const size_t IrSize, const HrtfFilter *oldparams, const MixHrtfFilter *newparams, + const size_t BufferSize) +{ + ASSUME(BufferSize > 0); + + const ConstHrirSpan OldCoeffs{oldparams->Coeffs}; + const float oldGainStep{oldparams->Gain / static_cast<float>(BufferSize)}; + const ConstHrirSpan NewCoeffs{newparams->Coeffs}; + const float newGainStep{newparams->GainStep}; + + if(oldparams->Gain > GainSilenceThreshold) LIKELY + { + size_t ldelay{HrtfHistoryLength - oldparams->Delay[0]}; + size_t rdelay{HrtfHistoryLength - oldparams->Delay[1]}; + auto stepcount = static_cast<float>(BufferSize); + for(size_t i{0u};i < BufferSize;++i) + { + const float g{oldGainStep*stepcount}; + const float left{InSamples[ldelay++] * g}; + const float right{InSamples[rdelay++] * g}; + ApplyCoeffs(AccumSamples+i, IrSize, OldCoeffs, left, right); + + stepcount -= 1.0f; + } + } + + if(newGainStep*static_cast<float>(BufferSize) > GainSilenceThreshold) LIKELY + { + size_t ldelay{HrtfHistoryLength+1 - newparams->Delay[0]}; + size_t rdelay{HrtfHistoryLength+1 - newparams->Delay[1]}; + float stepcount{1.0f}; + for(size_t i{1u};i < BufferSize;++i) + { + const float g{newGainStep*stepcount}; + const float left{InSamples[ldelay++] * g}; + const float right{InSamples[rdelay++] * g}; + ApplyCoeffs(AccumSamples+i, IrSize, NewCoeffs, left, right); + + stepcount += 1.0f; + } + } +} + +template<ApplyCoeffsT ApplyCoeffs> +inline void MixDirectHrtfBase(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut, + const al::span<const FloatBufferLine> InSamples, float2 *RESTRICT AccumSamples, + float *TempBuf, HrtfChannelState *ChanState, const size_t IrSize, const size_t BufferSize) +{ + ASSUME(BufferSize > 0); + + for(const FloatBufferLine &input : InSamples) + { + /* For dual-band processing, the signal needs extra scaling applied to + * the high frequency response. The band-splitter applies this scaling + * with a consistent phase shift regardless of the scale amount. + */ + ChanState->mSplitter.processHfScale({input.data(), BufferSize}, TempBuf, + ChanState->mHfScale); + + /* Now apply the HRIR coefficients to this channel. */ + const float *RESTRICT tempbuf{al::assume_aligned<16>(TempBuf)}; + const ConstHrirSpan Coeffs{ChanState->mCoeffs}; + for(size_t i{0u};i < BufferSize;++i) + { + const float insample{tempbuf[i]}; + ApplyCoeffs(AccumSamples+i, IrSize, Coeffs, insample, insample); + } + + ++ChanState; + } + + /* Add the HRTF signal to the existing "direct" signal. */ + float *RESTRICT left{al::assume_aligned<16>(LeftOut.data())}; + float *RESTRICT right{al::assume_aligned<16>(RightOut.data())}; + for(size_t i{0u};i < BufferSize;++i) + left[i] += AccumSamples[i][0]; + for(size_t i{0u};i < BufferSize;++i) + right[i] += AccumSamples[i][1]; + + /* Copy the new in-progress accumulation values to the front and clear the + * following samples for the next mix. + */ + auto accum_iter = std::copy_n(AccumSamples+BufferSize, HrirLength, AccumSamples); + std::fill_n(accum_iter, BufferSize, float2{}); +} + +#endif /* CORE_MIXER_HRTFBASE_H */ diff --git a/core/mixer/hrtfdefs.h b/core/mixer/hrtfdefs.h new file mode 100644 index 00000000..3c903ed8 --- /dev/null +++ b/core/mixer/hrtfdefs.h @@ -0,0 +1,53 @@ +#ifndef CORE_MIXER_HRTFDEFS_H +#define CORE_MIXER_HRTFDEFS_H + +#include <array> + +#include "alspan.h" +#include "core/ambidefs.h" +#include "core/bufferline.h" +#include "core/filters/splitter.h" + + +using float2 = std::array<float,2>; +using ubyte = unsigned char; +using ubyte2 = std::array<ubyte,2>; +using ushort = unsigned short; +using uint = unsigned int; +using uint2 = std::array<uint,2>; + +constexpr uint HrtfHistoryBits{6}; +constexpr uint HrtfHistoryLength{1 << HrtfHistoryBits}; +constexpr uint HrtfHistoryMask{HrtfHistoryLength - 1}; + +constexpr uint HrirBits{7}; +constexpr uint HrirLength{1 << HrirBits}; +constexpr uint HrirMask{HrirLength - 1}; + +constexpr uint MinIrLength{8}; + +using HrirArray = std::array<float2,HrirLength>; +using HrirSpan = al::span<float2,HrirLength>; +using ConstHrirSpan = al::span<const float2,HrirLength>; + +struct MixHrtfFilter { + const ConstHrirSpan Coeffs; + uint2 Delay; + float Gain; + float GainStep; +}; + +struct HrtfFilter { + alignas(16) HrirArray Coeffs; + uint2 Delay; + float Gain; +}; + + +struct HrtfChannelState { + BandSplitter mSplitter; + float mHfScale{}; + alignas(16) HrirArray mCoeffs{}; +}; + +#endif /* CORE_MIXER_HRTFDEFS_H */ diff --git a/core/mixer/mixer_c.cpp b/core/mixer/mixer_c.cpp new file mode 100644 index 00000000..28a92ef7 --- /dev/null +++ b/core/mixer/mixer_c.cpp @@ -0,0 +1,218 @@ +#include "config.h" + +#include <cassert> +#include <cmath> +#include <limits> + +#include "alnumeric.h" +#include "core/bsinc_defs.h" +#include "core/cubic_defs.h" +#include "defs.h" +#include "hrtfbase.h" + +struct CTag; +struct PointTag; +struct LerpTag; +struct CubicTag; +struct BSincTag; +struct FastBSincTag; + + +namespace { + +constexpr uint BsincPhaseDiffBits{MixerFracBits - BSincPhaseBits}; +constexpr uint BsincPhaseDiffOne{1 << BsincPhaseDiffBits}; +constexpr uint BsincPhaseDiffMask{BsincPhaseDiffOne - 1u}; + +constexpr uint CubicPhaseDiffBits{MixerFracBits - CubicPhaseBits}; +constexpr uint CubicPhaseDiffOne{1 << CubicPhaseDiffBits}; +constexpr uint CubicPhaseDiffMask{CubicPhaseDiffOne - 1u}; + +inline float do_point(const InterpState&, const float *RESTRICT vals, const uint) +{ return vals[0]; } +inline float do_lerp(const InterpState&, const float *RESTRICT vals, const uint frac) +{ return lerpf(vals[0], vals[1], static_cast<float>(frac)*(1.0f/MixerFracOne)); } +inline float do_cubic(const InterpState &istate, const float *RESTRICT vals, const uint frac) +{ + /* Calculate the phase index and factor. */ + const uint pi{frac >> CubicPhaseDiffBits}; + const float pf{static_cast<float>(frac&CubicPhaseDiffMask) * (1.0f/CubicPhaseDiffOne)}; + + const float *RESTRICT fil{al::assume_aligned<16>(istate.cubic.filter[pi].mCoeffs)}; + const float *RESTRICT phd{al::assume_aligned<16>(istate.cubic.filter[pi].mDeltas)}; + + /* Apply the phase interpolated filter. */ + return (fil[0] + pf*phd[0])*vals[0] + (fil[1] + pf*phd[1])*vals[1] + + (fil[2] + pf*phd[2])*vals[2] + (fil[3] + pf*phd[3])*vals[3]; +} +inline float do_bsinc(const InterpState &istate, const float *RESTRICT vals, const uint frac) +{ + const size_t m{istate.bsinc.m}; + ASSUME(m > 0); + + /* Calculate the phase index and factor. */ + const uint pi{frac >> BsincPhaseDiffBits}; + const float pf{static_cast<float>(frac&BsincPhaseDiffMask) * (1.0f/BsincPhaseDiffOne)}; + + const float *RESTRICT fil{istate.bsinc.filter + m*pi*2}; + const float *RESTRICT phd{fil + m}; + const float *RESTRICT scd{fil + BSincPhaseCount*2*m}; + const float *RESTRICT spd{scd + m}; + + /* Apply the scale and phase interpolated filter. */ + float r{0.0f}; + for(size_t j_f{0};j_f < m;j_f++) + r += (fil[j_f] + istate.bsinc.sf*scd[j_f] + pf*(phd[j_f] + istate.bsinc.sf*spd[j_f])) * vals[j_f]; + return r; +} +inline float do_fastbsinc(const InterpState &istate, const float *RESTRICT vals, const uint frac) +{ + const size_t m{istate.bsinc.m}; + ASSUME(m > 0); + + /* Calculate the phase index and factor. */ + const uint pi{frac >> BsincPhaseDiffBits}; + const float pf{static_cast<float>(frac&BsincPhaseDiffMask) * (1.0f/BsincPhaseDiffOne)}; + + const float *RESTRICT fil{istate.bsinc.filter + m*pi*2}; + const float *RESTRICT phd{fil + m}; + + /* Apply the phase interpolated filter. */ + float r{0.0f}; + for(size_t j_f{0};j_f < m;j_f++) + r += (fil[j_f] + pf*phd[j_f]) * vals[j_f]; + return r; +} + +using SamplerT = float(&)(const InterpState&, const float*RESTRICT, const uint); +template<SamplerT Sampler> +void DoResample(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + const InterpState istate{*state}; + ASSUME(frac < MixerFracOne); + for(float &out : dst) + { + out = Sampler(istate, src, frac); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } +} + +inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const ConstHrirSpan Coeffs, + const float left, const float right) +{ + ASSUME(IrSize >= MinIrLength); + for(size_t c{0};c < IrSize;++c) + { + Values[c][0] += Coeffs[c][0] * left; + Values[c][1] += Coeffs[c][1] * right; + } +} + +force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT dst, + float &CurrentGain, const float TargetGain, const float delta, const size_t min_len, + size_t Counter) +{ + float gain{CurrentGain}; + const float step{(TargetGain-gain) * delta}; + + size_t pos{0}; + if(!(std::abs(step) > std::numeric_limits<float>::epsilon())) + gain = TargetGain; + else + { + float step_count{0.0f}; + for(;pos != min_len;++pos) + { + dst[pos] += InSamples[pos] * (gain + step*step_count); + step_count += 1.0f; + } + if(pos == Counter) + gain = TargetGain; + else + gain += step*step_count; + } + CurrentGain = gain; + + if(!(std::abs(gain) > GainSilenceThreshold)) + return; + for(;pos != InSamples.size();++pos) + dst[pos] += InSamples[pos] * gain; +} + +} // namespace + +template<> +void Resample_<PointTag,CTag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ DoResample<do_point>(state, src, frac, increment, dst); } + +template<> +void Resample_<LerpTag,CTag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ DoResample<do_lerp>(state, src, frac, increment, dst); } + +template<> +void Resample_<CubicTag,CTag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ DoResample<do_cubic>(state, src-1, frac, increment, dst); } + +template<> +void Resample_<BSincTag,CTag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ DoResample<do_bsinc>(state, src-state->bsinc.l, frac, increment, dst); } + +template<> +void Resample_<FastBSincTag,CTag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ DoResample<do_fastbsinc>(state, src-state->bsinc.l, frac, increment, dst); } + + +template<> +void MixHrtf_<CTag>(const float *InSamples, float2 *AccumSamples, const uint IrSize, + const MixHrtfFilter *hrtfparams, const size_t BufferSize) +{ MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, BufferSize); } + +template<> +void MixHrtfBlend_<CTag>(const float *InSamples, float2 *AccumSamples, const uint IrSize, + const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize) +{ + MixHrtfBlendBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, oldparams, newparams, + BufferSize); +} + +template<> +void MixDirectHrtf_<CTag>(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut, + const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples, + float *TempBuf, HrtfChannelState *ChanState, const size_t IrSize, const size_t BufferSize) +{ + MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, TempBuf, ChanState, + IrSize, BufferSize); +} + + +template<> +void Mix_<CTag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer, + float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos) +{ + const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f}; + const auto min_len = minz(Counter, InSamples.size()); + + for(FloatBufferLine &output : OutBuffer) + MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++, + *TargetGains++, delta, min_len, Counter); +} + +template<> +void Mix_<CTag>(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain, + const float TargetGain, const size_t Counter) +{ + const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f}; + const auto min_len = minz(Counter, InSamples.size()); + + MixLine(InSamples, al::assume_aligned<16>(OutBuffer), CurrentGain, + TargetGain, delta, min_len, Counter); +} diff --git a/core/mixer/mixer_neon.cpp b/core/mixer/mixer_neon.cpp new file mode 100644 index 00000000..ef2936b3 --- /dev/null +++ b/core/mixer/mixer_neon.cpp @@ -0,0 +1,362 @@ +#include "config.h" + +#include <arm_neon.h> + +#include <cmath> +#include <limits> + +#include "alnumeric.h" +#include "core/bsinc_defs.h" +#include "core/cubic_defs.h" +#include "defs.h" +#include "hrtfbase.h" + +struct NEONTag; +struct LerpTag; +struct CubicTag; +struct BSincTag; +struct FastBSincTag; + + +#if defined(__GNUC__) && !defined(__clang__) && !defined(__ARM_NEON) +#pragma GCC target("fpu=neon") +#endif + +namespace { + +constexpr uint BSincPhaseDiffBits{MixerFracBits - BSincPhaseBits}; +constexpr uint BSincPhaseDiffOne{1 << BSincPhaseDiffBits}; +constexpr uint BSincPhaseDiffMask{BSincPhaseDiffOne - 1u}; + +constexpr uint CubicPhaseDiffBits{MixerFracBits - CubicPhaseBits}; +constexpr uint CubicPhaseDiffOne{1 << CubicPhaseDiffBits}; +constexpr uint CubicPhaseDiffMask{CubicPhaseDiffOne - 1u}; + +inline float32x4_t set_f4(float l0, float l1, float l2, float l3) +{ + float32x4_t ret{vmovq_n_f32(l0)}; + ret = vsetq_lane_f32(l1, ret, 1); + ret = vsetq_lane_f32(l2, ret, 2); + ret = vsetq_lane_f32(l3, ret, 3); + return ret; +} + +inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const ConstHrirSpan Coeffs, + const float left, const float right) +{ + float32x4_t leftright4; + { + float32x2_t leftright2{vmov_n_f32(left)}; + leftright2 = vset_lane_f32(right, leftright2, 1); + leftright4 = vcombine_f32(leftright2, leftright2); + } + + ASSUME(IrSize >= MinIrLength); + for(size_t c{0};c < IrSize;c += 2) + { + float32x4_t vals = vld1q_f32(&Values[c][0]); + float32x4_t coefs = vld1q_f32(&Coeffs[c][0]); + + vals = vmlaq_f32(vals, coefs, leftright4); + + vst1q_f32(&Values[c][0], vals); + } +} + +force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT dst, + float &CurrentGain, const float TargetGain, const float delta, const size_t min_len, + const size_t aligned_len, size_t Counter) +{ + float gain{CurrentGain}; + const float step{(TargetGain-gain) * delta}; + + size_t pos{0}; + if(!(std::abs(step) > std::numeric_limits<float>::epsilon())) + gain = TargetGain; + else + { + float step_count{0.0f}; + /* Mix with applying gain steps in aligned multiples of 4. */ + if(size_t todo{min_len >> 2}) + { + const float32x4_t four4{vdupq_n_f32(4.0f)}; + const float32x4_t step4{vdupq_n_f32(step)}; + const float32x4_t gain4{vdupq_n_f32(gain)}; + float32x4_t step_count4{vdupq_n_f32(0.0f)}; + step_count4 = vsetq_lane_f32(1.0f, step_count4, 1); + step_count4 = vsetq_lane_f32(2.0f, step_count4, 2); + step_count4 = vsetq_lane_f32(3.0f, step_count4, 3); + + do { + const float32x4_t val4 = vld1q_f32(&InSamples[pos]); + float32x4_t dry4 = vld1q_f32(&dst[pos]); + dry4 = vmlaq_f32(dry4, val4, vmlaq_f32(gain4, step4, step_count4)); + step_count4 = vaddq_f32(step_count4, four4); + vst1q_f32(&dst[pos], dry4); + pos += 4; + } while(--todo); + /* NOTE: step_count4 now represents the next four counts after the + * last four mixed samples, so the lowest element represents the + * next step count to apply. + */ + step_count = vgetq_lane_f32(step_count4, 0); + } + /* Mix with applying left over gain steps that aren't aligned multiples of 4. */ + for(size_t leftover{min_len&3};leftover;++pos,--leftover) + { + dst[pos] += InSamples[pos] * (gain + step*step_count); + step_count += 1.0f; + } + if(pos == Counter) + gain = TargetGain; + else + gain += step*step_count; + + /* Mix until pos is aligned with 4 or the mix is done. */ + for(size_t leftover{aligned_len&3};leftover;++pos,--leftover) + dst[pos] += InSamples[pos] * gain; + } + CurrentGain = gain; + + if(!(std::abs(gain) > GainSilenceThreshold)) + return; + if(size_t todo{(InSamples.size()-pos) >> 2}) + { + const float32x4_t gain4 = vdupq_n_f32(gain); + do { + const float32x4_t val4 = vld1q_f32(&InSamples[pos]); + float32x4_t dry4 = vld1q_f32(&dst[pos]); + dry4 = vmlaq_f32(dry4, val4, gain4); + vst1q_f32(&dst[pos], dry4); + pos += 4; + } while(--todo); + } + for(size_t leftover{(InSamples.size()-pos)&3};leftover;++pos,--leftover) + dst[pos] += InSamples[pos] * gain; +} + +} // namespace + +template<> +void Resample_<LerpTag,NEONTag>(const InterpState*, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + ASSUME(frac < MixerFracOne); + + const int32x4_t increment4 = vdupq_n_s32(static_cast<int>(increment*4)); + const float32x4_t fracOne4 = vdupq_n_f32(1.0f/MixerFracOne); + const int32x4_t fracMask4 = vdupq_n_s32(MixerFracMask); + alignas(16) uint pos_[4], frac_[4]; + int32x4_t pos4, frac4; + + InitPosArrays(frac, increment, frac_, pos_); + frac4 = vld1q_s32(reinterpret_cast<int*>(frac_)); + pos4 = vld1q_s32(reinterpret_cast<int*>(pos_)); + + auto dst_iter = dst.begin(); + for(size_t todo{dst.size()>>2};todo;--todo) + { + const int pos0{vgetq_lane_s32(pos4, 0)}; + const int pos1{vgetq_lane_s32(pos4, 1)}; + const int pos2{vgetq_lane_s32(pos4, 2)}; + const int pos3{vgetq_lane_s32(pos4, 3)}; + const float32x4_t val1{set_f4(src[pos0], src[pos1], src[pos2], src[pos3])}; + const float32x4_t val2{set_f4(src[pos0+1], src[pos1+1], src[pos2+1], src[pos3+1])}; + + /* val1 + (val2-val1)*mu */ + const float32x4_t r0{vsubq_f32(val2, val1)}; + const float32x4_t mu{vmulq_f32(vcvtq_f32_s32(frac4), fracOne4)}; + const float32x4_t out{vmlaq_f32(val1, mu, r0)}; + + vst1q_f32(dst_iter, out); + dst_iter += 4; + + frac4 = vaddq_s32(frac4, increment4); + pos4 = vaddq_s32(pos4, vshrq_n_s32(frac4, MixerFracBits)); + frac4 = vandq_s32(frac4, fracMask4); + } + + if(size_t todo{dst.size()&3}) + { + src += static_cast<uint>(vgetq_lane_s32(pos4, 0)); + frac = static_cast<uint>(vgetq_lane_s32(frac4, 0)); + + do { + *(dst_iter++) = lerpf(src[0], src[1], static_cast<float>(frac) * (1.0f/MixerFracOne)); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } while(--todo); + } +} + +template<> +void Resample_<CubicTag,NEONTag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + ASSUME(frac < MixerFracOne); + + const CubicCoefficients *RESTRICT filter = al::assume_aligned<16>(state->cubic.filter); + + src -= 1; + for(float &out_sample : dst) + { + const uint pi{frac >> CubicPhaseDiffBits}; + const float pf{static_cast<float>(frac&CubicPhaseDiffMask) * (1.0f/CubicPhaseDiffOne)}; + const float32x4_t pf4{vdupq_n_f32(pf)}; + + /* Apply the phase interpolated filter. */ + + /* f = fil + pf*phd */ + const float32x4_t f4 = vmlaq_f32(vld1q_f32(filter[pi].mCoeffs), pf4, + vld1q_f32(filter[pi].mDeltas)); + /* r = f*src */ + float32x4_t r4{vmulq_f32(f4, vld1q_f32(src))}; + + r4 = vaddq_f32(r4, vrev64q_f32(r4)); + out_sample = vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } +} + +template<> +void Resample_<BSincTag,NEONTag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + const float *const filter{state->bsinc.filter}; + const float32x4_t sf4{vdupq_n_f32(state->bsinc.sf)}; + const size_t m{state->bsinc.m}; + ASSUME(m > 0); + ASSUME(frac < MixerFracOne); + + src -= state->bsinc.l; + for(float &out_sample : dst) + { + // Calculate the phase index and factor. + const uint pi{frac >> BSincPhaseDiffBits}; + const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)}; + + // Apply the scale and phase interpolated filter. + float32x4_t r4{vdupq_n_f32(0.0f)}; + { + const float32x4_t pf4{vdupq_n_f32(pf)}; + const float *RESTRICT fil{filter + m*pi*2}; + const float *RESTRICT phd{fil + m}; + const float *RESTRICT scd{fil + BSincPhaseCount*2*m}; + const float *RESTRICT spd{scd + m}; + size_t td{m >> 2}; + size_t j{0u}; + + do { + /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */ + const float32x4_t f4 = vmlaq_f32( + vmlaq_f32(vld1q_f32(&fil[j]), sf4, vld1q_f32(&scd[j])), + pf4, vmlaq_f32(vld1q_f32(&phd[j]), sf4, vld1q_f32(&spd[j]))); + /* r += f*src */ + r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[j])); + j += 4; + } while(--td); + } + r4 = vaddq_f32(r4, vrev64q_f32(r4)); + out_sample = vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } +} + +template<> +void Resample_<FastBSincTag,NEONTag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + const float *const filter{state->bsinc.filter}; + const size_t m{state->bsinc.m}; + ASSUME(m > 0); + ASSUME(frac < MixerFracOne); + + src -= state->bsinc.l; + for(float &out_sample : dst) + { + // Calculate the phase index and factor. + const uint pi{frac >> BSincPhaseDiffBits}; + const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)}; + + // Apply the phase interpolated filter. + float32x4_t r4{vdupq_n_f32(0.0f)}; + { + const float32x4_t pf4{vdupq_n_f32(pf)}; + const float *RESTRICT fil{filter + m*pi*2}; + const float *RESTRICT phd{fil + m}; + size_t td{m >> 2}; + size_t j{0u}; + + do { + /* f = fil + pf*phd */ + const float32x4_t f4 = vmlaq_f32(vld1q_f32(&fil[j]), pf4, vld1q_f32(&phd[j])); + /* r += f*src */ + r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[j])); + j += 4; + } while(--td); + } + r4 = vaddq_f32(r4, vrev64q_f32(r4)); + out_sample = vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } +} + + +template<> +void MixHrtf_<NEONTag>(const float *InSamples, float2 *AccumSamples, const uint IrSize, + const MixHrtfFilter *hrtfparams, const size_t BufferSize) +{ MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, BufferSize); } + +template<> +void MixHrtfBlend_<NEONTag>(const float *InSamples, float2 *AccumSamples, const uint IrSize, + const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize) +{ + MixHrtfBlendBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, oldparams, newparams, + BufferSize); +} + +template<> +void MixDirectHrtf_<NEONTag>(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut, + const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples, + float *TempBuf, HrtfChannelState *ChanState, const size_t IrSize, const size_t BufferSize) +{ + MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, TempBuf, ChanState, + IrSize, BufferSize); +} + + +template<> +void Mix_<NEONTag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer, + float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos) +{ + const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f}; + const auto min_len = minz(Counter, InSamples.size()); + const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len; + + for(FloatBufferLine &output : OutBuffer) + MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++, + *TargetGains++, delta, min_len, aligned_len, Counter); +} + +template<> +void Mix_<NEONTag>(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain, + const float TargetGain, const size_t Counter) +{ + const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f}; + const auto min_len = minz(Counter, InSamples.size()); + const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len; + + MixLine(InSamples, al::assume_aligned<16>(OutBuffer), CurrentGain, TargetGain, delta, min_len, + aligned_len, Counter); +} diff --git a/core/mixer/mixer_sse.cpp b/core/mixer/mixer_sse.cpp new file mode 100644 index 00000000..0aa5d5fb --- /dev/null +++ b/core/mixer/mixer_sse.cpp @@ -0,0 +1,327 @@ +#include "config.h" + +#include <xmmintrin.h> + +#include <cmath> +#include <limits> + +#include "alnumeric.h" +#include "core/bsinc_defs.h" +#include "core/cubic_defs.h" +#include "defs.h" +#include "hrtfbase.h" + +struct SSETag; +struct CubicTag; +struct BSincTag; +struct FastBSincTag; + + +#if defined(__GNUC__) && !defined(__clang__) && !defined(__SSE__) +#pragma GCC target("sse") +#endif + +namespace { + +constexpr uint BSincPhaseDiffBits{MixerFracBits - BSincPhaseBits}; +constexpr uint BSincPhaseDiffOne{1 << BSincPhaseDiffBits}; +constexpr uint BSincPhaseDiffMask{BSincPhaseDiffOne - 1u}; + +constexpr uint CubicPhaseDiffBits{MixerFracBits - CubicPhaseBits}; +constexpr uint CubicPhaseDiffOne{1 << CubicPhaseDiffBits}; +constexpr uint CubicPhaseDiffMask{CubicPhaseDiffOne - 1u}; + +#define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z)) + +inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const ConstHrirSpan Coeffs, + const float left, const float right) +{ + const __m128 lrlr{_mm_setr_ps(left, right, left, right)}; + + ASSUME(IrSize >= MinIrLength); + /* This isn't technically correct to test alignment, but it's true for + * systems that support SSE, which is the only one that needs to know the + * alignment of Values (which alternates between 8- and 16-byte aligned). + */ + if(!(reinterpret_cast<uintptr_t>(Values)&15)) + { + for(size_t i{0};i < IrSize;i += 2) + { + const __m128 coeffs{_mm_load_ps(Coeffs[i].data())}; + __m128 vals{_mm_load_ps(Values[i].data())}; + vals = MLA4(vals, lrlr, coeffs); + _mm_store_ps(Values[i].data(), vals); + } + } + else + { + __m128 imp0, imp1; + __m128 coeffs{_mm_load_ps(Coeffs[0].data())}; + __m128 vals{_mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<__m64*>(Values[0].data()))}; + imp0 = _mm_mul_ps(lrlr, coeffs); + vals = _mm_add_ps(imp0, vals); + _mm_storel_pi(reinterpret_cast<__m64*>(Values[0].data()), vals); + size_t td{((IrSize+1)>>1) - 1}; + size_t i{1}; + do { + coeffs = _mm_load_ps(Coeffs[i+1].data()); + vals = _mm_load_ps(Values[i].data()); + imp1 = _mm_mul_ps(lrlr, coeffs); + imp0 = _mm_shuffle_ps(imp0, imp1, _MM_SHUFFLE(1, 0, 3, 2)); + vals = _mm_add_ps(imp0, vals); + _mm_store_ps(Values[i].data(), vals); + imp0 = imp1; + i += 2; + } while(--td); + vals = _mm_loadl_pi(vals, reinterpret_cast<__m64*>(Values[i].data())); + imp0 = _mm_movehl_ps(imp0, imp0); + vals = _mm_add_ps(imp0, vals); + _mm_storel_pi(reinterpret_cast<__m64*>(Values[i].data()), vals); + } +} + +force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT dst, + float &CurrentGain, const float TargetGain, const float delta, const size_t min_len, + const size_t aligned_len, size_t Counter) +{ + float gain{CurrentGain}; + const float step{(TargetGain-gain) * delta}; + + size_t pos{0}; + if(!(std::abs(step) > std::numeric_limits<float>::epsilon())) + gain = TargetGain; + else + { + float step_count{0.0f}; + /* Mix with applying gain steps in aligned multiples of 4. */ + if(size_t todo{min_len >> 2}) + { + const __m128 four4{_mm_set1_ps(4.0f)}; + const __m128 step4{_mm_set1_ps(step)}; + const __m128 gain4{_mm_set1_ps(gain)}; + __m128 step_count4{_mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f)}; + do { + const __m128 val4{_mm_load_ps(&InSamples[pos])}; + __m128 dry4{_mm_load_ps(&dst[pos])}; + + /* dry += val * (gain + step*step_count) */ + dry4 = MLA4(dry4, val4, MLA4(gain4, step4, step_count4)); + + _mm_store_ps(&dst[pos], dry4); + step_count4 = _mm_add_ps(step_count4, four4); + pos += 4; + } while(--todo); + /* NOTE: step_count4 now represents the next four counts after the + * last four mixed samples, so the lowest element represents the + * next step count to apply. + */ + step_count = _mm_cvtss_f32(step_count4); + } + /* Mix with applying left over gain steps that aren't aligned multiples of 4. */ + for(size_t leftover{min_len&3};leftover;++pos,--leftover) + { + dst[pos] += InSamples[pos] * (gain + step*step_count); + step_count += 1.0f; + } + if(pos == Counter) + gain = TargetGain; + else + gain += step*step_count; + + /* Mix until pos is aligned with 4 or the mix is done. */ + for(size_t leftover{aligned_len&3};leftover;++pos,--leftover) + dst[pos] += InSamples[pos] * gain; + } + CurrentGain = gain; + + if(!(std::abs(gain) > GainSilenceThreshold)) + return; + if(size_t todo{(InSamples.size()-pos) >> 2}) + { + const __m128 gain4{_mm_set1_ps(gain)}; + do { + const __m128 val4{_mm_load_ps(&InSamples[pos])}; + __m128 dry4{_mm_load_ps(&dst[pos])}; + dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4)); + _mm_store_ps(&dst[pos], dry4); + pos += 4; + } while(--todo); + } + for(size_t leftover{(InSamples.size()-pos)&3};leftover;++pos,--leftover) + dst[pos] += InSamples[pos] * gain; +} + +} // namespace + +template<> +void Resample_<CubicTag,SSETag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + ASSUME(frac < MixerFracOne); + + const CubicCoefficients *RESTRICT filter = al::assume_aligned<16>(state->cubic.filter); + + src -= 1; + for(float &out_sample : dst) + { + const uint pi{frac >> CubicPhaseDiffBits}; + const float pf{static_cast<float>(frac&CubicPhaseDiffMask) * (1.0f/CubicPhaseDiffOne)}; + const __m128 pf4{_mm_set1_ps(pf)}; + + /* Apply the phase interpolated filter. */ + + /* f = fil + pf*phd */ + const __m128 f4 = MLA4(_mm_load_ps(filter[pi].mCoeffs), pf4, + _mm_load_ps(filter[pi].mDeltas)); + /* r = f*src */ + __m128 r4{_mm_mul_ps(f4, _mm_loadu_ps(src))}; + + r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3))); + r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4)); + out_sample = _mm_cvtss_f32(r4); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } +} + +template<> +void Resample_<BSincTag,SSETag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + const float *const filter{state->bsinc.filter}; + const __m128 sf4{_mm_set1_ps(state->bsinc.sf)}; + const size_t m{state->bsinc.m}; + ASSUME(m > 0); + ASSUME(frac < MixerFracOne); + + src -= state->bsinc.l; + for(float &out_sample : dst) + { + // Calculate the phase index and factor. + const uint pi{frac >> BSincPhaseDiffBits}; + const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)}; + + // Apply the scale and phase interpolated filter. + __m128 r4{_mm_setzero_ps()}; + { + const __m128 pf4{_mm_set1_ps(pf)}; + const float *RESTRICT fil{filter + m*pi*2}; + const float *RESTRICT phd{fil + m}; + const float *RESTRICT scd{fil + BSincPhaseCount*2*m}; + const float *RESTRICT spd{scd + m}; + size_t td{m >> 2}; + size_t j{0u}; + + do { + /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */ + const __m128 f4 = MLA4( + MLA4(_mm_load_ps(&fil[j]), sf4, _mm_load_ps(&scd[j])), + pf4, MLA4(_mm_load_ps(&phd[j]), sf4, _mm_load_ps(&spd[j]))); + /* r += f*src */ + r4 = MLA4(r4, f4, _mm_loadu_ps(&src[j])); + j += 4; + } while(--td); + } + r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3))); + r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4)); + out_sample = _mm_cvtss_f32(r4); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } +} + +template<> +void Resample_<FastBSincTag,SSETag>(const InterpState *state, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + const float *const filter{state->bsinc.filter}; + const size_t m{state->bsinc.m}; + ASSUME(m > 0); + ASSUME(frac < MixerFracOne); + + src -= state->bsinc.l; + for(float &out_sample : dst) + { + // Calculate the phase index and factor. + const uint pi{frac >> BSincPhaseDiffBits}; + const float pf{static_cast<float>(frac&BSincPhaseDiffMask) * (1.0f/BSincPhaseDiffOne)}; + + // Apply the phase interpolated filter. + __m128 r4{_mm_setzero_ps()}; + { + const __m128 pf4{_mm_set1_ps(pf)}; + const float *RESTRICT fil{filter + m*pi*2}; + const float *RESTRICT phd{fil + m}; + size_t td{m >> 2}; + size_t j{0u}; + + do { + /* f = fil + pf*phd */ + const __m128 f4 = MLA4(_mm_load_ps(&fil[j]), pf4, _mm_load_ps(&phd[j])); + /* r += f*src */ + r4 = MLA4(r4, f4, _mm_loadu_ps(&src[j])); + j += 4; + } while(--td); + } + r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3))); + r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4)); + out_sample = _mm_cvtss_f32(r4); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } +} + + +template<> +void MixHrtf_<SSETag>(const float *InSamples, float2 *AccumSamples, const uint IrSize, + const MixHrtfFilter *hrtfparams, const size_t BufferSize) +{ MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, BufferSize); } + +template<> +void MixHrtfBlend_<SSETag>(const float *InSamples, float2 *AccumSamples, const uint IrSize, + const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize) +{ + MixHrtfBlendBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, oldparams, newparams, + BufferSize); +} + +template<> +void MixDirectHrtf_<SSETag>(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut, + const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples, + float *TempBuf, HrtfChannelState *ChanState, const size_t IrSize, const size_t BufferSize) +{ + MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, TempBuf, ChanState, + IrSize, BufferSize); +} + + +template<> +void Mix_<SSETag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer, + float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos) +{ + const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f}; + const auto min_len = minz(Counter, InSamples.size()); + const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len; + + for(FloatBufferLine &output : OutBuffer) + MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++, + *TargetGains++, delta, min_len, aligned_len, Counter); +} + +template<> +void Mix_<SSETag>(const al::span<const float> InSamples, float *OutBuffer, float &CurrentGain, + const float TargetGain, const size_t Counter) +{ + const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f}; + const auto min_len = minz(Counter, InSamples.size()); + const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len; + + MixLine(InSamples, al::assume_aligned<16>(OutBuffer), CurrentGain, TargetGain, delta, min_len, + aligned_len, Counter); +} diff --git a/core/mixer/mixer_sse2.cpp b/core/mixer/mixer_sse2.cpp new file mode 100644 index 00000000..edaaf7a1 --- /dev/null +++ b/core/mixer/mixer_sse2.cpp @@ -0,0 +1,90 @@ +/** + * OpenAL cross platform audio library + * Copyright (C) 2014 by Timothy Arceri <[email protected]>. + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * Or go to http://www.gnu.org/copyleft/lgpl.html + */ + +#include "config.h" + +#include <xmmintrin.h> +#include <emmintrin.h> + +#include "alnumeric.h" +#include "defs.h" + +struct SSE2Tag; +struct LerpTag; + + +#if defined(__GNUC__) && !defined(__clang__) && !defined(__SSE2__) +#pragma GCC target("sse2") +#endif + +template<> +void Resample_<LerpTag,SSE2Tag>(const InterpState*, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + ASSUME(frac < MixerFracOne); + + const __m128i increment4{_mm_set1_epi32(static_cast<int>(increment*4))}; + const __m128 fracOne4{_mm_set1_ps(1.0f/MixerFracOne)}; + const __m128i fracMask4{_mm_set1_epi32(MixerFracMask)}; + + alignas(16) uint pos_[4], frac_[4]; + InitPosArrays(frac, increment, frac_, pos_); + __m128i frac4{_mm_setr_epi32(static_cast<int>(frac_[0]), static_cast<int>(frac_[1]), + static_cast<int>(frac_[2]), static_cast<int>(frac_[3]))}; + __m128i pos4{_mm_setr_epi32(static_cast<int>(pos_[0]), static_cast<int>(pos_[1]), + static_cast<int>(pos_[2]), static_cast<int>(pos_[3]))}; + + auto dst_iter = dst.begin(); + for(size_t todo{dst.size()>>2};todo;--todo) + { + const int pos0{_mm_cvtsi128_si32(pos4)}; + const int pos1{_mm_cvtsi128_si32(_mm_srli_si128(pos4, 4))}; + const int pos2{_mm_cvtsi128_si32(_mm_srli_si128(pos4, 8))}; + const int pos3{_mm_cvtsi128_si32(_mm_srli_si128(pos4, 12))}; + const __m128 val1{_mm_setr_ps(src[pos0 ], src[pos1 ], src[pos2 ], src[pos3 ])}; + const __m128 val2{_mm_setr_ps(src[pos0+1], src[pos1+1], src[pos2+1], src[pos3+1])}; + + /* val1 + (val2-val1)*mu */ + const __m128 r0{_mm_sub_ps(val2, val1)}; + const __m128 mu{_mm_mul_ps(_mm_cvtepi32_ps(frac4), fracOne4)}; + const __m128 out{_mm_add_ps(val1, _mm_mul_ps(mu, r0))}; + + _mm_store_ps(dst_iter, out); + dst_iter += 4; + + frac4 = _mm_add_epi32(frac4, increment4); + pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, MixerFracBits)); + frac4 = _mm_and_si128(frac4, fracMask4); + } + + if(size_t todo{dst.size()&3}) + { + src += static_cast<uint>(_mm_cvtsi128_si32(pos4)); + frac = static_cast<uint>(_mm_cvtsi128_si32(frac4)); + + do { + *(dst_iter++) = lerpf(src[0], src[1], static_cast<float>(frac) * (1.0f/MixerFracOne)); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } while(--todo); + } +} diff --git a/core/mixer/mixer_sse3.cpp b/core/mixer/mixer_sse3.cpp new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/core/mixer/mixer_sse3.cpp diff --git a/core/mixer/mixer_sse41.cpp b/core/mixer/mixer_sse41.cpp new file mode 100644 index 00000000..8ccd9fd3 --- /dev/null +++ b/core/mixer/mixer_sse41.cpp @@ -0,0 +1,95 @@ +/** + * OpenAL cross platform audio library + * Copyright (C) 2014 by Timothy Arceri <[email protected]>. + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * Or go to http://www.gnu.org/copyleft/lgpl.html + */ + +#include "config.h" + +#include <xmmintrin.h> +#include <emmintrin.h> +#include <smmintrin.h> + +#include "alnumeric.h" +#include "defs.h" + +struct SSE4Tag; +struct LerpTag; + + +#if defined(__GNUC__) && !defined(__clang__) && !defined(__SSE4_1__) +#pragma GCC target("sse4.1") +#endif + +template<> +void Resample_<LerpTag,SSE4Tag>(const InterpState*, const float *RESTRICT src, uint frac, + const uint increment, const al::span<float> dst) +{ + ASSUME(frac < MixerFracOne); + + const __m128i increment4{_mm_set1_epi32(static_cast<int>(increment*4))}; + const __m128 fracOne4{_mm_set1_ps(1.0f/MixerFracOne)}; + const __m128i fracMask4{_mm_set1_epi32(MixerFracMask)}; + + alignas(16) uint pos_[4], frac_[4]; + InitPosArrays(frac, increment, frac_, pos_); + __m128i frac4{_mm_setr_epi32(static_cast<int>(frac_[0]), static_cast<int>(frac_[1]), + static_cast<int>(frac_[2]), static_cast<int>(frac_[3]))}; + __m128i pos4{_mm_setr_epi32(static_cast<int>(pos_[0]), static_cast<int>(pos_[1]), + static_cast<int>(pos_[2]), static_cast<int>(pos_[3]))}; + + auto dst_iter = dst.begin(); + for(size_t todo{dst.size()>>2};todo;--todo) + { + const int pos0{_mm_extract_epi32(pos4, 0)}; + const int pos1{_mm_extract_epi32(pos4, 1)}; + const int pos2{_mm_extract_epi32(pos4, 2)}; + const int pos3{_mm_extract_epi32(pos4, 3)}; + const __m128 val1{_mm_setr_ps(src[pos0 ], src[pos1 ], src[pos2 ], src[pos3 ])}; + const __m128 val2{_mm_setr_ps(src[pos0+1], src[pos1+1], src[pos2+1], src[pos3+1])}; + + /* val1 + (val2-val1)*mu */ + const __m128 r0{_mm_sub_ps(val2, val1)}; + const __m128 mu{_mm_mul_ps(_mm_cvtepi32_ps(frac4), fracOne4)}; + const __m128 out{_mm_add_ps(val1, _mm_mul_ps(mu, r0))}; + + _mm_store_ps(dst_iter, out); + dst_iter += 4; + + frac4 = _mm_add_epi32(frac4, increment4); + pos4 = _mm_add_epi32(pos4, _mm_srli_epi32(frac4, MixerFracBits)); + frac4 = _mm_and_si128(frac4, fracMask4); + } + + if(size_t todo{dst.size()&3}) + { + /* NOTE: These four elements represent the position *after* the last + * four samples, so the lowest element is the next position to + * resample. + */ + src += static_cast<uint>(_mm_cvtsi128_si32(pos4)); + frac = static_cast<uint>(_mm_cvtsi128_si32(frac4)); + + do { + *(dst_iter++) = lerpf(src[0], src[1], static_cast<float>(frac) * (1.0f/MixerFracOne)); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } while(--todo); + } +} |