From 81514c19c1eddfbce2fd34314eab9192e31c08d7 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 26 Jan 2016 14:52:30 +0100 Subject: Revert "Correct floordiv() to cope with implementation-defined division." This reverts commit cd9625fc3cacb4efd0da57d9f5780671f5c1310f. The ambiguity in division, with negative operands, goes away in C++11 (where division is defined to truncate, hence round towards zero), so we no longer need to be robust against it in 5.7. Added suitable commentary to make clear that we are relying on that. Change-Id: Id2c0d421bad4bcec87de9cc9519cd00df2456930 Reviewed-by: Thiago Macieira --- src/corelib/tools/qdatetime.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index 4f7d99b1d8..280f9ea006 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -100,24 +100,20 @@ static inline QDate fixedDate(int y, int m, int d) } /* - Until C++11, rounding direction is implementation-defined. + Division, rounding down (rather than towards zero). - For negative operands, implementations may chose to round down instead of - towards zero (truncation). We only actually care about the case a < 0, as all - uses of floordiv have b > 0. In this case, if rounding is down we have a % b - >= 0 and simple division works fine; but a % b = a - (a / b) * b always, so - rounding towards zero gives a % b <= 0; when < 0, we need to adjust. - - Once we assume C++11, we can safely test a < 0 instead of a % b < 0. + From C++11 onwards, integer division is defined to round towards zero, so we + can rely on that when implementing this. This is only used with denominator b + > 0, so we only have to treat negative numerator, a, specially. */ static inline qint64 floordiv(qint64 a, int b) { - return (a - (a % b < 0 ? b - 1 : 0)) / b; + return (a - (a < 0 ? b - 1 : 0)) / b; } static inline int floordiv(int a, int b) { - return (a - (a % b < 0 ? b - 1 : 0)) / b; + return (a - (a < 0 ? b - 1 : 0)) / b; } static inline qint64 julianDayFromDate(int year, int month, int day) -- cgit v1.2.3