summaryrefslogtreecommitdiffhomepage
path: root/compat
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2019-04-29 16:00:51 +0200
committerStanislaw Halik <sthalik@misaki.pl>2019-04-29 16:00:51 +0200
commit9da1695192518c8f2ca7692fa45e4acbdf4dc2b7 (patch)
tree4e6396fdb9ceb157279d0c8be29d7c95292eda5a /compat
parent50aac35fd1ea04861d108ddc98e9ef5637878ddd (diff)
compat/timer: fix multiply overflow
Diffstat (limited to 'compat')
-rw-r--r--compat/timer.cpp34
-rw-r--r--compat/timer.hpp6
2 files changed, 14 insertions, 26 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__
diff --git a/compat/timer.hpp b/compat/timer.hpp
index c107d28d..f7791a1a 100644
--- a/compat/timer.hpp
+++ b/compat/timer.hpp
@@ -10,19 +10,13 @@
#include "export.hpp"
#include "time.hpp"
-
#include <ctime>
-#include <cstdint>
-#include <type_traits>
struct OTR_COMPAT_EXPORT Timer final
{
- using time_type = std::int64_t;
-
Timer();
void start();
- time_type elapsed_nsecs() const;
double elapsed_ms() const;
double elapsed_seconds() const;