Skip to content

Commit

Permalink
win: more reliable uv__hrtime precision
Browse files Browse the repository at this point in the history
Reduces floating-point operations that can cause precision loss.

Thanks to @Arnavion for suggesting the fix:
nodejs/node#1272 (comment)
  • Loading branch information
Fishrock123 committed Apr 10, 2015
1 parent 2eb1c18 commit 5d68ccf
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/win/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/*
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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);
}


Expand Down

0 comments on commit 5d68ccf

Please sign in to comment.