From 599fbde71fba4397aa62fe81f957ee641654997c Mon Sep 17 00:00:00 2001 From: Pierre d'Herbemont Date: Thu, 30 Sep 2010 21:00:06 +0200 Subject: [PATCH] mtime: Minimize imprecision and prevent overflow on darwin. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Pointed-out-by: Rémi Denis-Courmont. --- src/misc/mtime.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) 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 */ -- 2.39.2