Skip to content

Commit ad42fca

Browse files
committed
Make isleap faster
The assembler generated is better if we compare directly instead of doing fancy ring buffer-eque logic. Proof: https://godbolt.org/z/dferPP9h4
1 parent 8255a77 commit ad42fca

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Sources/CoreFoundation/CFDate.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,14 @@ static const uint8_t daysInMonth[16] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 3
240240
static const uint16_t daysBeforeMonth[16] = {INVALID_MONTH_RESULT, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, INVALID_MONTH_RESULT, INVALID_MONTH_RESULT};
241241
static const uint16_t daysAfterMonth[16] = {365, 334, 306, 275, 245, 214, 184, 153, 122, 92, 61, 31, 0, 0, 0, 0};
242242

243-
CF_INLINE bool isleap(int64_t year) {
244-
int64_t y = (year + 1) % 400; /* correct to nearest multiple-of-400 year, then find the remainder */
245-
if (y < 0) y = -y;
246-
return (0 == (y & 3) && 100 != y && 200 != y && 300 != y);
243+
CF_INLINE bool isleap(int64_t year)
244+
{
245+
year++; // year is current year minus 1, so add 1 back
246+
if (year < 0) year = -year;
247+
if ((year & 3) != 0) return false;
248+
if (year % 400) return true;
249+
if (year % 100) return false;
250+
return true;
247251
}
248252

249253
/* year arg is absolute year; Gregorian 2001 == year 0; 2001/1/1 = absolute date 0 */

0 commit comments

Comments
 (0)