diff options
author | Chris Robinson <[email protected]> | 2023-01-05 16:20:35 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2023-01-05 16:20:35 -0800 |
commit | 6d613b308839eb216b32db265aed959a93fdd4bb (patch) | |
tree | 438730f4d63ec21c2293ce843a328b8ada03f9cb /core/mixer | |
parent | 23c8a35505fe6ab7a5c87754911a133b23ac75cf (diff) |
Combine some duplicate code to mix each channel
Diffstat (limited to 'core/mixer')
-rw-r--r-- | core/mixer/mixer_c.cpp | 91 | ||||
-rw-r--r-- | core/mixer/mixer_neon.cpp | 214 | ||||
-rw-r--r-- | core/mixer/mixer_sse.cpp | 211 |
3 files changed, 186 insertions, 330 deletions
diff --git a/core/mixer/mixer_c.cpp b/core/mixer/mixer_c.cpp index dabfa652..8137d866 100644 --- a/core/mixer/mixer_c.cpp +++ b/core/mixer/mixer_c.cpp @@ -96,6 +96,36 @@ inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const Cons } } +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<> @@ -166,37 +196,10 @@ void Mix_<CTag>(const al::span<const float> InSamples, const al::span<FloatBuffe { 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) - { - float *RESTRICT dst{al::assume_aligned<16>(output.data()+OutPos)}; - float gain{*CurrentGains}; - const float step{(*TargetGains-gain) * delta}; - size_t pos{0}; - if(!(std::abs(step) > std::numeric_limits<float>::epsilon())) - gain = *TargetGains; - 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 = *TargetGains; - else - gain += step*step_count; - } - *CurrentGains = gain; - ++CurrentGains; - ++TargetGains; - - if(!(std::abs(gain) > GainSilenceThreshold)) - continue; - for(;pos != InSamples.size();++pos) - dst[pos] += InSamples[pos] * gain; - } + for(FloatBufferLine &output : OutBuffer) + MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++, + *TargetGains++, delta, min_len, Counter); } template<> @@ -206,30 +209,6 @@ void Mix_<CTag>(const al::span<const float> InSamples, float *OutBuffer, float & const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f}; const auto min_len = minz(Counter, InSamples.size()); - float *RESTRICT dst{al::assume_aligned<16>(OutBuffer)}; - 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; + 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 index f3d54fec..68e6bc57 100644 --- a/core/mixer/mixer_neon.cpp +++ b/core/mixer/mixer_neon.cpp @@ -56,6 +56,78 @@ inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const Cons } } +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<> @@ -233,77 +305,8 @@ void Mix_<NEONTag>(const al::span<const float> InSamples, const al::span<FloatBu const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len; for(FloatBufferLine &output : OutBuffer) - { - float *RESTRICT dst{al::assume_aligned<16>(output.data()+OutPos)}; - float gain{*CurrentGains}; - const float step{(*TargetGains-gain) * delta}; - - size_t pos{0}; - if(!(std::abs(step) > std::numeric_limits<float>::epsilon())) - gain = *TargetGains; - 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 = *TargetGains; - 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; - } - *CurrentGains = gain; - ++CurrentGains; - ++TargetGains; - - if(!(std::abs(gain) > GainSilenceThreshold)) - continue; - 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; - } + MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++, + *TargetGains++, delta, min_len, aligned_len, Counter); } template<> @@ -314,71 +317,6 @@ void Mix_<NEONTag>(const al::span<const float> InSamples, float *OutBuffer, floa const auto min_len = minz(Counter, InSamples.size()); const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len; - float *RESTRICT dst{al::assume_aligned<16>(OutBuffer)}; - 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; + 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 index 89968fc8..702f9bd4 100644 --- a/core/mixer/mixer_sse.cpp +++ b/core/mixer/mixer_sse.cpp @@ -73,6 +73,77 @@ inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const Cons } } +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<> @@ -199,76 +270,8 @@ void Mix_<SSETag>(const al::span<const float> InSamples, const al::span<FloatBuf const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len; for(FloatBufferLine &output : OutBuffer) - { - float *RESTRICT dst{al::assume_aligned<16>(output.data()+OutPos)}; - float gain{*CurrentGains}; - const float step{(*TargetGains-gain) * delta}; - - size_t pos{0}; - if(!(std::abs(step) > std::numeric_limits<float>::epsilon())) - gain = *TargetGains; - 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 = *TargetGains; - 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; - } - *CurrentGains = gain; - ++CurrentGains; - ++TargetGains; - - if(!(std::abs(gain) > GainSilenceThreshold)) - continue; - 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; - } + MixLine(InSamples, al::assume_aligned<16>(output.data()+OutPos), *CurrentGains++, + *TargetGains++, delta, min_len, aligned_len, Counter); } template<> @@ -279,70 +282,6 @@ void Mix_<SSETag>(const al::span<const float> InSamples, float *OutBuffer, float const auto min_len = minz(Counter, InSamples.size()); const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len; - float *RESTRICT dst{al::assume_aligned<16>(OutBuffer)}; - 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; + MixLine(InSamples, al::assume_aligned<16>(OutBuffer), CurrentGain, TargetGain, delta, min_len, + aligned_len, Counter); } |