Skip to content

Commit

Permalink
Fixing edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepakRajendrakumaran committed Mar 4, 2024
1 parent 6005146 commit 31626ef
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 62 deletions.
3 changes: 3 additions & 0 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@ GenTree* Compiler::fgMorphExpandCast(GenTreeCast* tree)
}
else if (srcType == TYP_DOUBLE && dstType == TYP_UINT)
{
/*oper = gtNewCastNode(TYP_LONG, oper, false, TYP_LONG);
tree = gtNewCastNode(TYP_INT, oper, false, TYP_UINT);
return fgMorphTree(tree);*/
return fgMorphCastIntoHelper(tree, CORINFO_HELP_DBL2UINT, oper);
}
else if (srcType == TYP_DOUBLE && dstType == TYP_INT)
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,8 @@ void HelperCallProperties::init()
case CORINFO_HELP_LMUL:
case CORINFO_HELP_LNG2DBL:
case CORINFO_HELP_ULNG2DBL:
case CORINFO_HELP_DBL2INT:
case CORINFO_HELP_DBL2UINT:
case CORINFO_HELP_DBL2LNG:
case CORINFO_HELP_FLT2UINT:
case CORINFO_HELP_DBL2ULNG:
Expand Down
18 changes: 18 additions & 0 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12740,6 +12740,7 @@ void Compiler::fgValueNumberCall(GenTreeCall* call)

