From: Pierre d'Herbemont Date: Thu, 30 Sep 2010 19:00:06 +0000 (+0200) Subject: mtime: Minimize imprecision and prevent overflow on darwin. X-Git-Tag: 1.2.0-pre1~5197 X-Git-Url: https://git.sesse.net/?p=vlc;a=commitdiff_plain;h=599fbde71fba4397aa62fe81f957ee641654997c mtime: Minimize imprecision and prevent overflow on darwin. Pointed-out-by: RĂ©mi Denis-Courmont. --- diff --git a/src/misc/mtime.c b/src/misc/mtime.c index 80ed1169ee..115dbe9ffc 100644 --- a/src/misc/mtime.c +++ b/src/misc/mtime.c @@ -216,11 +216,16 @@ mtime_t mdate( void ) uint64_t date = mach_absolute_time(); mach_timebase_info_data_t tb = mtime_timebase_info; - /* Get the ssystem dependent factor. Switch to double to prevent overflow */ - double factor = (double) tb.numer / (double) tb.denom; - /* Convert to microseconds */ - double d = (double) date * factor / 1000; - res = d; + /* tb.denom is uint32_t, switch to 64 bits to prevent overflow. */ + uint64_t denom = tb.denom; + + /* Switch to microsecs */ + denom *= 1000LL; + + /* Split the division to prevent overflow */ + lldiv_t d = lldiv (tb.numer, denom); + + res = (d.quot * date) + ((d.rem * date) / denom); #elif defined( WIN32 ) || defined( UNDER_CE ) /* We don't need the real date, just the value of a high precision timer */