Skip to content

Commit b269447

Browse files
MichalPetrykajkotas
authored andcommitted
Simplify floating point mod and round math jit helpers
1 parent 78a895f commit b269447

File tree

28 files changed

+56
-289
lines changed

28 files changed

+56
-289
lines changed

src/coreclr/System.Private.CoreLib/src/System/Math.CoreCLR.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ public static unsafe (double Sin, double Cos) SinCos(double x)
115115
[MethodImpl(MethodImplOptions.InternalCall)]
116116
public static extern double Tanh(double value);
117117

118-
[Intrinsic]
119-
[MethodImpl(MethodImplOptions.InternalCall)]
120-
private static extern double FMod(double x, double y);
121-
122118
[MethodImpl(MethodImplOptions.InternalCall)]
123119
private static extern unsafe double ModF(double x, double* intptr);
124120

src/coreclr/System.Private.CoreLib/src/System/MathF.CoreCLR.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ public static unsafe (float Sin, float Cos) SinCos(float x)
112112
[MethodImpl(MethodImplOptions.InternalCall)]
113113
public static extern float Tanh(float x);
114114

115-
[Intrinsic]
116-
[MethodImpl(MethodImplOptions.InternalCall)]
117-
private static extern float FMod(float x, float y);
118-
119115
[MethodImpl(MethodImplOptions.InternalCall)]
120116
private static extern unsafe float ModF(float x, float* intptr);
121117

src/coreclr/classlibnative/float/floatdouble.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,6 @@ FCIMPLEND
181181
#pragma float_control(pop)
182182
#endif
183183

184-
/*=====================================FMod=====================================
185-
**
186-
==============================================================================*/
187-
FCIMPL2_VV(double, COMDouble::FMod, double x, double y)
188-
FCALL_CONTRACT;
189-
190-
return fmod(x, y);
191-
FCIMPLEND
192-
193184
/*=====================================FusedMultiplyAdd==========================
194185
**
195186
==============================================================================*/

src/coreclr/classlibnative/float/floatsingle.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,6 @@ FCIMPL1_V(float, COMSingle::Floor, float x)
156156
return floorf(x);
157157
FCIMPLEND
158158

159-
/*=====================================FMod=====================================
160-
**
161-
==============================================================================*/
162-
FCIMPL2_VV(float, COMSingle::FMod, float x, float y)
163-
FCALL_CONTRACT;
164-
165-
return fmodf(x, y);
166-
FCIMPLEND
167-
168159
/*=====================================FusedMultiplyAdd==========================
169160
**
170161
==============================================================================*/

