QRoundingDown: make the numerator a template parameter

It's always a constexpr (or static const that could be constexpr; fix
the cases of this while I'm here) or an integer literal, so we can, so
we might as well.

Change-Id: I61e9bcdb27f4a05f011ccce16b5f15d0dade0782
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
bb10
Edward Welbourne 2023-01-16 16:58:27 +01:00
parent 5c40cb0f1a
commit 2d7b9b02e9
9 changed files with 62 additions and 62 deletions

View File

@ -867,7 +867,7 @@ int QCalendarBackend::maximumMonthsInYear() const
*/
int QCalendarBackend::dayOfWeek(qint64 jd) const
{
return QRoundingDown::qMod(jd, 7) + 1;
return QRoundingDown::qMod<7>(jd) + 1;
}
// Month and week-day name look-ups (implemented in qlocale.cpp):

View File

@ -35,11 +35,11 @@ namespace QRoundingDown {
when that subtraction would underflow.
*/
template<typename Int> constexpr Int qDiv(Int a, unsigned b)
template<unsigned b, typename Int> constexpr Int qDiv(Int a)
{ return a < 0 ? (a + 1) / int(b) - 1 : a / int(b); }
template<typename Int> constexpr Int qMod(Int a, unsigned b)
{ return a - qDiv(a, b) * b; }
template<unsigned b, typename Int> constexpr Int qMod(Int a)
{ return a - qDiv<b>(a) * b; }
} // QRoundingDown

View File

