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
|
|
|
|
2019-11-23 18:58:51 +01:00
|
|
|
#include "SIMDHelpers.h"
|
2019-08-23 15:45:33 +02:00
|
|
|
#include "absl/types/span.h"
|
2019-08-21 18:18:12 +02:00
|
|
|
#include <benchmark/benchmark.h>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <iostream>
|
2019-08-23 15:45:33 +02:00
|
|
|
#include <numeric>
|
|
|
|
|
#include <random>
|
|
|
|
|
#include <vector>
|
2019-08-21 18:18:12 +02:00
|
|
|
|
2019-08-21 18:45:59 +02:00
|
|
|
class MyFixture : public benchmark::Fixture {
|
|
|
|
|
public:
|
2019-08-23 15:45:33 +02:00
|
|
|
void SetUp(const ::benchmark::State& state)
|
2019-08-21 18:18:12 +02:00
|
|
|
{
|
2019-08-23 15:45:33 +02:00
|
|
|
std::random_device rd {};
|
|
|
|
|
std::mt19937 gen { rd() };
|
2020-01-05 00:14:01 +01:00
|
|
|
std::uniform_real_distribution<float> dist { 0.1f, 1.0f };
|
2019-08-23 15:45:33 +02:00
|
|
|
source = std::vector<float>(state.range(0));
|
|
|
|
|
result = std::vector<float>(state.range(0));
|
2020-02-13 07:37:06 +01:00
|
|
|
intResult = std::vector<int>(state.range(0));
|
2019-08-23 15:45:33 +02:00
|
|
|
std::generate(source.begin(), source.end(), [&]() { return dist(gen); });
|
2019-08-21 18:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-11 00:18:18 +01:00
|
|
|
void TearDown(const ::benchmark::State& /* state */)
|
2019-08-21 18:18:12 +02:00
|
|
|
{
|
2019-08-23 15:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<float> source;
|
|
|
|
|
std::vector<float> result;
|
2020-02-13 07:37:06 +01:00
|
|
|
std::vector<int> intResult;
|
2019-08-23 15:45:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BENCHMARK_DEFINE_F(MyFixture, Dummy)
|
|
|
|
|
(benchmark::State& state)
|
|
|
|
|
{
|
|
|
|
|
for (auto _ : state) {
|
2019-08-21 18:18:12 +02:00
|
|
|
for (int i = 0; i < state.range(0); ++i)
|
2019-08-23 15:45:33 +02:00
|
|
|
result[i] = source[i];
|
2019-08-21 18:18:12 +02:00
|
|
|
benchmark::DoNotOptimize(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 07:37:06 +01:00
|
|
|
BENCHMARK_DEFINE_F(MyFixture, ScalarLibmFloorLog2)
|
|
|
|
|
(benchmark::State& state)
|
|
|
|
|
{
|
|
|
|
|
for (auto _ : state) {
|
|
|
|
|
for (size_t i = 0, n = source.size(); i < n; ++i) {
|
|
|
|
|
intResult[i] = static_cast<int>(
|
|
|
|
|
std::floor(std::log2(std::fabs(source[i]))));
|
|
|
|
|
}
|
|
|
|
|
benchmark::DoNotOptimize(intResult);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BENCHMARK_DEFINE_F(MyFixture, ScalarFastFloorLog2)
|
|
|
|
|
(benchmark::State& state)
|
|
|
|
|
{
|
|
|
|
|
for (auto _ : state) {
|
|
|
|
|
for (size_t i = 0, n = source.size(); i < n; ++i) {
|
|
|
|
|
intResult[i] = fp_exponent(source[i]);
|
|
|
|
|
}
|
|
|
|
|
benchmark::DoNotOptimize(intResult);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 18:45:59 +02:00
|
|
|
BENCHMARK_REGISTER_F(MyFixture, Dummy)->RangeMultiplier(4)->Range(1 << 6, 1 << 10);
|
2020-02-13 07:37:06 +01:00
|
|
|
BENCHMARK_REGISTER_F(MyFixture, ScalarLibmFloorLog2)->RangeMultiplier(4)->Range(1 << 6, 1 << 10);
|
|
|
|
|
BENCHMARK_REGISTER_F(MyFixture, ScalarFastFloorLog2)->RangeMultiplier(4)->Range(1 << 6, 1 << 10);
|
2019-08-21 18:45:59 +02:00
|
|
|
|
2020-01-05 00:14:01 +01:00
|
|
|
BENCHMARK_MAIN();
|