sfizz/benchmarks/BM_filterStereoMono.cpp

323 lines
11 KiB
C++
Raw Normal View History

2020-02-07 11:11:22 +01:00
// 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 "OnePoleFilter.h"
#include "SfzFilter.h"
#include "ScopedFTZ.h"
2020-02-07 11:11:22 +01:00
#include <benchmark/benchmark.h>
#include <random>
#include <numeric>
#include <vector>
#include <cmath>
#include <iostream>
constexpr int blockSize { 1024 };
constexpr float sampleRate { 48000.0f };
class FilterFixture : public benchmark::Fixture {
public:
void SetUp(const ::benchmark::State& state) {
2020-02-07 11:11:22 +01:00
inputLeft = std::vector<float>(blockSize);
inputRight = std::vector<float>(blockSize);
outputLeft = std::vector<float>(blockSize);
outputRight = std::vector<float>(blockSize);
cutoff = std::vector<float>(blockSize);
q = std::vector<float>(blockSize);
pksh = std::vector<float>(blockSize);
sfz::linearRamp<float>(absl::MakeSpan(cutoff), 500, 1.0f);
sfz::linearRamp<float>(absl::MakeSpan(q), 0.0f, 0.001f);
sfz::linearRamp<float>(absl::MakeSpan(pksh), 0.0f, 0.001f);
std::generate(inputLeft.begin(), inputLeft.end(), [&]() { return dist(gen); });
std::generate(inputRight.begin(), inputRight.end(), [&]() { return dist(gen); });
}
void TearDown(const ::benchmark::State& state [[maybe_unused]]) {
}
std::random_device rd { };
std::mt19937 gen { rd() };
std::normal_distribution<float> dist { 0, 0.5 };
std::vector<float> cutoff;
std::vector<float> q;
std::vector<float> pksh;
std::vector<float> inputLeft;
std::vector<float> inputRight;
std::vector<float> outputLeft;
std::vector<float> outputRight;
};
BENCHMARK_DEFINE_F(FilterFixture, OnePole_MonoOnce)(benchmark::State& state) {
ScopedFTZ ftz;
2020-02-08 12:50:44 +01:00
sfz::Filter filterLeft;
2020-02-08 12:56:03 +01:00
filterLeft.init(sampleRate);
2020-02-07 11:11:22 +01:00
filterLeft.setType(sfz::FilterType::kFilterLpf1p);
for (auto _ : state)
{
const auto step = static_cast<size_t>(state.range(0));
auto cutoffPtr = cutoff.data();
auto inLPtr = inputLeft.data();
auto outLPtr = outputRight.data();
const auto sentinel = cutoff.data() + blockSize;
while (cutoffPtr < sentinel)
2020-02-07 11:11:22 +01:00
{
filterLeft.process( &inLPtr, &outLPtr, *cutoffPtr, 0.0, 0.0, step);
cutoffPtr += step;
inLPtr += step;
outLPtr += step;
2020-02-07 11:11:22 +01:00
}
}
}
BENCHMARK_DEFINE_F(FilterFixture, OnePole_MonoTwice)(benchmark::State& state) {
ScopedFTZ ftz;
2020-02-08 12:50:44 +01:00
sfz::Filter filterLeft;
sfz::Filter filterRight;
2020-02-08 12:56:03 +01:00
filterLeft.init(sampleRate);
2020-02-07 11:11:22 +01:00
filterLeft.setType(sfz::FilterType::kFilterLpf1p);
2020-02-08 12:56:03 +01:00
filterRight.init(sampleRate);
2020-02-07 11:11:22 +01:00
filterRight.setType(sfz::FilterType::kFilterLpf1p);
for (auto _ : state)
{
const auto step = static_cast<size_t>(state.range(0));
auto cutoffPtr = cutoff.data();
auto inLPtr = inputLeft.data();
auto inRPtr = inputRight.data();
auto outRPtr = outputLeft.data();
auto outLPtr = outputRight.data();
const auto sentinel = cutoff.data() + blockSize;
while (cutoffPtr < sentinel)
2020-02-07 11:11:22 +01:00
{
filterLeft.process( &inLPtr, &outLPtr, *cutoffPtr, 0.0, 0.0, step);
filterRight.process(&inRPtr, &outRPtr, *cutoffPtr, 0.0, 0.0, step);
cutoffPtr += step;
inLPtr += step;
inRPtr += step;
outLPtr += step;
outRPtr += step;
2020-02-07 11:11:22 +01:00
}
}
}
BENCHMARK_DEFINE_F(FilterFixture, OnePole_Stereo)(benchmark::State& state) {
ScopedFTZ ftz;
2020-02-08 12:50:44 +01:00
sfz::Filter filter;
2020-02-08 12:56:03 +01:00
filter.init(sampleRate);
2020-02-08 12:50:44 +01:00
filter.setChannels(2);
2020-02-07 11:11:22 +01:00
filter.setType(sfz::FilterType::kFilterLpf1p);
for (auto _ : state)
{
const auto step = static_cast<size_t>(state.range(0));
auto cutoffPtr = cutoff.data();
auto inLPtr = inputLeft.data();
auto inRPtr = inputRight.data();
auto outRPtr = outputLeft.data();
auto outLPtr = outputRight.data();
const auto sentinel = cutoff.data() + blockSize;
while (cutoffPtr < sentinel)
2020-02-07 11:11:22 +01:00
{
float * inputs[2] = { inLPtr, inRPtr };
float * outputs[2] = { outLPtr, outRPtr };
filter.process(inputs, outputs, *cutoffPtr, 0.0, 0.0, step);
cutoffPtr += step;
inLPtr += step;
inRPtr += step;
outLPtr += step;
outRPtr += step;
2020-02-07 11:11:22 +01:00
}
}
}
BENCHMARK_DEFINE_F(FilterFixture, TwoPole_MonoOnce)(benchmark::State& state) {
ScopedFTZ ftz;
2020-02-08 12:50:44 +01:00
sfz::Filter filterLeft;
2020-02-08 12:56:03 +01:00
filterLeft.init(sampleRate);
2020-02-07 11:11:22 +01:00
filterLeft.setType(sfz::FilterType::kFilterLpf2p);
for (auto _ : state)
{
const auto step = static_cast<size_t>(state.range(0));
auto cutoffPtr = cutoff.data();
auto qPtr = q.data();
auto inLPtr = inputLeft.data();
auto outLPtr = outputRight.data();
const auto sentinel = cutoff.data() + blockSize;
while (cutoffPtr < sentinel)
2020-02-07 11:11:22 +01:00
{
filterLeft.process(&inLPtr, &outLPtr, *cutoffPtr, *qPtr, 0.0, step);
cutoffPtr += step;
qPtr += step;
inLPtr += step;
outLPtr += step;
2020-02-07 11:11:22 +01:00
}
}
}
BENCHMARK_DEFINE_F(FilterFixture, TwoPole_MonoTwice)(benchmark::State& state) {
ScopedFTZ ftz;
2020-02-08 12:50:44 +01:00
sfz::Filter filterLeft;
sfz::Filter filterRight;
2020-02-08 12:56:03 +01:00
filterLeft.init(sampleRate);
2020-02-07 11:11:22 +01:00
filterLeft.setType(sfz::FilterType::kFilterLpf2p);
2020-02-08 12:56:03 +01:00
filterRight.init(sampleRate);
2020-02-07 11:11:22 +01:00
filterRight.setType(sfz::FilterType::kFilterLpf2p);
for (auto _ : state)
{
const auto step = static_cast<size_t>(state.range(0));
auto cutoffPtr = cutoff.data();
auto qPtr = q.data();
auto inLPtr = inputLeft.data();
auto inRPtr = inputRight.data();
auto outRPtr = outputLeft.data();
auto outLPtr = outputRight.data();
const auto sentinel = cutoff.data() + blockSize;
while (cutoffPtr < sentinel)
2020-02-07 11:11:22 +01:00
{
filterLeft.process( &inLPtr, &outLPtr, *cutoffPtr, *qPtr, 0.0, step);
filterRight.process(&inRPtr, &outRPtr, *cutoffPtr, *qPtr, 0.0, step);
cutoffPtr += step;
qPtr += step;
inLPtr += step;
inRPtr += step;
outLPtr += step;
outRPtr += step;
2020-02-07 11:11:22 +01:00
}
}
}
BENCHMARK_DEFINE_F(FilterFixture, TwoPole_Stereo)(benchmark::State& state) {
ScopedFTZ ftz;
2020-02-08 12:50:44 +01:00
sfz::Filter filter;
2020-02-08 12:56:03 +01:00
filter.init(sampleRate);
2020-02-08 12:50:44 +01:00
filter.setChannels(2);
2020-02-07 11:11:22 +01:00
filter.setType(sfz::FilterType::kFilterLpf2p);
for (auto _ : state)
{
const auto step = static_cast<size_t>(state.range(0));
auto cutoffPtr = cutoff.data();
auto qPtr = q.data();
auto inLPtr = inputLeft.data();
auto inRPtr = inputRight.data();
auto outRPtr = outputLeft.data();
auto outLPtr = outputRight.data();
const auto sentinel = cutoff.data() + blockSize;
while (cutoffPtr < sentinel)
2020-02-07 11:11:22 +01:00
{
float * inputs[2] = { inLPtr, inRPtr };
float * outputs[2] = { outLPtr, outRPtr };
filter.process(inputs, outputs, *cutoffPtr, *qPtr, 0.0, step);
cutoffPtr += step;
qPtr += step;
inLPtr += step;
inRPtr += step;
outLPtr += step;
outRPtr += step;
2020-02-07 11:11:22 +01:00
}
}
}
BENCHMARK_DEFINE_F(FilterFixture, Shelf_MonoOnce)(benchmark::State& state) {
ScopedFTZ ftz;
2020-02-08 12:50:44 +01:00
sfz::Filter filterLeft;
2020-02-08 12:56:03 +01:00
filterLeft.init(sampleRate);
2020-02-07 11:11:22 +01:00
filterLeft.setType(sfz::FilterType::kFilterLpf2p);
for (auto _ : state)
{
const auto step = static_cast<size_t>(state.range(0));
auto cutoffPtr = cutoff.data();
auto qPtr = q.data();
auto pkshPtr = pksh.data();
auto inLPtr = inputLeft.data();
auto outLPtr = outputRight.data();
const auto sentinel = cutoff.data() + blockSize;
while (cutoffPtr < sentinel)
2020-02-07 11:11:22 +01:00
{
filterLeft.process(&inLPtr, &outLPtr, *cutoffPtr, *qPtr, *pkshPtr, step);
cutoffPtr += step;
qPtr += step;
pkshPtr += step;
inLPtr += step;
outLPtr += step;
2020-02-07 11:11:22 +01:00
}
}
}
BENCHMARK_DEFINE_F(FilterFixture, Shelf_MonoTwice)(benchmark::State& state) {
ScopedFTZ ftz;
2020-02-08 12:50:44 +01:00
sfz::Filter filterLeft;
sfz::Filter filterRight;
2020-02-08 12:56:03 +01:00
filterLeft.init(sampleRate);
2020-02-07 11:11:22 +01:00
filterLeft.setType(sfz::FilterType::kFilterLpf2p);
2020-02-08 12:56:03 +01:00
filterRight.init(sampleRate);
2020-02-07 11:11:22 +01:00
for (auto _ : state)
{
const auto step = static_cast<size_t>(state.range(0));
auto cutoffPtr = cutoff.data();
auto qPtr = q.data();
auto pkshPtr = pksh.data();
auto inLPtr = inputLeft.data();
auto inRPtr = inputRight.data();
auto outRPtr = outputLeft.data();
auto outLPtr = outputRight.data();
const auto sentinel = cutoff.data() + blockSize;
while (cutoffPtr < sentinel)
2020-02-07 11:11:22 +01:00
{
filterLeft.process( &inLPtr, &outLPtr, *cutoffPtr, *qPtr, *pkshPtr, step);
filterRight.process(&inRPtr, &outRPtr, *cutoffPtr, *qPtr, *pkshPtr, step);
cutoffPtr += step;
qPtr += step;
pkshPtr += step;
inLPtr += step;
inRPtr += step;
outLPtr += step;
outRPtr += step;
2020-02-07 11:11:22 +01:00
}
}
}
BENCHMARK_DEFINE_F(FilterFixture, Shelf_Stereo)(benchmark::State& state) {
ScopedFTZ ftz;
2020-02-08 12:50:44 +01:00
sfz::Filter filter;
2020-02-08 12:56:03 +01:00
filter.init(sampleRate);
2020-02-08 12:50:44 +01:00
filter.setChannels(2);
2020-02-07 11:11:22 +01:00
filter.setType(sfz::FilterType::kFilterLpf2p);
for (auto _ : state)
{
const auto step = static_cast<size_t>(state.range(0));
auto cutoffPtr = cutoff.data();
auto qPtr = q.data();
auto pkshPtr = pksh.data();
auto inLPtr = inputLeft.data();
auto inRPtr = inputRight.data();
auto outRPtr = outputLeft.data();
auto outLPtr = outputRight.data();
const auto sentinel = cutoff.data() + blockSize;
while (cutoffPtr < sentinel)
2020-02-07 11:11:22 +01:00
{
float * inputs[2] = { inLPtr, inRPtr };
float * outputs[2] = { outLPtr, outRPtr };
filter.process(inputs, outputs, *cutoffPtr, *qPtr, *pkshPtr, step);
cutoffPtr += step;
qPtr += step;
pkshPtr += step;
inLPtr += step;
inRPtr += step;
outLPtr += step;
outRPtr += step;
2020-02-07 11:11:22 +01:00
}
}
}
BENCHMARK_REGISTER_F(FilterFixture, OnePole_MonoOnce)->RangeMultiplier(2)->Range(1, 1 << 8);
BENCHMARK_REGISTER_F(FilterFixture, OnePole_MonoTwice)->RangeMultiplier(2)->Range(1, 1 << 8);
BENCHMARK_REGISTER_F(FilterFixture, OnePole_Stereo)->RangeMultiplier(2)->Range(1, 1 << 8);
BENCHMARK_REGISTER_F(FilterFixture, TwoPole_MonoOnce)->RangeMultiplier(2)->Range(1, 1 << 8);
BENCHMARK_REGISTER_F(FilterFixture, TwoPole_MonoTwice)->RangeMultiplier(2)->Range(1, 1 << 8);
BENCHMARK_REGISTER_F(FilterFixture, TwoPole_Stereo)->RangeMultiplier(2)->Range(1, 1 << 8);
BENCHMARK_REGISTER_F(FilterFixture, Shelf_MonoOnce)->RangeMultiplier(2)->Range(1, 1 << 8);
BENCHMARK_REGISTER_F(FilterFixture, Shelf_MonoTwice)->RangeMultiplier(2)->Range(1, 1 << 8);
BENCHMARK_REGISTER_F(FilterFixture, Shelf_Stereo)->RangeMultiplier(2)->Range(1, 1 << 8);
BENCHMARK_MAIN();