Revert "Correct floordiv() to cope with implementation-defined division."

This reverts commit cd9625fc3c.
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>
bb10
Edward Welbourne 2016-01-26 14:52:30 +01:00
parent 61efb292d2
commit 81514c19c1
1 changed files with 6 additions and 10 deletions

View File

@ -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)