src/coreclr/classlibnative/inc/floatdouble.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class COMDouble {
2222
FCDECL1_V(static double, Cosh, double x);
2323
FCDECL1_V(static double, Exp, double x);
2424
FCDECL1_V(static double, Floor, double x);
25-
FCDECL2_VV(static double, FMod, double x, double y);
2625
FCDECL3_VVV(static double, FusedMultiplyAdd, double x, double y, double z);
2726
FCDECL1_V(static double, Log, double x);
2827
FCDECL1_V(static double, Log2, double x);

src/coreclr/classlibnative/inc/floatsingle.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class COMSingle {
2222
FCDECL1_V(static float, Cosh, float x);
2323
FCDECL1_V(static float, Exp, float x);
2424
FCDECL1_V(static float, Floor, float x);
25-
FCDECL2_VV(static float, FMod, float x, float y);
2625
FCDECL3_VVV(static float, FusedMultiplyAdd, float x, float y, float z);
2726
FCDECL1_V(static float, Log, float x);
2827
FCDECL1_V(static float, Log2, float x);

src/coreclr/inc/corinfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ enum CorInfoHelpFunc
397397
CORINFO_HELP_DBL2ULNG_OVF,
398398
CORINFO_HELP_FLTREM,
399399
CORINFO_HELP_DBLREM,
400-
CORINFO_HELP_FLTROUND,
401-
CORINFO_HELP_DBLROUND,
400+
CORINFO_HELP_FLTROUND, // unused, remove once MINIMUM_READYTORUN_MAJOR_VERSION > 9
401+
CORINFO_HELP_DBLROUND, // unused, remove once MINIMUM_READYTORUN_MAJOR_VERSION > 9
402402

403403
/* Allocating a new object. Always use ICorClassInfo::getNewHelper() to decide
404404
which is the right helper to use to allocate an object of a given type. */

src/coreclr/inc/jithelpers.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
JITHELPER(CORINFO_HELP_DBL2ULNG_OVF, JIT_Dbl2ULngOvf, CORINFO_HELP_SIG_8_STACK)
6666
JITHELPER(CORINFO_HELP_FLTREM, JIT_FltRem, CORINFO_HELP_SIG_8_STACK)
6767
JITHELPER(CORINFO_HELP_DBLREM, JIT_DblRem, CORINFO_HELP_SIG_16_STACK)
68-
JITHELPER(CORINFO_HELP_FLTROUND, JIT_FloatRound, CORINFO_HELP_SIG_8_STACK)
69-
JITHELPER(CORINFO_HELP_DBLROUND, JIT_DoubleRound, CORINFO_HELP_SIG_16_STACK)
68+
DYNAMICJITHELPER(CORINFO_HELP_FLTROUND, NULL, CORINFO_HELP_SIG_8_STACK)
69+
DYNAMICJITHELPER(CORINFO_HELP_DBLROUND, NULL, CORINFO_HELP_SIG_16_STACK)
7070

7171
// Allocating a new object
7272
JITHELPER(CORINFO_HELP_NEWFAST, JIT_New, CORINFO_HELP_SIG_REG_ONLY)
@@ -203,7 +203,7 @@
203203
JITHELPER(CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE, JIT_GetSharedNonGCThreadStaticBase, CORINFO_HELP_SIG_REG_ONLY)
204204
JITHELPER(CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE_NOCTOR, JIT_GetSharedGCThreadStaticBase, CORINFO_HELP_SIG_REG_ONLY)
205205
JITHELPER(CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE_NOCTOR, JIT_GetSharedNonGCThreadStaticBase, CORINFO_HELP_SIG_REG_ONLY)
206-
JITHELPER(CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE_DYNAMICCLASS, JIT_GetSharedGCThreadStaticBaseDynamicClass, CORINFO_HELP_SIG_REG_ONLY)
206+
JITHELPER(CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE_DYNAMICCLASS, JIT_GetSharedGCThreadStaticBaseDynamicClass, CORINFO_HELP_SIG_REG_ONLY)
207207
JITHELPER(CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE_DYNAMICCLASS, JIT_GetSharedNonGCThreadStaticBaseDynamicClass, CORINFO_HELP_SIG_REG_ONLY)
208208
JITHELPER(CORINFO_HELP_GETSHARED_GCTHREADSTATIC_BASE_NOCTOR_OPTIMIZED, JIT_GetSharedGCThreadStaticBaseOptimized, CORINFO_HELP_SIG_REG_ONLY)
209209
JITHELPER(CORINFO_HELP_GETSHARED_NONGCTHREADSTATIC_BASE_NOCTOR_OPTIMIZED, JIT_GetSharedNonGCThreadStaticBaseOptimized, CORINFO_HELP_SIG_REG_ONLY)

src/coreclr/jit/gentree.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5627,7 +5627,6 @@ unsigned Compiler::gtSetEvalOrder(GenTree* tree)
56275627
case NI_System_Math_Cosh:
56285628
case NI_System_Math_Exp:
56295629
case NI_System_Math_Floor:
5630-
case NI_System_Math_FMod:
56315630
case NI_System_Math_FusedMultiplyAdd:
56325631
case NI_System_Math_ILogB:
56335632
case NI_System_Math_Log:
@@ -12771,9 +12770,6 @@ void Compiler::gtDispTree(GenTree* tree,
1277112770
case NI_System_Math_Floor:
1277212771
printf(" floor");
1277312772
break;
12774-
case NI_System_Math_FMod:
12775-
printf(" fmod");
12776-
break;
1277712773
case NI_System_Math_FusedMultiplyAdd:
1277812774
printf(" fma");
1277912775
break;

src/coreclr/jit/importercalls.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,7 +4033,6 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
40334033
case NI_System_Math_Cosh:
40344034
case NI_System_Math_Exp:
40354035
case NI_System_Math_Floor:
4036-
case NI_System_Math_FMod:
40374036
case NI_System_Math_ILogB:
40384037
case NI_System_Math_Log:
40394038
case NI_System_Math_Log2:
@@ -7369,7 +7368,6 @@ bool Compiler::IsMathIntrinsic(NamedIntrinsic intrinsicName)
73697368
case NI_System_Math_Cosh:
73707369
case NI_System_Math_Exp:
73717370
case NI_System_Math_Floor:
7372-
case NI_System_Math_FMod:
73737371
case NI_System_Math_FusedMultiplyAdd:
73747372
case NI_System_Math_ILogB:
73757373
case NI_System_Math_Log:
@@ -10084,10 +10082,6 @@ NamedIntrinsic Compiler::lookupPrimitiveFloatNamedIntrinsic(CORINFO_METHOD_HANDL
1008410082
{
1008510083
result = NI_System_Math_Floor;
1008610084
}
10087-
else if (strcmp(methodName, "FMod") == 0)
10088-
{
10089-
result = NI_System_Math_FMod;
10090-
}
1009110085
else if (strcmp(methodName, "FusedMultiplyAdd") == 0)
1009210086
{
1009310087
result = NI_System_Math_FusedMultiplyAdd;

src/coreclr/jit/namedintrinsiclist.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ enum NamedIntrinsic : unsigned short
3737
NI_System_Math_Cosh,
3838
NI_System_Math_Exp,
3939
NI_System_Math_Floor,
40-
NI_System_Math_FMod,
4140
NI_System_Math_FusedMultiplyAdd,
4241
NI_System_Math_ILogB,
4342
NI_System_Math_Log,

src/coreclr/jit/valuenum.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8743,14 +8743,6 @@ ValueNum ValueNumStore::EvalMathFuncBinary(var_types typ, NamedIntrinsic gtMathF
87438743
break;
87448744
}
87458745

8746-
case NI_System_Math_FMod:
8747-
{
8748-
assert(typ == TypeOfVN(arg1VN));
8749-
double arg1Val = GetConstantDouble(arg1VN);
8750-
res = fmod(arg0Val, arg1Val);
8751-
break;
8752-
}
8753-
87548746
case NI_System_Math_Pow:
87558747
{
87568748
assert(typ == TypeOfVN(arg1VN));
@@ -8848,14 +8840,6 @@ ValueNum ValueNumStore::EvalMathFuncBinary(var_types typ, NamedIntrinsic gtMathF
88488840
break;
88498841
}
88508842

8851-
case NI_System_Math_FMod:
8852-
{
8853-
assert(typ == TypeOfVN(arg1VN));
8854-
float arg1Val = GetConstantSingle(arg1VN);
8855-
res = fmodf(arg0Val, arg1Val);
8856-
break;
8857-
}
8858-
88598843
case NI_System_Math_Max:
88608844
{
88618845
assert(typ == TypeOfVN(arg1VN));
@@ -8946,10 +8930,6 @@ ValueNum ValueNumStore::EvalMathFuncBinary(var_types typ, NamedIntrinsic gtMathF
89468930
vnf = VNF_Atan2;
89478931
break;
89488932

8949-
case NI_System_Math_FMod:
8950-
vnf = VNF_FMod;
8951-
break;
8952-
89538933
case NI_System_Math_Max:
89548934
vnf = VNF_Max;
89558935
break;

src/coreclr/jit/valuenumfuncs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ ValueNumFuncDef(Cos, 1, false, false, false, false)
8383
ValueNumFuncDef(Cosh, 1, false, false, false, false)
8484
ValueNumFuncDef(Exp, 1, false, false, false, false)
8585
ValueNumFuncDef(Floor, 1, false, false, false, false)
86-
ValueNumFuncDef(FMod, 2, false, false, false, false)
8786
ValueNumFuncDef(ILogB, 1, false, false, false, false)
8887
ValueNumFuncDef(Log, 1, false, false, false, false)
8988
ValueNumFuncDef(Log2, 1, false, false, false, false)

src/coreclr/nativeaot/Runtime/MathHelpers.cpp

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,61 +26,6 @@ FCIMPL1_D(uint64_t, RhpDbl2ULng, double val)
2626
}
2727
FCIMPLEND
2828

29-
#undef min
30-
#undef max
31-
#include <cmath>
32-
33-
FCIMPL2_FF(float, RhpFltRem, float dividend, float divisor)
34-
{
35-
//
36-
// From the ECMA standard:
37-
//
38-
// If [divisor] is zero or [dividend] is infinity
39-
// the result is NaN.
40-
// If [divisor] is infinity,
41-
// the result is [dividend] (negated for -infinity***).
42-
//
43-
// ***"negated for -infinity" has been removed from the spec
44-
//
45-
46-
if (divisor==0 || !std::isfinite(dividend))
47-
{
48-
return -nanf("");
49-
}
50-
else if (!std::isfinite(divisor) && !std::isnan(divisor))
51-
{
52-
return dividend;
53-
}
54-
// else...
55-
return fmodf(dividend,divisor);
56-
}
57-
FCIMPLEND
58-
59-
FCIMPL2_DD(double, RhpDblRem, double dividend, double divisor)
60-
{
61-
//
62-
// From the ECMA standard:
63-
//
64-
// If [divisor] is zero or [dividend] is infinity
65-
// the result is NaN.
66-
// If [divisor] is infinity,
67-
// the result is [dividend] (negated for -infinity***).
68-
//
69-
// ***"negated for -infinity" has been removed from the spec
70-
//
71-
if (divisor==0 || !std::isfinite(dividend))
72-
{
73-
return -nan("");
74-
}
75-
else if (!std::isfinite(divisor) && !std::isnan(divisor))
76-
{
77-
return dividend;
78-
}
79-
// else...
80-
return(fmod(dividend,divisor));
81-
}
82-
FCIMPLEND
83-
8429
#ifndef HOST_64BIT
8530
EXTERN_C int64_t QCALLTYPE RhpLDiv(int64_t i, int64_t j)
8631
{
@@ -187,6 +132,10 @@ EXTERN_C int64_t F_CALL_CONV RhpLLsh(int64_t i, int32_t j)
187132

188133
#ifdef HOST_X86
189134

135+
#undef min
136+
#undef max
137+
#include <cmath>
138+
190139
FCIMPL1_D(double, acos, double x)
191140
return std::acos(x);
192141
FCIMPLEND
@@ -363,6 +312,14 @@ FCIMPL1_F(float, tanhf, float x)
363312
return std::tanhf(x);
364313
FCIMPLEND
365314

315+
FCIMPL2_DD(double, fmod, double x, double y)
316+
return std::fmod(x, y);
317+
FCIMPLEND
318+
319+
FCIMPL2_FF(float, fmodf, float x, float y)
320+
return std::fmodf(x, y);
321+
FCIMPLEND
322+
366323
FCIMPL3_DDD(double, fma, double x, double y, double z)
367324
return std::fma(x, y, z);
368325
FCIMPLEND

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Math.NativeAot.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,6 @@ public static double Tanh(double value)
154154
return RuntimeImports.tanh(value);
155155
}
156156

157-
[Intrinsic]
158-
private static double FMod(double x, double y)
159-
{
160-
return RuntimeImports.fmod(x, y);
161-
}
162-
163157
[Intrinsic]
164158
private static unsafe double ModF(double x, double* intptr)
165159
{

src/coreclr/nativeaot/System.Private.CoreLib/src/System/MathF.NativeAot.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,6 @@ public static float Tanh(float x)
154154
return RuntimeImports.tanhf(x);
155155
}
156156

157-
[Intrinsic]
158-
private static float FMod(float x, float y)
159-
{
160-
return RuntimeImports.fmodf(x, y);
161-
}
162-
163157
[Intrinsic]
164158
private static unsafe float ModF(float x, float* intptr)
165159
{

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/JitHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ public static void GetEntryPoint(TypeSystemContext context, ReadyToRunHelper id,
198198
break;
199199

200200
case ReadyToRunHelper.DblRem:
201-
mangledName = "RhpDblRem";
201+
mangledName = "fmod";
202202
break;
203203
case ReadyToRunHelper.FltRem:
204-
mangledName = "RhpFltRem";
204+
mangledName = "fmodf";
205205
break;
206206

207207
case ReadyToRunHelper.LMul:

src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ private void ImportBinaryOperation(ILOpcode opcode)
12241224
break;
12251225
case ILOpcode.mul_ovf:
12261226
case ILOpcode.mul_ovf_un:
1227-
if (_compilation.TypeSystemContext.Target.Architecture == TargetArchitecture.ARM)
1227+
if (_compilation.TypeSystemContext.Target.PointerSize == 4)
12281228
{
12291229
_dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.LMulOfv), "_lmulovf");
12301230
_dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.ULMulOvf), "_ulmulovf");
@@ -1244,6 +1244,10 @@ private void ImportBinaryOperation(ILOpcode opcode)
12441244
else if (_compilation.TypeSystemContext.Target.Architecture == TargetArchitecture.ARM64)
12451245
{
12461246
_dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.ThrowDivZero), "_divbyzero");
1247+
if (opcode == ILOpcode.div)
1248+
{
1249+
_dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.Overflow), "_ovf");
1250+
}
12471251
}
12481252
break;
12491253
case ILOpcode.rem:
@@ -1258,7 +1262,14 @@ private void ImportBinaryOperation(ILOpcode opcode)
12581262
else if (_compilation.TypeSystemContext.Target.Architecture == TargetArchitecture.ARM64)
12591263
{
12601264
_dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.ThrowDivZero), "_divbyzero");
1265+
if (opcode == ILOpcode.rem)
1266+
{
1267+
_dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.Overflow), "_ovf");
1268+
}
12611269
}
1270+
1271+
_dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.DblRem), "rem");
1272+
_dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.FltRem), "rem");
12621273
break;
12631274
}
12641275
}

