[前][次][番号順一覧][スレッド一覧]

ruby-changes:58779

From: Nobuyoshi <ko1@a...>
Date: Wed, 13 Nov 2019 16:58:10 +0900 (JST)
Subject: [ruby-changes:58779] e7ea6e078f (master): Check more likely condition first [Feature #16335]

https://git.ruby-lang.org/ruby.git/commit/?id=e7ea6e078f

From e7ea6e078fecb70fbc91b04878b69f696749afac Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 13 Nov 2019 16:53:12 +0900
Subject: Check more likely condition first [Feature #16335]


diff --git a/time.c b/time.c
index a7a95b6..8a573c0 100644
--- a/time.c
+++ b/time.c
@@ -3067,7 +3067,16 @@ time_arg(int argc, const VALUE *argv, struct vtm *vtm) https://github.com/ruby/ruby/blob/trunk/time.c#L3067
 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
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]