1#include <cstdint>
2
3#include "benchmark/benchmark.h"
4
5namespace {
6#if defined(__GNUC__)
7std::int64_t double_up(const std::int64_t x) __attribute__((const));
8#endif
9std::int64_t double_up(const std::int64_t x) { return x * 2; }
10} // namespace
11
12// Using DoNotOptimize on types like BitRef seem to cause a lot of problems
13// with the inline assembly on both GCC and Clang.
14struct BitRef {
15 int index;
16 unsigned char& byte;
17
18 public:
19 static BitRef Make() {
20 static unsigned char arr[2] = {};
21 BitRef b(1, arr[0]);
22 return b;
23 }
24
25 private:
26 BitRef(int i, unsigned char& b) : index(i), byte(b) {}
27};
28
29int main(int, char*[]) {
30 // this test verifies compilation of DoNotOptimize() for some types
31
32 char buffer1[1] = "";
33 benchmark::DoNotOptimize(value&: buffer1);
34
35 char buffer2[2] = "";
36 benchmark::DoNotOptimize(value&: buffer2);
37
38 char buffer3[3] = "";
39 benchmark::DoNotOptimize(value&: buffer3);
40
41 char buffer8[8] = "";
42 benchmark::DoNotOptimize(value&: buffer8);
43
44 char buffer20[20] = "";
45 benchmark::DoNotOptimize(value&: buffer20);
46
47 char buffer1024[1024] = "";
48 benchmark::DoNotOptimize(value&: buffer1024);
49 char* bptr = &buffer1024[0];
50 benchmark::DoNotOptimize(value&: bptr);
51
52 int x = 123;
53 benchmark::DoNotOptimize(value&: x);
54 int* xp = &x;
55 benchmark::DoNotOptimize(value&: xp);
56 benchmark::DoNotOptimize(value&: x += 42);
57
58 std::int64_t y = double_up(x);
59 benchmark::DoNotOptimize(value&: y);
60
61 // These tests are to e
62 BitRef lval = BitRef::Make();
63 benchmark::DoNotOptimize(value&: lval);
64
65#ifdef BENCHMARK_HAS_CXX11
66 // Check that accept rvalue.
67 benchmark::DoNotOptimize(value: BitRef::Make());
68#endif
69}
70

source code of third-party/benchmark/test/donotoptimize_test.cc