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 15 #ifndef ABSL_BASE_INTERNAL_CYCLECLOCK_CONFIG_H_ 16 #define ABSL_BASE_INTERNAL_CYCLECLOCK_CONFIG_H_ 17 18 #include <cstdint> 19 20 #include "absl/base/config.h" 21 #include "absl/base/internal/inline_variable.h" 22 #include "absl/base/internal/unscaledcycleclock_config.h" 23 24 namespace absl { 25 ABSL_NAMESPACE_BEGIN 26 namespace base_internal { 27 28 #if ABSL_USE_UNSCALED_CYCLECLOCK 29 #ifdef NDEBUG 30 #ifdef ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY 31 // Not debug mode and the UnscaledCycleClock frequency is the CPU 32 // frequency. Scale the CycleClock to prevent overflow if someone 33 // tries to represent the time as cycles since the Unix epoch. 34 ABSL_INTERNAL_INLINE_CONSTEXPR(int32_t, kCycleClockShift, 1); 35 #else 36 // Not debug mode and the UnscaledCycleClock isn't operating at the 37 // raw CPU frequency. There is no need to do any scaling, so don't 38 // needlessly sacrifice precision. 39 ABSL_INTERNAL_INLINE_CONSTEXPR(int32_t, kCycleClockShift, 0); 40 #endif 41 #else // NDEBUG 42 // In debug mode use a different shift to discourage depending on a 43 // particular shift value. 44 ABSL_INTERNAL_INLINE_CONSTEXPR(int32_t, kCycleClockShift, 2); 45 #endif // NDEBUG 46 47 ABSL_INTERNAL_INLINE_CONSTEXPR(double, kCycleClockFrequencyScale, 48 1.0 / (1 << kCycleClockShift)); 49 #endif // ABSL_USE_UNSCALED_CYCLECLOC 50 51 } // namespace base_internal 52 ABSL_NAMESPACE_END 53 } // namespace absl 54 55 #endif // ABSL_BASE_INTERNAL_CYCLECLOCK_CONFIG_H_ 56