diff options
author | Chris Robinson <[email protected]> | 2023-12-21 00:27:39 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2023-12-21 00:27:39 -0800 |
commit | e349b63335e9512a8434d72368520315c7b15197 (patch) | |
tree | d071a8608b1bdfa18bd6f6455c93fe5409ef06e3 /core | |
parent | 10693d306340dfc4c6c7ee7c71aa73a31f0b8ea9 (diff) |
Use a variant instead of a union for InterpState
Diffstat (limited to 'core')
-rw-r--r-- | core/mixer/defs.h | 18 | ||||
-rw-r--r-- | core/mixer/mixer_c.cpp | 66 | ||||
-rw-r--r-- | core/mixer/mixer_neon.cpp | 22 | ||||
-rw-r--r-- | core/mixer/mixer_sse.cpp | 18 | ||||
-rw-r--r-- | core/voice.h | 2 |
5 files changed, 74 insertions, 52 deletions
diff --git a/core/mixer/defs.h b/core/mixer/defs.h index 6e68978c..4d0d19bf 100644 --- a/core/mixer/defs.h +++ b/core/mixer/defs.h @@ -2,7 +2,8 @@ #define CORE_MIXER_DEFS_H #include <array> -#include <stdlib.h> +#include <cstdlib> +#include <variant> #include "alspan.h" #include "core/bufferline.h" @@ -17,12 +18,12 @@ 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}; +inline constexpr int MixerFracBits{16}; +inline constexpr int MixerFracOne{1 << MixerFracBits}; +inline constexpr int MixerFracMask{MixerFracOne - 1}; +inline constexpr int MixerFracHalf{MixerFracOne >> 1}; -constexpr float GainSilenceThreshold{0.00001f}; /* -100dB */ +inline constexpr float GainSilenceThreshold{0.00001f}; /* -100dB */ enum class Resampler : uint8_t { @@ -59,10 +60,7 @@ struct CubicState { const CubicCoefficients *filter; }; -union InterpState { - CubicState cubic; - BsincState bsinc; -}; +using InterpState = std::variant<CubicState,BsincState>; using ResamplerFunc = void(*)(const InterpState *state, const float *RESTRICT src, uint frac, const uint increment, const al::span<float> dst); diff --git a/core/mixer/mixer_c.cpp b/core/mixer/mixer_c.cpp index 0ad1c00e..971297a4 100644 --- a/core/mixer/mixer_c.cpp +++ b/core/mixer/mixer_c.cpp @@ -28,33 +28,33 @@ 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) +inline float do_point(const float *RESTRICT vals, const uint) { return vals[0]; } -inline float do_lerp(const InterpState&, const float *RESTRICT vals, const uint frac) +inline float do_lerp(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) +inline float do_cubic(const CubicState &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.data())}; - const float *RESTRICT phd{al::assume_aligned<16>(istate.cubic.filter[pi].mDeltas.data())}; + const float *RESTRICT fil{al::assume_aligned<16>(istate.filter[pi].mCoeffs.data())}; + const float *RESTRICT phd{al::assume_aligned<16>(istate.filter[pi].mDeltas.data())}; /* 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) +inline float do_bsinc(const BsincState &istate, const float *RESTRICT vals, const uint frac) { - const size_t m{istate.bsinc.m}; + const size_t m{istate.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 fil{istate.filter + m*pi*2}; const float *RESTRICT phd{fil + m}; const float *RESTRICT scd{fil + BSincPhaseCount*2*m}; const float *RESTRICT spd{scd + m}; @@ -62,19 +62,19 @@ inline float do_bsinc(const InterpState &istate, const float *RESTRICT vals, con /* 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]; + r += (fil[j_f] + istate.sf*scd[j_f] + pf*(phd[j_f] + istate.sf*spd[j_f])) * vals[j_f]; return r; } -inline float do_fastbsinc(const InterpState &istate, const float *RESTRICT vals, const uint frac) +inline float do_fastbsinc(const BsincState &istate, const float *RESTRICT vals, const uint frac) { - const size_t m{istate.bsinc.m}; + const size_t m{istate.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 fil{istate.filter + m*pi*2}; const float *RESTRICT phd{fil + m}; /* Apply the phase interpolated filter. */ @@ -84,16 +84,30 @@ inline float do_fastbsinc(const InterpState &istate, const float *RESTRICT vals, return r; } -using SamplerT = float(&)(const InterpState&, const float*RESTRICT, const uint); +using SamplerT = float(&)(const float*RESTRICT, const uint); template<SamplerT Sampler> -void DoResample(const InterpState *state, const float *RESTRICT src, uint frac, +void DoResample(const float *RESTRICT src, uint frac, const uint increment, + const al::span<float> dst) +{ + ASSUME(frac < MixerFracOne); + for(float &out : dst) + { + out = Sampler(src, frac); + + frac += increment; + src += frac>>MixerFracBits; + frac &= MixerFracMask; + } +} + +template<typename T, typename U> +void DoResample(T sampler, const U istate, 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); + out = sampler(istate, src, frac); frac += increment; src += frac>>MixerFracBits; @@ -146,29 +160,35 @@ force_inline void MixLine(const al::span<const float> InSamples, float *RESTRICT } // namespace template<> -void Resample_<PointTag,CTag>(const InterpState *state, const float *RESTRICT src, uint frac, +void Resample_<PointTag,CTag>(const InterpState*, const float *RESTRICT src, uint frac, const uint increment, const al::span<float> dst) -{ DoResample<do_point>(state, src, frac, increment, dst); } +{ DoResample<do_point>(src, frac, increment, dst); } template<> -void Resample_<LerpTag,CTag>(const InterpState *state, const float *RESTRICT src, uint frac, +void Resample_<LerpTag,CTag>(const InterpState*, const float *RESTRICT src, uint frac, const uint increment, const al::span<float> dst) -{ DoResample<do_lerp>(state, src, frac, increment, dst); } +{ DoResample<do_lerp>(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); } +{ DoResample(do_cubic, std::get<CubicState>(*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); } +{ + const auto istate = std::get<BsincState>(*state); + DoResample(do_bsinc, istate, src-istate.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); } +{ + const auto istate = std::get<BsincState>(*state); + DoResample(do_fastbsinc, istate, src-istate.l, frac, increment, dst); +} template<> diff --git a/core/mixer/mixer_neon.cpp b/core/mixer/mixer_neon.cpp index 6e26703b..59369215 100644 --- a/core/mixer/mixer_neon.cpp +++ b/core/mixer/mixer_neon.cpp @@ -196,7 +196,7 @@ void Resample_<CubicTag,NEONTag>(const InterpState *state, const float *RESTRICT { ASSUME(frac < MixerFracOne); - const CubicCoefficients *RESTRICT filter = al::assume_aligned<16>(state->cubic.filter); + const auto *RESTRICT filter = al::assume_aligned<16>(std::get<CubicState>(*state).filter); src -= 1; for(float &out_sample : dst) @@ -226,13 +226,14 @@ 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}; + const auto &bsinc = std::get<BsincState>(*state); + const float *const filter{bsinc.filter}; + const float32x4_t sf4{vdupq_n_f32(bsinc.sf)}; + const size_t m{bsinc.m}; ASSUME(m > 0); ASSUME(frac < MixerFracOne); - src -= state->bsinc.l; + src -= bsinc.l; for(float &out_sample : dst) { // Calculate the phase index and factor. @@ -270,15 +271,16 @@ void Resample_<BSincTag,NEONTag>(const InterpState *state, const float *RESTRICT } template<> -void Resample_<FastBSincTag,NEONTag>(const InterpState *state, const float *RESTRICT src, uint frac, - const uint increment, const al::span<float> dst) +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}; + const auto &bsinc = std::get<BsincState>(*state); + const float *const filter{bsinc.filter}; + const size_t m{bsinc.m}; ASSUME(m > 0); ASSUME(frac < MixerFracOne); - src -= state->bsinc.l; + src -= bsinc.l; for(float &out_sample : dst) { // Calculate the phase index and factor. diff --git a/core/mixer/mixer_sse.cpp b/core/mixer/mixer_sse.cpp index a84230a5..731ed884 100644 --- a/core/mixer/mixer_sse.cpp +++ b/core/mixer/mixer_sse.cpp @@ -159,7 +159,7 @@ void Resample_<CubicTag,SSETag>(const InterpState *state, const float *RESTRICT { ASSUME(frac < MixerFracOne); - const CubicCoefficients *RESTRICT filter = al::assume_aligned<16>(state->cubic.filter); + const auto *RESTRICT filter = al::assume_aligned<16>(std::get<CubicState>(*state).filter); src -= 1; for(float &out_sample : dst) @@ -190,13 +190,14 @@ 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}; + const auto &bsinc = std::get<BsincState>(*state); + const float *const filter{bsinc.filter}; + const __m128 sf4{_mm_set1_ps(bsinc.sf)}; + const size_t m{bsinc.m}; ASSUME(m > 0); ASSUME(frac < MixerFracOne); - src -= state->bsinc.l; + src -= bsinc.l; for(float &out_sample : dst) { // Calculate the phase index and factor. @@ -238,12 +239,13 @@ 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}; + const auto &bsinc = std::get<BsincState>(*state); + const float *const filter{bsinc.filter}; + const size_t m{bsinc.m}; ASSUME(m > 0); ASSUME(frac < MixerFracOne); - src -= state->bsinc.l; + src -= bsinc.l; for(float &out_sample : dst) { // Calculate the phase index and factor. diff --git a/core/voice.h b/core/voice.h index aaf1c5fd..f212ebe5 100644 --- a/core/voice.h +++ b/core/voice.h @@ -48,7 +48,7 @@ enum class DirectMode : unsigned char { }; -constexpr uint MaxPitch{10}; +inline constexpr uint MaxPitch{10}; enum { |