From 754a7f8be79fd2ee31bf8d9edbb3c88919456313 Mon Sep 17 00:00:00 2001 From: Paul Fd Date: Mon, 10 Feb 2020 21:23:30 +0100 Subject: [PATCH] Changed the width helpers ; need SIMD version still --- benchmarks/BM_widthPos.cpp | 57 ++------------------------------------ src/sfizz/SIMDHelpers.h | 28 +++++++++++-------- tests/SIMDHelpersT.cpp | 26 ++++++++--------- 3 files changed, 32 insertions(+), 79 deletions(-) diff --git a/benchmarks/BM_widthPos.cpp b/benchmarks/BM_widthPos.cpp index b7f82a4d..b55e5628 100644 --- a/benchmarks/BM_widthPos.cpp +++ b/benchmarks/BM_widthPos.cpp @@ -54,67 +54,16 @@ public: }; BENCHMARK_DEFINE_F(WidthPosArray, Scalar)(benchmark::State& state) { - ScopedFTZ ftz; - for (auto _ : state) - { - auto widthPtr = width.data(); - auto posPtr = position.data(); - auto leftPtr = left.data(); - auto rightPtr = right.data(); - const auto sentinel = width.data() + width.size(); - while(widthPtr < sentinel) - { - auto mid = (*leftPtr + *rightPtr) * sqrtTwoInv; - auto side = (*leftPtr - *rightPtr) * sqrtTwoInv; - sfz::_internals::snippetWidth(*widthPtr, mid, side); - auto midRight = mid; - sfz::_internals::snippetPan(*posPtr, mid, midRight); - *leftPtr = (mid + side) * sqrtTwoInv; - *rightPtr = (midRight - side) * sqrtTwoInv; - incrementAll(leftPtr, rightPtr, widthPtr, posPtr); - } - } -} - -BENCHMARK_DEFINE_F(WidthPosArray, BlockOps)(benchmark::State& state) { ScopedFTZ ftz; const auto leftBuffer = absl::MakeSpan(left); const auto rightBuffer = absl::MakeSpan(right); for (auto _ : state) { - const auto leftBufferCopy = span2; - sfz::copy(left, leftBufferCopy); - - const auto midBuffer = leftBuffer; - sfz::add(rightBuffer, midBuffer); - - const auto sideBuffer = rightBuffer; - sfz::applyGain(-1.0f, sideBuffer); - sfz::add(leftBufferCopy, sideBuffer); - - sfz::applyGain(sqrtTwoInv, midBuffer); - sfz::applyGain(sqrtTwoInv, sideBuffer); - - // Apply the width process - sfz::width(width, midBuffer, sideBuffer); - - // Copy the mid channel into another span - const auto midBufferCopy = span3; - sfz::copy(midBuffer, midBufferCopy); - sfz::pan(position, midBuffer, midBufferCopy); - - // Rebuild left/right - // Recall that midBuffer and leftBuffer point to the same buffer - sfz::add(sideBuffer, leftBuffer); - sfz::applyGain(sqrtTwoInv, leftBuffer); - - // Recall that sideBuffer and rightBuffer point to the same buffer - sfz::applyGain(-1.0f, sideBuffer); - sfz::add(midBufferCopy, sideBuffer); - sfz::applyGain(sqrtTwoInv, rightBuffer); + sfz::width(width, leftBuffer, rightBuffer); + sfz::pan(position, leftBuffer, rightBuffer); } } + BENCHMARK_REGISTER_F(WidthPosArray, Scalar)->RangeMultiplier(4)->Range(1 << 2, 1 << 12); -BENCHMARK_REGISTER_F(WidthPosArray, BlockOps)->RangeMultiplier(4)->Range(1 << 2, 1 << 12); BENCHMARK_MAIN(); diff --git a/src/sfizz/SIMDHelpers.h b/src/sfizz/SIMDHelpers.h index 77c220e4..355426d9 100644 --- a/src/sfizz/SIMDHelpers.h +++ b/src/sfizz/SIMDHelpers.h @@ -760,12 +760,16 @@ namespace _internals { } template - inline void snippetWidth(const T& width, T& mid, T& side) + inline void snippetWidth(T width, T& left, T& right) { - T w = std::abs(width) * T{0.5}; + T w = (width + T{1.0}) * T{0.5}; w = clamp(w, 0, 1); - mid *= panLookup(w); - side *= width > 0 ? panLookup(1 - w) : -panLookup(1 - w); + const auto coeff1 = panLookup(w); + const auto coeff2 = panLookup(1 - w); + const auto l = left; + const auto r = right; + left = l * coeff2 + r * coeff1; + right = l * coeff1 + r * coeff2; } } @@ -799,17 +803,17 @@ template <> void pan(absl::Span panEnvelope, absl::Span leftBuffer, absl::Span rightBuffer) noexcept; template -void width(absl::Span widthEnvelope, absl::Span midBuffer, absl::Span sideBuffer) noexcept +void width(absl::Span widthEnvelope, absl::Span leftBuffer, absl::Span rightBuffer) noexcept { - ASSERT(midBuffer.size() >= widthEnvelope.size()); - ASSERT(sideBuffer.size() >= widthEnvelope.size()); + ASSERT(leftBuffer.size() >= widthEnvelope.size()); + ASSERT(rightBuffer.size() >= widthEnvelope.size()); auto* width = widthEnvelope.begin(); - auto* mid = midBuffer.begin(); - auto* side = sideBuffer.begin(); - auto* sentinel = width + min(widthEnvelope.size(), midBuffer.size(), sideBuffer.size()); + auto* left = leftBuffer.begin(); + auto* right = rightBuffer.begin(); + auto* sentinel = width + min(widthEnvelope.size(), leftBuffer.size(), rightBuffer.size()); while (width < sentinel) { - _internals::snippetWidth(*width, *mid, *side); - incrementAll(width, mid, side); + _internals::snippetWidth(*width, *left, *right); + incrementAll(width, left, right); } } diff --git a/tests/SIMDHelpersT.cpp b/tests/SIMDHelpersT.cpp index 985be6e7..51f9426c 100644 --- a/tests/SIMDHelpersT.cpp +++ b/tests/SIMDHelpersT.cpp @@ -788,29 +788,29 @@ TEST_CASE("[Helpers] Pan Scalar") TEST_CASE("[Helpers] Width Scalar") { - std::array midValue { 1.0f }; - std::array sideValue { 1.0f }; - auto mid = absl::MakeSpan(midValue); - auto side = absl::MakeSpan(sideValue); + std::array leftValue { 1.0f }; + std::array rightValue { 1.0f }; + auto left = absl::MakeSpan(leftValue); + auto right = absl::MakeSpan(rightValue); SECTION("width = 1") { std::array width { 1.0f }; - sfz::width(width, mid, side); - REQUIRE(mid[0] == Approx(0.70711f).margin(0.001f)); - REQUIRE(side[0] == Approx(0.70711f).margin(0.001f)); + sfz::width(width, left, right); + REQUIRE(left[0] == Approx(1.0f).margin(0.001f)); + REQUIRE(right[0] == Approx(1.0f).margin(0.001f)); } SECTION("width = 0") { std::array width { 0.0f }; - sfz::width(width, mid, side); - REQUIRE(mid[0] == Approx(1.0f).margin(0.001f)); - REQUIRE(side[0] == Approx(0.0f).margin(0.001f)); + sfz::width(width, left, right); + REQUIRE(left[0] == Approx(1.414f).margin(0.001f)); + REQUIRE(right[0] == Approx(1.414f).margin(0.001f)); } SECTION("width = -1") { std::array width { -1.0f }; - sfz::width(width, mid, side); - REQUIRE(mid[0] == Approx(0.70711f).margin(0.001f)); - REQUIRE(side[0] == Approx(-0.70711f).margin(0.001f)); + sfz::width(width, left, right); + REQUIRE(left[0] == Approx(1.0f).margin(0.001f)); + REQUIRE(right[0] == Approx(1.0f).margin(0.001f)); } }