From 9b3ba8c80360c09dd32ee9645c536161b5dd0cff Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Mon, 30 Jun 2025 15:29:38 -0700 Subject: Fix unnecessary double-uint conversions --- timer/src/timer.c | 15 ++++++++------- 1 file 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 @@ #endif #ifdef _WIN32 -static const int64_t microseconds = 1000000; +static constexpr uint64_t microseconds = 1000000; #endif -static const int64_t nanoseconds = 1000000000; +static constexpr uint64_t nanoseconds = 1000000000; #ifdef _WIN32 static double seconds_per_count; @@ -61,7 +61,8 @@ time_delta time_diff(time_point start, time_point end) { // another processor, then the delta time can be negative. return std::max(0, end - start); #else - return (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec); + return (end.tv_sec - start.tv_sec) * nanoseconds + + (end.tv_nsec - start.tv_nsec); #endif } @@ -77,7 +78,7 @@ time_delta sec_to_time_delta(double seconds) { #ifdef _WIN32 return (time_delta)(seconds / seconds_per_count); #else - return (time_delta)(seconds * 1.0e9); + return (time_delta)(seconds * nanoseconds); #endif } @@ -85,7 +86,7 @@ uint64_t time_point_to_ns(time_point t) { #ifdef _WIN32 return (uint64_t)((double)t * seconds_per_count * 1.0e+9); #else - return (uint64_t)t.tv_sec * 1e+9 + (uint64_t)t.tv_nsec; + return (uint64_t)t.tv_sec * nanoseconds + (uint64_t)t.tv_nsec; #endif } @@ -94,10 +95,10 @@ void time_sleep(time_delta dt) { const int64_t ms = dt / microseconds; Sleep((DWORD)(ms)); #else - const int64_t sec = dt / nanoseconds; + const uint64_t sec = dt / nanoseconds; struct timespec ts; ts.tv_sec = (long)sec; ts.tv_nsec = (long)(dt % nanoseconds); - nanosleep(&ts, NULL); + nanosleep(&ts, nullptr); #endif } -- cgit v1.2.3