This commit is contained in:
Rian Drake 2026-10-15 14:09:11 +13:00
commit b578433e37
10 changed files with 784842 additions and 0 deletions

View file

@ -0,0 +1,113 @@
// <auto-generated /> - edit Vector.color.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct byte4
{
/// <summary>
/// Constructs a byte4 vector representing an RGB color with full opacity (alpha = 255).
/// </summary>
/// <param name="r">Red component.</param>
/// <param name="g">Green component.</param>
/// <param name="b">Blue component.</param>
/// <returns>A byte4 color with the specified RGB values and alpha set to 255.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Rgb(byte r, byte g, byte b)
{
return new byte4(r, g, b, 255);
}
/// <summary>
/// Constructs a byte4 vector representing a grayscale color with full opacity (alpha = 255).
/// </summary>
/// <param name="v">Single value used for red, green, and blue components.</param>
/// <returns>A byte4 grayscale color with the specified value and alpha set to 255.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Grayscale(byte v)
{
return new byte4(v, v, v, 255);
}
/// <summary>
/// Constructs a byte4 vector representing an RGBA color.
/// </summary>
/// <param name="r">Red component.</param>
/// <param name="g">Green component.</param>
/// <param name="b">Blue component.</param>
/// <param name="a">Alpha component.</param>
/// <returns>A byte4 color with the specified RGBA values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Rgba(byte r, byte g, byte b, byte a)
{
return new byte4(r, g, b, a);
}
/// <summary>
/// Constructs a byte4 vector representing a grayscale color with a specified alpha value.
/// </summary>
/// <param name="v">Single value used for red, green, and blue components.</param>
/// <param name="a">Alpha component.</param>
/// <returns>A byte4 grayscale color with the specified value and alpha.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Grayscale(byte v, byte a)
{
return new byte4(v, v, v, a);
}
}
// Specialized for byte4
public partial struct byte4
{
/// <summary>
/// Construct a byte4 vector from a uint hex value
/// </summary>
/// <param name="hex">Color value as a uint</param>
/// <returns>A byte4 color with the specified RGBA values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Hex(uint hex)
{
return new byte4((byte)(hex >> 24), (byte)(hex >> 16), (byte)(hex >> 8), (byte)(hex >> 0));
}
/// <summary>
/// Construct a byte4 vector in a RGBA color context, from a uint hex value
/// </summary>
/// <param name="hex">uint value</param>
/// <returns>A byte4 color with the specified RGBA values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 HexColor(uint hex)
{
return Hex(hex);
}
/// <summary>
/// Construct a byte4 vector from a uint hex value string
/// </summary>
/// <param name="hex">Hexadecimal string representation in 0xRRGGBBAA format.</param>
/// <returns>vector</returns>
public static byte4 Hex(ReadOnlySpan<char> hex)
{
if (hex.Length != 10 || hex[0] != '0' || hex[1] != 'x')
throw new FormatException("Invalid hex format");
return Hex(uint.Parse(hex[2..], NumberStyles.HexNumber));
}
/// <summary>
/// Construct a byte4 vector in an RGBA color context, from a uint hex value string
/// </summary>
/// <param name="hex">Hexadecimal string representation in 0xRRGGBBAA format.</param>
/// <returns>vector</returns>
public static byte4 HexColor(ReadOnlySpan<char> hex)
{
return Hex(hex);
}
/// <summary>
/// Converts the byte4 color to a hexadecimal string.
/// </summary>
public readonly string ToHexString()
{
return $"0x{R:X2}{G:X2}{B:X2}{A:X2}";
}
}

View file

