diff options
| author | Andre Weissflog <floooh@gmail.com> | 2018-07-07 22:12:46 +0200 |
|---|---|---|
| committer | Andre Weissflog <floooh@gmail.com> | 2018-07-07 22:12:46 +0200 |
| commit | d043a65aee596f55bab834163fe2a9511dc2671d (patch) | |
| tree | e573bc26f518a3540f69736e5b274c57218ba6d8 /sokol_time.h | |
| parent | 4211546539932bbc9e0e10aa8eda2a000920345a (diff) | |
sokol_time.h: fix potential 64-bit overflow on Win32 with QPF/QPC
Diffstat (limited to 'sokol_time.h')
| -rw-r--r-- | sokol_time.h | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/sokol_time.h b/sokol_time.h index acb98755..357ba57b 100644 --- a/sokol_time.h +++ b/sokol_time.h @@ -133,13 +133,24 @@ void stm_setup(void) { #endif } +/* prevent 64-bit overflow with QPC/QPF + see https://gist.github.com/jspohr/3dc4f00033d79ec5bdaf67bc46c813e3 +*/ +#if defined(_WIN32) +static int64_t int64_muldiv(int64_t value, int64_t numer, int64_t denom) { + int64_t q = value / denom; + int64_t r = value % denom; + return q * numer + r * numer / denom; +} +#endif + uint64_t stm_now(void) { SOKOL_ASSERT(_stm_initialized); uint64_t now; #if defined(_WIN32) LARGE_INTEGER qpc_t; QueryPerformanceCounter(&qpc_t); - now = ((qpc_t.QuadPart - _stm_win_start.QuadPart) * 1000000000) / _stm_win_freq.QuadPart; + now = int64_muldiv(qpc_t.QuadPart - _stm_win_start.QuadPart, 1000000000, _stm_win_freq.QuadPart); #elif defined(__APPLE__) && defined(__MACH__) now = ((mach_absolute_time()*_stm_osx_timebase.numer)/_stm_osx_timebase.denom) - _stm_osx_start; #else |