[Workflow] Try to fix code-formatter failing to find changes in some cases.
[llvm-project.git] / third-party / benchmark / test / donotoptimize_test.cc
blobc321f156a1e00f7541c212cd159969cb4afcb4bc
1 #include <cstdint>
3 #include "benchmark/benchmark.h"
5 namespace {
6 #if defined(__GNUC__)
7 std::uint64_t double_up(const std::uint64_t x) __attribute__((const));
8 #endif
9 std::uint64_t double_up(const std::uint64_t x) { return x * 2; }
10 } // namespace
12 // Using DoNotOptimize on types like BitRef seem to cause a lot of problems
13 // with the inline assembly on both GCC and Clang.
14 struct BitRef {
15 int index;
16 unsigned char& byte;
18 public:
19 static BitRef Make() {
20 static unsigned char arr[2] = {};
21 BitRef b(1, arr[0]);
22 return b;
25 private:
26 BitRef(int i, unsigned char& b) : index(i), byte(b) {}
29 int main(int, char*[]) {
30 // this test verifies compilation of DoNotOptimize() for some types
32 char buffer8[8] = "";
33 benchmark::DoNotOptimize(buffer8);
35 char buffer20[20] = "";
36 benchmark::DoNotOptimize(buffer20);
38 char buffer1024[1024] = "";
39 benchmark::DoNotOptimize(buffer1024);
40 benchmark::DoNotOptimize(&buffer1024[0]);
42 int x = 123;
43 benchmark::DoNotOptimize(x);
44 benchmark::DoNotOptimize(&x);
45 benchmark::DoNotOptimize(x += 42);
47 benchmark::DoNotOptimize(double_up(x));
49 // These tests are to e
50 benchmark::DoNotOptimize(BitRef::Make());
51 BitRef lval = BitRef::Make();
52 benchmark::DoNotOptimize(lval);