Move the div operation out of the SIMD helper

This commit is contained in:
Jean Pierre Cimalando 2020-08-27 08:08:37 +02:00
parent 4c3db6c2c8
commit a5c72375a9
7 changed files with 44 additions and 24 deletions

View file

@ -34,7 +34,7 @@ BENCHMARK_DEFINE_F(MeanSquaredArray, Scalar)
(benchmark::State& state) (benchmark::State& state)
{ {
for (auto _ : state) { for (auto _ : state) {
sfz::setSIMDOpStatus<float>(sfz::SIMDOps::meanSquared, false); sfz::setSIMDOpStatus<float>(sfz::SIMDOps::sumSquares, false);
auto result = sfz::meanSquared<float>(input); auto result = sfz::meanSquared<float>(input);
benchmark::DoNotOptimize(result); benchmark::DoNotOptimize(result);
} }
@ -44,7 +44,7 @@ BENCHMARK_DEFINE_F(MeanSquaredArray, SIMD)
(benchmark::State& state) (benchmark::State& state)
{ {
for (auto _ : state) { for (auto _ : state) {
sfz::setSIMDOpStatus<float>(sfz::SIMDOps::meanSquared, true); sfz::setSIMDOpStatus<float>(sfz::SIMDOps::sumSquares, true);
auto result = sfz::meanSquared<float>(input); auto result = sfz::meanSquared<float>(input);
benchmark::DoNotOptimize(result); benchmark::DoNotOptimize(result);
} }
@ -54,7 +54,7 @@ BENCHMARK_DEFINE_F(MeanSquaredArray, Scalar_Unaligned)
(benchmark::State& state) (benchmark::State& state)
{ {
for (auto _ : state) { for (auto _ : state) {
sfz::setSIMDOpStatus<float>(sfz::SIMDOps::meanSquared, false); sfz::setSIMDOpStatus<float>(sfz::SIMDOps::sumSquares, false);
auto result = sfz::meanSquared<float>(absl::MakeSpan(input).subspan(1)); auto result = sfz::meanSquared<float>(absl::MakeSpan(input).subspan(1));
benchmark::DoNotOptimize(result); benchmark::DoNotOptimize(result);
} }
@ -64,7 +64,7 @@ BENCHMARK_DEFINE_F(MeanSquaredArray, SIMD_Unaligned)
(benchmark::State& state) (benchmark::State& state)
{ {
for (auto _ : state) { for (auto _ : state) {
sfz::setSIMDOpStatus<float>(sfz::SIMDOps::meanSquared, true); sfz::setSIMDOpStatus<float>(sfz::SIMDOps::sumSquares, true);
auto result = sfz::meanSquared<float>(absl::MakeSpan(input).subspan(1)); auto result = sfz::meanSquared<float>(absl::MakeSpan(input).subspan(1));
benchmark::DoNotOptimize(result); benchmark::DoNotOptimize(result);
} }

View file

@ -39,7 +39,7 @@ struct SIMDDispatch {
decltype(&cumsumScalar<T>) cumsum = &cumsumScalar<T>; decltype(&cumsumScalar<T>) cumsum = &cumsumScalar<T>;
decltype(&diffScalar<T>) diff = &diffScalar<T>; decltype(&diffScalar<T>) diff = &diffScalar<T>;
decltype(&meanScalar<T>) mean = &meanScalar<T>; decltype(&meanScalar<T>) mean = &meanScalar<T>;
decltype(&meanSquaredScalar<T>) meanSquared = &meanSquaredScalar<T>; decltype(&sumSquaresScalar<T>) sumSquares = &sumSquaresScalar<T>;
decltype(&clampAllScalar<T>) clampAll = &clampAllScalar<T>; decltype(&clampAllScalar<T>) clampAll = &clampAllScalar<T>;
decltype(&allWithinScalar<T>) allWithin = &allWithinScalar<T>; decltype(&allWithinScalar<T>) allWithin = &allWithinScalar<T>;
@ -85,7 +85,7 @@ void SIMDDispatch<float>::setStatus(SIMDOps op, bool enable)
SIMD_OP(cumsum) SIMD_OP(cumsum)
SIMD_OP(diff) SIMD_OP(diff)
SIMD_OP(mean) SIMD_OP(mean)
SIMD_OP(meanSquared) SIMD_OP(sumSquares)
SIMD_OP(clampAll) SIMD_OP(clampAll)
SIMD_OP(allWithin) SIMD_OP(allWithin)
} }
@ -122,7 +122,7 @@ void SIMDDispatch<float>::setStatus(SIMDOps op, bool enable)
SIMD_OP(cumsum) SIMD_OP(cumsum)
SIMD_OP(diff) SIMD_OP(diff)
SIMD_OP(mean) SIMD_OP(mean)
SIMD_OP(meanSquared) SIMD_OP(sumSquares)
SIMD_OP(clampAll) SIMD_OP(clampAll)
SIMD_OP(allWithin) SIMD_OP(allWithin)
} }
@ -163,7 +163,7 @@ void SIMDDispatch<float>::resetStatus()
setStatus(SIMDOps::diff, false); setStatus(SIMDOps::diff, false);
setStatus(SIMDOps::sfzInterpolationCast, true); setStatus(SIMDOps::sfzInterpolationCast, true);
setStatus(SIMDOps::mean, false); setStatus(SIMDOps::mean, false);
setStatus(SIMDOps::meanSquared, false); setStatus(SIMDOps::sumSquares, false);
setStatus(SIMDOps::upsampling, true); setStatus(SIMDOps::upsampling, true);
setStatus(SIMDOps::clampAll, false); setStatus(SIMDOps::clampAll, false);
setStatus(SIMDOps::allWithin, true); setStatus(SIMDOps::allWithin, true);
@ -292,9 +292,9 @@ float mean<float>(const float* vector, unsigned size) noexcept
} }
template <> template <>
float meanSquared<float>(const float* vector, unsigned size) noexcept float sumSquares<float>(const float* vector, unsigned size) noexcept
{ {
return simdDispatch<float>().meanSquared(vector, size); return simdDispatch<float>().sumSquares(vector, size);
} }
template <> template <>

View file

@ -57,7 +57,7 @@ enum class SIMDOps {
diff, diff,
sfzInterpolationCast, sfzInterpolationCast,
mean, mean,
meanSquared, sumSquares,
upsampling, upsampling,
clampAll, clampAll,
allWithin, allWithin,
@ -515,7 +515,7 @@ T mean(absl::Span<const T> 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 T the underlying type
* @tparam SIMD use the SIMD version or the scalar version * @tparam SIMD use the SIMD version or the scalar version
@ -523,13 +523,33 @@ T mean(absl::Span<const T> vector) noexcept
* @return T * @return T
*/ */
template <class T> template <class T>
T meanSquared(const T* vector, unsigned size) noexcept T sumSquares(const T* vector, unsigned size) noexcept
{ {
meanSquaredScalar(vector, size); return sumSquaresScalar(vector, size);
} }
template <> template <>
float meanSquared<float>(const float* vector, unsigned size) noexcept; float sumSquares<float>(const float* vector, unsigned size) noexcept;
template <class T>
T sumSquares(absl::Span<const T> 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 <class T>
T meanSquared(const T* vector, unsigned size) noexcept
{
T sum = sumSquares(vector, size);
return sum / size;
}
template <class T> template <class T>
T meanSquared(absl::Span<const T> vector) noexcept T meanSquared(absl::Span<const T> vector) noexcept

View file

@ -370,7 +370,7 @@ float meanSSE(const float* vector, unsigned size) noexcept
return result / static_cast<float>(size); return result / static_cast<float>(size);
} }
float meanSquaredSSE(const float* vector, unsigned size) noexcept float sumSquaresSSE(const float* vector, unsigned size) noexcept
{ {
const auto sentinel = vector + size; const auto sentinel = vector + size;
@ -404,7 +404,7 @@ float meanSquaredSSE(const float* vector, unsigned size) noexcept
vector++; vector++;
} }
return result / static_cast<float>(size); return result;
} }
void cumsumSSE(const float* input, float* output, unsigned size) noexcept void cumsumSSE(const float* input, float* output, unsigned size) noexcept

View file

@ -22,7 +22,7 @@ void subtractSSE(const float* input, float* output, unsigned size) noexcept;
void subtract1SSE(float value, float* output, unsigned size) noexcept; void subtract1SSE(float value, float* output, unsigned size) noexcept;
void copySSE(const float* input, float* output, unsigned size) noexcept; void copySSE(const float* input, float* output, unsigned size) noexcept;
float meanSSE(const float* vector, 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 cumsumSSE(const float* input, float* output, unsigned size) noexcept;
void diffSSE(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; void clampAllSSE(float* input, float low, float high, unsigned size) noexcept;

View file

@ -142,7 +142,7 @@ T meanScalar(const T* vector, unsigned size) noexcept
} }
template <class T> template <class T>
T meanSquaredScalar(const T* vector, unsigned size) noexcept T sumSquaresScalar(const T* vector, unsigned size) noexcept
{ {
T result{ 0.0 }; T result{ 0.0 };
if (size == 0) if (size == 0)
@ -154,7 +154,7 @@ T meanSquaredScalar(const T* vector, unsigned size) noexcept
vector++; vector++;
} }
return result / static_cast<T>(size); return result;
} }
template <class T> template <class T>

