aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-06-30 15:29:38 -0700
committer3gg <3gg@shellblade.net>2025-06-30 15:29:38 -0700
commit9b3ba8c80360c09dd32ee9645c536161b5dd0cff (patch)
tree516b6abfd57ee74d0ec64be7ad7427368dc508b7
parent0be996419b77a2e58ba3977519e77b8d11d27189 (diff)
Fix unnecessary double-uint conversions
-rw-r--r--timer/src/timer.c15
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
10static const int64_t microseconds = 1000000; 10static constexpr uint64_t microseconds = 1000000;
11#endif 11#endif
12static const int64_t nanoseconds = 1000000000; 12static constexpr uint64_t nanoseconds = 1000000000;
13 13
14#ifdef _WIN32 14#ifdef _WIN32
15static double seconds_per_count; 15static 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}