Skip to content

Commit

Permalink
Move arithmetic helpers to managed code
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed Oct 21, 2024
1 parent 8b1d30a commit ac4bd28
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 223 deletions.
16 changes: 8 additions & 8 deletions src/coreclr/inc/jithelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
JITHELPER(CORINFO_HELP_UNDEF, NULL, METHOD__NIL)

// Arithmetic
JITHELPER(CORINFO_HELP_DIV, JIT_Div, METHOD__NIL)
JITHELPER(CORINFO_HELP_MOD, JIT_Mod, METHOD__NIL)
JITHELPER(CORINFO_HELP_UDIV, JIT_UDiv, METHOD__NIL)
JITHELPER(CORINFO_HELP_UMOD, JIT_UMod, METHOD__NIL)
DYNAMICJITHELPER(CORINFO_HELP_DIV, NULL, METHOD__MATH__DIVIDE_CHECKED)
DYNAMICJITHELPER(CORINFO_HELP_MOD, NULL, METHOD__MATH__MODULUS_CHECKED)
DYNAMICJITHELPER(CORINFO_HELP_UDIV, NULL, METHOD__MATH__DIVIDE_UNSIGNED)
DYNAMICJITHELPER(CORINFO_HELP_UMOD, NULL, METHOD__MATH__MODULUS_UNSIGNED)

