perf(math): fully optimize PortableMath from its first call

Without tiering, PortableMath's Sin took 25.5 ns against the runtime's
15.0 ns in Release; marked AggressiveOptimization it takes 17.2 ns (Atan2
18.1 against 17.0). Results are unchanged: the cross-platform digest test
passes in Release and Debug on Linux and in Release on Windows.
This commit is contained in:
aj committed 2026-11-09 19:58:50 -05:00
1 parent 8e9946aa47
commit 9627bb9625
1 file changed
+17
+17
View File
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
namespace OpenNest.Math
{
@@ -23,6 +24,7 @@ namespace OpenNest.Math
/// </remarks>
public static class PortableMath
{
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static double Sin(double x)
{
var ix = High(x) & 0x7fffffff;
@@ -45,6 +47,8 @@ namespace OpenNest.Math
}
}
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static double Cos(double x)
{
var ix = High(x) & 0x7fffffff;
@@ -67,8 +71,12 @@ namespace OpenNest.Math
}
}
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static double Tan(double x) => Sin(x) / Cos(x);
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static double Asin(double x)
{
if (!(System.Math.Abs(x) <= 1))
@@ -76,6 +84,8 @@ namespace OpenNest.Math
return Atan2(x, System.Math.Sqrt((1 - x) * (1 + x)));
}
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static double Acos(double x)
{
if (!(System.Math.Abs(x) <= 1))
@@ -114,6 +124,8 @@ namespace OpenNest.Math
1.62858201153657823623e-02,
};
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static double Atan(double x)
{
var hx = High(x);
@@ -180,6 +192,8 @@ namespace OpenNest.Math
private const double Pi = 3.1415926535897931160E+00;
private const double PiLo = 1.2246467991473531772E-16;
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static double Atan2(double y, double x)
{
if (double.IsNaN(x) || double.IsNaN(y))
@@ -279,6 +293,7 @@ namespace OpenNest.Math
private const double S6 = 1.58969099521155010221e-10;
/// <summary>sin(x + y) for |x| ≤ π/4, y the tail of x.</summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static double KernelSin(double x, double y, bool hasTail)
{
var ix = High(x) & 0x7fffffff;
@@ -301,6 +316,7 @@ namespace OpenNest.Math
private const double C6 = -1.13596475577881948265e-11;
/// <summary>cos(x + y) for |x| ≤ π/4, y the tail of x.</summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static double KernelCos(double x, double y)
{
var ix = High(x) & 0x7fffffff;
@@ -330,6 +346,7 @@ namespace OpenNest.Math
/// <summary>
/// Reduces x to y0 + y1 in [-π/4, π/4] and returns the number of quarter turns removed.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static int RemPio2(double x, out double y0, out double y1)
{
var hx = High(x);