cycleclock_config.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2022 The Abseil Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_BASE_INTERNAL_CYCLECLOCK_CONFIG_H_
  15. #define ABSL_BASE_INTERNAL_CYCLECLOCK_CONFIG_H_
  16. #include <cstdint>
  17. #include "absl/base/config.h"
  18. #include "absl/base/internal/inline_variable.h"
  19. #include "absl/base/internal/unscaledcycleclock_config.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace base_internal {
  23. #if ABSL_USE_UNSCALED_CYCLECLOCK
  24. #ifdef NDEBUG
  25. #ifdef ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
  26. // Not debug mode and the UnscaledCycleClock frequency is the CPU
  27. // frequency. Scale the CycleClock to prevent overflow if someone
  28. // tries to represent the time as cycles since the Unix epoch.
  29. ABSL_INTERNAL_INLINE_CONSTEXPR(int32_t, kCycleClockShift, 1);
  30. #else
  31. // Not debug mode and the UnscaledCycleClock isn't operating at the
  32. // raw CPU frequency. There is no need to do any scaling, so don't
  33. // needlessly sacrifice precision.
  34. ABSL_INTERNAL_INLINE_CONSTEXPR(int32_t, kCycleClockShift, 0);
  35. #endif
  36. #else // NDEBUG
  37. // In debug mode use a different shift to discourage depending on a
  38. // particular shift value.
  39. ABSL_INTERNAL_INLINE_CONSTEXPR(int32_t, kCycleClockShift, 2);
  40. #endif // NDEBUG
  41. ABSL_INTERNAL_INLINE_CONSTEXPR(double, kCycleClockFrequencyScale,
  42. 1.0 / (1 << kCycleClockShift));
  43. #endif // ABSL_USE_UNSCALED_CYCLECLOC
  44. } // namespace base_internal
  45. ABSL_NAMESPACE_END
  46. } // namespace absl
  47. #endif // ABSL_BASE_INTERNAL_CYCLECLOCK_CONFIG_H_