// CORINFO_HELP_DBL2INT, CORINFO_HELP_DBL2UINT, and CORINFO_HELP_DBL2LONG get
// patched for CPUs that support SSE2 (P4 and above).
Expand All @@ -59,10 +59,10 @@
DYNAMICJITHELPER(CORINFO_HELP_LMUL_OVF, NULL, METHOD__NIL)
DYNAMICJITHELPER(CORINFO_HELP_ULMUL_OVF, NULL, METHOD__NIL)
#endif // TARGET_64BIT
JITHELPER(CORINFO_HELP_LDIV, JIT_LDiv, METHOD__NIL)
JITHELPER(CORINFO_HELP_LMOD, JIT_LMod, METHOD__NIL)
JITHELPER(CORINFO_HELP_ULDIV, JIT_ULDiv, METHOD__NIL)
JITHELPER(CORINFO_HELP_ULMOD, JIT_ULMod, METHOD__NIL)
DYNAMICJITHELPER(CORINFO_HELP_LDIV, NULL, METHOD__MATH__DIVIDE_LONGS)
DYNAMICJITHELPER(CORINFO_HELP_LMOD, NULL, METHOD__MATH__MODULUS_LONGS)
DYNAMICJITHELPER(CORINFO_HELP_ULDIV, NULL, METHOD__MATH__DIVIDE_ULONGS)
DYNAMICJITHELPER(CORINFO_HELP_ULMOD, NULL, METHOD__MATH__MODULUS_ULONGS)
JITHELPER(CORINFO_HELP_LNG2DBL, JIT_Lng2Dbl, METHOD__NIL)
JITHELPER(CORINFO_HELP_ULNG2DBL, JIT_ULng2Dbl, METHOD__NIL)
JITHELPER(CORINFO_HELP_DBL2INT, JIT_Dbl2Int, METHOD__NIL)
Expand Down
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 @@ -234,29 +234,29 @@ public static void GetEntryPoint(TypeSystemContext context, ReadyToRunHelper id,
break;

case ReadyToRunHelper.Mod:
methodDesc = context.GetHelperEntryPoint("MathHelpers", "IMod");
methodDesc = context.SystemModule.GetKnownType("System", "Math").GetKnownMethod("ModulusChecked", null);
break;
case ReadyToRunHelper.UMod:
methodDesc = context.GetHelperEntryPoint("MathHelpers", "UMod");
methodDesc = context.SystemModule.GetKnownType("System", "Math").GetKnownMethod("ModulusUnsigned", null);
break;
case ReadyToRunHelper.ULMod:
methodDesc = context.GetHelperEntryPoint("MathHelpers", "ULMod");
methodDesc = context.SystemModule.GetKnownType("System", "Math").GetKnownMethod("ModulusUnsignedLongs", null);
break;
case ReadyToRunHelper.LMod:
methodDesc = context.GetHelperEntryPoint("MathHelpers", "LMod");
methodDesc = context.SystemModule.GetKnownType("System", "Math").GetKnownMethod("ModulusLongs", null);
break;

case ReadyToRunHelper.Div:
methodDesc = context.GetHelperEntryPoint("MathHelpers", "IDiv");
methodDesc = context.SystemModule.GetKnownType("System", "Math").GetKnownMethod("DivideChecked", null);
break;
case ReadyToRunHelper.UDiv:
methodDesc = context.GetHelperEntryPoint("MathHelpers", "UDiv");
methodDesc = context.SystemModule.GetKnownType("System", "Math").GetKnownMethod("DivideUnsigned", null);
break;
case ReadyToRunHelper.ULDiv:
methodDesc = context.GetHelperEntryPoint("MathHelpers", "ULDiv");
methodDesc = context.SystemModule.GetKnownType("System", "Math").GetKnownMethod("DivideUnsignedLongs", null);
break;
case ReadyToRunHelper.LDiv:
methodDesc = context.GetHelperEntryPoint("MathHelpers", "LDiv");
methodDesc = context.SystemModule.GetKnownType("System", "Math").GetKnownMethod("DivideLongs", null);
break;

case ReadyToRunHelper.LRsz:
Expand Down
16 changes: 12 additions & 4 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,18 @@ DEFINE_CLASS(MATH, System, Math)
DEFINE_METHOD(MATH, MULTIPLY_CHECKED_INT64, MultiplyChecked, SM_Long_Long_RetLong)
DEFINE_METHOD(MATH, MULTIPLY_CHECKED_UINT64, MultiplyChecked, SM_ULong_ULong_RetULong)
#endif
DEFINE_METHOD(MATH, CONVERT_TO_INT32_CHECKED, ConvertToInt32Checked, NoSig)
DEFINE_METHOD(MATH, CONVERT_TO_UINT32_CHECKED, ConvertToUInt32Checked, NoSig)
DEFINE_METHOD(MATH, CONVERT_TO_INT64_CHECKED, ConvertToInt64Checked, NoSig)
DEFINE_METHOD(MATH, CONVERT_TO_UINT64_CHECKED, ConvertToUInt64Checked, NoSig)
DEFINE_METHOD(MATH, CONVERT_TO_INT32_CHECKED, ConvertToInt32Checked, NoSig)
DEFINE_METHOD(MATH, CONVERT_TO_UINT32_CHECKED, ConvertToUInt32Checked, NoSig)
DEFINE_METHOD(MATH, CONVERT_TO_INT64_CHECKED, ConvertToInt64Checked, NoSig)
DEFINE_METHOD(MATH, CONVERT_TO_UINT64_CHECKED, ConvertToUInt64Checked, NoSig)
DEFINE_METHOD(MATH, DIVIDE_CHECKED, DivideChecked, NoSig)
DEFINE_METHOD(MATH, MODULUS_CHECKED, ModulusChecked, NoSig)
DEFINE_METHOD(MATH, DIVIDE_UNSIGNED, DivideUnsigned, NoSig)
DEFINE_METHOD(MATH, MODULUS_UNSIGNED, ModulusUnsigned, NoSig)
DEFINE_METHOD(MATH, DIVIDE_LONGS, DivideLongs, NoSig)
DEFINE_METHOD(MATH, MODULUS_LONGS, ModulusLongs, NoSig)
DEFINE_METHOD(MATH, DIVIDE_ULONGS, DivideUnsignedLongs, NoSig)
DEFINE_METHOD(MATH, MODULUS_ULONGS, ModulusUnsignedLongs, NoSig)

DEFINE_CLASS(DYNAMICMETHOD, ReflectionEmit, DynamicMethod)

Expand Down
204 changes: 1 addition & 203 deletions src/coreclr/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,208 +126,6 @@ HCIMPL2_VV(INT64, JIT_LMul, INT64 val1, INT64 val2)
HCIMPLEND
#endif // !TARGET_X86 || TARGET_UNIX

/*********************************************************************/
HCIMPL2(INT32, JIT_Div, INT32 dividend, INT32 divisor)
{
FCALL_CONTRACT;

RuntimeExceptionKind ehKind;

if (((UINT32) (divisor + 1)) <= 1) // Unsigned test for divisor in [-1 .. 0]
{
if (divisor == 0)
{
ehKind = kDivideByZeroException;
goto ThrowExcep;
}
else if (divisor == -1)
{
if (dividend == INT32_MIN)
{
ehKind = kOverflowException;
goto ThrowExcep;
}
return -dividend;
}
}

return(dividend / divisor);

ThrowExcep:
FCThrow(ehKind);
}
HCIMPLEND

/*********************************************************************/
HCIMPL2(INT32, JIT_Mod, INT32 dividend, INT32 divisor)
{
FCALL_CONTRACT;

RuntimeExceptionKind ehKind;

if (((UINT32) (divisor + 1)) <= 1) // Unsigned test for divisor in [-1 .. 0]
{
if (divisor == 0)
{
ehKind = kDivideByZeroException;
goto ThrowExcep;
}
else if (divisor == -1)
{
if (dividend == INT32_MIN)
{
ehKind = kOverflowException;
goto ThrowExcep;
}
return 0;
}
}

return(dividend % divisor);

ThrowExcep:
FCThrow(ehKind);
}
HCIMPLEND

/*********************************************************************/
HCIMPL2(UINT32, JIT_UDiv, UINT32 dividend, UINT32 divisor)
{
FCALL_CONTRACT;

if (divisor == 0)
FCThrow(kDivideByZeroException);

return(dividend / divisor);
}
HCIMPLEND

/*********************************************************************/
HCIMPL2(UINT32, JIT_UMod, UINT32 dividend, UINT32 divisor)
{
FCALL_CONTRACT;

if (divisor == 0)
FCThrow(kDivideByZeroException);

return(dividend % divisor);
}
HCIMPLEND

/*********************************************************************/
HCIMPL2_VV(INT64, JIT_LDiv, INT64 dividend, INT64 divisor)
{
FCALL_CONTRACT;

RuntimeExceptionKind ehKind;

if (Is32BitSigned(divisor))
{
if ((INT32)divisor == 0)
{
ehKind = kDivideByZeroException;
goto ThrowExcep;
}

if ((INT32)divisor == -1)
{
if ((UINT64) dividend == UI64(0x8000000000000000))
{
ehKind = kOverflowException;
goto ThrowExcep;
}
return -dividend;
}

// Check for -ive or +ive numbers in the range -2**31 to 2**31
if (Is32BitSigned(dividend))
return((INT32)dividend / (INT32)divisor);
}

// For all other combinations fallback to int64 div.
return(dividend / divisor);

ThrowExcep:
FCThrow(ehKind);
}
HCIMPLEND

/*********************************************************************/
HCIMPL2_VV(INT64, JIT_LMod, INT64 dividend, INT64 divisor)
{
FCALL_CONTRACT;

RuntimeExceptionKind ehKind;

if (Is32BitSigned(divisor))
{
if ((INT32)divisor == 0)
{
ehKind = kDivideByZeroException;
goto ThrowExcep;
}

if ((INT32)divisor == -1)
{
// <TODO>TODO, we really should remove this as it lengthens the code path
// and the spec really says that it should not throw an exception. </TODO>
if ((UINT64) dividend == UI64(0x8000000000000000))
{
ehKind = kOverflowException;
goto ThrowExcep;
}
return 0;
}

// Check for -ive or +ive numbers in the range -2**31 to 2**31
if (Is32BitSigned(dividend))
return((INT32)dividend % (INT32)divisor);
}

// For all other combinations fallback to int64 div.
return(dividend % divisor);

ThrowExcep:
FCThrow(ehKind);
}
HCIMPLEND

/*********************************************************************/
HCIMPL2_VV(UINT64, JIT_ULDiv, UINT64 dividend, UINT64 divisor)
{
FCALL_CONTRACT;

if (Hi32Bits(divisor) == 0)
{
if ((UINT32)(divisor) == 0)
FCThrow(kDivideByZeroException);

if (Hi32Bits(dividend) == 0)
return((UINT32)dividend / (UINT32)divisor);
}

return(dividend / divisor);
}
HCIMPLEND

/*********************************************************************/
HCIMPL2_VV(UINT64, JIT_ULMod, UINT64 dividend, UINT64 divisor)
{
FCALL_CONTRACT;

if (Hi32Bits(divisor) == 0)
{
if ((UINT32)(divisor) == 0)
FCThrow(kDivideByZeroException);

if (Hi32Bits(dividend) == 0)
return((UINT32)dividend % (UINT32)divisor);
}

return(dividend % divisor);
}
HCIMPLEND

#if !defined(HOST_64BIT) && !defined(TARGET_X86)
/*********************************************************************/
HCIMPL2_VV(UINT64, JIT_LLsh, UINT64 num, int shift)
Expand Down Expand Up @@ -4528,7 +4326,7 @@ bool IndirectionAllowedForJitHelper(CorInfoHelpFunc ftnNum)
{
return false;
}

return true;
}

Expand Down
Loading

0 comments on commit ac4bd28

Please sign in to comment.