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 82256bf commit b6f3e2b
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 360 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public static int OffsetToStringData

[Intrinsic]
public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle);

public const string QCall = "*";
}
}
16 changes: 8 additions & 8 deletions src/coreclr/nativeaot/Runtime/MathHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,25 @@ FCIMPL1_D(uint32_t, RhpDbl2UInt, double val)
FCIMPLEND

#ifndef HOST_64BIT
EXTERN_C int64_t QCALLTYPE RhpLDiv(int64_t i, int64_t j)
EXTERN_C int64_t QCALLTYPE Math_ActualDivisionLong(int64_t i, int64_t j)
{
ASSERT(j && "Divide by zero!");
return i / j;
}

EXTERN_C uint64_t QCALLTYPE RhpULDiv(uint64_t i, uint64_t j)
EXTERN_C uint64_t QCALLTYPE Math_ActualDivisionULong(uint64_t i, uint64_t j)
{
ASSERT(j && "Divide by zero!");
return i / j;
}

EXTERN_C int64_t QCALLTYPE RhpLMod(int64_t i, int64_t j)
EXTERN_C int64_t QCALLTYPE Math_ActualModulusLong(int64_t i, int64_t j)
{
ASSERT(j && "Divide by zero!");
return i % j;
}

EXTERN_C uint64_t QCALLTYPE RhpULMod(uint64_t i, uint64_t j)
EXTERN_C uint64_t QCALLTYPE Math_ActualModulusULong(uint64_t i, uint64_t j)
{
ASSERT(j && "Divide by zero!");
return i % j;
Expand All @@ -95,25 +95,25 @@ FCIMPLEND
#endif

#ifdef HOST_ARM
EXTERN_C int32_t F_CALL_CONV RhpIDiv(int32_t i, int32_t j)
EXTERN_C int32_t QCALLTYPE Math_ActualDivisionInt(int32_t i, int32_t j)
{
ASSERT(j && "Divide by zero!");
return i / j;
}

EXTERN_C uint32_t F_CALL_CONV RhpUDiv(uint32_t i, uint32_t j)
EXTERN_C uint32_t QCALLTYPE Math_ActualDivisionUInt(uint32_t i, uint32_t j)
{
ASSERT(j && "Divide by zero!");
return i / j;
}

EXTERN_C int32_t F_CALL_CONV RhpIMod(int32_t i, int32_t j)
EXTERN_C int32_t QCALLTYPE Math_ActualModulusInt(int32_t i, int32_t j)
{
ASSERT(j && "Divide by zero!");
return i % j;
}

EXTERN_C uint32_t F_CALL_CONV RhpUMod(uint32_t i, uint32_t j)
EXTERN_C uint32_t QCALLTYPE Math_ActualModulusUInt(uint32_t i, uint32_t j)
{
ASSERT(j && "Divide by zero!");
return i % j;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
<Compile Include="Internal\Runtime\IDynamicInterfaceCastableSupport.cs" />
<Compile Include="Internal\Runtime\MethodTable.Runtime.cs" />
<Compile Include="Internal\Runtime\CompilerHelpers\ThrowHelpers.cs" />
<Compile Include="Internal\Runtime\CompilerHelpers\MathHelpers.cs" />
<Compile Include="Internal\Runtime\CompilerServices\FunctionPointerOps.cs" />
<Compile Include="Internal\Runtime\CompilerServices\GenericMethodDescriptor.cs" />
<Compile Include="Internal\Runtime\CompilerServices\RuntimeFieldHandleInfo.cs" />
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
67 changes: 67 additions & 0 deletions src/coreclr/vm/JitQCallHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,71 @@ extern "C" void * QCALLTYPE ResolveVirtualFunctionPointer(QCall::ObjectHandleOnS
extern "C" CORINFO_GENERIC_HANDLE QCALLTYPE GenericHandleWorker(MethodDesc * pMD, MethodTable * pMT, LPVOID signature, DWORD dictionaryIndexAndSlot, Module* pModule);
extern "C" void QCALLTYPE InitClassHelper(MethodTable* pMT);

#ifdef TARGET_32BIT
extern "C" int32_t QCALLTYPE Math_ActualDivisionInt(int32_t i, int32_t j)
{
QCALL_CONTRACT_NO_GC_TRANSITION

ASSERT(j && "Divide by zero!");
return i / j;
}

extern "C" uint32_t QCALLTYPE Math_ActualDivisionUInt(uint32_t i, uint32_t j)
{
QCALL_CONTRACT_NO_GC_TRANSITION

ASSERT(j && "Divide by zero!");
return i / j;
}

extern "C" int32_t QCALLTYPE Math_ActualModulusInt(int32_t i, int32_t j)
{
QCALL_CONTRACT_NO_GC_TRANSITION

ASSERT(j && "Divide by zero!");
return i % j;
}

extern "C" uint32_t QCALLTYPE Math_ActualModulusUInt(uint32_t i, uint32_t j)
{
QCALL_CONTRACT_NO_GC_TRANSITION

ASSERT(j && "Divide by zero!");
return i % j;
}

extern "C" int64_t QCALLTYPE Math_ActualDivisionLong(int64_t i, int64_t j)
{
QCALL_CONTRACT_NO_GC_TRANSITION

ASSERT(j && "Divide by zero!");
return i / j;
}

extern "C" uint64_t QCALLTYPE Math_ActualDivisionULong(uint64_t i, uint64_t j)
{
QCALL_CONTRACT_NO_GC_TRANSITION

ASSERT(j && "Divide by zero!");
return i / j;
}

extern "C" int64_t QCALLTYPE Math_ActualModulusLong(int64_t i, int64_t j)
{
QCALL_CONTRACT_NO_GC_TRANSITION

ASSERT(j && "Divide by zero!");
return i % j;
}

extern "C" uint64_t QCALLTYPE Math_ActualModulusULong(uint64_t i, uint64_t j)
{
QCALL_CONTRACT_NO_GC_TRANSITION

ASSERT(j && "Divide by zero!");
return i % j;
}

#endif // TARGET_32BIT

#endif //_JITQCALLHELPERS_H
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
Loading

0 comments on commit b6f3e2b

Please sign in to comment.