19
19
#include < cstdlib>
20
20
#include < cstring>
21
21
#include < ctime>
22
- #include < chrono>
23
22
#ifdef _WIN32
24
23
#include " flang/Common/windows-include.h"
25
24
#else
@@ -124,17 +123,10 @@ static constexpr inline unsigned_count_t GetHUGE(int kind) {
124
123
return (unsigned_count_t {1 } << ((8 * kind) - 1 )) - 1 ;
125
124
}
126
125
127
- // This is the fallback implementation, which should work everywhere.
128
- template <typename Unused = void >
129
- count_t GetSystemClockCount (int kind, fallback_implementation) {
130
- std::timespec tspec;
131
-
132
- if (std::timespec_get (&tspec, TIME_UTC) < 0 ) {
133
- // Return -HUGE(COUNT) to represent failure.
134
- return -static_cast <count_t >(GetHUGE (kind));
135
- }
136
-
137
- // compute the timestamp as seconds plus nanoseconds
126
+ // Function converts a std::timespec_t into the desired count to
127
+ // be returned by the timing functions in accordance with the requested
128
+ // kind at the call site.
129
+ count_t ConvertTimeSpecToCount (int kind, const std::timespec &tspec) {
138
130
const unsigned_count_t huge{GetHUGE (kind)};
139
131
unsigned_count_t sec{static_cast <unsigned_count_t >(tspec.tv_sec )};
140
132
unsigned_count_t nsec{static_cast <unsigned_count_t >(tspec.tv_nsec )};
@@ -147,6 +139,21 @@ count_t GetSystemClockCount(int kind, fallback_implementation) {
147
139
}
148
140
}
149
141
142
+ // This is the fallback implementation, which should work everywhere.
143
+ template <typename Unused = void >
144
+ count_t GetSystemClockCount (int kind, fallback_implementation) {
145
+ std::timespec tspec;
146
+
147
+ if (std::timespec_get (&tspec, TIME_UTC) < 0 ) {
148
+ // Return -HUGE(COUNT) to represent failure.
149
+ return -static_cast <count_t >(GetHUGE (kind));
150
+ }
151
+
152
+ // Compute the timestamp as seconds plus nanoseconds in accordance
153
+ // with the requested kind at the call site.
154
+ return ConvertTimeSpecToCount (kind, tspec);
155
+ }
156
+
150
157
template <typename Unused = void >
151
158
count_t GetSystemClockCountRate (int kind, fallback_implementation) {
152
159
return kind >= 8 ? NS_PER_SEC : kind >= 2 ? MS_PER_SEC : DS_PER_SEC;
@@ -169,15 +176,10 @@ count_t GetSystemClockCount(int kind, preferred_implementation,
169
176
if (clock_gettime (CLOCKID_ELAPSED_TIME, &tspec) != 0 ) {
170
177
return -huge; // failure
171
178
}
172
- unsigned_count_t sec{static_cast <unsigned_count_t >(tspec.tv_sec )};
173
- unsigned_count_t nsec{static_cast <unsigned_count_t >(tspec.tv_nsec )};
174
- if (kind >= 8 ) {
175
- return (sec * NS_PER_SEC + nsec) % (huge + 1 );
176
- } else if (kind >= 2 ) {
177
- return (sec * MS_PER_SEC + (nsec / (NS_PER_SEC / MS_PER_SEC))) % (huge + 1 );
178
- } else { // kind == 1
179
- return (sec * DS_PER_SEC + (nsec / (NS_PER_SEC / DS_PER_SEC))) % (huge + 1 );
180
- }
179
+
180
+ // Compute the timestamp as seconds plus nanoseconds in accordance
181
+ // with the requested kind at the call site.
182
+ return ConvertTimeSpecToCount (kind, tspec);
181
183
}
182
184
#endif // CLOCKID_ELAPSED_TIME
183
185
0 commit comments