From 0b39611285a3d9d0360c3087578411c334e1761f Mon Sep 17 00:00:00 2001 From: Deepak Rajendrakumaran Date: Tue, 5 Mar 2024 17:07:06 -0800 Subject: [PATCH] Handling NaN for double->ulong --- src/coreclr/vm/jitinterface.cpp | 2 ++ src/libraries/System.Private.CoreLib/src/System/Math.cs | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index dbf4aa43de507..cb3eba7360c25 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -10697,6 +10697,8 @@ void* CEEJitInfo::getHelperFtn(CorInfoHelpFunc ftnNum, /* IN */ dynamicFtnNum == DYNAMIC_CORINFO_HELP_ULMUL_OVF || dynamicFtnNum == DYNAMIC_CORINFO_HELP_ULNG2DBL || dynamicFtnNum == DYNAMIC_CORINFO_HELP_DBL2ULNG || + dynamicFtnNum == DYNAMIC_CORINFO_HELP_DBL2INT || + dynamicFtnNum == DYNAMIC_CORINFO_HELP_DBL2UINT || dynamicFtnNum == DYNAMIC_CORINFO_HELP_DBL2INT_OVF || dynamicFtnNum == DYNAMIC_CORINFO_HELP_DBL2UINT_OVF || dynamicFtnNum == DYNAMIC_CORINFO_HELP_DBL2LNG_OVF || diff --git a/src/libraries/System.Private.CoreLib/src/System/Math.cs b/src/libraries/System.Private.CoreLib/src/System/Math.cs index d4073a5691911..54aa01b2f06e4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Math.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Math.cs @@ -1624,6 +1624,9 @@ private static double ULongToDouble(ulong val) private static ulong DoubleToULong(double val) { + if (double.IsNaN(val)) + return 0; + const double two63 = 2147483648.0 * 4294967296.0; ulong ret; if (val < two63) @@ -1735,10 +1738,13 @@ private static double DoubleReminder(double dividend, double divisor) return FMod(dividend, divisor); } + [StackTraceHidden] private static int DoubleToInt(double val) { return double.IsNaN(val) || ((long)val < int.MinValue) ? int.MinValue : ((long)val > int.MaxValue) ? int.MaxValue : (int)(long)val; } + + [StackTraceHidden] private static uint DoubleToUInt(double val) { return double.IsNaN(val) || (val < 0) ? 0 : ((ulong)val > uint.MaxValue) ? uint.MaxValue : (uint)(ulong)val;