File: bit_benchmark.cpp

package info (click to toggle)
seqan3 3.4.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,580 kB
  • sloc: cpp: 145,192; sh: 307; xml: 264; javascript: 95; makefile: 70; perl: 29; php: 15
file content (80 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause

#include <benchmark/benchmark.h>

#include <bit>
#include <cstdlib>

template <typename size_type>
static void is_power_of_two_popcount(benchmark::State & state)
{
    std::srand(0);
    size_type n = 0;
    for (auto _ : state)
    {
        n = std::rand();
        if constexpr (std::is_same_v<size_type, unsigned long long>)
        {
            bool result = __builtin_popcountll(n) == 1;
            benchmark::DoNotOptimize(result);
        }
        else if constexpr (std::is_same_v<size_type, unsigned long>)
        {
            bool result = __builtin_popcountl(n) == 1;
            benchmark::DoNotOptimize(result);
        }
        else if constexpr (std::is_same_v<size_type, unsigned>)
        {
            bool result = __builtin_popcount(n) == 1;
            benchmark::DoNotOptimize(result);
        }
        else
            static_assert(std::is_same_v<size_type, void>, "FAILED");
    }
}
BENCHMARK_TEMPLATE(is_power_of_two_popcount, unsigned);
BENCHMARK_TEMPLATE(is_power_of_two_popcount, unsigned long);
BENCHMARK_TEMPLATE(is_power_of_two_popcount, unsigned long long);

static void is_power_of_two_arithmetic(benchmark::State & state)
{
    std::srand(0);
    size_t n = 0;
    for (auto _ : state)
    {
        n = std::rand();
        bool result = n > 0 && (n & (n - 1)) == 0;
        benchmark::DoNotOptimize(result);
    }
}
BENCHMARK(is_power_of_two_arithmetic);

static void is_power_of_two_std(benchmark::State & state)
{
    std::srand(0);
    size_t n = 0;
    for (auto _ : state)
    {
        n = std::rand();
        bool result = std::has_single_bit(n);
        benchmark::DoNotOptimize(result);
    }
}
BENCHMARK(is_power_of_two_std);

static void next_power_of_two_std(benchmark::State & state)
{
    std::srand(0);
    size_t n = 0;
    for (auto _ : state)
    {
        n = std::rand();
        bool result = std::bit_ceil(n);
        benchmark::DoNotOptimize(result);
    }
}
BENCHMARK(next_power_of_two_std);

BENCHMARK_MAIN();