From cdc15a4783e61dd1a838f25dd668e19b28449b19 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 19 Oct 2020 07:55:25 -0700 Subject: [PATCH] Use constexpr variables in place of some macros --- alc/alu.cpp | 8 ++-- alc/bsinc_defs.h | 15 ++++--- alc/bsinc_tables.cpp | 89 ++++++++++++++++++---------------------- alc/bsinc_tables.h | 4 +- alc/mixer/mixer_c.cpp | 12 +++--- alc/mixer/mixer_neon.cpp | 14 +++---- alc/mixer/mixer_sse.cpp | 14 +++---- 7 files changed, 74 insertions(+), 82 deletions(-) diff --git a/alc/alu.cpp b/alc/alu.cpp index bda8b08d..ba44980c 100644 --- a/alc/alu.cpp +++ b/alc/alu.cpp @@ -103,8 +103,8 @@ struct BSincTag; struct FastBSincTag; -static_assert(!(MAX_RESAMPLER_PADDING&1) && MAX_RESAMPLER_PADDING >= BSINC_POINTS_MAX, - "MAX_RESAMPLER_PADDING is not a multiple of two, or is too small"); +static_assert(MAX_RESAMPLER_PADDING >= BSincPointsMax, "MAX_RESAMPLER_PADDING is too small"); +static_assert(!(MAX_RESAMPLER_PADDING&1), "MAX_RESAMPLER_PADDING is not a multiple of two"); namespace { @@ -174,13 +174,13 @@ inline HrtfDirectMixerFunc SelectHrtfMixer(void) inline void BsincPrepare(const ALuint increment, BsincState *state, const BSincTable *table) { - size_t si{BSINC_SCALE_COUNT - 1}; + size_t si{BSincScaleCount - 1}; float sf{0.0f}; if(increment > FRACTIONONE) { sf = FRACTIONONE / static_cast(increment); - sf = maxf(0.0f, (BSINC_SCALE_COUNT-1) * (sf-table->scaleBase) * table->scaleRange); + sf = maxf(0.0f, (BSincScaleCount-1) * (sf-table->scaleBase) * table->scaleRange); si = float2uint(sf); /* The interpolation factor is fit to this diagonally-symmetric curve * to reduce the transition ripple caused by interpolating different diff --git a/alc/bsinc_defs.h b/alc/bsinc_defs.h index 30d1219e..179e025d 100644 --- a/alc/bsinc_defs.h +++ b/alc/bsinc_defs.h @@ -2,12 +2,15 @@ #define BSINC_DEFS_H /* The number of distinct scale and phase intervals within the filter table. */ -#define BSINC_SCALE_BITS 4 -#define BSINC_SCALE_COUNT (1< 21.19) - return (rejection - 7.95) / (order * 2.285 * al::MathDefs::Tau()); + return (rejection - 7.95) / (order * 2.285 * al::MathDefs::Tau()); /* This enforces a minimum rejection of just above 21.18dB */ return 5.79 / (order * al::MathDefs::Tau()); } @@ -102,9 +95,9 @@ constexpr double CalcKaiserWidth(const double rejection, const int order) constexpr double CalcKaiserBeta(const double rejection) { if(rejection > 50.0) - return 0.1102 * (rejection-8.7); + return 0.1102 * (rejection-8.7); else if(rejection >= 21.0) - return (0.5842 * std::pow(rejection-21.0, 0.4)) + (0.07886 * (rejection-21.0)); + return (0.5842 * std::pow(rejection-21.0, 0.4)) + (0.07886 * (rejection-21.0)); return 0.0; } @@ -116,10 +109,10 @@ struct BSincHeader { double scaleRange{}; double besseli_0_beta{}; - int a[BSINC_SCALE_COUNT]{}; - int total_size{}; + uint a[BSincScaleCount]{}; + uint total_size{}; - constexpr BSincHeader(int Rejection, int Order) noexcept + constexpr BSincHeader(uint Rejection, uint Order) noexcept { width = CalcKaiserWidth(Rejection, Order); beta = CalcKaiserBeta(Rejection); @@ -127,15 +120,15 @@ struct BSincHeader { scaleRange = 1.0 - scaleBase; besseli_0_beta = BesselI_0(beta); - int num_points{Order+1}; - for(int si{0};si < BSincScaleCount;++si) + uint num_points{Order+1}; + for(uint si{0};si < BSincScaleCount;++si) { - const double scale{scaleBase + (scaleRange * si / (BSincScaleCount - 1))}; - const int a_{std::min(static_cast(num_points / 2.0 / scale), num_points)}; - const int m{2 * a_}; + const double scale{scaleBase + (scaleRange * si / (BSincScaleCount-1))}; + const uint a_{std::min(static_cast(num_points / 2.0 / scale), num_points)}; + const uint m{2 * a_}; a[si] = a_; - total_size += 4 * BSincPhaseCount * ((m+3) & ~3); + total_size += 4 * BSincPhaseCount * ((m+3) & ~3u); } } }; @@ -164,23 +157,23 @@ struct BSincFilterArray { /* Calculate the Kaiser-windowed Sinc filter coefficients for each * scale and phase index. */ - for(unsigned int si{0};si < BSincScaleCount;++si) + for(uint si{0};si < BSincScaleCount;++si) { - const int m{hdr.a[si] * 2}; - const int o{BSincPointsHalf - (m/2)}; - const int l{hdr.a[si] - 1}; - const int a{hdr.a[si]}; - const double scale{hdr.scaleBase + (hdr.scaleRange * si / (BSincScaleCount - 1))}; + const uint m{hdr.a[si] * 2}; + const size_t o{(BSincPointsMax-m) / 2}; + const double scale{hdr.scaleBase + (hdr.scaleRange * si / (BSincScaleCount-1))}; const double cutoff{scale - (hdr.scaleBase * std::max(0.5, scale) * 2.0)}; + const auto a = static_cast(hdr.a[si]); + const double l{a - 1.0}; /* Do one extra phase index so that the phase delta has a proper * target for its last index. */ - for(int pi{0};pi <= BSincPhaseCount;++pi) + for(uint pi{0};pi <= BSincPhaseCount;++pi) { const double phase{l + (pi/double{BSincPhaseCount})}; - for(int i{0};i < m;++i) + for(uint i{0};i < m;++i) { const double x{i - phase}; filter[si][pi][o+i] = Kaiser(hdr.beta, x/a, hdr.besseli_0_beta) * cutoff * @@ -190,23 +183,23 @@ struct BSincFilterArray { } size_t idx{0}; - for(unsigned int si{0};si < BSincScaleCount-1;++si) + for(size_t si{0};si < BSincScaleCount-1;++si) { - const int m{((hdr.a[si]*2) + 3) & ~3}; - const int o{BSincPointsHalf - (m/2)}; + const size_t m{((hdr.a[si]*2) + 3) & ~3u}; + const size_t o{(BSincPointsMax-m) / 2}; - for(int pi{0};pi < BSincPhaseCount;++pi) + for(size_t pi{0};pi < BSincPhaseCount;++pi) { /* Write out the filter. Also calculate and write out the phase * and scale deltas. */ - for(int i{0};i < m;++i) + for(size_t i{0};i < m;++i) mTable[idx++] = static_cast(filter[si][pi][o+i]); /* Linear interpolation between phases is simplified by pre- * calculating the delta (b - a) in: x = a + f (b - a) */ - for(int i{0};i < m;++i) + for(size_t i{0};i < m;++i) { const double phDelta{filter[si][pi+1][o+i] - filter[si][pi][o+i]}; mTable[idx++] = static_cast(phDelta); @@ -217,7 +210,7 @@ struct BSincFilterArray { * Given a difference in points between scales, the destination * points will be 0, thus: x = a + f (-a) */ - for(int i{0};i < m;++i) + for(size_t i{0};i < m;++i) { const double scDelta{filter[si+1][pi][o+i] - filter[si][pi][o+i]}; mTable[idx++] = static_cast(scDelta); @@ -226,7 +219,7 @@ struct BSincFilterArray { /* This last simplification is done to complete the bilinear * equation for the combination of phase and scale. */ - for(int i{0};i < m;++i) + for(size_t i{0};i < m;++i) { const double spDelta{(filter[si+1][pi+1][o+i] - filter[si+1][pi][o+i]) - (filter[si][pi+1][o+i] - filter[si][pi][o+i])}; @@ -238,22 +231,22 @@ struct BSincFilterArray { /* The last scale index doesn't have any scale or scale-phase * deltas. */ - const unsigned int si{BSincScaleCount - 1}; - const int m{((hdr.a[si]*2) + 3) & ~3}; - const int o{BSincPointsHalf - (m/2)}; + constexpr size_t si{BSincScaleCount-1}; + const size_t m{((hdr.a[si]*2) + 3) & ~3u}; + const size_t o{(BSincPointsMax-m) / 2}; - for(int pi{0};pi < BSincPhaseCount;++pi) + for(size_t pi{0};pi < BSincPhaseCount;++pi) { - for(int i{0};i < m;++i) + for(size_t i{0};i < m;++i) mTable[idx++] = static_cast(filter[si][pi][o+i]); - for(int i{0};i < m;++i) + for(size_t i{0};i < m;++i) { const double phDelta{filter[si][pi+1][o+i] - filter[si][pi][o+i]}; mTable[idx++] = static_cast(phDelta); } - for(int i{0};i < m;++i) + for(size_t i{0};i < m;++i) mTable[idx++] = 0.0f; - for(int i{0};i < m;++i) + for(size_t i{0};i < m;++i) mTable[idx++] = 0.0f; } } @@ -273,10 +266,10 @@ constexpr BSincTable GenerateBSincTable(const BSincHeader &hdr, const float *tab BSincTable ret{}; ret.scaleBase = static_cast(hdr.scaleBase); ret.scaleRange = static_cast(1.0 / hdr.scaleRange); - for(int i{0};i < BSincScaleCount;++i) - ret.m[i] = static_cast(((hdr.a[i]*2) + 3) & ~3); + for(size_t i{0};i < BSincScaleCount;++i) + ret.m[i] = ((hdr.a[i]*2) + 3) & ~3u; ret.filterOffset[0] = 0; - for(int i{1};i < BSincScaleCount;++i) + for(size_t i{1};i < BSincScaleCount;++i) ret.filterOffset[i] = ret.filterOffset[i-1] + ret.m[i-1]*4*BSincPhaseCount; ret.Tab = tab; return ret; diff --git a/alc/bsinc_tables.h b/alc/bsinc_tables.h index 2e2443e5..8e37336d 100644 --- a/alc/bsinc_tables.h +++ b/alc/bsinc_tables.h @@ -6,8 +6,8 @@ struct BSincTable { float scaleBase, scaleRange; - unsigned int m[BSINC_SCALE_COUNT]; - unsigned int filterOffset[BSINC_SCALE_COUNT]; + unsigned int m[BSincScaleCount]; + unsigned int filterOffset[BSincScaleCount]; const float *Tab; }; diff --git a/alc/mixer/mixer_c.cpp b/alc/mixer/mixer_c.cpp index b665b0a5..af8ff3b1 100644 --- a/alc/mixer/mixer_c.cpp +++ b/alc/mixer/mixer_c.cpp @@ -21,8 +21,8 @@ struct FastBSincTag; namespace { -#define FRAC_PHASE_BITDIFF (FRACTIONBITS - BSINC_PHASE_BITS) -#define FRAC_PHASE_DIFFONE (1<> FRAC_PHASE_BITDIFF}; - const float pf{static_cast(frac & (FRAC_PHASE_DIFFONE-1)) * (1.0f/FRAC_PHASE_DIFFONE)}; + const ALuint pi{frac >> FracPhaseBitDiff}; + const float pf{static_cast(frac & (FracPhaseDiffOne-1)) * (1.0f/FracPhaseDiffOne)}; const float *fil{istate.bsinc.filter + m*pi*4}; const float *phd{fil + m}; @@ -54,8 +54,8 @@ inline float do_fastbsinc(const InterpState &istate, const float *RESTRICT vals, const size_t m{istate.bsinc.m}; // Calculate the phase index and factor. - const ALuint pi{frac >> FRAC_PHASE_BITDIFF}; - const float pf{static_cast(frac & (FRAC_PHASE_DIFFONE-1)) * (1.0f/FRAC_PHASE_DIFFONE)}; + const ALuint pi{frac >> FracPhaseBitDiff}; + const float pf{static_cast(frac & (FracPhaseDiffOne-1)) * (1.0f/FracPhaseDiffOne)}; const float *fil{istate.bsinc.filter + m*pi*4}; const float *phd{fil + m}; diff --git a/alc/mixer/mixer_neon.cpp b/alc/mixer/mixer_neon.cpp index 9d21b1c5..2573e791 100644 --- a/alc/mixer/mixer_neon.cpp +++ b/alc/mixer/mixer_neon.cpp @@ -21,8 +21,8 @@ struct FastBSincTag; namespace { -#define FRAC_PHASE_BITDIFF (FRACTIONBITS - BSINC_PHASE_BITS) -#define FRAC_PHASE_DIFFONE (1<(const InterpState *state, const float * for(float &out_sample : dst) { // Calculate the phase index and factor. - const ALuint pi{frac >> FRAC_PHASE_BITDIFF}; - const float pf{static_cast(frac & (FRAC_PHASE_DIFFONE-1)) * - (1.0f/FRAC_PHASE_DIFFONE)}; + const ALuint pi{frac >> FracPhaseBitDiff}; + const float pf{static_cast(frac & (FracPhaseDiffOne-1)) * (1.0f/FracPhaseDiffOne)}; // Apply the scale and phase interpolated filter. float32x4_t r4{vdupq_n_f32(0.0f)}; @@ -160,9 +159,8 @@ const float *Resample_(const InterpState *state, for(float &out_sample : dst) { // Calculate the phase index and factor. - const ALuint pi{frac >> FRAC_PHASE_BITDIFF}; - const float pf{static_cast(frac & (FRAC_PHASE_DIFFONE-1)) * - (1.0f/FRAC_PHASE_DIFFONE)}; + const ALuint pi{frac >> FracPhaseBitDiff}; + const float pf{static_cast(frac & (FracPhaseDiffOne-1)) * (1.0f/FracPhaseDiffOne)}; // Apply the phase interpolated filter. float32x4_t r4{vdupq_n_f32(0.0f)}; diff --git a/alc/mixer/mixer_sse.cpp b/alc/mixer/mixer_sse.cpp index d2e021ff..85cddcae 100644 --- a/alc/mixer/mixer_sse.cpp +++ b/alc/mixer/mixer_sse.cpp @@ -20,8 +20,8 @@ struct FastBSincTag; namespace { -#define FRAC_PHASE_BITDIFF (FRACTIONBITS - BSINC_PHASE_BITS) -#define FRAC_PHASE_DIFFONE (1<(const InterpState *state, const float *R for(float &out_sample : dst) { // Calculate the phase index and factor. - const ALuint pi{frac >> FRAC_PHASE_BITDIFF}; - const float pf{static_cast(frac & (FRAC_PHASE_DIFFONE-1)) * - (1.0f/FRAC_PHASE_DIFFONE)}; + const ALuint pi{frac >> FracPhaseBitDiff}; + const float pf{static_cast(frac & (FracPhaseDiffOne-1)) * (1.0f/FracPhaseDiffOne)}; // Apply the scale and phase interpolated filter. __m128 r4{_mm_setzero_ps()}; @@ -133,9 +132,8 @@ const float *Resample_(const InterpState *state, const floa for(float &out_sample : dst) { // Calculate the phase index and factor. - const ALuint pi{frac >> FRAC_PHASE_BITDIFF}; - const float pf{static_cast(frac & (FRAC_PHASE_DIFFONE-1)) * - (1.0f/FRAC_PHASE_DIFFONE)}; + const ALuint pi{frac >> FracPhaseBitDiff}; + const float pf{static_cast(frac & (FracPhaseDiffOne-1)) * (1.0f/FracPhaseDiffOne)}; // Apply the phase interpolated filter. __m128 r4{_mm_setzero_ps()};