diff options
| author | Andre Weissflog <floooh@gmail.com> | 2018-07-07 22:23:44 +0200 |
|---|---|---|
| committer | Andre Weissflog <floooh@gmail.com> | 2018-07-07 22:23:44 +0200 |
| commit | 0304475a089471aa84aba479b9e49ae10322cb34 (patch) | |
| tree | 3669b415b0fd9b8f3cdc0926295a7995defd91f6 /sokol_time.h | |
| parent | d043a65aee596f55bab834163fe2a9511dc2671d (diff) | |
sokol_tim.h: apply the same 64-bit overflow protection on OSX as for Windows
Diffstat (limited to 'sokol_time.h')
| -rw-r--r-- | sokol_time.h | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/sokol_time.h b/sokol_time.h index 357ba57b..ee207a27 100644 --- a/sokol_time.h +++ b/sokol_time.h @@ -117,6 +117,18 @@ static uint64_t _stm_osx_start; static uint64_t _stm_posix_start; #endif +/* prevent 64-bit overflow when computing relative timestamp + see https://gist.github.com/jspohr/3dc4f00033d79ec5bdaf67bc46c813e3 +*/ +#if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__)) +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 + + void stm_setup(void) { SOKOL_ASSERT(0 == _stm_initialized); _stm_initialized = 1; @@ -125,7 +137,7 @@ void stm_setup(void) { QueryPerformanceCounter(&_stm_win_start); #elif defined(__APPLE__) && defined(__MACH__) mach_timebase_info(&_stm_osx_timebase); - _stm_osx_start = (mach_absolute_time()*_stm_osx_timebase.numer)/_stm_osx_timebase.denom; + _stm_osx_start = mach_absolute_time(); #else struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); @@ -133,17 +145,6 @@ 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; @@ -152,7 +153,8 @@ uint64_t stm_now(void) { QueryPerformanceCounter(&qpc_t); 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; + const uint64_t mach_now = mach_absolute_time() - _stm_osx_start; + now = int64_muldiv(mach_now, _stm_osx_timebase.numer, _stm_osx_timebase.denom); #else struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); |