diff options
author | Edward Welbourne <edward.welbourne@theqtcompany.com> | 2016-01-26 14:52:30 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@theqtcompany.com> | 2016-01-29 10:35:17 +0000 |
commit | 81514c19c1eddfbce2fd34314eab9192e31c08d7 (patch) | |
tree | db9be152b16b79b21cda3905a48b35c6fdaf0c0a /src/corelib/tools/qdatetime.cpp | |
parent | 61efb292d293604122b5a7445c8331b547fcd732 (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qdatetime.cpp')
-rw-r--r-- | src/corelib/tools/qdatetime.cpp | 16 |
1 files changed, 6 insertions, 10 deletions
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) |