@ -2139,7 +2139,7 @@ QTime QTime::addMSecs(int ms) const
{
QTime t;
if (isValid())
t.mds = QRoundingDown::qMod(ds() + ms, MSECS_PER_DAY);
t.mds = QRoundingDown::qMod<MSECS_PER_DAY>(ds() + ms);
return t;
}
@ -2507,7 +2507,7 @@ typedef QDateTimePrivate::QDateTimeData QDateTimeData;
// Converts milliseconds since the start of 1970 into a date and/or time:
static qint64 msecsToJulianDay(qint64 msecs)
{
return JULIAN_DAY_FOR_EPOCH + QRoundingDown::qDiv(msecs, MSECS_PER_DAY);
return JULIAN_DAY_FOR_EPOCH + QRoundingDown::qDiv<MSECS_PER_DAY>(msecs);
}
static QDate msecsToDate(qint64 msecs)
@ -2517,7 +2517,7 @@ static QDate msecsToDate(qint64 msecs)
static QTime msecsToTime(qint64 msecs)
{
return QTime::fromMSecsSinceStartOfDay(QRoundingDown::qMod(msecs, MSECS_PER_DAY));
return QTime::fromMSecsSinceStartOfDay(QRoundingDown::qMod<MSECS_PER_DAY>(msecs));
}
// True if combining days with millis overflows; otherwise, stores result in *sumMillis
@ -3053,7 +3053,7 @@ static QPair<QDate, QTime> getDateTime(const QDateTimeData &d)
{
auto status = getStatus(d);
const qint64 msecs = getMSecs(d);
const qint64 days = QRoundingDown::qDiv(msecs, MSECS_PER_DAY);
const qint64 days = QRoundingDown::qDiv<MSECS_PER_DAY>(msecs);
return { status.testFlag(QDateTimePrivate::ValidDate)
? QDate::fromJulianDay(JULIAN_DAY_FOR_EPOCH + days)
: QDate(),

View File

@ -73,7 +73,7 @@ bool QGregorianCalendar::validParts(int year, int month, int day)
int QGregorianCalendar::weekDayOfJulian(qint64 jd)
{
return qMod(jd, 7) + 1;
return qMod<7>(jd) + 1;
}
bool QGregorianCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const
@ -98,8 +98,8 @@ bool QGregorianCalendar::julianFromParts(int year, int month, int day, qint64 *j
int a = month < 3 ? 1 : 0;
qint64 y = qint64(year) + 4800 - a;
int m = month + 12 * a - 3;
*jd = day + qDiv(153 * m + 2, 5) - 32045
+ 365 * y + qDiv(y, 4) - qDiv(y, 100) + qDiv(y, 400);
*jd = day + qDiv<5>(153 * m + 2) - 32045
+ 365 * y + qDiv<4>(y) - qDiv<100>(y) + qDiv<400>(y);
return true;
}
@ -107,7 +107,7 @@ int QGregorianCalendar::yearStartWeekDay(int year)
{
// Equivalent to weekDayOfJulian(julianForParts({year, 1, 1})
const int y = year - (year < 0 ? 800 : 801);
return qMod(y + qDiv(y, 4) - qDiv(y, 100) + qDiv(y, 400), 7) + 1;
return qMod<7>(y + qDiv<4>(y) - qDiv<100>(y) + qDiv<400>(y)) + 1;
}
int QGregorianCalendar::yearSharingWeekDays(QDate date)
@ -169,19 +169,19 @@ QCalendar::YearMonthDay QGregorianCalendar::partsFromJulian(qint64 jd)
* division (round to negative infinity), not c++11 integer division (round to zero)
*/
qint64 a = jd + 32044;
qint64 b = qDiv(4 * a + 3, 146097);
int c = a - qDiv(146097 * b, 4);
qint64 b = qDiv<146097>(4 * a + 3);
int c = a - qDiv<4>(146097 * b);
int d = qDiv(4 * c + 3, 1461);
int e = c - qDiv(1461 * d, 4);
int m = qDiv(5 * e + 2, 153);
int d = qDiv<1461>(4 * c + 3);
int e = c - qDiv<4>(1461 * d);
int m = qDiv<153>(5 * e + 2);
int y = 100 * b + d - 4800 + qDiv(m, 10);
int y = 100 * b + d - 4800 + qDiv<10>(m);
// Adjust for no year 0
int year = y > 0 ? y : y - 1;
int month = m + 3 - 12 * qDiv(m, 10);
int day = e - qDiv(153 * m + 2, 5) + 1;
int month = m + 3 - 12 * qDiv<10>(m);
int day = e - qDiv<5>(153 * m + 2) + 1;
return QCalendar::YearMonthDay(year, month, day);
}

View File

@ -58,7 +58,7 @@ bool QIslamicCivilCalendar::isLeapYear(int year) const
return false;
if (year < 0)
++year;
return qMod(year * 11 + 14, 30) < 11;
return qMod<30>(year * 11 + 14) < 11;
}
bool QIslamicCivilCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const
@ -68,20 +68,20 @@ bool QIslamicCivilCalendar::dateToJulianDay(int year, int month, int day, qint64
return false;
if (year <= 0)
++year;
*jd = qDiv(10631 * year - 10617, 30)
+ qDiv(325 * month - 320, 11)
*jd = qDiv<30>(10631 * year - 10617)
+ qDiv<11>(325 * month - 320)
+ day + 1948439;
return true;
}
QCalendar::YearMonthDay QIslamicCivilCalendar::julianDayToDate(qint64 jd) const
{
const qint64 epoch = 1948440;
constexpr qint64 epoch = 1948440;
const int32_t k2 = 30 * (jd - epoch) + 15;
const int32_t k1 = 11 * qDiv(qMod(k2, 10631), 30) + 5;
int y = qDiv(k2, 10631) + 1;
const int month = qDiv(k1, 325) + 1;
const int day = qDiv(qMod(k1, 325), 11) + 1;
const int32_t k1 = 11 * qDiv<30>(qMod<10631>(k2)) + 5;
int y = qDiv<10631>(k2) + 1;
const int month = qDiv<325>(k1) + 1;
const int day = qDiv<11>(qMod<325>(k1)) + 1;
return QCalendar::YearMonthDay(y > 0 ? y : y - 1, month, day);
}

View File

@ -13,16 +13,16 @@ using namespace QRoundingDown;
// Constants
static const qint64 cycleDays = 1029983;
static const int cycleYears = 2820;
static const double yearLength = 365.24219858156028368; // 365 + leapRatio;
static const qint64 jalaliEpoch = 2121446; // 475/01/01 AP, start of 2820 cycle
constexpr qint64 cycleDays = 1029983;
constexpr int cycleYears = 2820;
constexpr double yearLength = 365.24219858156028368; // 365 + leapRatio;
constexpr qint64 jalaliEpoch = 2121446; // 475/01/01 AP, start of 2820 cycle
// Calendar implementation
static inline int cycle(qint64 jdn)
{
return qDiv(jdn - jalaliEpoch, cycleDays);
return qDiv<cycleDays>(jdn - jalaliEpoch);
}
qint64 cycleStart(int cycleNo)
@ -96,7 +96,7 @@ bool QJalaliCalendar::isLeapYear(int year) const
return false;
if (year < 0)
year++;
return qMod((year + 2346) * 683, 2820) < 683;
return qMod<2820>((year + 2346) * 683) < 683;
}
bool QJalaliCalendar::isLunar() const
@ -121,7 +121,7 @@ bool QJalaliCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd)
return false;
const int y = year - (year < 0 ? 474 : 475);
const int c = qDiv(y, cycleYears);
const int c = qDiv<cycleYears>(y);
const int yearInCycle = y - c * cycleYears;
int dayInYear = day;
for (int i = 1; i < month; ++i)

