From 5d68ccf5b7677c5094cac881caf2423527afef46 Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Thu, 26 Mar 2015 16:29:03 -0400 Subject: [PATCH] win: more reliable uv__hrtime precision Reduces floating-point operations that can cause precision loss. Thanks to @Arnavion for suggesting the fix: https://github.com/iojs/io.js/pull/1272#issuecomment-86644307 --- src/win/util.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/win/util.c b/src/win/util.c index 8df9362c8ac..5eb7796de4f 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -59,7 +59,7 @@ static char *process_title; static CRITICAL_SECTION process_title_lock; /* Interval (in seconds) of the high-resolution clock. */ -static double hrtime_interval_ = 0; +static uint64_t hrtime_interval_ = 0; /* @@ -75,9 +75,9 @@ void uv__util_init() { * and precompute its reciprocal. */ if (QueryPerformanceFrequency(&perf_frequency)) { - hrtime_interval_ = 1.0 / perf_frequency.QuadPart; + hrtime_interval_ = perf_frequency.QuadPart; } else { - hrtime_interval_= 0; + hrtime_interval_= 1; } } @@ -484,7 +484,7 @@ uint64_t uv__hrtime(double scale) { * performance counter interval, integer math could cause this computation * to overflow. Therefore we resort to floating point math. */ - return (uint64_t) ((double) counter.QuadPart * hrtime_interval_ * scale); + return (uint64_t) ((counter.QuadPart / hrtime_interval_) * scale); }