Skip to content

Commit 7967448

Browse files
committed
win, util: rearrange uv_hrtime
Rearrange math operations in uv_hrtime. This is a workaround for a probable compiler bug in VS2019. Fixes: #1633 PR-URL: #2866 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 0356c65 commit 7967448

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/win/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ void uv__fs_poll_endgame(uv_loop_t* loop, uv_fs_poll_t* handle);
266266
*/
267267
void uv__util_init(void);
268268

269-
uint64_t uv__hrtime(double scale);
269+
uint64_t uv__hrtime(unsigned int scale);
270270
__declspec(noreturn) void uv_fatal_error(const int errorno, const char* syscall);
271271
int uv__getpwuid_r(uv_passwd_t* pwd);
272272
int uv__convert_utf16_to_utf8(const WCHAR* utf16, int utf16len, char** utf8);

src/win/util.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ extern BOOLEAN NTAPI SystemFunction036(PVOID Buffer, ULONG BufferLength);
6767
static char *process_title;
6868
static CRITICAL_SECTION process_title_lock;
6969

70-
/* Interval (in seconds) of the high-resolution clock. */
71-
static double hrtime_interval_ = 0;
70+
/* Frequency of the high-resolution clock. */
71+
static uint64_t hrtime_frequency_ = 0;
7272

7373

7474
/*
@@ -84,9 +84,9 @@ void uv__util_init(void) {
8484
* and precompute its reciprocal.
8585
*/
8686
if (QueryPerformanceFrequency(&perf_frequency)) {
87-
hrtime_interval_ = 1.0 / perf_frequency.QuadPart;
87+
hrtime_frequency_ = perf_frequency.QuadPart;
8888
} else {
89-
hrtime_interval_= 0;
89+
uv_fatal_error(GetLastError(), "QueryPerformanceFrequency");
9090
}
9191
}
9292

@@ -490,23 +490,23 @@ uint64_t uv_hrtime(void) {
490490
return uv__hrtime(UV__NANOSEC);
491491
}
492492

493-
uint64_t uv__hrtime(double scale) {
493+
uint64_t uv__hrtime(unsigned int scale) {
494494
LARGE_INTEGER counter;
495495

496-
/* If the performance interval is zero, there's no support. */
497-
if (hrtime_interval_ == 0) {
498-
return 0;
499-
}
500-
496+
assert(hrtime_frequency_ != 0);
497+
assert(scale != 0);
501498
if (!QueryPerformanceCounter(&counter)) {
502-
return 0;
499+
uv_fatal_error(GetLastError(), "QueryPerformanceCounter");
503500
}
501+
assert(counter.QuadPart != 0);
504502

505503
/* Because we have no guarantee about the order of magnitude of the
506504
* performance counter interval, integer math could cause this computation
507505
* to overflow. Therefore we resort to floating point math.
508506
*/
509-
return (uint64_t) ((double) counter.QuadPart * hrtime_interval_ * scale);
507+
double scaled_freq = (double)hrtime_frequency_ / scale;
508+
double result = (double) counter.QuadPart / scaled_freq;
509+
return (uint64_t) result;
510510
}
511511

512512

0 commit comments

Comments
 (0)