|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#include <assert.h> |
| 5 | +#include <benchmark/benchmark.h> |
| 6 | +#include <bit> |
| 7 | + |
| 8 | +#include <utility.hpp> |
| 9 | + |
| 10 | +template <typename T> |
| 11 | +static void has_single_bit_if(benchmark::State& state) { |
| 12 | + const auto random_v = random_vector<T>(8); |
| 13 | + for (auto _ : state) { |
| 14 | + benchmark::DoNotOptimize(random_v); |
| 15 | + int count_true = 0, count_false = 0; |
| 16 | + for (const auto& x : random_v) { |
| 17 | + if (std::has_single_bit(x)) { |
| 18 | + benchmark::DoNotOptimize(count_true++); |
| 19 | + } else { |
| 20 | + benchmark::DoNotOptimize(count_false++); |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +template <typename T> |
| 27 | +static void has_single_bit(benchmark::State& state) { |
| 28 | + const auto random_v = random_vector<T>(8); |
| 29 | + assert(random_v.size() % 4 == 0); |
| 30 | + for (auto _ : state) { |
| 31 | + benchmark::DoNotOptimize(random_v); |
| 32 | + int r = 0, count_false = 0; |
| 33 | + for (size_t i = 0; i < random_v.size(); i += 4) { |
| 34 | + r += std::has_single_bit(random_v[i + 0]); |
| 35 | + r += std::has_single_bit(random_v[i + 1]); |
| 36 | + r += std::has_single_bit(random_v[i + 2]); |
| 37 | + r += std::has_single_bit(random_v[i + 3]); |
| 38 | + } |
| 39 | + benchmark::DoNotOptimize(r); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +BENCHMARK(has_single_bit_if<uint16_t>); |
| 44 | +BENCHMARK(has_single_bit_if<uint32_t>); |
| 45 | +BENCHMARK(has_single_bit_if<uint64_t>); |
| 46 | + |
| 47 | +BENCHMARK(has_single_bit<uint16_t>); |
| 48 | +BENCHMARK(has_single_bit<uint32_t>); |
| 49 | +BENCHMARK(has_single_bit<uint64_t>); |
| 50 | + |
| 51 | +BENCHMARK_MAIN(); |
0 commit comments