xref: /aosp_15_r20/external/abseil-cpp/absl/base/internal/unscaledcycleclock_config.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
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_UNSCALEDCYCLECLOCK_CONFIG_H_
16 #define ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_CONFIG_H_
17 
18 #if defined(__APPLE__)
19 #include <TargetConditionals.h>
20 #endif
21 
22 // The following platforms have an implementation of a hardware counter.
23 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || \
24     defined(__powerpc__) || defined(__ppc__) || defined(_M_IX86) ||     \
25     (defined(_M_X64) && !defined(_M_ARM64EC))
26 #define ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION 1
27 #else
28 #define ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION 0
29 #endif
30 
31 // The following platforms often disable access to the hardware
32 // counter (through a sandbox) even if the underlying hardware has a
33 // usable counter. The CycleTimer interface also requires a *scaled*
34 // CycleClock that runs at atleast 1 MHz. We've found some Android
35 // ARM64 devices where this is not the case, so we disable it by
36 // default on Android ARM64.
37 #if defined(__native_client__) || (defined(__APPLE__)) || \
38     (defined(__ANDROID__) && defined(__aarch64__))
39 #define ABSL_USE_UNSCALED_CYCLECLOCK_DEFAULT 0
40 #else
41 #define ABSL_USE_UNSCALED_CYCLECLOCK_DEFAULT 1
42 #endif
43 
44 // UnscaledCycleClock is an optional internal feature.
45 // Use "#if ABSL_USE_UNSCALED_CYCLECLOCK" to test for its presence.
46 // Can be overridden at compile-time via -DABSL_USE_UNSCALED_CYCLECLOCK=0|1
47 #if !defined(ABSL_USE_UNSCALED_CYCLECLOCK)
48 #define ABSL_USE_UNSCALED_CYCLECLOCK               \
49   (ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION && \
50    ABSL_USE_UNSCALED_CYCLECLOCK_DEFAULT)
51 #endif
52 
53 #if ABSL_USE_UNSCALED_CYCLECLOCK
54 // This macro can be used to test if UnscaledCycleClock::Frequency()
55 // is NominalCPUFrequency() on a particular platform.
56 #if (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
57      defined(_M_X64))
58 #define ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
59 #endif
60 #endif
61 
62 #endif  // ABSL_BASE_INTERNAL_UNSCALEDCYCLECLOCK_CONFIG_H_
63