src/coreclr/vm/corelib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ DEFINE_METHOD(DELEGATE, GET_INVOKE_METHOD, GetInvokeMethod,
262262
DEFINE_CLASS(INT128, System, Int128)
263263
DEFINE_CLASS(UINT128, System, UInt128)
264264

265+
DEFINE_CLASS(MATH, System, Math)
266+
DEFINE_METHOD(MATH, ROUND, Round, SM_Dbl_RetDbl)
267+
268+
DEFINE_CLASS(MATHF, System, MathF)
269+
DEFINE_METHOD(MATHF, ROUND, Round, SM_Flt_RetFlt)
270+
265271
DEFINE_CLASS(DYNAMICMETHOD, ReflectionEmit, DynamicMethod)
266272

267273
DEFINE_CLASS(DYNAMICRESOLVER, ReflectionEmit, DynamicResolver)

src/coreclr/vm/ecall.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ void ECall::PopulateManagedHelpers()
156156
pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__SPAN_HELPERS__MEMCOPY));
157157
pDest = pMD->GetMultiCallableAddrOfCode();
158158
SetJitHelperFunction(CORINFO_HELP_MEMCPY, pDest);
159+
160+
pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__MATH__ROUND));
161+
pDest = pMD->GetMultiCallableAddrOfCode();
162+
SetJitHelperFunction(CORINFO_HELP_DBLROUND, pDest);
163+
164+
pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__MATHF__ROUND));
165+
pDest = pMD->GetMultiCallableAddrOfCode();
166+
SetJitHelperFunction(CORINFO_HELP_FLTROUND, pDest);
159167
}
160168

161169
static CrstStatic gFCallLock;

0 commit comments

Comments
 (0)