View File

@ -54,7 +54,7 @@ bool QJulianCalendar::isLeapYear(int year) const
if (year == QCalendar::Unspecified || !year)
return false;
return qMod(year < 0 ? year + 1 : year, 4) == 0;
return qMod<4>(year < 0 ? year + 1 : year) == 0;
}
// Julian Day 0 was January the first in the proleptic Julian calendar's 4713 BC
@ -67,8 +67,8 @@ bool QJulianCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd)
if (year < 0)
++year;
const qint64 c0 = month < 3 ? -1 : 0;
const qint64 j1 = qDiv(1461 * (year + c0), 4);
const qint64 j2 = qDiv(153 * month - 1836 * c0 - 457, 5);
const qint64 j1 = qDiv<4>(1461 * (year + c0));
const qint64 j2 = qDiv<5>(153 * month - 1836 * c0 - 457);
*jd = j1 + j2 + day + 1721117;
return true;
}
@ -77,12 +77,12 @@ QCalendar::YearMonthDay QJulianCalendar::julianDayToDate(qint64 jd) const
{
const qint64 y2 = jd - 1721118;
const qint64 k2 = 4 * y2 + 3;
const qint64 k1 = 5 * qDiv(qMod(k2, 1461), 4) + 2;
const qint64 x1 = qDiv(k1, 153);
const qint64 c0 = qDiv(x1 + 2, 12);
const int y = qint16(qDiv(k2, 1461) + c0);
const qint64 k1 = 5 * qDiv<4>(qMod<1461>(k2)) + 2;
const qint64 x1 = qDiv<153>(k1);
const qint64 c0 = qDiv<12>(x1 + 2);
const int y = qint16(qDiv<1461>(k2) + c0);
const int month = quint8(x1 - 12 * c0 + 3);
const int day = qDiv(qMod(k1, 153), 5) + 1;
const int day = qDiv<5>(qMod<153>(k1)) + 1;
return QCalendar::YearMonthDay(y > 0 ? y : y - 1, month, day);
}

View File

