Skip to content

Commit fc929d0

Browse files
committed
Refactor code a bit to reduce redundancy
1 parent a220155 commit fc929d0

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

flang/runtime/time-intrinsic.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <cstdlib>
2020
#include <cstring>
2121
#include <ctime>
22-
#include <chrono>
2322
#ifdef _WIN32
2423
#include "flang/Common/windows-include.h"
2524
#else
@@ -124,17 +123,10 @@ static constexpr inline unsigned_count_t GetHUGE(int kind) {
124123
return (unsigned_count_t{1} << ((8 * kind) - 1)) - 1;
125124
}
126125

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) {
138130
const unsigned_count_t huge{GetHUGE(kind)};
139131
unsigned_count_t sec{static_cast<unsigned_count_t>(tspec.tv_sec)};
140132
unsigned_count_t nsec{static_cast<unsigned_count_t>(tspec.tv_nsec)};
@@ -147,6 +139,21 @@ count_t GetSystemClockCount(int kind, fallback_implementation) {
147139
}
148140
}
149141

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+
150157
template <typename Unused = void>
151158
count_t GetSystemClockCountRate(int kind, fallback_implementation) {
152159
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,
169176
if (clock_gettime(CLOCKID_ELAPSED_TIME, &tspec) != 0) {
170177
return -huge; // failure
171178
}
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);
181183
}
182184
#endif // CLOCKID_ELAPSED_TIME
183185

0 commit comments

Comments
 (0)