xref: /aosp_15_r20/external/webrtc/third_party/abseil-cpp/absl/base/internal/unscaledcycleclock.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 // Copyright 2017 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 #include "absl/base/internal/unscaledcycleclock.h"
16 
17 #if ABSL_USE_UNSCALED_CYCLECLOCK
18 
19 #if defined(_WIN32)
20 #include <intrin.h>
21 #endif
22 
23 #if defined(__powerpc__) || defined(__ppc__)
24 #ifdef __GLIBC__
25 #include <sys/platform/ppc.h>
26 #elif defined(__FreeBSD__)
27 // clang-format off
28 // This order does actually matter =(.
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
31 // clang-format on
32 
33 #include "absl/base/call_once.h"
34 #endif
35 #endif
36 
37 #include "absl/base/internal/sysinfo.h"
38 
39 namespace absl {
40 ABSL_NAMESPACE_BEGIN
41 namespace base_internal {
42 
43 #if defined(__i386__)
44 
Now()45 int64_t UnscaledCycleClock::Now() {
46   int64_t ret;
47   __asm__ volatile("rdtsc" : "=A"(ret));
48   return ret;
49 }
50 
Frequency()51 double UnscaledCycleClock::Frequency() {
52   return base_internal::NominalCPUFrequency();
53 }
54 
55 #elif defined(__x86_64__)
56 
57 double UnscaledCycleClock::Frequency() {
58   return base_internal::NominalCPUFrequency();
59 }
60 
61 #elif defined(__powerpc__) || defined(__ppc__)
62 
63 int64_t UnscaledCycleClock::Now() {
64 #ifdef __GLIBC__
65   return __ppc_get_timebase();
66 #else
67 #ifdef __powerpc64__
68   int64_t tbr;
69   asm volatile("mfspr %0, 268" : "=r"(tbr));
70   return tbr;
71 #else
72   int32_t tbu, tbl, tmp;
73   asm volatile(
74       "0:\n"
75       "mftbu %[hi32]\n"
76       "mftb %[lo32]\n"
77       "mftbu %[tmp]\n"
78       "cmpw %[tmp],%[hi32]\n"
79       "bne 0b\n"
80       : [ hi32 ] "=r"(tbu), [ lo32 ] "=r"(tbl), [ tmp ] "=r"(tmp));
81   return (static_cast<int64_t>(tbu) << 32) | tbl;
82 #endif
83 #endif
84 }
85 
86 double UnscaledCycleClock::Frequency() {
87 #ifdef __GLIBC__
88   return __ppc_get_timebase_freq();
89 #elif defined(_AIX)
90   // This is the same constant value as returned by
91   // __ppc_get_timebase_freq().
92   return static_cast<double>(512000000);
93 #elif defined(__FreeBSD__)
94   static once_flag init_timebase_frequency_once;
95   static double timebase_frequency = 0.0;
96   base_internal::LowLevelCallOnce(&init_timebase_frequency_once, [&]() {
97     size_t length = sizeof(timebase_frequency);
98     sysctlbyname("kern.timecounter.tc.timebase.frequency", &timebase_frequency,
99                  &length, nullptr, 0);
100   });
101   return timebase_frequency;
102 #else
103 #error Must implement UnscaledCycleClock::Frequency()
104 #endif
105 }
106 
107 #elif defined(__aarch64__)
108 
109 // System timer of ARMv8 runs at a different frequency than the CPU's.
110 // The frequency is fixed, typically in the range 1-50MHz.  It can be
111 // read at CNTFRQ special register.  We assume the OS has set up
112 // the virtual timer properly.
113 int64_t UnscaledCycleClock::Now() {
114   int64_t virtual_timer_value;
115   asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
116   return virtual_timer_value;
117 }
118 
119 double UnscaledCycleClock::Frequency() {
120   uint64_t aarch64_timer_frequency;
121   asm volatile("mrs %0, cntfrq_el0" : "=r"(aarch64_timer_frequency));
122   return aarch64_timer_frequency;
123 }
124 
125 #elif defined(__riscv)
126 
127 int64_t UnscaledCycleClock::Now() {
128   int64_t virtual_timer_value;
129   asm volatile("rdcycle %0" : "=r"(virtual_timer_value));
130   return virtual_timer_value;
131 }
132 
133 double UnscaledCycleClock::Frequency() {
134   return base_internal::NominalCPUFrequency();
135 }
136 
137 #elif defined(_M_IX86) || defined(_M_X64)
138 
139 #pragma intrinsic(__rdtsc)
140 
141 int64_t UnscaledCycleClock::Now() { return __rdtsc(); }
142 
143 double UnscaledCycleClock::Frequency() {
144   return base_internal::NominalCPUFrequency();
145 }
146 
147 #endif
148 
149 }  // namespace base_internal
150 ABSL_NAMESPACE_END
151 }  // namespace absl
152 
153 #endif  // ABSL_USE_UNSCALED_CYCLECLOCK
154