From f948c30a3b3274455b5cfd95618f49059df10efb Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 28 Apr 2019 23:20:01 -0400 Subject: [PATCH] Simplified testingTimerNsec() on Windows. _div128() would make this super simple, but that's only available on VS2019, and it seems no one has implemented this in software elsewhere (the most I can find are unsigned ones...). --- test/testing_windows.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/test/testing_windows.c b/test/testing_windows.c index f20350d8..55651bb5 100644 --- a/test/testing_windows.c +++ b/test/testing_windows.c @@ -50,24 +50,19 @@ void testingTimerEnd(testingTimer *t) int64_t testingTimerNsec(testingTimer *t) { LARGE_INTEGER qpf; + int64_t qpnsQuot, qpnsRem; int64_t c; - int64_t sec; - int64_t subsec; - double subsecf; + int64_t ret; QueryPerformanceFrequency(&qpf); + qpnsQuot = testingNsecPerSec / qpf.QuadPart; + qpnsRem = testingNsecPerSec % qpf.QuadPart; c = t->end.QuadPart - t->start.QuadPart; - sec = c / qpf.QuadPart; - sec *= testingNsecPerSec; - - subsec = c % qpf.QuadPart; - subsecf = (double) subsec; - subsecf /= qpf.QuadPart; - subsecf *= testingNsecPerSec; - subsec = (int64_t) subsecf; - - return sec + subsec; + ret = c * qpnsQuot; + // TODO figure out if the following multiplication can overflow + ret += (c * qpnsRem) / qpf.QuadPart; + return ret; } // note: the idea for the SetThreadContext() nuttery is from https://www.codeproject.com/Articles/71529/Exception-Injection-Throwing-an-Exception-in-Other