From 14f7ad94226f85067977f433ad08102469d13439 Mon Sep 17 00:00:00 2001 From: soul Date: Tue, 10 Nov 2026 14:08:19 +0000 Subject: [PATCH] CultMath: HLSL vector mul(v, v) = dot for float2/3/4, closing the quaternion-conversion overload trap mul(float4, float4) would otherwise bind to a quaternion product once mul(quaternion, quaternion) lands. Co-Authored-By: Claude Sonnet 5.5 --- packages/cultmath/src/CultMath/math.cs | 6 +++++ .../tests/CultMath.Tests/QuaternionTests.cs | 27 +++++++++++++++++++ .../CultMath.Tests/QuaternionTests.cs.meta | 2 ++ 3 files changed, 35 insertions(+) create mode 100644 packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs create mode 100644 packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs.meta diff --git a/packages/cultmath/src/CultMath/math.cs b/packages/cultmath/src/CultMath/math.cs index a7c41f48..76df64e7 100644 --- a/packages/cultmath/src/CultMath/math.cs +++ b/packages/cultmath/src/CultMath/math.cs @@ -515,6 +515,12 @@ public static partial class math // HLSL mul: mul(m, v) treats v as a column vector, mul(v, m) as a row vector. public static float mul(float left, float right) => left * right; + // mul(v, v) is the inner product, as in HLSL. It also keeps mul(float4, float4) from binding to the + // quaternion product through float4's implicit conversion to quaternion. + public static float mul(float2 left, float2 right) => dot(left, right); + public static float mul(float3 left, float3 right) => dot(left, right); + public static float mul(float4 left, float4 right) => dot(left, right); + public static float2 mul(float2x2 m, float2 v) => new(m._m00 * v.x + m._m01 * v.y, m._m10 * v.x + m._m11 * v.y); diff --git a/packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs b/packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs new file mode 100644 index 00000000..c57c7900 --- /dev/null +++ b/packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs @@ -0,0 +1,27 @@ +using CultMath; +using Xunit; +using static CultMath.math; + +namespace CultMath.Tests; + +/// +/// The rotation algebra of math.Quaternion.cs. Conventions: Hamilton, xyzw, radians, right-handed, +/// mul(a, b) applies b first. Rotations are compared by what they do to vectors or by |dot| near 1, +/// never by component equality (q and -q are one rotation), except where a test says bit-identical. +/// +public sealed class QuaternionTests +{ + [Fact] + public void MulOfTwoFloat4IsTheInnerProduct() + { + // quaternion converts implicitly from float4; without the vector overloads this call would bind + // to the quaternion product and return a float4. + var a = new float4(1.0f, 2.0f, 3.0f, 4.0f); + var b = new float4(5.0f, 6.0f, 7.0f, 8.0f); + float r = mul(a, b); + Assert.Equal(70.0f, r); + Assert.Equal(dot(a, b), r); + Assert.Equal(11.0f, mul(new float2(1.0f, 2.0f), new float2(3.0f, 4.0f))); + Assert.Equal(32.0f, mul(new float3(1.0f, 2.0f, 3.0f), new float3(4.0f, 5.0f, 6.0f))); + } +} diff --git a/packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs.meta b/packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs.meta new file mode 100644 index 00000000..afb84c3e --- /dev/null +++ b/packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 4bee56d64b8862632f92e8fdbe20379c