sfizz/benchmarks/BM_OPF_high_vs_low.cpp

156 lines
4.4 KiB
C++
Raw Permalink Normal View History

2020-01-25 10:04:31 +01:00
// SPDX-License-Identifier: BSD-2-Clause
2020-01-25 13:13:07 +01:00
// 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
2019-08-30 00:49:58 +02:00
2020-01-04 17:38:51 +01:00
#include "Config.h"
2020-02-09 14:58:21 +01:00
#include "SIMDConfig.h"
2019-08-09 18:24:27 +02:00
#include <benchmark/benchmark.h>
2019-08-25 14:05:30 +02:00
#include <absl/types/span.h>
2019-08-09 18:24:27 +02:00
#include <random>
#include <algorithm>
2019-08-25 14:05:30 +02:00
2020-02-09 14:58:21 +01:00
#if SFIZZ_HAVE_SSE2
#include <emmintrin.h>
2019-08-25 14:05:30 +02:00
#endif
2019-08-09 18:24:27 +02:00
constexpr float filterGain { 0.25f };
2019-08-25 14:05:30 +02:00
void lowpass(absl::Span<const float> input, absl::Span<float> lowpass, float gain)
2019-08-09 18:24:27 +02:00
{
float state = 0.0f;
float intermediate;
const auto G = gain / (1 - gain);
2019-09-21 13:01:50 +02:00
auto in = input.begin();
auto out = lowpass.begin();
while (in < input.end()) {
2019-08-09 18:24:27 +02:00
intermediate = G * (*in - state);
*out = intermediate + state;
state = *out + intermediate;
2019-09-21 13:01:50 +02:00
in++;
out++;
2019-08-09 18:24:27 +02:00
}
}
2019-08-25 14:05:30 +02:00
void highpass(absl::Span<const float> input, absl::Span<float> highpass, float gain)
2019-08-09 18:24:27 +02:00
{
float state = 0.0f;
float intermediate;
const auto G = gain / (1 - gain);
2019-09-21 13:01:50 +02:00
auto in = input.begin();
auto out = highpass.begin();
while (in < input.end()) {
2019-08-09 18:24:27 +02:00
intermediate = G * (*in - state);
*out = *in - intermediate - state;
state += 2*intermediate;
2019-09-21 13:01:50 +02:00
in++;
out++;
2019-08-09 18:24:27 +02:00
}
}
2019-08-25 14:05:30 +02:00
void highpass_raw(absl::Span<const float> input, absl::Span<float> highpass, float gain)
2019-08-09 18:24:27 +02:00
{
lowpass(input, highpass, gain);
auto in = input.data();
auto out = highpass.data();
2019-09-21 13:01:50 +02:00
while (in < input.end()) {
2019-08-09 18:24:27 +02:00
*out = *in - *out;
out++;
in++;
}
}
2019-08-25 14:05:30 +02:00
void highpass_sse(absl::Span<const float> input, absl::Span<float> highpass, float gain)
2019-08-09 18:24:27 +02:00
{
lowpass(input, highpass, gain);
auto in = input.data();
auto out = highpass.data();
constexpr int FloatAlignment { 4 };
const auto inputAlignedEnd = input.data() + (input.size() - (input.size() & (FloatAlignment - 1)));
const auto outputAlignedEnd = highpass.data() + (highpass.size() - (highpass.size() & (FloatAlignment - 1)));
while (in < inputAlignedEnd && out < outputAlignedEnd)
{
const auto inputRegister = _mm_loadu_ps(in);
auto outputRegister = _mm_loadu_ps(out);
outputRegister = _mm_sub_ps(inputRegister, outputRegister);
_mm_storeu_ps(out, outputRegister);
in += 4;
out += 4;
}
while (in < input.end() && out < highpass.end())
{
*out = *in - *out;
out++;
in++;
}
}
static void Low(benchmark::State& state) {
std::vector<float> input(state.range(0));
std::vector<float> output(state.range(0));
std::random_device rd { };
std::mt19937 gen { rd() };
2020-01-04 17:38:51 +01:00
2019-08-09 18:24:27 +02:00
std::normal_distribution<float> dist { };
std::generate(input.begin(), input.end(), [&]() {
return dist(gen);
});
for (auto _ : state)
2019-08-25 14:05:30 +02:00
lowpass(input, absl::MakeSpan(output), filterGain);
2019-08-09 18:24:27 +02:00
}
static void High(benchmark::State& state) {
std::vector<float> input(state.range(0));
std::vector<float> output(state.range(0));
std::random_device rd { };
std::mt19937 gen { rd() };
2020-01-04 17:38:51 +01:00
2019-08-09 18:24:27 +02:00
std::normal_distribution<float> dist { };
std::generate(input.begin(), input.end(), [&]() {
return dist(gen);
});
for (auto _ : state)
2019-08-25 14:05:30 +02:00
highpass(input, absl::MakeSpan(output), filterGain);
2019-08-09 18:24:27 +02:00
}
static void High_Raw(benchmark::State& state) {
std::vector<float> input(state.range(0));
std::vector<float> output(state.range(0));
std::random_device rd { };
std::mt19937 gen { rd() };
2020-01-04 17:38:51 +01:00
2019-08-09 18:24:27 +02:00
std::normal_distribution<float> dist { };
std::generate(input.begin(), input.end(), [&]() {
return dist(gen);
});
for (auto _ : state)
2019-08-25 14:05:30 +02:00
highpass_raw(input, absl::MakeSpan(output), filterGain);
2019-08-09 18:24:27 +02:00
}
static void High_SSE(benchmark::State& state) {
std::vector<float> input(state.range(0));
std::vector<float> output(state.range(0));
std::random_device rd { };
std::mt19937 gen { rd() };
2020-01-04 17:38:51 +01:00
2019-08-09 18:24:27 +02:00
std::normal_distribution<float> dist { };
std::generate(input.begin(), input.end(), [&]() {
return dist(gen);
});
for (auto _ : state)
2019-08-25 14:05:30 +02:00
highpass_sse(input, absl::MakeSpan(output), filterGain);
2019-08-09 18:24:27 +02:00
}
BENCHMARK(Low)->RangeMultiplier(2)->Range((2<<5), (2<<10));
BENCHMARK(High)->RangeMultiplier(2)->Range((2<<5), (2<<10));
BENCHMARK(High_Raw)->RangeMultiplier(2)->Range((2<<5), (2<<10));
BENCHMARK(High_SSE)->RangeMultiplier(2)->Range((2<<5), (2<<10));
2020-01-04 17:38:51 +01:00
BENCHMARK_MAIN();