File: log2.cpp

package info (click to toggle)
charls 2.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,636 kB
  • sloc: cpp: 13,392; ansic: 986; makefile: 22
file content (67 lines) | stat: -rw-r--r-- 1,851 bytes parent folder | download | duplicates (2)
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
// Copyright (c) Team CharLS.
// SPDX-License-Identifier: BSD-3-Clause

#include <benchmark/benchmark.h>

#include "../src/util.h"

#include <cmath>
#include <limits>


uint32_t log2_floor(const uint32_t n) noexcept
{
    return 31 - charls::countl_zero(n);
}

uint32_t max_value_to_bits_per_sample(const uint32_t max_value) noexcept
{
    ASSERT(max_value > 0);
    return log2_floor(max_value) + 1;
}


static void bm_log2_floor_floating_point(benchmark::State& state)
{
    for (const auto _ : state)
    {
        benchmark::DoNotOptimize(std::floor(std::log2(255)));
        benchmark::DoNotOptimize(std::floor(std::log2(1023)));
        benchmark::DoNotOptimize(std::floor(std::log2(std::numeric_limits<uint16_t>::max())));
    }
}
BENCHMARK(bm_log2_floor_floating_point);


static void bm_log2_floor_uint32(benchmark::State& state)
{
    for (const auto _ : state)
    {
        benchmark::DoNotOptimize(log2_floor(255));
        benchmark::DoNotOptimize(log2_floor(1023));
        benchmark::DoNotOptimize(log2_floor(std::numeric_limits<uint16_t>::max()));
    }
}
BENCHMARK(bm_log2_floor_uint32);

static void bm_log2_ceil_int32(benchmark::State& state)
{
    for (const auto _ : state)
    {
        benchmark::DoNotOptimize(charls::log2_ceil(256));
        benchmark::DoNotOptimize(charls::log2_ceil(1024));
        benchmark::DoNotOptimize(charls::log2_ceil(std::numeric_limits<uint16_t>::max()));
    }
}
BENCHMARK(bm_log2_ceil_int32);

static void bm_max_value_to_bits_per_sample(benchmark::State& state)
{
    for (const auto _ : state)
    {
        benchmark::DoNotOptimize(max_value_to_bits_per_sample(255));
        benchmark::DoNotOptimize(max_value_to_bits_per_sample(1023));
        benchmark::DoNotOptimize(max_value_to_bits_per_sample(std::numeric_limits<uint16_t>::max()));
    }
}
BENCHMARK(bm_max_value_to_bits_per_sample);