Skip to content

Commit

Permalink
Add managed implementation of Math/MathF.Abs (#63881)
Browse files Browse the repository at this point in the history
* Add managed implementation of  Math/MathF.Abs

* Cleanup trailing whitespaces in changeset

* Delete unused _copysignf macro
  • Loading branch information
am11 authored Jan 19, 2022
1 parent fc3875f commit 0b307c9
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 159 deletions.
8 changes: 0 additions & 8 deletions src/coreclr/System.Private.CoreLib/src/System/Math.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ namespace System
{
public static partial class Math
{
[Intrinsic]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Abs(double value);

[Intrinsic]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float Abs(float value);

[Intrinsic]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Acos(double d);
Expand Down
9 changes: 0 additions & 9 deletions src/coreclr/classlibnative/float/floatdouble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@
#pragma float_control(precise, off)
#endif

/*=====================================Abs======================================
**
==============================================================================*/
FCIMPL1_V(double, COMDouble::Abs, double x)
FCALL_CONTRACT;

return fabs(x);
FCIMPLEND

/*=====================================Acos=====================================
**
==============================================================================*/
Expand Down
21 changes: 0 additions & 21 deletions src/coreclr/classlibnative/float/floatsingle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@

#include "floatsingle.h"

// Windows x86 and Windows ARM/ARM64 may not define _isnanf() or _copysignf() but they do
// define _isnan() and _copysign(). We will redirect the macros to these other functions if
// the macro is not defined for the platform. This has the side effect of a possible implicit
// upcasting for arguments passed in and an explicit downcasting for the _copysign() call.
#if (defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_ARM64)) && !defined(TARGET_UNIX)

#if !defined(_copysignf)
#define _copysignf (float)_copysign
#endif

#endif

// The default compilation mode is /fp:precise, which disables floating-point intrinsics. This
// default compilation mode has previously caused performance regressions in floating-point code.
// We enable /fp:fast semantics for the majority of the math functions, as it will speed up performance
Expand All @@ -40,15 +28,6 @@
#pragma float_control(precise, off)
#endif

/*=====================================Abs=====================================
**
==============================================================================*/
FCIMPL1_V(float, COMSingle::Abs, float x)
FCALL_CONTRACT;

return fabsf(x);
FCIMPLEND

/*=====================================Acos=====================================
**
==============================================================================*/
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/classlibnative/inc/floatdouble.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class COMDouble {
public:
FCDECL1_V(static double, Abs, double x);
FCDECL1_V(static double, Acos, double x);
FCDECL1_V(static double, Acosh, double x);
FCDECL1_V(static double, Asin, double x);
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/classlibnative/inc/floatsingle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class COMSingle {
public:
FCDECL1_V(static float, Abs, float x);
FCDECL1_V(static float, Acos, float x);
FCDECL1_V(static float, Acosh, float x);
FCDECL1_V(static float, Asin, float x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ namespace System
{
public static partial class Math
{
[Intrinsic]
public static float Abs(float value)
{
return RuntimeImports.fabsf(value);
}

[Intrinsic]
public static double Abs(double value)
{
return RuntimeImports.fabs(value);
}

[Intrinsic]
public static double Acos(double d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,18 +679,6 @@ internal struct ConservativelyReportedRegionDesc
[RuntimeImport(RuntimeLibrary, "RhpMemoryBarrier")]
internal static extern void MemoryBarrier();

[Intrinsic]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[RuntimeImport(RuntimeLibrary, "fabs")]
internal static extern double fabs(double x);

[Intrinsic]
internal static float fabsf(float x)
{
// fabsf is not a real export for some architectures
return (float)fabs(x);
}

[Intrinsic]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[RuntimeImport(RuntimeLibrary, "acos")]
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,6 @@ FCFuncStart(gDelegateFuncs)
FCFuncEnd()

FCFuncStart(gMathFuncs)
FCFuncElementSig("Abs", &gsig_SM_Dbl_RetDbl, COMDouble::Abs)
FCFuncElementSig("Abs", &gsig_SM_Flt_RetFlt, COMSingle::Abs)
FCFuncElement("Acos", COMDouble::Acos)
FCFuncElement("Acosh", COMDouble::Acosh)
FCFuncElement("Asin", COMDouble::Asin)
Expand Down
24 changes: 22 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public static partial class Math

private const double SCALEB_C3 = 9007199254740992; // 0x1p53

private const int ILogB_NaN = 0x7fffffff;
private const int ILogB_NaN = 0x7FFFFFFF;

private const int ILogB_Zero = (-1 - 0x7fffffff);
private const int ILogB_Zero = (-1 - 0x7FFFFFFF);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short Abs(short value)
Expand Down Expand Up @@ -133,6 +133,26 @@ public static decimal Abs(decimal value)
return decimal.Abs(value);
}

[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Abs(double value)
{
const ulong mask = 0x7FFFFFFFFFFFFFFF;
ulong raw = BitConverter.DoubleToUInt64Bits(value);

return BitConverter.UInt64BitsToDouble(raw & mask);
}

[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Abs(float value)
{
const uint mask = 0x7FFFFFFF;
uint raw = BitConverter.SingleToUInt32Bits(value);

return BitConverter.UInt32BitsToSingle(raw & mask);
}

[DoesNotReturn]
[StackTraceHidden]
private static void ThrowAbsOverflow()
Expand Down
6 changes: 0 additions & 6 deletions src/mono/System.Private.CoreLib/src/System/Math.Mono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ namespace System
{
public partial class Math
{
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Abs(double value);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float Abs(float value);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Acos(double d);

Expand Down
2 changes: 0 additions & 2 deletions src/mono/mono/metadata/icall-decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ ICALL_EXPORT void ves_icall_System_ArgIterator_Setup (MonoArgIterator*, char*, c
ICALL_EXPORT MonoType* ves_icall_System_ArgIterator_IntGetNextArgType (MonoArgIterator*);
ICALL_EXPORT void ves_icall_System_ArgIterator_IntGetNextArg (MonoArgIterator*, MonoTypedRef*);
ICALL_EXPORT void ves_icall_System_ArgIterator_IntGetNextArgWithType (MonoArgIterator*, MonoTypedRef*, MonoType*);
ICALL_EXPORT double ves_icall_System_Math_Abs_double (double);
ICALL_EXPORT double ves_icall_System_Math_Acos (double);
ICALL_EXPORT double ves_icall_System_Math_Acosh (double);
ICALL_EXPORT double ves_icall_System_Math_Asin (double);
Expand Down Expand Up @@ -116,7 +115,6 @@ ICALL_EXPORT float ves_icall_System_MathF_Sinh (float);
ICALL_EXPORT float ves_icall_System_MathF_Sqrt (float);
ICALL_EXPORT float ves_icall_System_MathF_Tan (float);
ICALL_EXPORT float ves_icall_System_MathF_Tanh (float);
ICALL_EXPORT float ves_icall_System_Math_Abs_single (float);
ICALL_EXPORT double ves_icall_System_Math_Log2 (double);
ICALL_EXPORT double ves_icall_System_Math_FusedMultiplyAdd (double, double, double);
ICALL_EXPORT float ves_icall_System_MathF_Log2 (float);
Expand Down
4 changes: 1 addition & 3 deletions src/mono/mono/metadata/icall-def-netcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ ICALL_TYPE(STREAM, "System.IO.Stream", STREAM_1)
HANDLES(STREAM_1, "HasOverriddenBeginEndRead", ves_icall_System_IO_Stream_HasOverriddenBeginEndRead, MonoBoolean, 1, (MonoObject))
HANDLES(STREAM_2, "HasOverriddenBeginEndWrite", ves_icall_System_IO_Stream_HasOverriddenBeginEndWrite, MonoBoolean, 1, (MonoObject))

ICALL_TYPE(MATH, "System.Math", MATH_19)
NOHANDLES(ICALL(MATH_19, "Abs(double)", ves_icall_System_Math_Abs_double))
NOHANDLES(ICALL(MATH_20, "Abs(single)", ves_icall_System_Math_Abs_single))
ICALL_TYPE(MATH, "System.Math", MATH_1)
NOHANDLES(ICALL(MATH_1, "Acos", ves_icall_System_Math_Acos))
NOHANDLES(ICALL(MATH_1a, "Acosh", ves_icall_System_Math_Acosh))
NOHANDLES(ICALL(MATH_2, "Asin", ves_icall_System_Math_Asin))
Expand Down
50 changes: 19 additions & 31 deletions src/mono/mono/metadata/sysmath.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* \file
* these are based on bob smith's csharp routines
* these are based on bob smith's csharp routines
*
* Author:
* Mono Project (http://www.mono-project.com)
Expand Down Expand Up @@ -55,49 +55,49 @@ ves_icall_System_Math_ModF (gdouble x, gdouble *d)
return modf (x, d);
}

gdouble
gdouble
ves_icall_System_Math_Sin (gdouble x)
{
return sin (x);
}

gdouble
gdouble
ves_icall_System_Math_Cos (gdouble x)
{
return cos (x);
}

gdouble
gdouble
ves_icall_System_Math_Cbrt (gdouble x)
{
return cbrt (x);
}

gdouble
gdouble
ves_icall_System_Math_Tan (gdouble x)
{
return tan (x);
}

gdouble
gdouble
ves_icall_System_Math_Sinh (gdouble x)
{
return sinh (x);
}

gdouble
gdouble
ves_icall_System_Math_Cosh (gdouble x)
{
return cosh (x);
}

gdouble
gdouble
ves_icall_System_Math_Tanh (gdouble x)
{
return tanh (x);
}

gdouble
gdouble
ves_icall_System_Math_Acos (gdouble x)
{
return acos (x);
Expand All @@ -109,78 +109,66 @@ ves_icall_System_Math_Acosh (gdouble x)
return acosh (x);
}

gdouble
gdouble
ves_icall_System_Math_Asin (gdouble x)
{
return asin (x);
}

gdouble
gdouble
ves_icall_System_Math_Asinh (gdouble x)
{
return asinh (x);
}

gdouble
gdouble
ves_icall_System_Math_Atan (gdouble x)
{
return atan (x);
}

gdouble
gdouble
ves_icall_System_Math_Atan2 (gdouble y, gdouble x)
{
return atan2 (y, x);
}

gdouble
gdouble
ves_icall_System_Math_Atanh (gdouble x)
{
return atanh (x);
}

gdouble
gdouble
ves_icall_System_Math_Exp (gdouble x)
{
return exp (x);
}

gdouble
gdouble
ves_icall_System_Math_Log (gdouble x)
{
return log (x);
}

gdouble
gdouble
ves_icall_System_Math_Log10 (gdouble x)
{
return log10 (x);
}

gdouble
gdouble
ves_icall_System_Math_Pow (gdouble x, gdouble y)
{
return pow (x, y);
}

gdouble
gdouble
ves_icall_System_Math_Sqrt (gdouble x)
{
return sqrt (x);
}

gdouble
ves_icall_System_Math_Abs_double (gdouble v)
{
return fabs (v);
}

float
ves_icall_System_Math_Abs_single (float v)
{
return fabsf (v);
}

gdouble
ves_icall_System_Math_Ceiling (gdouble v)
{
Expand Down
Loading

0 comments on commit 0b307c9

Please sign in to comment.