From 9627bb9625b997bc9f6c5691e9da0c3a343b827f Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 9 Nov 2026 19:58:50 -0500 Subject: [PATCH] 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. --- OpenNest.Core/Math/PortableMath.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/OpenNest.Core/Math/PortableMath.cs b/OpenNest.Core/Math/PortableMath.cs index d1938fee..6a6b7755 100644 --- a/OpenNest.Core/Math/PortableMath.cs +++ b/OpenNest.Core/Math/PortableMath.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; namespace OpenNest.Math { @@ -23,6 +24,7 @@ namespace OpenNest.Math /// 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; /// sin(x + y) for |x| ≤ π/4, y the tail of x. + [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; /// cos(x + y) for |x| ≤ π/4, y the tail of x. + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private static double KernelCos(double x, double y) { var ix = High(x) & 0x7fffffff; @@ -330,6 +346,7 @@ namespace OpenNest.Math /// /// Reduces x to y0 + y1 in [-π/4, π/4] and returns the number of quarter turns removed. /// + [MethodImpl(MethodImplOptions.AggressiveOptimization)] private static int RemPio2(double x, out double y0, out double y1) { var hx = High(x);