From: Paul Eggert Date: Sun, 22 Dec 2019 20:32:31 +0000 (-0800) Subject: gethrxtime: fix rounding bug with negative args X-Git-Tag: v1.0~4432 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=0da79aa8083359d3a97f7c743a28bd9f380caa92;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index 25c8c4755f..47cbfb2cdf 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 a442da5c0e..5dea467671 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. */