]> Savannah Git Hosting - gnulib.git/commitdiff
gethrxtime: fix rounding bug with negative args
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 22 Dec 2019 20:32:31 +0000 (12:32 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 22 Dec 2019 20:45:34 +0000 (12:45 -0800)
Problem reported by Bruno Haible in:
https://lists.gnu.org/r/bug-gnulib/2019-12/msg00192.html
* lib/xtime.h (xtime_sec): Simplify calculation and correct bug
with negative rounding.  Common platforms can compute / and % with
a single instruction, so the simplified code should be shorter and
faster on these platforms anyway.

ChangeLog
lib/xtime.h

index 25c8c4755fc2f80edd3ae4cad8292c842383c94f..47cbfb2cdfb47cdff4cb5f8f3fee2c4406253c65 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2019-12-22  Paul Eggert  <eggert@cs.ucla.edu>
+
+       gethrxtime: fix rounding bug with negative args
+       Problem reported by Bruno Haible in:
+       https://lists.gnu.org/r/bug-gnulib/2019-12/msg00192.html
+       * lib/xtime.h (xtime_sec): Simplify calculation and correct bug
+       with negative rounding.  Common platforms can compute / and % with
+       a single instruction, so the simplified code should be shorter and
+       faster on these platforms anyway.
+
 2019-12-22  Bruno Haible  <bruno@clisp.org>
 
        gethrxtime: remove incorrect overflow detection
index a442da5c0e15fa6a0a05228822228646013cb3ed..5dea46767194a757760940d4cca505e9468f022c 100644 (file)
@@ -61,9 +61,7 @@ xtime_nonnegative_sec (xtime_t t)
 XTIME_INLINE xtime_t
 xtime_sec (xtime_t t)
 {
-  return (t < 0
-          ? (t + XTIME_PRECISION - 1) / XTIME_PRECISION - 1
-          : xtime_nonnegative_sec (t));
+  return t / XTIME_PRECISION - (t % XTIME_PRECISION < 0);
 }
 
 /* Return the number of nanoseconds in T, which must be nonnegative.  */