>From a39cf946bf1db2175d65749474cca12c9e88d842 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 22 Dec 2019 12:32:31 -0800 Subject: [PATCH 2/2] 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. --- ChangeLog | 10 ++++++++++ lib/xtime.h | 4 +--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 25c8c4755..47cbfb2cd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2019-12-22 Paul Eggert + + 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 gethrxtime: remove incorrect overflow detection diff --git a/lib/xtime.h b/lib/xtime.h index a442da5c0..5dea46767 100644 --- a/lib/xtime.h +++ b/lib/xtime.h @@ -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. */ -- 2.17.1