@ -0,0 +1,28 @@
// <auto-generated /> - edit Vector.copy.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct byte4
{
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void CopyTo(Span<byte> destination)
{
if (destination.Length < ComponentCount)
throw new ArgumentException("Length must be at least 4.", nameof(destination));
Unsafe.WriteUnaligned(ref Unsafe.As<byte, byte>(ref MemoryMarshal.GetReference(destination)), this);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryCopyTo(Span<byte> destination)
{
if (destination.Length < ComponentCount)
return false;
Unsafe.WriteUnaligned(ref Unsafe.As<byte, byte>(ref MemoryMarshal.GetReference(destination)), this);
return true;
}
}

View file

@ -0,0 +1,41 @@
// <auto-generated /> - edit Vector.equality.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct byte4
{
/// <inheritdoc />
public readonly override int GetHashCode()
{
return HashCode.Combine(X, Y, Z, W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool Equals(byte4 other)
{
return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z) && W.Equals(other.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is byte4 other && Equals(other);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(byte4 a, byte4 b)
{
return a.X == b.X && a.Y == b.Y && a.Z == b.Z && a.W == b.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(byte4 a, byte4 b)
{
return !(a == b);
}
}

View file

@ -0,0 +1,92 @@
// <auto-generated /> - edit Vector.format.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct byte4 : ISpanFormattable, IUtf8SpanFormattable, ISpanParsable<byte4>
{
/// <inheritdoc />
public readonly override string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
}
/// <inheritdoc cref="Vector4.ToString(string?)" />
public readonly string ToString(string? format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
/// <inheritdoc />
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
var separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
return $"<{X.ToString(format, formatProvider)}{separator} {Y.ToString(format, formatProvider)}{separator} {Z.ToString(format, formatProvider)}{separator} {W.ToString(format, formatProvider)}>";
}
/// <inheritdoc />
public readonly bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{
return StringHelpers.TryFormat<byte4, byte>(this, destination, out charsWritten, format, provider);
}
/// <inheritdoc />
public readonly bool TryFormat(Span<byte> destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{
return StringHelpers.TryFormat<byte4, byte>(this, destination, out bytesWritten, format, provider);
}
/// <inheritdoc cref="Parse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?)" />
public static byte4 Parse(string s, IFormatProvider? provider)
{
ArgumentNullException.ThrowIfNull(s);
return Parse(s.AsSpan(), NumberStyles.Integer, provider);
}
/// <inheritdoc cref="Parse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?)" />
public static byte4 Parse(ReadOnlySpan<char> s, IFormatProvider? provider = null)
{
return Parse(s, NumberStyles.Integer, provider);
}
/// <inheritdoc cref="Parse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?)" />
public static byte4 Parse(string s, NumberStyles style, IFormatProvider? provider)
{
ArgumentNullException.ThrowIfNull(s);
return Parse(s.AsSpan(), style, provider);
}
/// <summary>Parses from the <see cref="ToString()" /> format, <c>&lt;1, 2, 3&gt;</c>.</summary>
/// <exception cref="FormatException">The input is not in a valid format, or a component fails to parse.</exception>
public static byte4 Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider)
{
if (!TryParse(s, style, provider, out var result))
throw new FormatException($"'{s}' is not in a valid byte4 format.");
return result;
}
/// <inheritdoc cref="TryParse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?, out byte4)" />
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out byte4 result)
{
return TryParse(s.AsSpan(), NumberStyles.Integer, provider, out result);
}
/// <inheritdoc cref="TryParse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?, out byte4)" />
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out byte4 result)
{
return TryParse(s, NumberStyles.Integer, provider, out result);
}
/// <inheritdoc cref="TryParse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?, out byte4)" />
public static bool TryParse([NotNullWhen(true)] string? s, NumberStyles style, IFormatProvider? provider, out byte4 result)
{
return TryParse(s.AsSpan(), style, provider, out result);
}
/// <summary>Parses from the <see cref="ToString()" /> format, <c>&lt;1, 2, 3&gt;</c>.</summary>
/// <returns>true if the input parsed as a byte4.</returns>
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out byte4 result)
{
return StringHelpers.TryParse<byte4, byte>(s, style, provider, out result);
}
}

View file

@ -0,0 +1,147 @@
// <auto-generated /> - edit Vector.tt to modify
#nullable enable
namespace Brutal.Numerics;
/// <summary>
/// Implements a 4D vector with byte components.
/// </summary>
[DebuggerTypeProxy(typeof(VectorDebugView4<byte4, byte, float>))]
public partial struct byte4 : IVector4<byte4, byte>
, IIntegerVector<byte4, byte, int, float>
{
/// <inheritdoc />
public static int ComponentSize => sizeof(byte);
/// <summary>Gets the number of components.</summary>
public static int ComponentCount => 4;
/// <inheritdoc cref="Vector4.X" />
[XmlAttribute]
public byte X;
/// <inheritdoc cref="Vector4.Y" />
[XmlAttribute]
public byte Y;
/// <inheritdoc cref="Vector4.Z" />
[XmlAttribute]
public byte Z;
/// <inheritdoc cref="Vector4.W" />
[XmlAttribute]
public byte W;
byte IVector4<byte4, byte>.X { readonly get => X; set => X = value; }
byte IVector4<byte4, byte>.Y { readonly get => Y; set => Y = value; }
byte IVector4<byte4, byte>.Z { readonly get => Z; set => Z = value; }
byte IVector4<byte4, byte>.W { readonly get => W; set => W = value; }
/// <inheritdoc cref="Vector4(float, float, float, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte4(byte x, byte y, byte z, byte w)
{
X = x;
Y = y;
Z = z;
W = w;
}
/// <inheritdoc cref="Vector4(float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte4(byte xyzw)
: this(xyzw, xyzw, xyzw, xyzw) { }
/// <inheritdoc cref="Vector4(Vector3, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte4(byte3 xyz, byte w)
: this(xyz.X, xyz.Y, xyz.Z, w) { }
/// <summary>
/// Creates a new instance of <see cref="byte4" /> from two <see cref="byte2" /> instances.
/// </summary>
/// <param name="xy">The first two components.</param>
/// <param name="zw">The last two components.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte4(byte2 xy, byte2 zw)
: this(xy.X, xy.Y, zw.X, zw.Y) { }
/// <inheritdoc cref="Vector4(Vector2, float, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte4(byte2 xy, byte z, byte w)
: this(xy.X, xy.Y, z, w) { }
/// <summary>Creates a vector from the leading 4 elements of a span.</summary>
/// <param name="span">The elements to read; must contain at least 4, and any beyond that are ignored.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="span" /> holds fewer than 4 elements.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte4(ReadOnlySpan<byte> span)
{
if (span.Length < 4)
throw new ArgumentOutOfRangeException(nameof(span), "Length must be at least 4.");
this = Unsafe.ReadUnaligned<byte4>(ref Unsafe.As<byte, byte>(ref MemoryMarshal.GetReference(span)));
}
/// <inheritdoc cref="byte4(ReadOnlySpan{byte})" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static byte4 IComponentTuple<byte4, byte>.Create(ReadOnlySpan<byte> span) => new(span);
/// <inheritdoc />
public static byte4 UnitX => new(1, 0, 0, 0);
/// <inheritdoc />
public static byte4 UnitY => new(0, 1, 0, 0);
/// <inheritdoc />
public static byte4 UnitZ => new(0, 0, 1, 0);
/// <inheritdoc />
public static byte4 UnitW => new(0, 0, 0, 1);
/// <inheritdoc />
public byte this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get
{
if ((uint)index >= ComponentCount)
throw new ArgumentOutOfRangeException(nameof(index));
return Unsafe.Add(ref Unsafe.AsRef(in X), index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
if ((uint)index >= ComponentCount)
throw new ArgumentOutOfRangeException(nameof(index));
Unsafe.Add(ref X, index) = value;
}
}
public readonly void Deconstruct(out byte x, out byte y, out byte z, out byte w)
{
x = X;
y = Y;
z = Z;
w = W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
return MemoryMarshal.CreateSpan(ref Unsafe.As<byte4, byte>(ref this), 4);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlySpan<byte> AsReadOnlySpan()
{
return MemoryMarshal.CreateReadOnlySpan(in Unsafe.As<byte4, byte>(ref Unsafe.AsRef(in this)), 4);
}
}

View file

@ -0,0 +1,42 @@
// <auto-generated /> - edit Vector.math-metric.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct byte4
{
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float Length()
{
return (float)double.Sqrt((double)X * X + (double)Y * Y + (double)Z * Z + (double)W * W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int LengthSquared()
{
return Dot(this, this);
}
/// <inheritdoc cref="Vector4.Distance" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Distance(byte4 a, byte4 b)
{
return (float)double.Sqrt(((double)a.X - b.X) * ((double)a.X - b.X) + ((double)a.Y - b.Y) * ((double)a.Y - b.Y) + ((double)a.Z - b.Z) * ((double)a.Z - b.Z) + ((double)a.W - b.W) * ((double)a.W - b.W));
}
/// <inheritdoc cref="Vector4.DistanceSquared" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int DistanceSquared(byte4 a, byte4 b)
{
return (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y) + (a.Z - b.Z) * (a.Z - b.Z) + (a.W - b.W) * (a.W - b.W);
}
/// <inheritdoc cref="Vector4.Dot" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Dot(byte4 a, byte4 b)
{
return a.X * b.X + a.Y * b.Y + a.Z * b.Z + a.W * b.W;
}
}

View file

@ -0,0 +1,100 @@
// <auto-generated /> - edit Vector.numerics-binary.tt to modify
#nullable enable
namespace Brutal.Numerics;
// IBinaryNumber
public partial struct byte4 : IBinaryNumber<byte4>
{
/// <summary>Determines if every component of a value is a power of two.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool IBinaryNumber<byte4>.IsPow2(byte4 value)
{
return byte.IsPow2(value.X) && byte.IsPow2(value.Y) && byte.IsPow2(value.Z) && byte.IsPow2(value.W);
}
/// <inheritdoc cref="IBinaryNumber{TSelf}.Log2" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Log2(byte4 value)
{
return new byte4(byte.Log2(value.X), byte.Log2(value.Y), byte.Log2(value.Z), byte.Log2(value.W));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator &(byte4 a, byte4 b)
{
return new((byte)(a.X & b.X), (byte)(a.Y & b.Y), (byte)(a.Z & b.Z), (byte)(a.W & b.W));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator |(byte4 a, byte4 b)
{
return new((byte)(a.X | b.X), (byte)(a.Y | b.Y), (byte)(a.Z | b.Z), (byte)(a.W | b.W));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator ^(byte4 a, byte4 b)
{
return new((byte)(a.X ^ b.X), (byte)(a.Y ^ b.Y), (byte)(a.Z ^ b.Z), (byte)(a.W ^ b.W));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator ~(byte4 value)
{
return new((byte)(~value.X), (byte)(~value.Y), (byte)(~value.Z), (byte)(~value.W));
}
}
// the componentwise predicate mask (no interface to mirror) - the per-lane truth IsPow2 collapses
public partial struct byte4
{
/// <summary>Determines which components of a value are powers of two.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool4 IsPow2(byte4 value)
{
return new bool4(byte.IsPow2(value.X), byte.IsPow2(value.Y), byte.IsPow2(value.Z), byte.IsPow2(value.W));
}
}
// the good bits from IBinaryInteger - declared on IIntegerVector
public partial struct byte4
{
/// <inheritdoc cref="IBinaryInteger{TSelf}.PopCount" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 PopCount(byte4 value)
{
return new byte4(byte.PopCount(value.X), byte.PopCount(value.Y), byte.PopCount(value.Z), byte.PopCount(value.W));
}
/// <inheritdoc cref="IBinaryInteger{TSelf}.LeadingZeroCount" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 LeadingZeroCount(byte4 value)
{
return new byte4(byte.LeadingZeroCount(value.X), byte.LeadingZeroCount(value.Y), byte.LeadingZeroCount(value.Z), byte.LeadingZeroCount(value.W));
}
/// <inheritdoc cref="IBinaryInteger{TSelf}.TrailingZeroCount" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 TrailingZeroCount(byte4 value)
{
return new byte4(byte.TrailingZeroCount(value.X), byte.TrailingZeroCount(value.Y), byte.TrailingZeroCount(value.Z), byte.TrailingZeroCount(value.W));
}
/// <inheritdoc cref="IBinaryInteger{TSelf}.RotateLeft" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 RotateLeft(byte4 value, int rotateAmount)
{
return new byte4(byte.RotateLeft(value.X, rotateAmount), byte.RotateLeft(value.Y, rotateAmount), byte.RotateLeft(value.Z, rotateAmount), byte.RotateLeft(value.W, rotateAmount));
}
/// <inheritdoc cref="IBinaryInteger{TSelf}.RotateRight" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 RotateRight(byte4 value, int rotateAmount)
{
return new byte4(byte.RotateRight(value.X, rotateAmount), byte.RotateRight(value.Y, rotateAmount), byte.RotateRight(value.Z, rotateAmount), byte.RotateRight(value.W, rotateAmount));
}
}

View file

@ -0,0 +1,389 @@
// <auto-generated /> - edit Vector.numerics.tt to modify
#nullable enable
namespace Brutal.Numerics;
// INumberBase
public partial struct byte4 : INumberBase<byte4>
{
/// <inheritdoc />
public static byte4 Zero => new(0);
/// <inheritdoc />
public static byte4 One => new(1);
static byte4 IAdditiveIdentity<byte4, byte4>.AdditiveIdentity => Zero;
static byte4 IMultiplicativeIdentity<byte4, byte4>.MultiplicativeIdentity => One;
static int INumberBase<byte4>.Radix => 2; // every component type is binary
static byte4 INumberBase<byte4>.Abs(byte4 value) => value;
static bool INumberBase<byte4>.IsCanonical(byte4 value) => true;
static bool INumberBase<byte4>.IsComplexNumber(byte4 value) => false;
static bool INumberBase<byte4>.IsImaginaryNumber(byte4 value) => false;
/// <summary>Determines if every component of a value is zero.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase<byte4>.IsZero(byte4 value)
{
return value == Zero;
}
/// <summary>Determines if every component of a value is an even integral value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase<byte4>.IsEvenInteger(byte4 value)
{
return (value.X & 1) == 0 && (value.Y & 1) == 0 && (value.Z & 1) == 0 && (value.W & 1) == 0;
}
/// <summary>Determines if every component of a value is an odd integral value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase<byte4>.IsOddInteger(byte4 value)
{
return (value.X & 1) == 1 && (value.Y & 1) == 1 && (value.Z & 1) == 1 && (value.W & 1) == 1;
}
static bool INumberBase<byte4>.IsNegative(byte4 value) => false;
static bool INumberBase<byte4>.IsPositive(byte4 value) => true;
static bool INumberBase<byte4>.IsFinite(byte4 value) => true;
static bool INumberBase<byte4>.IsInfinity(byte4 value) => false;
static bool INumberBase<byte4>.IsNegativeInfinity(byte4 value) => false;
static bool INumberBase<byte4>.IsPositiveInfinity(byte4 value) => false;
static bool INumberBase<byte4>.IsInteger(byte4 value) => true;
static bool INumberBase<byte4>.IsRealNumber(byte4 value) => true;
static bool INumberBase<byte4>.IsNaN(byte4 value) => false;
static bool INumberBase<byte4>.IsNormal(byte4 value) => value.X != 0 && value.Y != 0 && value.Z != 0 && value.W != 0;
static bool INumberBase<byte4>.IsSubnormal(byte4 value) => false;
/// <summary>Computes, componentwise, the value whose magnitude is greater.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 MaxMagnitude(byte4 a, byte4 b)
{
return Max(a, b);
}
/// <summary>Computes, componentwise, the value whose magnitude is greater.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 MaxMagnitudeNumber(byte4 a, byte4 b)
{
return Max(a, b);
}
/// <summary>Computes, componentwise, the value whose magnitude is smaller.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 MinMagnitude(byte4 a, byte4 b)
{
return Min(a, b);
}
/// <summary>Computes, componentwise, the value whose magnitude is smaller.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 MinMagnitudeNumber(byte4 a, byte4 b)
{
return Min(a, b);
}
/// <inheritdoc cref="Vector4.Add" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Add(byte4 a, byte4 b)
{
return a + b;
}
/// <inheritdoc cref="Vector4.Divide(Vector4, Vector4)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Divide(byte4 a, byte4 b)
{
return a / b;
}
/// <inheritdoc cref="Vector4.Multiply(Vector4, Vector4)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Multiply(byte4 a, byte4 b)
{
return a * b;
}
/// <inheritdoc cref="Vector4.Negate" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Negate(byte4 value)
{
return -value;
}
/// <inheritdoc cref="Vector4.Subtract" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Subtract(byte4 a, byte4 b)
{
return a - b;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator +(byte4 a, byte4 b)
{
return new((byte)(a.X + b.X), (byte)(a.Y + b.Y), (byte)(a.Z + b.Z), (byte)(a.W + b.W));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator -(byte4 a, byte4 b)
{
return new((byte)(a.X - b.X), (byte)(a.Y - b.Y), (byte)(a.Z - b.Z), (byte)(a.W - b.W));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator *(byte4 a, byte4 b)
{
return new((byte)(a.X * b.X), (byte)(a.Y * b.Y), (byte)(a.Z * b.Z), (byte)(a.W * b.W));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator /(byte4 a, byte4 b)
{
return new((byte)(a.X / b.X), (byte)(a.Y / b.Y), (byte)(a.Z / b.Z), (byte)(a.W / b.W));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator -(byte4 a)
{
return new((byte)(0 - a.X), (byte)(0 - a.Y), (byte)(0 - a.Z), (byte)(0 - a.W));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator +(byte4 a)
{
return a;
}
// ++/-- are well-defined (+/- One) but a footgun on a concrete vector: generic-plane only.
static byte4 IIncrementOperators<byte4>.operator ++(byte4 value) => value + One;
static byte4 IDecrementOperators<byte4>.operator --(byte4 value) => value - One;
static bool INumberBase<byte4>.TryConvertFromChecked<TOther>(TOther value, out byte4 result)
{
return NumberConversion.TryConvertFrom<byte4, byte, TOther, NumberConversion.Checked>(value, out result);
}
static bool INumberBase<byte4>.TryConvertToChecked<TOther>(byte4 value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase<byte4>.TryConvertFromSaturating<TOther>(TOther value, out byte4 result)
{
return NumberConversion.TryConvertFrom<byte4, byte, TOther, NumberConversion.Saturating>(value, out result);
}
static bool INumberBase<byte4>.TryConvertToSaturating<TOther>(byte4 value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase<byte4>.TryConvertFromTruncating<TOther>(TOther value, out byte4 result)
{
return NumberConversion.TryConvertFrom<byte4, byte, TOther, NumberConversion.Truncating>(value, out result);
}
static bool INumberBase<byte4>.TryConvertToTruncating<TOther>(byte4 value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
}
// IMultiplyOperators<TSelf, TComponent, TSelf>, IDivisionOperators<TSelf, TComponent, TSelf>
public partial struct byte4
: IMultiplyOperators<byte4, byte, byte4>
, IDivisionOperators<byte4, byte, byte4>
{
/// <inheritdoc cref="Vector4.Divide(Vector4, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Divide(byte4 a, byte b)
{
return a / b;
}
/// <inheritdoc cref="Vector4.Multiply(Vector4, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Multiply(byte4 a, byte b)
{
return a * b;
}
/// <inheritdoc cref="Vector4.Multiply(float, Vector4)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Multiply(byte a, byte4 b)
{
return a * b;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator *(byte4 a, byte b)
{
return new((byte)(a.X * b), (byte)(a.Y * b), (byte)(a.Z * b), (byte)(a.W * b));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator /(byte4 a, byte b)
{
return new((byte)(a.X / b), (byte)(a.Y / b), (byte)(a.Z / b), (byte)(a.W / b));
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator *(byte a, byte4 b)
{
return new((byte)(a * b.X), (byte)(a * b.Y), (byte)(a * b.Z), (byte)(a * b.W));
}
}
// INumber
public partial struct byte4 : INumber<byte4>
{
/// <inheritdoc cref="Vector4.Clamp" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Clamp(byte4 value, byte4 min, byte4 max)
{
// HLSL contract when min > max: max wins (clamp is Max-then-Min); pinned in
// Clamp_InvertedBounds_MatchesBcl. NOT scalar T.Clamp, which throws on inversion.
return Min(Max(value, min), max);
}
/// <inheritdoc cref="Vector4.Max" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Max(byte4 a, byte4 b)
{
return new(byte.Max(a.X, b.X), byte.Max(a.Y, b.Y), byte.Max(a.Z, b.Z), byte.Max(a.W, b.W));
}
/// <inheritdoc cref="Vector4.Min" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 Min(byte4 a, byte4 b)
{
return new(byte.Min(a.X, b.X), byte.Min(a.Y, b.Y), byte.Min(a.Z, b.Z), byte.Min(a.W, b.W));
}
static byte4 INumber<byte4>.MaxNumber(byte4 a, byte4 b) => Max(a, b);
static byte4 INumber<byte4>.MinNumber(byte4 a, byte4 b) => Min(a, b);
static byte4 INumber<byte4>.CopySign(byte4 value, byte4 sign) => value;
// the Native variants permit either NaN/signed-zero behavior - forwarding is always legal
static byte4 INumber<byte4>.ClampNative(byte4 value, byte4 min, byte4 max) => Clamp(value, min, max);
static byte4 INumber<byte4>.MaxNative(byte4 a, byte4 b) => Max(a, b);
static byte4 INumber<byte4>.MinNative(byte4 a, byte4 b) => Min(a, b);
// Sign's int return cannot hold a per-lane answer, and a collapsed one would lie for mixed signs
static int INumber<byte4>.Sign(byte4 value) =>
throw new NotSupportedException($"{nameof(byte4)} has no scalar sign.");
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 operator %(byte4 a, byte4 b)
{
return new byte4((byte)(a.X % b.X), (byte)(a.Y % b.Y), (byte)(a.Z % b.Z), (byte)(a.W % b.W));
}
int IComparable.CompareTo(object? obj) =>
throw new NotSupportedException($"{nameof(byte4)} has no total order.");
int IComparable<byte4>.CompareTo(byte4 other) =>
throw new NotSupportedException($"{nameof(byte4)} has no total order.");
static bool IComparisonOperators<byte4, byte4, bool>.operator <(byte4 a, byte4 b) =>
throw new NotSupportedException($"{nameof(byte4)} has no total order.");
static bool IComparisonOperators<byte4, byte4, bool>.operator <=(byte4 a, byte4 b) =>
throw new NotSupportedException($"{nameof(byte4)} has no total order.");
static bool IComparisonOperators<byte4, byte4, bool>.operator >(byte4 a, byte4 b) =>
throw new NotSupportedException($"{nameof(byte4)} has no total order.");
static bool IComparisonOperators<byte4, byte4, bool>.operator >=(byte4 a, byte4 b) =>
throw new NotSupportedException($"{nameof(byte4)} has no total order.");
}
// IComponentwiseComparison + the componentwise predicate masks (no interface to mirror)
public partial struct byte4 : IComponentwiseComparison<byte4, bool4>
{
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool4 operator <(byte4 a, byte4 b)
{
return new bool4(a.X < b.X, a.Y < b.Y, a.Z < b.Z, a.W < b.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool4 operator <=(byte4 a, byte4 b)
{
return new bool4(a.X <= b.X, a.Y <= b.Y, a.Z <= b.Z, a.W <= b.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool4 operator >(byte4 a, byte4 b)
{
return new bool4(a.X > b.X, a.Y > b.Y, a.Z > b.Z, a.W > b.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool4 operator >=(byte4 a, byte4 b)
{
return new bool4(a.X >= b.X, a.Y >= b.Y, a.Z >= b.Z, a.W >= b.W);
}
/// <summary>Determines which components of a value are zero.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool4 IsZero(byte4 value)
{
return new bool4(value.X == 0, value.Y == 0, value.Z == 0, value.W == 0);
}
/// <summary>Determines which components of a value are an even integral value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool4 IsEvenInteger(byte4 value)
{
return new bool4(byte.IsEvenInteger(value.X), byte.IsEvenInteger(value.Y), byte.IsEvenInteger(value.Z), byte.IsEvenInteger(value.W));
}
/// <summary>Determines which components of a value are an odd integral value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool4 IsOddInteger(byte4 value)
{
return new bool4(byte.IsOddInteger(value.X), byte.IsOddInteger(value.Y), byte.IsOddInteger(value.Z), byte.IsOddInteger(value.W));
}
}
// IUnsignedNumber - a marker, so there is nothing to implement
public partial struct byte4 : IUnsignedNumber<byte4>;

View file

@ -0,0 +1,217 @@
// <auto-generated /> - edit Vector.pack.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct byte4
{
public static byte4 Unpack(in sbyte4 vector, Func<sbyte, byte> elementConverter)
{
VectorPack.Pack(vector, out byte4 result, elementConverter);
return result;
}
public static byte4 Pack(in short4 vector, Func<short, byte> elementConverter)
{
VectorPack.Pack(vector, out byte4 result, elementConverter);
return result;
}
public static byte4 Pack(in ushort4 vector, Func<ushort, byte> elementConverter)
{
VectorPack.Pack(vector, out byte4 result, elementConverter);
return result;
}
public static byte4 Pack(in int4 vector, Func<int, byte> elementConverter)
{
VectorPack.Pack(vector, out byte4 result, elementConverter);
return result;
}
public static byte4 Pack(in uint4 vector, Func<uint, byte> elementConverter)
{
VectorPack.Pack(vector, out byte4 result, elementConverter);
return result;
}
public static byte4 Pack(in float4 vector, Func<float, byte> elementConverter)
{
VectorPack.Pack(vector, out byte4 result, elementConverter);
return result;
}
public static byte4 Pack(in double4 vector, Func<double, byte> elementConverter)
{
VectorPack.Pack(vector, out byte4 result, elementConverter);
return result;
}
public static byte4 Unpack(in sbyte4 vector, Unpack.IntegerMixed mode = Numerics.Unpack.IntegerMixed.Cast)
{
return Unpack(vector, PackUtils.SByte.ToByte(mode));
}
public static byte4 Pack(in short4 vector, Pack.IntegerMixed mode = Numerics.Pack.IntegerMixed.Cast)
{
return Pack(vector, PackUtils.Short.ToByte(mode));
}
public static byte4 Pack(in ushort4 vector, Pack.Integer mode = Numerics.Pack.Integer.Cast)
{
return Pack(vector, PackUtils.UShort.ToByte(mode));
}
public static byte4 Pack(in int4 vector, Pack.IntegerMixed mode = Numerics.Pack.IntegerMixed.Cast)
{
return Pack(vector, PackUtils.Int.ToByte(mode));
}
public static byte4 Pack(in uint4 vector, Pack.Integer mode = Numerics.Pack.Integer.Cast)
{
return Pack(vector, PackUtils.UInt.ToByte(mode));
}
public static byte4 Pack(in float4 vector, Pack.Float mode = Numerics.Pack.Float.Truncate)
{
return Pack(vector, PackUtils.Float.ToByte(mode));
}
public static byte4 Pack(in double4 vector, Pack.Float mode = Numerics.Pack.Float.Truncate)
{
return Pack(vector, PackUtils.Double.ToByte(mode));
}
public static void UnpackSpan(
in ReadOnlySpan<sbyte4> source,
Span<byte4> destination,
Func<sbyte, byte> elementConverter
)
{
VectorPack.Pack(source, destination, elementConverter);
}
public static void PackSpan(
in ReadOnlySpan<short4> source,
Span<byte4> destination,
Func<short, byte> elementConverter
)
{
VectorPack.Pack(source, destination, elementConverter);
}
public static void PackSpan(
in ReadOnlySpan<ushort4> source,
Span<byte4> destination,
Func<ushort, byte> elementConverter
)
{
VectorPack.Pack(source, destination, elementConverter);
}
public static void PackSpan(
in ReadOnlySpan<int4> source,
Span<byte4> destination,
Func<int, byte> elementConverter
)
{
VectorPack.Pack(source, destination, elementConverter);
}
public static void PackSpan(
in ReadOnlySpan<uint4> source,
Span<byte4> destination,
Func<uint, byte> elementConverter
)
{
VectorPack.Pack(source, destination, elementConverter);
}
public static void PackSpan(
in ReadOnlySpan<float4> source,
Span<byte4> destination,
Func<float, byte> elementConverter
)
{
VectorPack.Pack(source, destination, elementConverter);
}
public static void PackSpan(
in ReadOnlySpan<double4> source,
Span<byte4> destination,
Func<double, byte> elementConverter
)
{
VectorPack.Pack(source, destination, elementConverter);
}
public static void UnpackSpan(
in ReadOnlySpan<sbyte4> source,
Span<byte4> destination,
Unpack.IntegerMixed mode = Numerics.Unpack.IntegerMixed.Cast
)
{
UnpackSpan(source, destination, PackUtils.SByte.ToByte(mode));
}
public static void PackSpan(
in ReadOnlySpan<short4> source,
Span<byte4> destination,
Pack.IntegerMixed mode = Numerics.Pack.IntegerMixed.Cast
)
{
PackSpan(source, destination, PackUtils.Short.ToByte(mode));
}
public static void PackSpan(
in ReadOnlySpan<ushort4> source,
Span<byte4> destination,
Pack.Integer mode = Numerics.Pack.Integer.Cast
)
{
PackSpan(source, destination, PackUtils.UShort.ToByte(mode));
}
public static void PackSpan(
in ReadOnlySpan<int4> source,
Span<byte4> destination,
Pack.IntegerMixed mode = Numerics.Pack.IntegerMixed.Cast
)
{
PackSpan(source, destination, PackUtils.Int.ToByte(mode));
}
public static void PackSpan(
in ReadOnlySpan<uint4> source,
Span<byte4> destination,
Pack.Integer mode = Numerics.Pack.Integer.Cast
)
{
PackSpan(source, destination, PackUtils.UInt.ToByte(mode));
}
public static void PackSpan(
in ReadOnlySpan<float4> source,
Span<byte4> destination,
Pack.Float mode = Numerics.Pack.Float.Truncate
)
{
PackSpan(source, destination, PackUtils.Float.ToByte(mode));
}
public static void PackSpan(
in ReadOnlySpan<double4> source,
Span<byte4> destination,
Pack.Float mode = Numerics.Pack.Float.Truncate
)
{
PackSpan(source, destination, PackUtils.Double.ToByte(mode));
}
// the packed-RGBA8888 color affordance (byte4 is the RGBA byte color); implicitness to revisit with the Color milestone
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator uint(byte4 v)
{
return Unsafe.BitCast<byte4, uint>(v);
}
}

File diff suppressed because it is too large Load diff