RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
BitStreamerBenchmark.cpp
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2017 Roman Lebedev
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#include "adt/Array1DRef.h"
22#include "adt/Casts.h"
23#include "bench/Common.h"
29#include <cassert>
30#include <cstddef>
31#include <benchmark/benchmark.h>
32
33#ifndef NDEBUG
34#include "io/Buffer.h"
35#endif
36
37#ifndef DEBUG
38#include <cstdint>
39#include <string>
40#include <vector>
41#endif
42
43#ifdef DEBUG
44#include "common/Common.h"
45#include <limits>
46#endif
47
53
54namespace {
55
56constexpr const int STEP_MAX = 32;
57
58template <typename Pump>
59inline void BM_BitStreamer(benchmark::State& state, int fillSize, int Step) {
60 assert(state.range(0) > 0);
61 assert(static_cast<size_t>(state.range(0)) <=
62 std::numeric_limits<rawspeed::Buffer::size_type>::max());
63
64 assert(fillSize > 0);
65 assert(fillSize <= STEP_MAX);
66
67 assert(Step > 0);
68 assert(Step <= STEP_MAX);
69
70 assert(Step <= fillSize);
71
72 assert((Step == 1) || rawspeed::isAligned(Step, 2));
73 assert((fillSize == 1) || rawspeed::isAligned(fillSize, 2));
74
75 const std::vector<uint8_t> inputStorage(
76 rawspeed::implicit_cast<size_t>(state.range(0)));
78 inputStorage.data(), rawspeed::implicit_cast<int>(state.range(0)));
79
80 int processedBits = 0;
81 for (auto _ : state) {
82 Pump pump(input);
83
84 for (processedBits = 0; processedBits <= 8 * input.size();) {
85 pump.fill(fillSize);
86
87 // NOTE: you may want to change the callee here
88 for (auto i = 0; i < fillSize; i += Step)
89 pump.skipBitsNoFill(Step);
90
91 processedBits += fillSize;
92 }
93 }
94
95 assert(processedBits > fillSize);
96 processedBits -= fillSize;
97
98 assert(rawspeed::roundUp(8 * input.size(), fillSize) ==
100
101 state.SetComplexityN(processedBits / 8);
102 state.SetItemsProcessed(processedBits * state.iterations());
103 state.SetBytesProcessed(state.items_processed() / 8);
104}
105
106inline void CustomArguments(benchmark::internal::Benchmark* b) {
107 if (benchmarkDryRun()) {
108 b->Arg((512U * (1U << 10U)) / 10);
109 return;
110 }
111
112 b->RangeMultiplier(2);
113 if constexpr ((true)) {
114 b->Arg(256 << 20);
115 } else {
116 b->Range(1, 1024 << 20);
117 b->Complexity(benchmark::oN);
118 }
119 b->Unit(benchmark::kMillisecond);
120}
121
122template <typename PUMP> void registerPump(const char* pumpName) {
123 for (size_t i = 1; i <= STEP_MAX; i *= 2) {
124 for (size_t j = 1; j <= i && j <= STEP_MAX; j *= 2) {
125 std::string name("BM_BitStreamer<");
126 name += "Spec<";
127 name += pumpName;
128 name += ">, Fill<";
129 name += std::to_string(i);
130 name += ">, Step<";
131 name += std::to_string(j);
132 name += ">>";
133
134 const auto Fn = BM_BitStreamer<PUMP>;
135 auto* b = benchmark::RegisterBenchmark(name, Fn, i, j);
136 b->Apply(CustomArguments);
137 }
138 }
139}
140
141} // namespace
142
143#define REGISTER_PUMP(PUMP) registerPump<PUMP>(#PUMP)
144
145int main(int argc, char** argv) {
146 benchmark::MaybeReenterWithoutASLR(argc, argv);
147
153
154 benchmark::Initialize(&argc, argv);
155 benchmark::RunSpecifiedBenchmarks();
156}
#define REGISTER_PUMP(PUMP)
assert(dim.area() >=area)
bool RAWSPEED_READNONE benchmarkDryRun()
int RAWSPEED_READONLY size() const
int main()
void CustomArguments(benchmark::internal::Benchmark *b)
void BM_BitStreamer(benchmark::State &state, int fillSize, int Step)
constexpr uint64_t RAWSPEED_READNONE roundUp(uint64_t value, uint64_t multiple)
Definition Common.h:134
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32
constexpr RAWSPEED_READNONE bool isAligned(T value, size_t multiple)
Definition Common.h:151