// - edit Vector.numerics.tt to modify
#nullable enable
namespace Brutal.Numerics;
// INumberBase
public partial struct int2 : INumberBase
{
///
public static int2 Zero => new(0);
///
public static int2 One => new(1);
static int2 IAdditiveIdentity.AdditiveIdentity => Zero;
static int2 IMultiplicativeIdentity.MultiplicativeIdentity => One;
static int INumberBase.Radix => 2; // every component type is binary
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Abs(int2 value)
{
return new int2(WrappingAbs(value.X), WrappingAbs(value.Y));
}
/* |MinValue| is unrepresentable, so that lane keeps MinValue and Abs can return a NEGATIVE
* number. Deliberate, settled 2026-08-25: Math.Abs throws there instead, which kills a whole
* vector for one pathological lane. Everyone else wraps - HLSL, GLSL, SPIR-V SAbs, LLVM's
* llvm.abs on its non-poison branch, and .NET's own Vector128.Abs, whose Scalar.Abs even
* carries the comment "avoid Math.Abs for integers since it throws for MinValue".
* unchecked is explicit because the wrap IS the contract here: on the narrow types the
* truncating cast is what performs it (-short.MinValue widens to 32768, which no short holds),
* so this must not depend on the project's overflow setting. */
private static int WrappingAbs(int value) =>
unchecked(value < 0 ? -value : value);
static bool INumberBase.IsCanonical(int2 value) => true;
static bool INumberBase.IsComplexNumber(int2 value) => false;
static bool INumberBase.IsImaginaryNumber(int2 value) => false;
/// Determines if every component of a value is zero.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase.IsZero(int2 value)
{
return value == Zero;
}
/// Determines if every component of a value is an even integral value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase.IsEvenInteger(int2 value)
{
return (value.X & 1) == 0 && (value.Y & 1) == 0;
}
/// Determines if every component of a value is an odd integral value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase.IsOddInteger(int2 value)
{
return (value.X & 1) == 1 && (value.Y & 1) == 1;
}
/// Determines if every component of a value is negative.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase.IsNegative(int2 value)
{
return value.X < 0 && value.Y < 0;
}
/// Determines if every component of a value is positive.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool INumberBase.IsPositive(int2 value)
{
return value.X >= 0 && value.Y >= 0;
}
static bool INumberBase.IsFinite(int2 value) => true;
static bool INumberBase.IsInfinity(int2 value) => false;
static bool INumberBase.IsNegativeInfinity(int2 value) => false;
static bool INumberBase.IsPositiveInfinity(int2 value) => false;
static bool INumberBase.IsInteger(int2 value) => true;
static bool INumberBase.IsRealNumber(int2 value) => true;
static bool INumberBase.IsNaN(int2 value) => false;
static bool INumberBase.IsNormal(int2 value) => value.X != 0 && value.Y != 0;
static bool INumberBase.IsSubnormal(int2 value) => false;
/// Computes, componentwise, the value whose magnitude is greater.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 MaxMagnitude(int2 a, int2 b)
{
return new int2(int.MaxMagnitude(a.X, b.X), int.MaxMagnitude(a.Y, b.Y));
}
/// Computes, componentwise, the value whose magnitude is greater.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 MaxMagnitudeNumber(int2 a, int2 b)
{
return new int2(int.MaxMagnitude(a.X, b.X), int.MaxMagnitude(a.Y, b.Y));
}
/// Computes, componentwise, the value whose magnitude is smaller.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 MinMagnitude(int2 a, int2 b)
{
return new int2(int.MinMagnitude(a.X, b.X), int.MinMagnitude(a.Y, b.Y));
}
/// Computes, componentwise, the value whose magnitude is smaller.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 MinMagnitudeNumber(int2 a, int2 b)
{
return new int2(int.MinMagnitude(a.X, b.X), int.MinMagnitude(a.Y, b.Y));
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Add(int2 a, int2 b)
{
return a + b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Divide(int2 a, int2 b)
{
return a / b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Multiply(int2 a, int2 b)
{
return a * b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Negate(int2 value)
{
return -value;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Subtract(int2 a, int2 b)
{
return a - b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator +(int2 a, int2 b)
{
return new(a.X + b.X, a.Y + b.Y);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator -(int2 a, int2 b)
{
return new(a.X - b.X, a.Y - b.Y);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator *(int2 a, int2 b)
{
return new(a.X * b.X, a.Y * b.Y);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator /(int2 a, int2 b)
{
return new(a.X / b.X, a.Y / b.Y);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator -(int2 a)
{
return new(-a.X, -a.Y);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator +(int2 a)
{
return a;
}
// ++/-- are well-defined (+/- One) but a footgun on a concrete vector: generic-plane only.
static int2 IIncrementOperators.operator ++(int2 value) => value + One;
static int2 IDecrementOperators.operator --(int2 value) => value - One;
static bool INumberBase.TryConvertFromChecked(TOther value, out int2 result)
{
return NumberConversion.TryConvertFrom(value, out result);
}
static bool INumberBase.TryConvertToChecked(int2 value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase.TryConvertFromSaturating(TOther value, out int2 result)
{
return NumberConversion.TryConvertFrom(value, out result);
}
static bool INumberBase.TryConvertToSaturating(int2 value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase.TryConvertFromTruncating(TOther value, out int2 result)
{
return NumberConversion.TryConvertFrom(value, out result);
}
static bool INumberBase.TryConvertToTruncating(int2 value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
}
// IMultiplyOperators, IDivisionOperators
public partial struct int2
: IMultiplyOperators
, IDivisionOperators
{
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Divide(int2 a, int b)
{
return a / b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Multiply(int2 a, int b)
{
return a * b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Multiply(int a, int2 b)
{
return a * b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator *(int2 a, int b)
{
return new(a.X * b, a.Y * b);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator /(int2 a, int b)
{
return new(a.X / b, a.Y / b);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator *(int a, int2 b)
{
return new(a * b.X, a * b.Y);
}
}
// INumber
public partial struct int2 : INumber
{
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Clamp(int2 value, int2 min, int2 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);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Max(int2 a, int2 b)
{
return new(int.Max(a.X, b.X), int.Max(a.Y, b.Y));
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 Min(int2 a, int2 b)
{
return new(int.Min(a.X, b.X), int.Min(a.Y, b.Y));
}
static int2 INumber.MaxNumber(int2 a, int2 b) => Max(a, b);
static int2 INumber.MinNumber(int2 a, int2 b) => Min(a, b);
/// Copies, componentwise, the sign of onto the magnitude of .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 CopySign(int2 value, int2 sign)
{
return new int2(int.CopySign(value.X, sign.X), int.CopySign(value.Y, sign.Y));
}
// the Native variants permit either NaN/signed-zero behavior - forwarding is always legal
static int2 INumber.ClampNative(int2 value, int2 min, int2 max) => Clamp(value, min, max);
static int2 INumber.MaxNative(int2 a, int2 b) => Max(a, b);
static int2 INumber.MinNative(int2 a, int2 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.Sign(int2 value) =>
throw new NotSupportedException($"{nameof(int2)} has no scalar sign.");
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int2 operator %(int2 a, int2 b)
{
return new int2(a.X % b.X, a.Y % b.Y);
}
int IComparable.CompareTo(object? obj) =>
throw new NotSupportedException($"{nameof(int2)} has no total order.");
int IComparable.CompareTo(int2 other) =>
throw new NotSupportedException($"{nameof(int2)} has no total order.");
static bool IComparisonOperators.operator <(int2 a, int2 b) =>
throw new NotSupportedException($"{nameof(int2)} has no total order.");
static bool IComparisonOperators.operator <=(int2 a, int2 b) =>
throw new NotSupportedException($"{nameof(int2)} has no total order.");
static bool IComparisonOperators.operator >(int2 a, int2 b) =>
throw new NotSupportedException($"{nameof(int2)} has no total order.");
static bool IComparisonOperators.operator >=(int2 a, int2 b) =>
throw new NotSupportedException($"{nameof(int2)} has no total order.");
}
// IComponentwiseComparison + the componentwise predicate masks (no interface to mirror)
public partial struct int2 : IComponentwiseComparison
{
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 operator <(int2 a, int2 b)
{
return new bool2(a.X < b.X, a.Y < b.Y);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 operator <=(int2 a, int2 b)
{
return new bool2(a.X <= b.X, a.Y <= b.Y);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 operator >(int2 a, int2 b)
{
return new bool2(a.X > b.X, a.Y > b.Y);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 operator >=(int2 a, int2 b)
{
return new bool2(a.X >= b.X, a.Y >= b.Y);
}
/// Determines which components of a value are zero.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 IsZero(int2 value)
{
return new bool2(value.X == 0, value.Y == 0);
}
/// Determines which components of a value are an even integral value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 IsEvenInteger(int2 value)
{
return new bool2(int.IsEvenInteger(value.X), int.IsEvenInteger(value.Y));
}
/// Determines which components of a value are an odd integral value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 IsOddInteger(int2 value)
{
return new bool2(int.IsOddInteger(value.X), int.IsOddInteger(value.Y));
}
/// Determines which components of a value are negative.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 IsNegative(int2 value)
{
return new bool2(int.IsNegative(value.X), int.IsNegative(value.Y));
}
/// Determines which components of a value are positive.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 IsPositive(int2 value)
{
return new bool2(int.IsPositive(value.X), int.IsPositive(value.Y));
}
}
// ISignedNumber
public partial struct int2 : ISignedNumber
{
///
public static int2 NegativeOne => new(-1);
}