| 1 |
#include <cstdint> |
| 2 |
|
| 3 |
#include "benchmark/benchmark.h" |
| 4 |
|
| 5 |
namespace { |
| 6 |
#if defined(__GNUC__) |
| 7 |
std::int64_t double_up(const std::int64_t x) __attribute__((const)); |
| 8 |
#endif |
| 9 |
std::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. |
| 14 |
struct 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 |
|
| 29 |
int main(int, char*[]) { |
| 30 |
// this test verifies compilation of DoNotOptimize() for some types |
| 31 |
|
| 32 |
char buffer1[1] = ""; |
| 33 |
benchmark::DoNotOptimize(buffer1); |
| 34 |
|
| 35 |
char buffer2[2] = ""; |
| 36 |
benchmark::DoNotOptimize(buffer2); |
| 37 |
|
| 38 |
char buffer3[3] = ""; |
| 39 |
benchmark::DoNotOptimize(buffer3); |
| 40 |
|
| 41 |
char buffer8[8] = ""; |
| 42 |
benchmark::DoNotOptimize(buffer8); |
| 43 |
|
| 44 |
char buffer20[20] = ""; |
| 45 |
benchmark::DoNotOptimize(buffer20); |
| 46 |
|
| 47 |
char buffer1024[1024] = ""; |
| 48 |
benchmark::DoNotOptimize(buffer1024); |
| 49 |
char* bptr = &buffer1024[0]; |
| 50 |
benchmark::DoNotOptimize(bptr); |
| 51 |
|
| 52 |
int x = 123; |
| 53 |
benchmark::DoNotOptimize(x); |
| 54 |
int* xp = &x; |
| 55 |
benchmark::DoNotOptimize(xp); |
| 56 |
benchmark::DoNotOptimize(x += 42); |
| 57 |
|
| 58 |
std::int64_t y = double_up(x); |
| 59 |
benchmark::DoNotOptimize(y); |
| 60 |
|
| 61 |
// These tests are to e |
| 62 |
BitRef lval = BitRef::Make(); |
| 63 |
benchmark::DoNotOptimize(lval); |
| 64 |
|
| 65 |
#ifdef BENCHMARK_HAS_CXX11 |
| 66 |
// Check that accept rvalue. |
| 67 |
benchmark::DoNotOptimize(BitRef::Make()); |
| 68 |
#endif |
| 69 |
} |