You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.2 KiB

  1. #include "benchmark/benchmark.h"
  2. #include <cstdint>
  3. namespace {
  4. #if defined(__GNUC__)
  5. std::uint64_t double_up(const std::uint64_t x) __attribute__((const));
  6. #endif
  7. std::uint64_t double_up(const std::uint64_t x) { return x * 2; }
  8. }
  9. // Using DoNotOptimize on types like BitRef seem to cause a lot of problems
  10. // with the inline assembly on both GCC and Clang.
  11. struct BitRef {
  12. int index;
  13. unsigned char &byte;
  14. public:
  15. static BitRef Make() {
  16. static unsigned char arr[2] = {};
  17. BitRef b(1, arr[0]);
  18. return b;
  19. }
  20. private:
  21. BitRef(int i, unsigned char& b) : index(i), byte(b) {}
  22. };
  23. int main(int, char*[]) {
  24. // this test verifies compilation of DoNotOptimize() for some types
  25. char buffer8[8] = "";
  26. benchmark::DoNotOptimize(buffer8);
  27. char buffer20[20] = "";
  28. benchmark::DoNotOptimize(buffer20);
  29. char buffer1024[1024] = "";
  30. benchmark::DoNotOptimize(buffer1024);
  31. benchmark::DoNotOptimize(&buffer1024[0]);
  32. int x = 123;
  33. benchmark::DoNotOptimize(x);
  34. benchmark::DoNotOptimize(&x);
  35. benchmark::DoNotOptimize(x += 42);
  36. benchmark::DoNotOptimize(double_up(x));
  37. // These tests are to e
  38. benchmark::DoNotOptimize(BitRef::Make());
  39. BitRef lval = BitRef::Make();
  40. benchmark::DoNotOptimize(lval);
  41. }