[vlc-commits] commit: mtime: Minimize imprecision and prevent overflow on darwin. ( Pierre d'Herbemont )

git at videolan.org git at videolan.org
Fri Oct 1 21:45:49 CEST 2010


vlc | branch: master | Pierre d'Herbemont <pdherbemont at free.fr> | Thu Sep 30 21:00:06 2010 +0200| [599fbde71fba4397aa62fe81f957ee641654997c] | committer: Pierre d'Herbemont 

mtime: Minimize imprecision and prevent overflow on darwin.

Pointed-out-by: Rémi Denis-Courmont.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=599fbde71fba4397aa62fe81f957ee641654997c
---

 src/misc/mtime.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/misc/mtime.c b/src/misc/mtime.c
index 80ed116..115dbe9 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 */



More information about the vlc-commits mailing list