View file

@ -690,9 +690,9 @@ TEST_CASE("[Helpers] Mean (SIMD vs scalar)")
TEST_CASE("[Helpers] Mean Squared") TEST_CASE("[Helpers] Mean Squared")
{ {
std::array<float, 10> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f }; std::array<float, 10> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };
sfz::setSIMDOpStatus<float>(sfz::SIMDOps::meanSquared, false); sfz::setSIMDOpStatus<float>(sfz::SIMDOps::sumSquares, false);
REQUIRE(sfz::meanSquared<float>(input) == 38.5f); REQUIRE(sfz::meanSquared<float>(input) == 38.5f);
sfz::setSIMDOpStatus<float>(sfz::SIMDOps::meanSquared, true); sfz::setSIMDOpStatus<float>(sfz::SIMDOps::sumSquares, true);
REQUIRE(sfz::meanSquared<float>(input) == 38.5f); REQUIRE(sfz::meanSquared<float>(input) == 38.5f);
} }
@ -700,9 +700,9 @@ TEST_CASE("[Helpers] Mean Squared (SIMD vs scalar)")
{ {
std::vector<float> input(medBufferSize); std::vector<float> input(medBufferSize);
absl::c_iota(input, 0.0f); absl::c_iota(input, 0.0f);
sfz::setSIMDOpStatus<float>(sfz::SIMDOps::meanSquared, false); sfz::setSIMDOpStatus<float>(sfz::SIMDOps::sumSquares, false);
auto scalarResult = sfz::meanSquared<float>(input); auto scalarResult = sfz::meanSquared<float>(input);
sfz::setSIMDOpStatus<float>(sfz::SIMDOps::meanSquared, true); sfz::setSIMDOpStatus<float>(sfz::SIMDOps::sumSquares, true);
auto simdResult = sfz::meanSquared<float>(input); auto simdResult = sfz::meanSquared<float>(input);
REQUIRE( scalarResult == Approx(simdResult).margin(1e-3) ); REQUIRE( scalarResult == Approx(simdResult).margin(1e-3) );
} }