Add multiplyAdd with fixed gain
This commit is contained in:
parent
3bfa573dc2
commit
236906fec4
5 changed files with 128 additions and 0 deletions
78
benchmarks/BM_multiplyAddFixedGain.cpp
Normal file
78
benchmarks/BM_multiplyAddFixedGain.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
// SPDX-License-Identifier: BSD-2-Clause
|
||||
|
||||
// This code is part of the sfizz library and is licensed under a BSD 2-clause
|
||||
// license. You should have receive a LICENSE.md file along with the code.
|
||||
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
|
||||
|
||||
#include "SIMDHelpers.h"
|
||||
#include <benchmark/benchmark.h>
|
||||
#include <random>
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
class MultiplyAddFixedGain : public benchmark::Fixture {
|
||||
public:
|
||||
void SetUp(const ::benchmark::State& state) {
|
||||
std::random_device rd { };
|
||||
std::mt19937 gen { rd() };
|
||||
std::uniform_real_distribution<float> dist { 0, 1 };
|
||||
input = std::vector<float>(state.range(0));
|
||||
output = std::vector<float>(state.range(0));
|
||||
gain = dist(gen);
|
||||
std::fill(output.begin(), output.end(), 1.0f );
|
||||
std::generate(input.begin(), input.end(), [&]() { return dist(gen); });
|
||||
}
|
||||
|
||||
void TearDown(const ::benchmark::State& state [[maybe_unused]]) {
|
||||
|
||||
}
|
||||
|
||||
float gain = {};
|
||||
std::vector<float> input;
|
||||
std::vector<float> output;
|
||||
};
|
||||
|
||||
BENCHMARK_DEFINE_F(MultiplyAddFixedGain, Straight)(benchmark::State& state) {
|
||||
for (auto _ : state)
|
||||
{
|
||||
for (int i = 0; i < state.range(0); ++i)
|
||||
output[i] += gain * input[i];
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK_DEFINE_F(MultiplyAddFixedGain, Scalar)(benchmark::State& state) {
|
||||
for (auto _ : state)
|
||||
{
|
||||
sfz::multiplyAdd<float, false>(gain, input, absl::MakeSpan(output));
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK_DEFINE_F(MultiplyAddFixedGain, SIMD)(benchmark::State& state) {
|
||||
for (auto _ : state)
|
||||
{
|
||||
sfz::multiplyAdd<float, true>(gain, input, absl::MakeSpan(output));
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK_DEFINE_F(MultiplyAddFixedGain, Scalar_Unaligned)(benchmark::State& state) {
|
||||
for (auto _ : state)
|
||||
{
|
||||
sfz::multiplyAdd<float, false>(gain, absl::MakeSpan(input).subspan(1), absl::MakeSpan(output).subspan(1));
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK_DEFINE_F(MultiplyAddFixedGain, SIMD_Unaligned)(benchmark::State& state) {
|
||||
for (auto _ : state)
|
||||
{
|
||||
sfz::multiplyAdd<float, true>(gain, absl::MakeSpan(input).subspan(1), absl::MakeSpan(output).subspan(1));
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK_REGISTER_F(MultiplyAddFixedGain, Straight)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
|
||||
BENCHMARK_REGISTER_F(MultiplyAddFixedGain, Scalar)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
|
||||
BENCHMARK_REGISTER_F(MultiplyAddFixedGain, SIMD)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
|
||||
BENCHMARK_REGISTER_F(MultiplyAddFixedGain, Scalar_Unaligned)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
|
||||
BENCHMARK_REGISTER_F(MultiplyAddFixedGain, SIMD_Unaligned)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
|
||||
BENCHMARK_MAIN();
|
||||
|
|
@ -46,6 +46,7 @@ target_link_libraries(bm_ADSR PRIVATE sfizz::sfizz)
|
|||
|
||||
sfizz_add_benchmark(bm_add BM_add.cpp)
|
||||
sfizz_add_benchmark(bm_multiplyAdd BM_multiplyAdd.cpp)
|
||||
sfizz_add_benchmark(bm_multiplyAddFixedGain BM_multiplyAddFixedGain.cpp)
|
||||
sfizz_add_benchmark(bm_subtract BM_subtract.cpp)
|
||||
sfizz_add_benchmark(bm_copy BM_copy.cpp)
|
||||
sfizz_add_benchmark(bm_pan BM_pan.cpp)
|
||||
|
|
|
|||
|
|
@ -76,6 +76,12 @@ void sfz::multiplyAdd<float, true>(absl::Span<const float> gain, absl::Span<cons
|
|||
multiplyAdd<float, false>(gain, input, output);
|
||||
}
|
||||
|
||||
template <>
|
||||
void sfz::multiplyAdd<float, true>(const float gain, absl::Span<const float> input, absl::Span<float> output) noexcept
|
||||
{
|
||||
multiplyAdd<float, false>(gain, input, output);
|
||||
}
|
||||
|
||||
template <>
|
||||
float sfz::loopingSFZIndex<float, true>(absl::Span<const float> jumps, absl::Span<float> leftCoeff, absl::Span<float> rightCoeff, absl::Span<int> indices, float floatIndex, float loopEnd, float loopStart) noexcept
|
||||
{
|
||||
|
|
|
|||
|
|
@ -491,6 +491,12 @@ namespace _internals {
|
|||
{
|
||||
*output++ += (*gain++) * (*input++);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void snippetMultiplyAdd(const T gain, const T*& input, T*& output)
|
||||
{
|
||||
*output++ += gain * (*input++);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -520,6 +526,20 @@ void multiplyAdd(absl::Span<const T> gain, absl::Span<const T> input, absl::Span
|
|||
template <>
|
||||
void multiplyAdd<float, true>(absl::Span<const float> gain, absl::Span<const float> input, absl::Span<float> output) noexcept;
|
||||
|
||||
template <class T, bool SIMD = SIMDConfig::multiplyAdd>
|
||||
void multiplyAdd(const T gain, absl::Span<const T> input, absl::Span<T> output) noexcept
|
||||
{
|
||||
ASSERT(input.size() <= output.size());
|
||||
auto* in = input.begin();
|
||||
auto* out = output.begin();
|
||||
auto* sentinel = out + std::min(output.size(), input.size());
|
||||
while (out < sentinel)
|
||||
_internals::snippetMultiplyAdd<T>(gain, in, out);
|
||||
}
|
||||
|
||||
template <>
|
||||
void multiplyAdd<float, true>(const float gain, absl::Span<const float> input, absl::Span<float> output) noexcept;
|
||||
|
||||
namespace _internals {
|
||||
template <class T>
|
||||
inline void snippetRampLinear(T*& output, T& value, T step)
|
||||
|
|
|
|||
|
|
@ -316,6 +316,29 @@ void sfz::multiplyAdd<float, true>(absl::Span<const float> gain, absl::Span<cons
|
|||
_internals::snippetMultiplyAdd<float>(g, in, out);
|
||||
}
|
||||
|
||||
template <>
|
||||
void sfz::multiplyAdd<float, true>(const float gain, absl::Span<const float> input, absl::Span<float> output) noexcept
|
||||
{
|
||||
auto* in = input.begin();
|
||||
auto* out = output.begin();
|
||||
const auto size = std::min(output.size(), input.size());
|
||||
const auto* lastAligned = prevAligned(output.begin() + size);
|
||||
|
||||
while (unaligned(out, in) && out < lastAligned)
|
||||
_internals::snippetMultiplyAdd<float>(gain, in, out);
|
||||
|
||||
auto mmGain = _mm_set1_ps(gain);
|
||||
while (out < lastAligned) {
|
||||
auto mmOut = _mm_load_ps(out);
|
||||
mmOut = _mm_add_ps(_mm_mul_ps(mmGain, _mm_load_ps(in)), mmOut);
|
||||
_mm_store_ps(out, mmOut);
|
||||
incrementAll<TypeAlignment>(in, out);
|
||||
}
|
||||
|
||||
while (out < output.end())
|
||||
_internals::snippetMultiplyAdd<float>(gain, in, out);
|
||||
}
|
||||
|
||||
template <>
|
||||
float sfz::loopingSFZIndex<float, true>(absl::Span<const float> jumps,
|
||||
absl::Span<float> leftCoeffs,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue