1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Check more likely condition first [Feature #16335]

This commit is contained in:
Nobuyoshi Nakada 2019-11-13 16:53:12 +09:00
parent 3324bc9d17
commit e7ea6e078f
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60

11
time.c
View file

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