From e7ea6e078fecb70fbc91b04878b69f696749afac Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 13 Nov 2019 16:53:12 +0900 Subject: [PATCH] Check more likely condition first [Feature #16335] --- time.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/time.c b/time.c index a7a95b6b86..8a573c0d67 100644 --- a/time.c +++ b/time.c @@ -3067,7 +3067,16 @@ time_arg(int argc, const VALUE *argv, struct vtm *vtm) static int leap_year_p(long y) { - return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0); + /* TODO: + * ensure about negative years in proleptic Gregorian calendar. + */ + unsigned long uy = (unsigned long)(LIKELY(y >= 0) ? y : -y); + + if (LIKELY(uy % 4 != 0)) return 0; + + unsigned long century = uy / 100; + if (LIKELY(uy != century * 100)) return 1; + return century % 4 == 0; } static time_t