Skip to content

Commit

Permalink
Adding some new functions to System.Math and System.MathF (dotnet/cor…
Browse files Browse the repository at this point in the history
…eclr#20788)

* Adding BitIncrement, BitDecrement, CopySign, MaxMagnitude, and MinMagnitude to Math and MathF

* Adding FusedMultiplyAdd, IlogB, Log2, and ScaleB to Math and MathF

* Adding some basic PAL tests for fma, ilogb, log2, and scalbn

* Fixing a couple typos and adding clarifying comments

* Fixing the MSVC _VVV FCALL declarations


Commit migrated from dotnet/coreclr@2841758
  • Loading branch information
tannergooding authored Nov 5, 2018
1 parent 491f2aa commit 7a5da6d
Show file tree
Hide file tree
Showing 47 changed files with 1,814 additions and 50 deletions.
12 changes: 12 additions & 0 deletions src/coreclr/src/System.Private.CoreLib/src/System/Math.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,27 @@ public static partial class Math
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Floor(double d);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double FusedMultiplyAdd(double x, double y, double z);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern int IlogB(double x);

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

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Log2(double x);

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

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Pow(double x, double y);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double ScaleB(double x, int n);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Sin(double a);

Expand Down
12 changes: 12 additions & 0 deletions src/coreclr/src/System.Private.CoreLib/src/System/MathF.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,27 @@ public static partial class MathF
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float Floor(float x);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float FusedMultiplyAdd(float x, float y, float z);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern int IlogB(float x);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float Log(float x);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float Log2(float x);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float Log10(float x);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float Pow(float x, float y);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float ScaleB(float x, int n);

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern float Sin(float x);

Expand Down
36 changes: 36 additions & 0 deletions src/coreclr/src/classlibnative/float/floatdouble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ FCIMPL2_VV(double, COMDouble::FMod, double x, double y)
return (double)fmod(x, y);
FCIMPLEND

/*=====================================FusedMultiplyAdd==========================
**
==============================================================================*/
FCIMPL3_VVV(double, COMDouble::FusedMultiplyAdd, double x, double y, double z)
FCALL_CONTRACT;

return (double)fma(x, y, z);
FCIMPLEND

/*=====================================Ilog2====================================
**
==============================================================================*/
FCIMPL1_V(int, COMDouble::IlogB, double x)
FCALL_CONTRACT;

return (int)ilogb(x);
FCIMPLEND

/*=====================================Log======================================
**
==============================================================================*/
Expand All @@ -209,6 +227,15 @@ FCIMPL1_V(double, COMDouble::Log, double x)
return (double)log(x);
FCIMPLEND

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

return (double)log2(x);
FCIMPLEND

/*====================================Log10=====================================
**
==============================================================================*/
Expand Down Expand Up @@ -236,6 +263,15 @@ FCIMPL2_VV(double, COMDouble::Pow, double x, double y)
return (double)pow(x, y);
FCIMPLEND

/*=====================================ScaleB===================================
**
==============================================================================*/
FCIMPL2_VI(double, COMDouble::ScaleB, double x, int n)
FCALL_CONTRACT;

return (double)scalbn(x, n);
FCIMPLEND

/*=====================================Sin======================================
**
==============================================================================*/
Expand Down
36 changes: 36 additions & 0 deletions src/coreclr/src/classlibnative/float/floatsingle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@ FCIMPL2_VV(float, COMSingle::FMod, float x, float y)
return (float)fmodf(x, y);
FCIMPLEND

/*=====================================FusedMultiplyAdd==========================
**
==============================================================================*/
FCIMPL3_VVV(float, COMSingle::FusedMultiplyAdd, float x, float y, float z)
FCALL_CONTRACT;

return (float)fmaf(x, y, z);
FCIMPLEND

/*=====================================Ilog2====================================
**
==============================================================================*/
FCIMPL1_V(int, COMSingle::IlogB, float x)
FCALL_CONTRACT;

return (int)ilogbf(x);
FCIMPLEND

/*=====================================Log======================================
**
==============================================================================*/
Expand All @@ -196,6 +214,15 @@ FCIMPL1_V(float, COMSingle::Log, float x)
return (float)logf(x);
FCIMPLEND

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

return (float)log2f(x);
FCIMPLEND

/*====================================Log10=====================================
**
==============================================================================*/
Expand Down Expand Up @@ -223,6 +250,15 @@ FCIMPL2_VV(float, COMSingle::Pow, float x, float y)
return (float)powf(x, y);
FCIMPLEND

/*=====================================ScaleB===================================
**
==============================================================================*/
FCIMPL2_VI(float, COMSingle::ScaleB, float x, int n)
FCALL_CONTRACT;

return (float)scalbnf(x, n);
FCIMPLEND

/*=====================================Sin======================================
**
==============================================================================*/
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/src/classlibnative/inc/floatdouble.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ class COMDouble {
FCDECL1_V(static double, Exp, double x);
FCDECL1_V(static double, Floor, double x);
FCDECL2_VV(static double, FMod, double x, double y);
FCDECL3_VVV(static double, FusedMultiplyAdd, double x, double y, double z);
FCDECL1_V(static int, IlogB, double x);
FCDECL1_V(static double, Log, double x);
FCDECL1_V(static double, Log2, double x);
FCDECL1_V(static double, Log10, double x);
FCDECL2_VI(static double, ModF, double x, double* intptr);
FCDECL2_VV(static double, Pow, double x, double y);
FCDECL2_VI(static double, ScaleB, double x, int n);
FCDECL1_V(static double, Sin, double x);
FCDECL1_V(static double, Sinh, double x);
FCDECL1_V(static double, Sqrt, double x);
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/src/classlibnative/inc/floatsingle.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ class COMSingle {
FCDECL1_V(static float, Exp, float x);
FCDECL1_V(static float, Floor, float x);
FCDECL2_VV(static float, FMod, float x, float y);
FCDECL3_VVV(static float, FusedMultiplyAdd, float x, float y, float z);
FCDECL1_V(static int, IlogB, float x);
FCDECL1_V(static float, Log, float x);
FCDECL1_V(static float, Log2, float x);
FCDECL1_V(static float, Log10, float x);
FCDECL2_VI(static float, ModF, float x, float* intptr);
FCDECL2_VV(static float, Pow, float x, float y);
FCDECL2_VI(static float, ScaleB, float x, int n);
FCDECL1_V(static float, Sin, float x);
FCDECL1_V(static float, Sinh, float x);
FCDECL1_V(static float, Sqrt, float x);
Expand Down
12 changes: 1 addition & 11 deletions src/coreclr/src/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3964,17 +3964,7 @@ GenTree* Compiler::impMathIntrinsic(CORINFO_METHOD_HANDLE method,
GenTree* op2;

assert(callType != TYP_STRUCT);
assert((intrinsicID == CORINFO_INTRINSIC_Sin) || intrinsicID == CORINFO_INTRINSIC_Cbrt ||
(intrinsicID == CORINFO_INTRINSIC_Sqrt) || (intrinsicID == CORINFO_INTRINSIC_Abs) ||
(intrinsicID == CORINFO_INTRINSIC_Cos) || (intrinsicID == CORINFO_INTRINSIC_Round) ||
(intrinsicID == CORINFO_INTRINSIC_Cosh) || (intrinsicID == CORINFO_INTRINSIC_Sinh) ||
(intrinsicID == CORINFO_INTRINSIC_Tan) || (intrinsicID == CORINFO_INTRINSIC_Tanh) ||
(intrinsicID == CORINFO_INTRINSIC_Asin) || (intrinsicID == CORINFO_INTRINSIC_Asinh) ||
(intrinsicID == CORINFO_INTRINSIC_Acos) || (intrinsicID == CORINFO_INTRINSIC_Acosh) ||
(intrinsicID == CORINFO_INTRINSIC_Atan) || (intrinsicID == CORINFO_INTRINSIC_Atan2) ||
(intrinsicID == CORINFO_INTRINSIC_Atanh) || (intrinsicID == CORINFO_INTRINSIC_Log10) ||
(intrinsicID == CORINFO_INTRINSIC_Pow) || (intrinsicID == CORINFO_INTRINSIC_Exp) ||
(intrinsicID == CORINFO_INTRINSIC_Ceiling) || (intrinsicID == CORINFO_INTRINSIC_Floor));
assert(IsMathIntrinsic(intrinsicID));

op1 = nullptr;

Expand Down
4 changes: 1 addition & 3 deletions src/coreclr/src/jit/lsraxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1797,9 +1797,6 @@ int LinearScan::BuildIntrinsic(GenTree* tree)

switch (tree->gtIntrinsic.gtIntrinsicId)
{
case CORINFO_INTRINSIC_Sqrt:
break;

case CORINFO_INTRINSIC_Abs:
// Abs(float x) = x & 0x7fffffff
// Abs(double x) = x & 0x7ffffff ffffffff
Expand All @@ -1826,6 +1823,7 @@ int LinearScan::BuildIntrinsic(GenTree* tree)
break;
#endif // _TARGET_X86_

case CORINFO_INTRINSIC_Sqrt:
case CORINFO_INTRINSIC_Round:
case CORINFO_INTRINSIC_Ceiling:
case CORINFO_INTRINSIC_Floor:
Expand Down
18 changes: 17 additions & 1 deletion src/coreclr/src/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -4158,18 +4158,26 @@ SetThreadIdealProcessorEx(
#define asinh PAL_asinh
#define atan2 PAL_atan2
#define exp PAL_exp
#define fma PAL_fma
#define ilogb PAL_ilogb
#define log PAL_log
#define log2 PAL_log2
#define log10 PAL_log10
#define pow PAL_pow
#define scalbn PAL_scalbn
#define acosf PAL_acosf
#define acoshf PAL_acoshf
#define asinf PAL_asinf
#define asinhf PAL_asinhf
#define atan2f PAL_atan2f
#define expf PAL_expf
#define fmaf PAL_fmaf
#define ilogbf PAL_ilogbf
#define logf PAL_logf
#define log2f PAL_log2f
#define log10f PAL_log10f
#define powf PAL_powf
#define scalbnf PAL_scalbnf
#define malloc PAL_malloc
#define free PAL_free
#define mkstemp PAL_mkstemp
Expand Down Expand Up @@ -4424,10 +4432,14 @@ PALIMPORT double __cdecl exp(double);
PALIMPORT double __cdecl fabs(double);
PALIMPORT double __cdecl floor(double);
PALIMPORT double __cdecl fmod(double, double);
PALIMPORT double __cdecl fma(double, double, double);
PALIMPORT int __cdecl ilogb(double);
PALIMPORT double __cdecl log(double);
PALIMPORT double __cdecl log2(double);
PALIMPORT double __cdecl log10(double);
PALIMPORT double __cdecl modf(double, double*);
PALIMPORT double __cdecl pow(double, double);
PALIMPORT double __cdecl scalbn(double, int);
PALIMPORT double __cdecl sin(double);
PALIMPORT double __cdecl sinh(double);
PALIMPORT double __cdecl sqrt(double);
Expand All @@ -4452,11 +4464,15 @@ PALIMPORT float __cdecl coshf(float);
PALIMPORT float __cdecl expf(float);
PALIMPORT float __cdecl fabsf(float);
PALIMPORT float __cdecl floorf(float);
PALIMPORT float __cdecl fmodf(float, float);
PALIMPORT float __cdecl fmodf(float, float);
PALIMPORT float __cdecl fmaf(float, float, float);
PALIMPORT int __cdecl ilogbf(float);
PALIMPORT float __cdecl logf(float);
PALIMPORT float __cdecl log2f(float);
PALIMPORT float __cdecl log10f(float);
PALIMPORT float __cdecl modff(float, float*);
PALIMPORT float __cdecl powf(float, float);
PALIMPORT float __cdecl scalbnf(float, int);
PALIMPORT float __cdecl sinf(float);
PALIMPORT float __cdecl sinhf(float);
PALIMPORT float __cdecl sqrtf(float);
Expand Down
Loading

0 comments on commit 7a5da6d

Please sign in to comment.