void Compiler::fgValueNumberCastHelper(GenTreeCall* call)
{
// printf("\n Deepak fgValueNumberCastHelper IN \n");
CorInfoHelpFunc helpFunc = eeGetHelperNum(call->gtCallMethHnd);
var_types castToType = TYP_UNDEF;
var_types castFromType = TYP_UNDEF;
Expand Down Expand Up @@ -12770,6 +12771,16 @@ void Compiler::fgValueNumberCastHelper(GenTreeCall* call)
castFromType = TYP_DOUBLE;
break;

case CORINFO_HELP_DBL2INT:
castToType = TYP_INT;
castFromType = TYP_DOUBLE;
break;

case CORINFO_HELP_DBL2UINT:
castToType = TYP_UINT;
castFromType = TYP_DOUBLE;
break;

case CORINFO_HELP_DBL2LNG_OVF:
castToType = TYP_LONG;
castFromType = TYP_DOUBLE;
Expand Down Expand Up @@ -13081,13 +13092,17 @@ VNFunc Compiler::fgValueNumberJitHelperMethodVNFunc(CorInfoHelpFunc helpFunc)
bool Compiler::fgValueNumberHelperCall(GenTreeCall* call)
{
CorInfoHelpFunc helpFunc = eeGetHelperNum(call->gtCallMethHnd);
// printf("\n Deepak Call = [%06u], helpFunc = %d \n", dspTreeID(call), (int)helpFunc);
// printTreeID(call);

switch (helpFunc)
{
case CORINFO_HELP_LNG2DBL:
case CORINFO_HELP_ULNG2DBL:
case CORINFO_HELP_DBL2INT_OVF:
case CORINFO_HELP_DBL2LNG:
case CORINFO_HELP_DBL2INT:
case CORINFO_HELP_DBL2UINT:
case CORINFO_HELP_DBL2LNG_OVF:
case CORINFO_HELP_FLT2UINT:
case CORINFO_HELP_DBL2UINT_OVF:
Expand Down Expand Up @@ -13117,7 +13132,10 @@ bool Compiler::fgValueNumberHelperCall(GenTreeCall* call)
break;

default:
{
// printf("\n Deepak DEFAULT!! \n");
break;
}
}

bool pure = s_helperCallProperties.IsPure(helpFunc);
Expand Down
65 changes: 15 additions & 50 deletions src/coreclr/nativeaot/Runtime/MathHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,6 @@
#include "CommonMacros.h"
#include "rhassert.h"

//
// Floating point and 64-bit integer math helpers.
//


EXTERN_C NATIVEAOT_API int64_t REDHAWK_CALLCONV RhpDbl2Lng(double val)
{
#if defined(HOST_X86) || defined(HOST_AMD64)
const double int64_min = (double)INT64_MIN;
const double int64_max = (double)INT64_MAX;
return (val!= val) ? 0 : (val <= int64_min) ? INT64_MIN : (val >= int64_max) ? INT64_MAX : (int64_t)val;
#else
return (int64_t)val;
#endif //HOST_X86 || HOST_AMD64
}

EXTERN_C NATIVEAOT_API int32_t REDHAWK_CALLCONV RhpDbl2Int(double val)
{
#if defined(HOST_X86) || defined(HOST_AMD64)
const double int32_min = (double)INT32_MIN - 1.0;
const double int32_max = -2.0 * (double)INT32_MIN;
return (val!= val) ? 0 : (val <= int32_min) ? INT32_MIN : (val >= int32_max) ? INT32_MAX : (int32_t)val;
#else
return (int32_t)val;
#endif //HOST_X86 || HOST_AMD64
}

EXTERN_C NATIVEAOT_API uint32_t REDHAWK_CALLCONV RhpDbl2UInt(double val)
{
#if defined(HOST_X86) || defined(HOST_AMD64)
const double uint32_max_plus_1 = -2.0 * (double)INT32_MIN;
return (val < 0) ? 0 : (val != val || val >= uint32_max_plus_1) ? UINT32_MAX : (uint32_t)val;
#else
return (uint32_t)val;
#endif //HOST_X86 || HOST_AMD64
}

EXTERN_C NATIVEAOT_API uint32_t REDHAWK_CALLCONV RhpFlt2UInt(float val)
{
#if defined(HOST_X86) || defined(HOST_AMD64)
const float uint32_max_plus_1 = -2.0 * (float)INT32_MIN;
return (val != val || val < 0) ? 0 : (val >= uint32_max_plus_1) ? UINT32_MAX : (uint32_t)val;
#else
return (uint32_t)val;
#endif //HOST_X86 || HOST_AMD64
}

#undef min
#undef max
#include <cmath>
Expand Down Expand Up @@ -129,14 +82,26 @@ EXTERN_C NATIVEAOT_API int64_t REDHAWK_CALLCONV RhpLLsh(int64_t i, int32_t j)
return i << (j & 0x3f);
}

EXTERN_C NATIVEAOT_API double REDHAWK_CALLCONV RhpLng2Dbl(int64_t val)
{
return (double)val;
}

EXTERN_C NATIVEAOT_API int64_t REDHAWK_CALLCONV RhpDbl2Lng(double val)
{
return (int64_t)val;
}

EXTERN_C NATIVEAOT_API double REDHAWK_CALLCONV RhpLng2Dbl(int64_t val)
#else // HOST_X86 || HOST_AMD64

EXTERN_C NATIVEAOT_API int64_t REDHAWK_CALLCONV RhpDbl2Lng(double val)
{
return (double)val;
const double int64_min = (double)INT64_MIN;
const double int64_max = (double)INT64_MAX;
return (val!= val) ? 0 : (val <= int64_min) ? INT64_MIN : (val >= int64_max) ? INT64_MAX : (int64_t)val;
}
#endif




#endif // HOST_ARM
16 changes: 8 additions & 8 deletions src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/JitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,15 @@ public static void GetEntryPoint(TypeSystemContext context, ReadyToRunHelper id,
case ReadyToRunHelper.Dbl2ULng:
methodDesc = context.GetHelperEntryPoint("System", "Math", "DoubleToULong");
break;
case ReadyToRunHelper.Dbl2UInt:
methodDesc = context.GetHelperEntryPoint("System", "Math", "DoubleToUInt");
break;
case ReadyToRunHelper.Dbl2Int:
methodDesc = context.GetHelperEntryPoint("System", "Math", "DoubleToInt");
break;
case ReadyToRunHelper.Flt2UInt:
mangledName = "RhpFlt2UInt";
methodDesc = context.GetHelperEntryPoint("System", "Math", "FloatToUInt");
//mangledName = "RhpFlt2UInt";
break;

case ReadyToRunHelper.Dbl2IntOvf:
Expand All @@ -200,13 +207,6 @@ public static void GetEntryPoint(TypeSystemContext context, ReadyToRunHelper id,
case ReadyToRunHelper.FltRem:
methodDesc = context.GetHelperEntryPoint("System", "MathF", "FloatReminder");
break;

case ReadyToRunHelper.Dbl2Int:
methodDesc = context.GetHelperEntryPoint("System", "Math", "DoubleToInt");
break;
case ReadyToRunHelper.Dbl2UInt:
methodDesc = context.GetHelperEntryPoint("System", "MathF", "DoubleToUInt");
break;
case ReadyToRunHelper.DblRound:
DefType doubleType = context.GetWellKnownType(WellKnownType.Double);
methodDesc = context.SystemModule.GetKnownType("System", "Math").GetKnownMethod("Round",
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ DEFINE_METHOD(MATH, DOUBLE_TO_ULONG_OVERFLOW,DoubleToULongOverfl
DEFINE_METHOD(MATH, DOUBLE_REMINDER, DoubleReminder, NoSig)
DEFINE_METHOD(MATH, DOUBLE_TO_INT, DoubleToInt, NoSig)
DEFINE_METHOD(MATH, DOUBLE_TO_UINT, DoubleToUInt, NoSig)
DEFINE_METHOD(MATH, FLOAT_TO_UINT, FloatToUInt, NoSig)
DEFINE_METHOD(MATH, ROUND, Round, SM_Dbl_RetDbl)

DEFINE_CLASS(MATHF, System, MathF)
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/vm/ecall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ void ECall::PopulateManagedHelpers()
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_DBL2UINT, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__MATH__FLOAT_TO_UINT));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_FLT2UINT, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__MATH__ROUND));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_DBLROUND, pDest);
Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,10 @@ HCIMPLEND
HCIMPL1_V(INT64, JIT_Dbl2Lng, double val)
{
FCALL_CONTRACT;

return (INT64)val;
const double int64_min = (double)INT64_MIN;
const double int64_max = (double)INT64_MAX;
return (val!= val) ? 0 : (val <= int64_min) ? INT64_MIN : (val >= int64_max) ? INT64_MAX : (INT64)val;
//return (INT64)val;
}
HCIMPLEND

Expand Down
16 changes: 14 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,19 @@ private static double DoubleReminder(double dividend, double divisor)
return FMod(dividend, divisor);
}

private static int DoubleToInt(double val) => (int)(long)val;
private static uint DoubleToUInt(double val) => (uint)(long)val;
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;
}
private static uint DoubleToUInt(double val)
{
return double.IsNaN(val) || (val < 0) ? 0 : ((ulong)val > uint.MaxValue) ? uint.MaxValue : (uint)(ulong)val;
}

private static uint FloatToUInt(float val)
{
//return 0;
return float.IsNaN(val) || (val < 0) ? 0 : ((ulong)val > uint.MaxValue) ? uint.MaxValue : (uint)(ulong)val;
}
}
}

0 comments on commit 31626ef

Please sign in to comment.