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.

64 lines
1.4 KiB

2 months ago
  1. #include <cstdint>
  2. #include "benchmark/benchmark.h"
  3. namespace {
  4. #if defined(__GNUC__)
  5. std::int64_t double_up(const std::int64_t x) __attribute__((const));
  6. #endif
  7. std::int64_t double_up(const std::int64_t x) { return x * 2; }
  8. } // namespace
  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 buffer1[1] = "";
  26. benchmark::DoNotOptimize(buffer1);
  27. char buffer2[2] = "";
  28. benchmark::DoNotOptimize(buffer2);
  29. char buffer3[3] = "";
  30. benchmark::DoNotOptimize(buffer3);
  31. char buffer8[8] = "";
  32. benchmark::DoNotOptimize(buffer8);
  33. char buffer20[20] = "";
  34. benchmark::DoNotOptimize(buffer20);
  35. char buffer1024[1024] = "";
  36. benchmark::DoNotOptimize(buffer1024);
  37. char* bptr = &buffer1024[0];
  38. benchmark::DoNotOptimize(bptr);
  39. int x = 123;
  40. benchmark::DoNotOptimize(x);
  41. int* xp = &x;
  42. benchmark::DoNotOptimize(xp);
  43. benchmark::DoNotOptimize(x += 42);
  44. std::int64_t y = double_up(x);
  45. benchmark::DoNotOptimize(y);
  46. // These tests are to e
  47. BitRef lval = BitRef::Make();
  48. benchmark::DoNotOptimize(lval);
  49. }