| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include <array> |
| 10 |
#include <benchmark/benchmark.h> |
| 11 |
#include <cstring> |
| 12 |
#include <numeric> |
| 13 |
#include <random> |
| 14 |
|
| 15 |
template <class T> |
| 16 |
static std::array<T, 1000> generate(std::uniform_int_distribution<T> distribution = std::uniform_int_distribution<T>{ |
| 17 |
std::numeric_limits<T>::min() + 1, std::numeric_limits<T>::max()}) { |
| 18 |
std::mt19937 generator; |
| 19 |
std::array<T, 1000> result; |
| 20 |
std::generate_n(result.begin(), result.size(), [&] { return distribution(generator); }); |
| 21 |
return result; |
| 22 |
} |
| 23 |
|
| 24 |
static void bm_gcd_random(benchmark::State& state) { |
| 25 |
std::array data = generate<int>(); |
| 26 |
while (state.KeepRunningBatch(data.size())) |
| 27 |
for (auto v0 : data) |
| 28 |
for (auto v1 : data) |
| 29 |
benchmark::DoNotOptimize(std::gcd(v0, v1)); |
| 30 |
} |
| 31 |
BENCHMARK(bm_gcd_random); |
| 32 |
|
| 33 |
static void bm_gcd_trivial(benchmark::State& state) { |
| 34 |
int lhs = ~static_cast<int>(0), rhs = 1; |
| 35 |
for (auto _ : state) { |
| 36 |
benchmark::DoNotOptimize(lhs); |
| 37 |
benchmark::DoNotOptimize(rhs); |
| 38 |
benchmark::DoNotOptimize(std::gcd(lhs, rhs)); |
| 39 |
} |
| 40 |
} |
| 41 |
BENCHMARK(bm_gcd_trivial); |
| 42 |
|
| 43 |
static void bm_gcd_complex(benchmark::State& state) { |
| 44 |
int lhs = 2971215073, rhs = 1836311903; |
| 45 |
for (auto _ : state) { |
| 46 |
benchmark::DoNotOptimize(lhs); |
| 47 |
benchmark::DoNotOptimize(rhs); |
| 48 |
benchmark::DoNotOptimize(std::gcd(lhs, rhs)); |
| 49 |
} |
| 50 |
} |
| 51 |
BENCHMARK(bm_gcd_complex); |
| 52 |
|
| 53 |
BENCHMARK_MAIN(); |