@ -312,7 +312,7 @@ inline bool secondsAndMillisOverflow(qint64 epochSeconds, qint64 millis, qint64
// returns the local milliseconds, offset from UTC and DST status.
QDateTimePrivate::ZoneState utcToLocal(qint64 utcMillis)
{
const time_t epochSeconds = QRoundingDown::qDiv(utcMillis, MSECS_PER_SEC);
const time_t epochSeconds = QRoundingDown::qDiv<MSECS_PER_SEC>(utcMillis);
const int msec = utcMillis - epochSeconds * MSECS_PER_SEC;
Q_ASSERT(msec >= 0 && msec < MSECS_PER_SEC);
if (qint64(epochSeconds) * MSECS_PER_SEC + msec != utcMillis) // time_t range too narrow
@ -341,7 +341,7 @@ QDateTimePrivate::ZoneState utcToLocal(qint64 utcMillis)
QString localTimeAbbbreviationAt(qint64 local, QDateTimePrivate::DaylightStatus dst)
{
const qint64 localDays = QRoundingDown::qDiv(local, MSECS_PER_DAY);
const qint64 localDays = QRoundingDown::qDiv<MSECS_PER_DAY>(local);
qint64 millis = local - localDays * MSECS_PER_DAY;
Q_ASSERT(0 <= millis && millis < MSECS_PER_DAY); // Definition of QRD::qDiv.
struct tm tmLocal = timeToTm(localDays, int(millis / MSECS_PER_SEC), dst);
@ -356,7 +356,7 @@ QDateTimePrivate::ZoneState mapLocalTime(qint64 local, QDateTimePrivate::Dayligh
{
qint64 localSecs = local / MSECS_PER_SEC;
qint64 millis = local - localSecs * MSECS_PER_SEC; // 0 or with same sign as local
const qint64 localDays = QRoundingDown::qDiv(localSecs, SECS_PER_DAY);
const qint64 localDays = QRoundingDown::qDiv<SECS_PER_DAY>(localSecs);
qint64 daySecs = localSecs - localDays * SECS_PER_DAY;
Q_ASSERT(0 <= daySecs && daySecs < SECS_PER_DAY); // Definition of QRD::qDiv.
@ -368,7 +368,7 @@ QDateTimePrivate::ZoneState mapLocalTime(qint64 local, QDateTimePrivate::Dayligh
// TODO: for glibc, we could use tmLocal.tm_gmtoff
// That would give us offset directly, hence localSecs = offset + utcSecs
// Provisional offset, until we have a revised localSeconds:
int offset = QRoundingDown::qDiv(local, MSECS_PER_SEC) - utcSecs;
int offset = QRoundingDown::qDiv<MSECS_PER_SEC>(local) - utcSecs;
dst = tmLocal.tm_isdst > 0 ? QDateTimePrivate::DaylightTime : QDateTimePrivate::StandardTime;
qint64 jd;
if (Q_UNLIKELY(!QGregorianCalendar::julianFromParts(

View File

@ -53,10 +53,10 @@ bool QMilankovicCalendar::isLeapYear(int year) const
return false;
if (year <= 0)
++year;
if (qMod(year, 4))
if (qMod<4>(year))
return false;
if (qMod(year, 100) == 0) {
const qint16 century = qMod(qDiv(year, 100), 9);
if (qMod<100>(year) == 0) {
const qint16 century = qMod<9>(qDiv<100>(year));
if (century != 2 && century != 6)
return false;
}
@ -73,11 +73,11 @@ bool QMilankovicCalendar::dateToJulianDay(int year, int month, int day, qint64 *
const qint16 c0 = month < 3 ? -1 : 0;
const qint16 x1 = month - 12 * c0 - 3;
const qint16 x4 = year + c0;
const qint16 x3 = qDiv(x4, 100);
const qint16 x2 = qMod(x4, 100);
*jd = qDiv(328718 * x3 + 6, 9)
+ qDiv(36525 * x2 , 100)
+ qDiv(153 * x1 + 2 , 5)
const qint16 x3 = qDiv<100>(x4);
const qint16 x2 = qMod<100>(x4);
*jd = qDiv<9>(328718 * x3 + 6)
+ qDiv<100>(36525 * x2)
+ qDiv<5>(153 * x1 + 2)
+ day + 1721119;
return true;
}
@ -85,15 +85,15 @@ bool QMilankovicCalendar::dateToJulianDay(int year, int month, int day, qint64 *
QCalendar::YearMonthDay QMilankovicCalendar::julianDayToDate(qint64 jd) const
{
const qint64 k3 = 9 * (jd - 1721120) + 2;
const qint64 x3 = qDiv(k3, 328718);
const qint64 k2 = 100 * qDiv(qMod(k3, 328718), 9) + 99;
const qint64 k1 = qDiv(qMod(k2, 36525), 100) * 5 + 2;
const qint64 x2 = qDiv(k2, 36525);
const qint64 x1 = qDiv(5 * qDiv(qMod(k2, 36525), 100) + 2, 153);
const qint64 c0 = qDiv(x1 + 2, 12);
const qint64 x3 = qDiv<328718>(k3);
const qint64 k2 = 100 * qDiv<9>(qMod<328718>(k3)) + 99;
const qint64 k1 = qDiv<100>(qMod<36525>(k2)) * 5 + 2;
const qint64 x2 = qDiv<36525>(k2);
const qint64 x1 = qDiv<153>(5 * qDiv<100>(qMod<36525>(k2)) + 2);
const qint64 c0 = qDiv<12>(x1 + 2);
const int y = 100 * x3 + x2 + c0;
const int month = x1 - 12 * c0 + 3;
const int day = qDiv(qMod(k1, 153), 5) + 1;
const int day = qDiv<5>(qMod<153>(k1)) + 1;
return QCalendar::YearMonthDay(y > 0 ? y : y - 1, month, day);
}