Skip to content

Commit

Permalink
Handling NaN for double->ulong
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepakRajendrakumaran committed Mar 7, 2024
1 parent 885ecab commit 0b39611
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
6 changes: 6 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0b39611

Please sign in to comment.