diff --git a/packages/cultmath/shaders/CultMath.Cycles.hlsl b/packages/cultmath/shaders/CultMath.Cycles.hlsl
index 829a27b3..7098394a 100644
--- a/packages/cultmath/shaders/CultMath.Cycles.hlsl
+++ b/packages/cultmath/shaders/CultMath.Cycles.hlsl
@@ -81,10 +81,10 @@ float cultmath_cycles_modulo(float a, float b)
return b != 0.0 ? (a < 0.0 ? -remainder : remainder) : 0.0;
}
-// safe_floored_modulo
+// safe_floored_modulo; a build that contracts it differs by a bound stated at cycles_floored_modulo in math.Cycles.cs and the README
float cultmath_cycles_floored_modulo(float a, float b) { return b != 0.0 ? a - floor(a / b) * b : 0.0; }
-// wrapf (note the argument order: value, max, min)
+// wrapf (note the argument order: value, max, min); contraction bound as for floored_modulo
float cultmath_cycles_wrap(float value, float max_value, float min_value)
{
float range = max_value - min_value;
@@ -129,6 +129,15 @@ float cultmath_cycles_fract(float a) { return a - floor(a); }
// compatible_signf
float cultmath_cycles_sign(float a) { return a == 0.0 ? 0.0 : (a < 0.0 ? -1.0 : 1.0); }
+// safe_asinf
+float cultmath_cycles_arcsine(float a) { return asin(clamp(a, -1.0, 1.0)); }
+
+// safe_acosf
+float cultmath_cycles_arccosine(float a) { return acos(clamp(a, -1.0, 1.0)); }
+
+// compatible_atan2 (a is y, b is x)
+float cultmath_cycles_arctan2(float a, float b) { return (b == 0.0 && a == 0.0) ? 0.0 : atan2(a, b); }
+
// ---- Map Range node (kernel/svm/map_range.h svm_node_map_range) ----
// A Map Range node's Clamp option is a Clamp node of type Range on its result (MapRangeNode::expand):
// compose cultmath_cycles_clamp_range over these.
diff --git a/packages/cultmath/src/CultMath/math.Cycles.cs b/packages/cultmath/src/CultMath/math.Cycles.cs
index 120d6f24..9144ef9c 100644
--- a/packages/cultmath/src/CultMath/math.Cycles.cs
+++ b/packages/cultmath/src/CultMath/math.Cycles.cs
@@ -92,10 +92,17 @@ public static partial class math
return b != 0.0f ? (a < 0.0f ? -remainder : remainder) : 0.0f;
}
- /// Math node Floored Modulo (safe_floored_modulo): a - floor(a / b) b, or 0 when b is 0.
+ /// Math node Floored Modulo (safe_floored_modulo): a - floor(a / b) b, or 0 when b is 0, with one IEEE single
+ /// rounding per operation (the reference of ruling cycles-kernel-reference-ruled). A build that contracts
+ /// a - floor(a / b) b into one fused operation differs by at most 2^-23 (|a / b| + 1) |b| (measured 0.91 of 2^-24 (|a| + |b|)
+ /// on Blender 5.2.2's AVX2 kernel); a fast-math build may also move floor(a / b) by one near an integer quotient,
+ /// so the result differs by a whole |b|. See tools/cycles-oracle/README.md.
public static float cycles_floored_modulo(float a, float b) => b != 0.0f ? a - floor(a / b) * b : 0.0f;
- /// Math node Wrap (wrapf; note the argument order): value wrapped into [min_value, max_value), or min_value for an empty range.
+ /// Math node Wrap (wrapf; note the argument order): value wrapped into [min_value, max_value), or min_value for an empty range,
+ /// with one IEEE single rounding per operation. A build that contracts value - range floor(...) into one fused operation differs by at
+ /// most 2^-23 (|value| + |min_value| + |range|) (measured 0.77 of 2^-24 on the AVX2 kernel); a fast-math build may also move floor by one,
+ /// a whole |range|. See tools/cycles-oracle/README.md.
public static float cycles_wrap(float value, float max_value, float min_value)
{
var range = max_value - min_value;
@@ -138,6 +145,15 @@ public static partial class math
/// Math node Sign (compatible_signf): 0 for 0, -1 below, 1 above.
public static float cycles_sign(float a) => a == 0.0f ? 0.0f : (a < 0.0f ? -1.0f : 1.0f);
+ /// Math node Arcsine (safe_asinf, util/math_base.h): asin of a clamped to [-1, 1].
+ public static float cycles_arcsine(float a) => asin(clamp(a, -1.0f, 1.0f));
+
+ /// Math node Arccosine (safe_acosf, util/math_base.h): acos of a clamped to [-1, 1].
+ public static float cycles_arccosine(float a) => acos(clamp(a, -1.0f, 1.0f));
+
+ /// Math node Arctan2 (compatible_atan2, util/math_base.h; a is y, b is x): 0 at the origin, else atan2(a, b).
+ public static float cycles_arctan2(float a, float b) => (b == 0.0f && a == 0.0f) ? 0.0f : atan2(a, b);
+
// ---- Map Range node (kernel/svm/map_range.h svm_node_map_range) ----
// A Map Range node's Clamp option is a Clamp node of type Range on its result (MapRangeNode::expand):
// compose cycles_clamp_range over these.
diff --git a/packages/cultmath/tests/CultMath.Tests/CyclesOracleTests.cs b/packages/cultmath/tests/CultMath.Tests/CyclesOracleTests.cs
index e1a2bc8a..a26d53b0 100644
--- a/packages/cultmath/tests/CultMath.Tests/CyclesOracleTests.cs
+++ b/packages/cultmath/tests/CultMath.Tests/CyclesOracleTests.cs
@@ -6,25 +6,24 @@ namespace CultMath.Tests;
///
/// math.cycles_* and the shader mirror's cultmath_cycles_* (shaders/CultMath.Cycles.hlsl, compiled as C#) against
-/// the results of Blender 5.2.2's own Cycles on the CPU. The fixtures under fixtures/cycles-oracle are written by
+/// the results of Blender 5.2.2's own Cycles on the CPU, the reference of ruling
+/// cultmath-tapes:ruling:cycles-kernel-reference-ruled: Cycles' formulas with one IEEE single rounding per
+/// operation. The fixtures under fixtures/cycles-oracle are written by
/// tools/cycles-oracle/oracle.py (never by hand): each case is the operands and the node's baked results as
/// float32 bit patterns. Every function is compared at every case within the tolerance named in
/// ; a function not named there is held to 0 ulp. Signed zeros are not distinguished (-0
/// equals 0), and NaN equals NaN. The fixtures come from Cycles' SSE4.2 CPU kernel, which does not fuse
-/// multiply-adds as C# does not (oracle.py checks this before it bakes). Cycles' film writes 0 for an infinite or
+/// multiply-adds as C# does not (oracle.py checks this before it bakes); other builds are other tolerance classes
+/// (tools/cycles-oracle/README.md). Cycles' film writes 0 for an infinite or
/// NaN result, so a case whose result here is not finite and whose fixture result is 0 holds nothing to compare.
///
public sealed class CyclesOracleTests
{
- // The ulp distance a function is allowed from Cycles. The two that call the platform's libm have the bound the
- // golden fixture gives a platform libm family (GlslMirrorTests, Platform.Ulp): Blender's powf and logf are
- // glibc's, measured here at 0 ulp from MathF.Pow and MathF.Log on Linux, and another OS's libm may differ by
- // one. That allowance is the golden fixture's, not a measurement on another OS.
- private static readonly Dictionary Tolerances = new()
- {
- ["cycles_power"] = (1, "0 ulp on Linux"),
- ["cycles_logarithm"] = (1, "0 ulp on Linux"),
- };
+ // The ulp distance a function is allowed from the reference. Empty: the fixtures are the SSE4.2 kernel's results
+ // and every function equals them, power and logarithm included (glibc's powf and logf, 0 ulp from MathF on Linux).
+ // An entry needs a measured case on the reference named in it (a failing case's bits and ulp); other builds'
+ // tolerances belong to the class table in tools/cycles-oracle/README.md, not here.
+ private static readonly Dictionary Tolerances = new();
private const string CSharp = "C#";
private const string Mirror = "HLSL mirror";
@@ -234,6 +233,145 @@ public sealed class CyclesOracleTests
Assert.Equal(0.0f, One("cycles_floored_modulo", 1.0f, 0.0f));
}
+ [Theory]
+ [MemberData(nameof(Targets))]
+ public void InverseTrigonometryFollowsTheCyclesGuards(string target)
+ {
+ // util/math_base.h: safe_asinf and safe_acosf clamp to [-1, 1] before asinf and acosf; compatible_atan2 returns 0
+ // at the origin (x == 0 && y == 0) and is atan2f everywhere else, so its guard is only the origin.
+ float One(string function, params float[] arguments) => (float)Call(target, function, arguments.Cast