test: refine reciprocal division (chrono).

This commit is contained in:
Leonid Yuriev 2017-04-25 00:26:11 +03:00 committed by Leo Yuriev
parent 993730d2f1
commit e7e8e1c59a

View File

@ -89,12 +89,12 @@ time now_realtime() {
time now_motonic() { time now_motonic() {
#if defined(_WIN32) || defined(_WIN64) || defined(_WINDOWS) #if defined(_WIN32) || defined(_WIN64) || defined(_WINDOWS)
static uint32_t reciprocal; static uint64_t reciprocal;
static LARGE_INTEGER Frequency; static LARGE_INTEGER Frequency;
if (reciprocal == 0) { if (reciprocal == 0) {
if (!QueryPerformanceFrequency(&Frequency)) if (!QueryPerformanceFrequency(&Frequency))
failure_perror("QueryPerformanceFrequency()", GetLastError()); failure_perror("QueryPerformanceFrequency()", GetLastError());
reciprocal = (uint32_t)(((UINT64_C(1) << 32) + Frequency.QuadPart / 2) / reciprocal = (((UINT64_C(1) << 48) + Frequency.QuadPart / 2 + 1) /
Frequency.QuadPart); Frequency.QuadPart);
assert(reciprocal); assert(reciprocal);
} }
@ -104,10 +104,9 @@ time now_motonic() {
failure_perror("QueryPerformanceCounter()", GetLastError()); failure_perror("QueryPerformanceCounter()", GetLastError());
time result; time result;
result.integer = (uint32_t)(Counter.QuadPart / Frequency.QuadPart); result.fixedpoint = (Counter.QuadPart / Frequency.QuadPart) << 32;
uint64_t mod = Counter.QuadPart % Frequency.QuadPart; uint64_t mod = Counter.QuadPart % Frequency.QuadPart;
assert(mod < UINT32_MAX); result.fixedpoint += (mod * reciprocal) >> 16;
result.fractional = UInt32x32To64((uint32_t)mod, reciprocal);
return result; return result;
#else #else
struct timespec ts; struct timespec ts;