From f60c53b27e1afab06786161b84d13154564d996a Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 11 Oct 2015 06:54:30 +0200 Subject: timer: fix overflow Issue: #253 --- opentrack-compat/timer.hpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'opentrack-compat/timer.hpp') diff --git a/opentrack-compat/timer.hpp b/opentrack-compat/timer.hpp index fd710499..8192aa83 100644 --- a/opentrack-compat/timer.hpp +++ b/opentrack-compat/timer.hpp @@ -24,11 +24,12 @@ static inline void opentrack_clock_gettime(int, struct timespec* ts) (void) QueryPerformanceCounter(&d); - d.QuadPart *= 1000000000L; - d.QuadPart /= freq.QuadPart; + long long part = d.QuadPart; + part *= 1000000000ULL; + part /= freq.QuadPart; - ts->tv_sec = d.QuadPart / 1000000000L; - ts->tv_nsec = d.QuadPart % 1000000000L; + ts->tv_sec = part / 1000000000ULL; + ts->tv_nsec = part % 1000000000ULL; } # define clock_gettime opentrack_clock_gettime #else @@ -53,9 +54,9 @@ static inline void clock_gettime(int, struct timespec* ts) class Timer { private: struct timespec state; - long conv(const struct timespec& cur) + long long conv(const struct timespec& cur) { - return (cur.tv_sec - state.tv_sec) * 1000000000L + (cur.tv_nsec - state.tv_nsec); + return (cur.tv_sec - state.tv_sec) * 1000000000LL + (cur.tv_nsec - state.tv_nsec); } public: Timer() { @@ -64,7 +65,7 @@ public: void start() { (void) clock_gettime(CLOCK_MONOTONIC, &state); } - long elapsed() { + long long elapsed() { struct timespec cur; (void) clock_gettime(CLOCK_MONOTONIC, &cur); return conv(cur); -- cgit v1.2.3