From 0acacc697741fe50e0d2afc0eb3e81faa5950e1f Mon Sep 17 00:00:00 2001 From: EgorBo Date: Sat, 19 Dec 2020 13:24:07 +0300 Subject: [PATCH 1/2] Optimize DateToTicks and IsLeapYear --- .../System.Private.CoreLib/src/System/DateTime.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/DateTime.cs b/src/libraries/System.Private.CoreLib/src/System/DateTime.cs index f45333cbad1cf..1734f6a59e766 100644 --- a/src/libraries/System.Private.CoreLib/src/System/DateTime.cs +++ b/src/libraries/System.Private.CoreLib/src/System/DateTime.cs @@ -635,8 +635,8 @@ private static long DateToTicks(int year, int month, int day) ThrowHelper.ThrowArgumentOutOfRange_BadYearMonthDay(); } - int y = year - 1; - int n = y * 365 + y / 4 - y / 100 + y / 400 + days[month - 1] + day - 1; + uint y = (uint)(year - 1); + uint n = y * 365 + y / 4 - y / 100 + y / 400 + (uint)days[month - 1] + (uint)day - 1; return n * TicksPerDay; } @@ -1130,7 +1130,9 @@ public static bool IsLeapYear(int year) { ThrowHelper.ThrowArgumentOutOfRange_Year(); } - return (year & 3) == 0 && ((year & 15) == 0 || (year % 25) != 0); + return (year & 3) == 0 && ((year & 15) == 0 || + // TODO: optimize "(uint)year % 25 != 0" in JIT to produce: + (year * -1030792151 > 171798691)); } // Constructs a DateTime from a string. The string must specify a From 69f28bf8649975460745836ded8a6020335efa51 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Sat, 19 Dec 2020 14:55:27 +0300 Subject: [PATCH 2/2] Update DateTime.cs --- src/libraries/System.Private.CoreLib/src/System/DateTime.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/DateTime.cs b/src/libraries/System.Private.CoreLib/src/System/DateTime.cs index 1734f6a59e766..dba0b81ed848f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/DateTime.cs +++ b/src/libraries/System.Private.CoreLib/src/System/DateTime.cs @@ -1132,7 +1132,7 @@ public static bool IsLeapYear(int year) } return (year & 3) == 0 && ((year & 15) == 0 || // TODO: optimize "(uint)year % 25 != 0" in JIT to produce: - (year * -1030792151 > 171798691)); + ((uint)(year * -1030792151) > 171798691)); } // Constructs a DateTime from a string. The string must specify a