diff --git a/benchmarks/BM_meanSquared.cpp b/benchmarks/BM_meanSquared.cpp index 0b1c159e..34c29a8f 100644 --- a/benchmarks/BM_meanSquared.cpp +++ b/benchmarks/BM_meanSquared.cpp @@ -34,7 +34,7 @@ BENCHMARK_DEFINE_F(MeanSquaredArray, Scalar) (benchmark::State& state) { for (auto _ : state) { - sfz::setSIMDOpStatus(sfz::SIMDOps::meanSquared, false); + sfz::setSIMDOpStatus(sfz::SIMDOps::sumSquares, false); auto result = sfz::meanSquared(input); benchmark::DoNotOptimize(result); } @@ -44,7 +44,7 @@ BENCHMARK_DEFINE_F(MeanSquaredArray, SIMD) (benchmark::State& state) { for (auto _ : state) { - sfz::setSIMDOpStatus(sfz::SIMDOps::meanSquared, true); + sfz::setSIMDOpStatus(sfz::SIMDOps::sumSquares, true); auto result = sfz::meanSquared(input); benchmark::DoNotOptimize(result); } @@ -54,7 +54,7 @@ BENCHMARK_DEFINE_F(MeanSquaredArray, Scalar_Unaligned) (benchmark::State& state) { for (auto _ : state) { - sfz::setSIMDOpStatus(sfz::SIMDOps::meanSquared, false); + sfz::setSIMDOpStatus(sfz::SIMDOps::sumSquares, false); auto result = sfz::meanSquared(absl::MakeSpan(input).subspan(1)); benchmark::DoNotOptimize(result); } @@ -64,7 +64,7 @@ BENCHMARK_DEFINE_F(MeanSquaredArray, SIMD_Unaligned) (benchmark::State& state) { for (auto _ : state) { - sfz::setSIMDOpStatus(sfz::SIMDOps::meanSquared, true); + sfz::setSIMDOpStatus(sfz::SIMDOps::sumSquares, true); auto result = sfz::meanSquared(absl::MakeSpan(input).subspan(1)); benchmark::DoNotOptimize(result); } diff --git a/src/sfizz/SIMDHelpers.cpp b/src/sfizz/SIMDHelpers.cpp index c86cecf8..c0287772 100644 --- a/src/sfizz/SIMDHelpers.cpp +++ b/src/sfizz/SIMDHelpers.cpp @@ -39,7 +39,7 @@ struct SIMDDispatch { decltype(&cumsumScalar) cumsum = &cumsumScalar; decltype(&diffScalar) diff = &diffScalar; decltype(&meanScalar) mean = &meanScalar; - decltype(&meanSquaredScalar) meanSquared = &meanSquaredScalar; + decltype(&sumSquaresScalar) sumSquares = &sumSquaresScalar; decltype(&clampAllScalar) clampAll = &clampAllScalar; decltype(&allWithinScalar) allWithin = &allWithinScalar; @@ -85,7 +85,7 @@ void SIMDDispatch::setStatus(SIMDOps op, bool enable) SIMD_OP(cumsum) SIMD_OP(diff) SIMD_OP(mean) - SIMD_OP(meanSquared) + SIMD_OP(sumSquares) SIMD_OP(clampAll) SIMD_OP(allWithin) } @@ -122,7 +122,7 @@ void SIMDDispatch::setStatus(SIMDOps op, bool enable) SIMD_OP(cumsum) SIMD_OP(diff) SIMD_OP(mean) - SIMD_OP(meanSquared) + SIMD_OP(sumSquares) SIMD_OP(clampAll) SIMD_OP(allWithin) } @@ -163,7 +163,7 @@ void SIMDDispatch::resetStatus() setStatus(SIMDOps::diff, false); setStatus(SIMDOps::sfzInterpolationCast, true); setStatus(SIMDOps::mean, false); - setStatus(SIMDOps::meanSquared, false); + setStatus(SIMDOps::sumSquares, false); setStatus(SIMDOps::upsampling, true); setStatus(SIMDOps::clampAll, false); setStatus(SIMDOps::allWithin, true); @@ -292,9 +292,9 @@ float mean(const float* vector, unsigned size) noexcept } template <> -float meanSquared(const float* vector, unsigned size) noexcept +float sumSquares(const float* vector, unsigned size) noexcept { - return simdDispatch().meanSquared(vector, size); + return simdDispatch().sumSquares(vector, size); } template <> diff --git a/src/sfizz/SIMDHelpers.h b/src/sfizz/SIMDHelpers.h index c0a27c09..ae34f732 100644 --- a/src/sfizz/SIMDHelpers.h +++ b/src/sfizz/SIMDHelpers.h @@ -57,7 +57,7 @@ enum class SIMDOps { diff, sfzInterpolationCast, mean, - meanSquared, + sumSquares, upsampling, clampAll, allWithin, @@ -515,7 +515,7 @@ T mean(absl::Span vector) noexcept } /** - * @brief Computes the mean squared of a span + * @brief Computes the sum of squares of a span * * @tparam T the underlying type * @tparam SIMD use the SIMD version or the scalar version @@ -523,13 +523,33 @@ T mean(absl::Span vector) noexcept * @return T */ template -T meanSquared(const T* vector, unsigned size) noexcept +T sumSquares(const T* vector, unsigned size) noexcept { - meanSquaredScalar(vector, size); + return sumSquaresScalar(vector, size); } template <> -float meanSquared(const float* vector, unsigned size) noexcept; +float sumSquares(const float* vector, unsigned size) noexcept; + +template +T sumSquares(absl::Span vector) noexcept +{ + return sumSquares(vector.data(), vector.size()); +} + +/** + * @brief Computes the mean squared of a span + * + * @tparam T the underlying type + * @param vector + * @return T + */ +template +T meanSquared(const T* vector, unsigned size) noexcept +{ + T sum = sumSquares(vector, size); + return sum / size; +} template T meanSquared(absl::Span vector) noexcept diff --git a/src/sfizz/simd/HelpersSSE.cpp b/src/sfizz/simd/HelpersSSE.cpp index 3ae8b984..d17288e7 100644 --- a/src/sfizz/simd/HelpersSSE.cpp +++ b/src/sfizz/simd/HelpersSSE.cpp @@ -370,7 +370,7 @@ float meanSSE(const float* vector, unsigned size) noexcept return result / static_cast(size); } -float meanSquaredSSE(const float* vector, unsigned size) noexcept +float sumSquaresSSE(const float* vector, unsigned size) noexcept { const auto sentinel = vector + size; @@ -404,7 +404,7 @@ float meanSquaredSSE(const float* vector, unsigned size) noexcept vector++; } - return result / static_cast(size); + return result; } void cumsumSSE(const float* input, float* output, unsigned size) noexcept diff --git a/src/sfizz/simd/HelpersSSE.h b/src/sfizz/simd/HelpersSSE.h index cff28650..5046d914 100644 --- a/src/sfizz/simd/HelpersSSE.h +++ b/src/sfizz/simd/HelpersSSE.h @@ -22,7 +22,7 @@ void subtractSSE(const float* input, float* output, unsigned size) noexcept; void subtract1SSE(float value, float* output, unsigned size) noexcept; void copySSE(const float* input, float* output, unsigned size) noexcept; float meanSSE(const float* vector, unsigned size) noexcept; -float meanSquaredSSE(const float* vector, unsigned size) noexcept; +float sumSquaresSSE(const float* vector, unsigned size) noexcept; void cumsumSSE(const float* input, float* output, unsigned size) noexcept; void diffSSE(const float* input, float* output, unsigned size) noexcept; void clampAllSSE(float* input, float low, float high, unsigned size) noexcept; diff --git a/src/sfizz/simd/HelpersScalar.h b/src/sfizz/simd/HelpersScalar.h index d5ac1162..871a9ff6 100644 --- a/src/sfizz/simd/HelpersScalar.h +++ b/src/sfizz/simd/HelpersScalar.h @@ -142,7 +142,7 @@ T meanScalar(const T* vector, unsigned size) noexcept } template -T meanSquaredScalar(const T* vector, unsigned size) noexcept +T sumSquaresScalar(const T* vector, unsigned size) noexcept { T result{ 0.0 }; if (size == 0) @@ -154,7 +154,7 @@ T meanSquaredScalar(const T* vector, unsigned size) noexcept vector++; } - return result / static_cast(size); + return result; } template diff --git a/tests/SIMDHelpersT.cpp b/tests/SIMDHelpersT.cpp index 805bb0f9..f0ef8277 100644 --- a/tests/SIMDHelpersT.cpp +++ b/tests/SIMDHelpersT.cpp @@ -690,9 +690,9 @@ TEST_CASE("[Helpers] Mean (SIMD vs scalar)") TEST_CASE("[Helpers] Mean Squared") { std::array input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f }; - sfz::setSIMDOpStatus(sfz::SIMDOps::meanSquared, false); + sfz::setSIMDOpStatus(sfz::SIMDOps::sumSquares, false); REQUIRE(sfz::meanSquared(input) == 38.5f); - sfz::setSIMDOpStatus(sfz::SIMDOps::meanSquared, true); + sfz::setSIMDOpStatus(sfz::SIMDOps::sumSquares, true); REQUIRE(sfz::meanSquared(input) == 38.5f); } @@ -700,9 +700,9 @@ TEST_CASE("[Helpers] Mean Squared (SIMD vs scalar)") { std::vector input(medBufferSize); absl::c_iota(input, 0.0f); - sfz::setSIMDOpStatus(sfz::SIMDOps::meanSquared, false); + sfz::setSIMDOpStatus(sfz::SIMDOps::sumSquares, false); auto scalarResult = sfz::meanSquared(input); - sfz::setSIMDOpStatus(sfz::SIMDOps::meanSquared, true); + sfz::setSIMDOpStatus(sfz::SIMDOps::sumSquares, true); auto simdResult = sfz::meanSquared(input); REQUIRE( scalarResult == Approx(simdResult).margin(1e-3) ); }