diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2019-04-29 16:00:51 +0200 | 
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2019-04-29 16:00:51 +0200 | 
| commit | 9da1695192518c8f2ca7692fa45e4acbdf4dc2b7 (patch) | |
| tree | 4e6396fdb9ceb157279d0c8be29d7c95292eda5a /compat/timer.cpp | |
| parent | 50aac35fd1ea04861d108ddc98e9ef5637878ddd (diff) | |
compat/timer: fix multiply overflow
Diffstat (limited to 'compat/timer.cpp')
| -rw-r--r-- | compat/timer.cpp | 34 | 
1 files changed, 14 insertions, 20 deletions
| diff --git a/compat/timer.cpp b/compat/timer.cpp index 4bfc37d9..d8ea242d 100644 --- a/compat/timer.cpp +++ b/compat/timer.cpp @@ -11,11 +11,8 @@  #include "timer.hpp"  #include <cassert>  #include <cmath> -  #include <QDebug> -using time_type = Timer::time_type; -  Timer::Timer()  {      start(); @@ -28,19 +25,11 @@ void Timer::start()  struct timespec Timer::gettime_() const  { -    struct timespec ts{}; +    struct timespec ts; // NOLINT      gettime(&ts); -    ts.tv_sec -= state.tv_sec; -    ts.tv_nsec -= state.tv_nsec; -    return ts; -} - -// nanoseconds - -time_type Timer::elapsed_nsecs() const -{ -    struct timespec delta = gettime_(); -    return (time_type)delta.tv_sec * 1000000000 + (time_type)delta.tv_nsec; +    unsigned long long a = ts.tv_sec, b = state.tv_sec; +    unsigned c = ts.tv_nsec, d = state.tv_nsec; +    return { (time_t)(a - b), (long)(c - d) };  }  // milliseconds @@ -72,17 +61,22 @@ static auto otr_get_clock_frequency()      return freq.QuadPart;  } -void Timer::gettime(timespec* ts) +void Timer::gettime(timespec* state)  {      static const unsigned long long freq = otr_get_clock_frequency(); -      LARGE_INTEGER d;      BOOL ret = QueryPerformanceCounter(&d);      assert(ret && "QueryPerformanceCounter failed"); -    auto part = (long long)std::roundl((d.QuadPart * 1000000000.L) / freq); -    ts->tv_sec = d.QuadPart/freq; -    ts->tv_nsec = part % 1000000000; +    constexpr int usec = 1000000; +    unsigned long long tm = d.QuadPart; +    tm *= usec; +    tm /= freq; +    tm %= usec; +    tm *= 1000; + +    state->tv_sec = (time_t)((unsigned long long)d.QuadPart/freq); +    state->tv_nsec = (long)tm;  }  #elif defined __MACH__ | 
