mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 20:48:24 +01:00
189 lines
7 KiB
C#
189 lines
7 KiB
C#
// <auto-generated /> - edit Matrix.tt to modify
|
|
#nullable enable
|
|
|
|
namespace Brutal.Numerics;
|
|
|
|
/// <summary>
|
|
/// Implements a 4x4 matrix with double components.
|
|
/// </summary>
|
|
[DebuggerTypeProxy(typeof(DebugView))]
|
|
public partial struct double4x4 : IMatrix<double4x4, double>
|
|
, IFloatingMatrix4x4<double4x4, double, double3, double4, doubleQuat>
|
|
{
|
|
internal class DebugView(double4x4 m)
|
|
{
|
|
public double4 X => m.X;
|
|
public double4 Y => m.Y;
|
|
public double4 Z => m.Z;
|
|
public double4 W => m.W;
|
|
}
|
|
|
|
/// <summary>The first row of the matrix.</summary>
|
|
[XmlIgnore]
|
|
public double4 X;
|
|
|
|
/// <summary>The second row of the matrix.</summary>
|
|
[XmlIgnore]
|
|
public double4 Y;
|
|
|
|
/// <summary>The third row of the matrix.</summary>
|
|
[XmlIgnore]
|
|
public double4 Z;
|
|
|
|
/// <summary>The fourth row of the matrix.</summary>
|
|
[XmlIgnore]
|
|
public double4 W;
|
|
|
|
/// <inheritdoc cref="Matrix4x4(float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public double4x4(
|
|
double m11,
|
|
double m12,
|
|
double m13,
|
|
double m14,
|
|
double m21,
|
|
double m22,
|
|
double m23,
|
|
double m24,
|
|
double m31,
|
|
double m32,
|
|
double m33,
|
|
double m34,
|
|
double m41,
|
|
double m42,
|
|
double m43,
|
|
double m44
|
|
)
|
|
{
|
|
X = new double4(m11, m12, m13, m14);
|
|
Y = new double4(m21, m22, m23, m24);
|
|
Z = new double4(m31, m32, m33, m34);
|
|
W = new double4(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 double4x4(ReadOnlySpan<double> span)
|
|
{
|
|
if (span.Length < 16)
|
|
throw new ArgumentOutOfRangeException(nameof(span), "Length must be at least 16.");
|
|
|
|
this = Unsafe.ReadUnaligned<double4x4>(ref Unsafe.As<double, byte>(ref MemoryMarshal.GetReference(span)));
|
|
}
|
|
|
|
/// <inheritdoc cref="Matrix4x4(Matrix3x2)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public double4x4(double3x2 value)
|
|
{
|
|
X = new double4(value.X, 0, 0);
|
|
Y = new double4(value.Y, 0, 0);
|
|
Z = double4.UnitZ;
|
|
W = new double4(value.Z, 0, 1);
|
|
}
|
|
|
|
static double4x4 IMatrix<double4x4, double>.Create(ReadOnlySpan<double> span) => new(span);
|
|
|
|
/// <inheritdoc />
|
|
public static double4x4 Identity
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get
|
|
{
|
|
double4x4 result;
|
|
|
|
result.X = double4.UnitX;
|
|
result.Y = double4.UnitY;
|
|
result.Z = double4.UnitZ;
|
|
result.W = double4.UnitW;
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public static int ComponentSize => sizeof(double);
|
|
|
|
/// <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 == double4.UnitX && Y == double4.UnitY && Z == double4.UnitZ && W == double4.UnitW;
|
|
}
|
|
|
|
/// <summary>The kernel view: the same 64/128 bytes as the row fields, seen as SIMD vectors.</summary>
|
|
private readonly struct Rows(Vector256<double> x, Vector256<double> y, Vector256<double> z, Vector256<double> w)
|
|
{
|
|
public readonly Vector256<double> 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 Vector256<double> Transform(Vector256<double> row)
|
|
{
|
|
var result = X * row.GetElement(0);
|
|
result = Vector256.MultiplyAddEstimate(Y, Vector256.Create(row.GetElement(1)), result);
|
|
result = Vector256.MultiplyAddEstimate(Z, Vector256.Create(row.GetElement(2)), result);
|
|
result = Vector256.MultiplyAddEstimate(W, Vector256.Create(row.GetElement(3)), result);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
[UnscopedRef]
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private readonly ref readonly Rows AsRows()
|
|
{
|
|
return ref Unsafe.As<double4x4, 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(Vector512<double> lo, Vector512<double> hi)
|
|
{
|
|
public readonly Vector512<double> 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 double4x4 TransposeKernel(in double4x4 matrix)
|
|
{
|
|
if (Vector512.IsHardwareAccelerated)
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
static ref readonly Halves AsHalves(in double4x4 value)
|
|
{
|
|
return ref Unsafe.As<double4x4, Halves>(ref Unsafe.AsRef(in value));
|
|
}
|
|
|
|
ref readonly var halves = ref AsHalves(in matrix);
|
|
|
|
var idxLo = Vector512.Create(0, 4, 0, 4, 1, 5, 1, 5);
|
|
var idxHi = Vector512.Create(2, 6, 2, 6, 3, 7, 3, 7);
|
|
var takeHi = Vector512.Create(0, 0, -1, -1, 0, 0, -1, -1).AsDouble();
|
|
|
|
var transposed = new Halves(
|
|
Vector512.ConditionalSelect(takeHi, Vector512.ShuffleNative(halves.Hi, idxLo), Vector512.ShuffleNative(halves.Lo, idxLo)),
|
|
Vector512.ConditionalSelect(takeHi, Vector512.ShuffleNative(halves.Hi, idxHi), Vector512.ShuffleNative(halves.Lo, idxHi)));
|
|
return Unsafe.BitCast<Halves, double4x4>(transposed);
|
|
}
|
|
|
|
// no accelerated width to widen into (arm64 tops out at V128): plain field shuffle
|
|
double4x4 result;
|
|
|
|
result.X = new double4(matrix.X.X, matrix.Y.X, matrix.Z.X, matrix.W.X);
|
|
result.Y = new double4(matrix.X.Y, matrix.Y.Y, matrix.Z.Y, matrix.W.Y);
|
|
result.Z = new double4(matrix.X.Z, matrix.Y.Z, matrix.Z.Z, matrix.W.Z);
|
|
result.W = new double4(matrix.X.W, matrix.Y.W, matrix.Z.W, matrix.W.W);
|
|
|
|
return result;
|
|
}
|
|
}
|