diff options
Diffstat (limited to 'timer/src')
-rw-r--r-- | timer/src/timer.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/timer/src/timer.c b/timer/src/timer.c index 2b34851..340cd98 100644 --- a/timer/src/timer.c +++ b/timer/src/timer.c | |||
@@ -7,9 +7,9 @@ | |||
7 | #endif | 7 | #endif |
8 | 8 | ||
9 | #ifdef _WIN32 | 9 | #ifdef _WIN32 |
10 | static const int64_t microseconds = 1000000; | 10 | static constexpr uint64_t microseconds = 1000000; |
11 | #endif | 11 | #endif |
12 | static const int64_t nanoseconds = 1000000000; | 12 | static constexpr uint64_t nanoseconds = 1000000000; |
13 | 13 | ||
14 | #ifdef _WIN32 | 14 | #ifdef _WIN32 |
15 | static double seconds_per_count; | 15 | static double seconds_per_count; |
@@ -61,7 +61,8 @@ time_delta time_diff(time_point start, time_point end) { | |||
61 | // another processor, then the delta time can be negative. | 61 | // another processor, then the delta time can be negative. |
62 | return std::max(0, end - start); | 62 | return std::max(0, end - start); |
63 | #else | 63 | #else |
64 | return (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec); | 64 | return (end.tv_sec - start.tv_sec) * nanoseconds + |
65 | (end.tv_nsec - start.tv_nsec); | ||
65 | #endif | 66 | #endif |
66 | } | 67 | } |
67 | 68 | ||
@@ -77,7 +78,7 @@ time_delta sec_to_time_delta(double seconds) { | |||
77 | #ifdef _WIN32 | 78 | #ifdef _WIN32 |
78 | return (time_delta)(seconds / seconds_per_count); | 79 | return (time_delta)(seconds / seconds_per_count); |
79 | #else | 80 | #else |
80 | return (time_delta)(seconds * 1.0e9); | 81 | return (time_delta)(seconds * nanoseconds); |
81 | #endif | 82 | #endif |
82 | } | 83 | } |
83 | 84 | ||
@@ -85,7 +86,7 @@ uint64_t time_point_to_ns(time_point t) { | |||
85 | #ifdef _WIN32 | 86 | #ifdef _WIN32 |
86 | return (uint64_t)((double)t * seconds_per_count * 1.0e+9); | 87 | return (uint64_t)((double)t * seconds_per_count * 1.0e+9); |
87 | #else | 88 | #else |
88 | return (uint64_t)t.tv_sec * 1e+9 + (uint64_t)t.tv_nsec; | 89 | return (uint64_t)t.tv_sec * nanoseconds + (uint64_t)t.tv_nsec; |
89 | #endif | 90 | #endif |
90 | } | 91 | } |
91 | 92 | ||
@@ -94,10 +95,10 @@ void time_sleep(time_delta dt) { | |||
94 | const int64_t ms = dt / microseconds; | 95 | const int64_t ms = dt / microseconds; |
95 | Sleep((DWORD)(ms)); | 96 | Sleep((DWORD)(ms)); |
96 | #else | 97 | #else |
97 | const int64_t sec = dt / nanoseconds; | 98 | const uint64_t sec = dt / nanoseconds; |
98 | struct timespec ts; | 99 | struct timespec ts; |
99 | ts.tv_sec = (long)sec; | 100 | ts.tv_sec = (long)sec; |
100 | ts.tv_nsec = (long)(dt % nanoseconds); | 101 | ts.tv_nsec = (long)(dt % nanoseconds); |
101 | nanosleep(&ts, NULL); | 102 | nanosleep(&ts, nullptr); |
102 | #endif | 103 | #endif |
103 | } | 104 | } |