mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 20:48:24 +01:00
197 lines
7.3 KiB
C#
197 lines
7.3 KiB
C#
// <auto-generated /> - edit Matrix.tt to modify
|
|
#nullable enable
|
|
|
|
namespace Brutal.Numerics;
|
|
|
|
/// <summary>
|
|
/// Implements a 4x4 matrix with float components.
|
|
/// </summary>
|
|
[DebuggerTypeProxy(typeof(DebugView))]
|
|
public partial struct float4x4 : IMatrix<float4x4, float>
|
|
, IFloatingMatrix4x4<float4x4, float, float3, float4, floatQuat>
|
|
{
|
|
internal class DebugView(float4x4 m)
|
|
{
|
|
public float4 X => m.X;
|
|
public float4 Y => m.Y;
|
|
public float4 Z => m.Z;
|
|
public float4 W => m.W;
|
|
}
|
|
|
|
/// <summary>The first row of the matrix.</summary>
|
|
[XmlIgnore]
|
|
public float4 X;
|
|
|
|
/// <summary>The second row of the matrix.</summary>
|
|
[XmlIgnore]
|
|
public float4 Y;
|
|
|
|
/// <summary>The third row of the matrix.</summary>
|
|
[XmlIgnore]
|
|
public float4 Z;
|
|
|
|
/// <summary>The fourth row of the matrix.</summary>
|
|
[XmlIgnore]
|
|
public float4 W;
|
|
|
|
/// <inheritdoc cref="Matrix4x4(float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public float4x4(
|
|
float m11,
|
|
float m12,
|
|
float m13,
|
|
float m14,
|
|
float m21,
|
|
float m22,
|
|
float m23,
|
|
float m24,
|
|
float m31,
|
|
float m32,
|
|
float m33,
|
|
float m34,
|
|
float m41,
|
|
float m42,
|
|
float m43,
|
|
float m44
|
|
)
|
|
{
|
|
X = new float4(m11, m12, m13, m14);
|
|
Y = new float4(m21, m22, m23, m24);
|
|
Z = new float4(m31, m32, m33, m34);
|
|
W = new float4(m41, m42, m43, m44);
|
|
}
|
|
|
|
/// <summary>Creates a matrix from the leading 16 elements of a span, row by row.</summary>
|
|
/// <param name="span">The elements to read; must contain at least 16, and any beyond that are ignored.</param>
|
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="span" /> holds fewer than 16 elements.</exception>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public float4x4(ReadOnlySpan<float> span)
|
|
{
|
|
if (span.Length < 16)
|
|
throw new ArgumentOutOfRangeException(nameof(span), "Length must be at least 16.");
|
|
|
|
this = Unsafe.ReadUnaligned<float4x4>(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(span)));
|
|
}
|
|
|
|
/// <inheritdoc cref="Matrix4x4(Matrix3x2)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public float4x4(float3x2 value)
|
|
{
|
|
X = new float4(value.X, 0, 0);
|
|
Y = new float4(value.Y, 0, 0);
|
|
Z = float4.UnitZ;
|
|
W = new float4(value.Z, 0, 1);
|
|
}
|
|
|
|
static float4x4 IMatrix<float4x4, float>.Create(ReadOnlySpan<float> span) => new(span);
|
|
|
|
/// <inheritdoc />
|
|
public static float4x4 Identity
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get
|
|
{
|
|
float4x4 result;
|
|
|
|
result.X = float4.UnitX;
|
|
result.Y = float4.UnitY;
|
|
result.Z = float4.UnitZ;
|
|
result.W = float4.UnitW;
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public static int ComponentSize => sizeof(float);
|
|
|
|
/// <inheritdoc />
|
|
public static int RowCount => 4;
|
|
|
|
/// <inheritdoc />
|
|
public static int ColumnCount => 4;
|
|
|
|
/// <inheritdoc cref="Matrix4x4.IsIdentity" />
|
|
public readonly bool IsIdentity
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => X == float4.UnitX && Y == float4.UnitY && Z == float4.UnitZ && W == float4.UnitW;
|
|
}
|
|
|
|
/// <summary>The kernel view: the same 64/128 bytes as the row fields, seen as SIMD vectors.</summary>
|
|
private readonly struct Rows(Vector128<float> x, Vector128<float> y, Vector128<float> z, Vector128<float> w)
|
|
{
|
|
public readonly Vector128<float> X = x, Y = y, Z = z, W = w;
|
|
|
|
/// <summary>One row through this whole matrix: one multiply, three fused-estimate accumulations.</summary>
|
|
// fused-estimate is permitted to fuse: results may differ by an ulp between FMA and non-FMA hardware
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public readonly Vector128<float> Transform(Vector128<float> row)
|
|
{
|
|
var result = X * row.GetElement(0);
|
|
result = Vector128.MultiplyAddEstimate(Y, Vector128.Create(row.GetElement(1)), result);
|
|
result = Vector128.MultiplyAddEstimate(Z, Vector128.Create(row.GetElement(2)), result);
|
|
result = Vector128.MultiplyAddEstimate(W, Vector128.Create(row.GetElement(3)), result);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
[UnscopedRef]
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private readonly ref readonly Rows AsRows()
|
|
{
|
|
return ref Unsafe.As<float4x4, Rows>(ref Unsafe.AsRef(in this));
|
|
}
|
|
|
|
|
|
/// <summary>The half-matrix view: rows 01 and 23 as one vector each - the width where a two-source lane mix is a single permute.</summary>
|
|
private readonly struct Halves(Vector256<float> lo, Vector256<float> hi)
|
|
{
|
|
public readonly Vector256<float> Lo = lo, Hi = hi;
|
|
}
|
|
|
|
/// <summary>Transpose in registers. net10 has no two-source shuffle (Zips are net11), so this rides WIDTH instead: at double the row width each transposed half is one permute per source half plus a blend.</summary>
|
|
// one index constant serves both sources - the don't-care lanes land under the blend mask;
|
|
// every arm is a JIT constant, so exactly one survives per machine
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static float4x4 TransposeKernel(in float4x4 matrix)
|
|
{
|
|
if (Vector512.IsHardwareAccelerated)
|
|
{
|
|
// the whole matrix is one Vector512: transpose is ONE 16-lane permute
|
|
var whole = Unsafe.As<float4x4, Vector512<float>>(ref Unsafe.AsRef(in matrix));
|
|
var transposed = Vector512.ShuffleNative(whole, Vector512.Create(0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15));
|
|
return Unsafe.BitCast<Vector512<float>, float4x4>(transposed);
|
|
}
|
|
|
|
if (Vector256.IsHardwareAccelerated)
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
static ref readonly Halves AsHalves(in float4x4 value)
|
|
{
|
|
return ref Unsafe.As<float4x4, Halves>(ref Unsafe.AsRef(in value));
|
|
}
|
|
|
|
ref readonly var halves = ref AsHalves(in matrix);
|
|
|
|
var idxLo = Vector256.Create(0, 4, 0, 4, 1, 5, 1, 5);
|
|
var idxHi = Vector256.Create(2, 6, 2, 6, 3, 7, 3, 7);
|
|
var takeHi = Vector256.Create(0, 0, -1, -1, 0, 0, -1, -1).AsSingle();
|
|
|
|
var transposed = new Halves(
|
|
Vector256.ConditionalSelect(takeHi, Vector256.ShuffleNative(halves.Hi, idxLo), Vector256.ShuffleNative(halves.Lo, idxLo)),
|
|
Vector256.ConditionalSelect(takeHi, Vector256.ShuffleNative(halves.Hi, idxHi), Vector256.ShuffleNative(halves.Lo, idxHi)));
|
|
return Unsafe.BitCast<Halves, float4x4>(transposed);
|
|
}
|
|
|
|
// no accelerated width to widen into (arm64 tops out at V128): plain field shuffle
|
|
float4x4 result;
|
|
|
|
result.X = new float4(matrix.X.X, matrix.Y.X, matrix.Z.X, matrix.W.X);
|
|
result.Y = new float4(matrix.X.Y, matrix.Y.Y, matrix.Z.Y, matrix.W.Y);
|
|
result.Z = new float4(matrix.X.Z, matrix.Y.Z, matrix.Z.Z, matrix.W.Z);
|
|
result.W = new float4(matrix.X.W, matrix.Y.W, matrix.Z.W, matrix.W.W);
|
|
|
|
return result;
|
|
}
|
|
}
|