// - edit Quat.numerics.tt to modify
#nullable enable
namespace Brutal.Numerics;
// INumberBase
public partial struct floatQuat : INumberBase
{
///
public static floatQuat Zero => default;
///
public static floatQuat Identity => new(0, 0, 0, 1);
/* One is the multiplicative identity - the identity quaternion - which the concrete API
* already names Identity; a public One would read as all-ones. */
static floatQuat INumberBase.One => Identity;
static floatQuat IAdditiveIdentity.AdditiveIdentity => Zero;
static floatQuat IMultiplicativeIdentity.MultiplicativeIdentity => Identity;
static int INumberBase.Radix => 2;
// Abs is the norm, embedded on the real axis as a scalar quaternion (cf. Complex.Abs)
static floatQuat INumberBase.Abs(floatQuat value) => new(0, 0, 0, value.Length());
static bool INumberBase.IsCanonical(floatQuat value) => true;
static bool INumberBase.IsComplexNumber(floatQuat value) =>
value.W != 0 && !(value.X == 0 && value.Y == 0 && value.Z == 0);
static bool INumberBase.IsImaginaryNumber(floatQuat value) =>
value.W == 0 && float.IsRealNumber(value.X) && float.IsRealNumber(value.Y) && float.IsRealNumber(value.Z);
static bool INumberBase.IsRealNumber(floatQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && float.IsRealNumber(value.W);
/// Determines if a value is zero.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsZero(floatQuat value)
{
return value == Zero;
}
static bool INumberBase.IsEvenInteger(floatQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && float.IsEvenInteger(value.W);
static bool INumberBase.IsOddInteger(floatQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && float.IsOddInteger(value.W);
static bool INumberBase.IsInteger(floatQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && float.IsInteger(value.W);
static bool INumberBase.IsNegative(floatQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && float.IsRealNumber(value.W) && float.IsNegative(value.W);
static bool INumberBase.IsPositive(floatQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && float.IsRealNumber(value.W) && float.IsPositive(value.W);
static bool INumberBase.IsFinite(floatQuat value) =>
float.IsFinite(value.X) && float.IsFinite(value.Y) && float.IsFinite(value.Z) && float.IsFinite(value.W);
static bool INumberBase.IsInfinity(floatQuat value) =>
float.IsInfinity(value.X) || float.IsInfinity(value.Y) || float.IsInfinity(value.Z) || float.IsInfinity(value.W);
static bool INumberBase.IsNegativeInfinity(floatQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && float.IsNegativeInfinity(value.W);
static bool INumberBase.IsPositiveInfinity(floatQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && float.IsPositiveInfinity(value.W);
// NaN present and no infinity - infinity dominates, per Complex
static bool INumberBase.IsNaN(floatQuat value) =>
(float.IsNaN(value.X) || float.IsNaN(value.Y) || float.IsNaN(value.Z) || float.IsNaN(value.W))
&& !(float.IsInfinity(value.X) || float.IsInfinity(value.Y) || float.IsInfinity(value.Z) || float.IsInfinity(value.W));
static bool INumberBase.IsNormal(floatQuat value) =>
float.IsNormal(value.W)
&& (value.X == 0 && value.Y == 0 && value.Z == 0 || (float.IsNormal(value.X) && float.IsNormal(value.Y) && float.IsNormal(value.Z)));
static bool INumberBase.IsSubnormal(floatQuat value) =>
float.IsSubnormal(value.X) || float.IsSubnormal(value.Y) || float.IsSubnormal(value.Z) || float.IsSubnormal(value.W);
static floatQuat INumberBase.MaxMagnitude(floatQuat a, floatQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (float.IsNaN(magnitudeA))
return a;
if (float.IsNaN(magnitudeB))
return b;
return magnitudeA >= magnitudeB ? a : b;
}
static floatQuat INumberBase.MaxMagnitudeNumber(floatQuat a, floatQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (float.IsNaN(magnitudeA))
return b;
if (float.IsNaN(magnitudeB))
return a;
return magnitudeA >= magnitudeB ? a : b;
}
static floatQuat INumberBase.MinMagnitude(floatQuat a, floatQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (float.IsNaN(magnitudeA))
return a;
if (float.IsNaN(magnitudeB))
return b;
return magnitudeA <= magnitudeB ? a : b;
}
static floatQuat INumberBase.MinMagnitudeNumber(floatQuat a, floatQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (float.IsNaN(magnitudeA))
return b;
if (float.IsNaN(magnitudeB))
return a;
return magnitudeA <= magnitudeB ? a : b;
}
/* ++/-- add/subtract One = Identity, bumping W - exactly what Complex does. Generic-plane
* only: a footgun on a concrete quat. */
static floatQuat IIncrementOperators.operator ++(floatQuat value) => value + Identity;
static floatQuat IDecrementOperators.operator --(floatQuat value) => value - Identity;
static bool INumberBase.TryConvertFromChecked(TOther value, out floatQuat result)
{
if (typeof(TOther) == typeof(floatQuat))
{
result = (floatQuat)(object)value!;
return true;
}
if (typeof(TOther) == typeof(doubleQuat))
{
var other = (doubleQuat)(object)value!;
result = new(float.CreateChecked(other.X), float.CreateChecked(other.Y), float.CreateChecked(other.Z), float.CreateChecked(other.W));
return true;
}
if (typeof(TOther) == typeof(sbyte))
{
result = new(0, 0, 0, float.CreateChecked((sbyte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(byte))
{
result = new(0, 0, 0, float.CreateChecked((byte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(short))
{
result = new(0, 0, 0, float.CreateChecked((short)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ushort))
{
result = new(0, 0, 0, float.CreateChecked((ushort)(object)value!));
return true;
}
if (typeof(TOther) == typeof(int))
{
result = new(0, 0, 0, float.CreateChecked((int)(object)value!));
return true;
}
if (typeof(TOther) == typeof(uint))
{
result = new(0, 0, 0, float.CreateChecked((uint)(object)value!));
return true;
}
if (typeof(TOther) == typeof(long))
{
result = new(0, 0, 0, float.CreateChecked((long)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ulong))
{
result = new(0, 0, 0, float.CreateChecked((ulong)(object)value!));
return true;
}
if (typeof(TOther) == typeof(float))
{
result = new(0, 0, 0, float.CreateChecked((float)(object)value!));
return true;
}
if (typeof(TOther) == typeof(double))
{
result = new(0, 0, 0, float.CreateChecked((double)(object)value!));
return true;
}
result = default;
return false;
}
static bool INumberBase.TryConvertToChecked(floatQuat value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase.TryConvertFromSaturating(TOther value, out floatQuat result)
{
if (typeof(TOther) == typeof(floatQuat))
{
result = (floatQuat)(object)value!;
return true;
}
if (typeof(TOther) == typeof(doubleQuat))
{
var other = (doubleQuat)(object)value!;
result = new(float.CreateSaturating(other.X), float.CreateSaturating(other.Y), float.CreateSaturating(other.Z), float.CreateSaturating(other.W));
return true;
}
if (typeof(TOther) == typeof(sbyte))
{
result = new(0, 0, 0, float.CreateSaturating((sbyte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(byte))
{
result = new(0, 0, 0, float.CreateSaturating((byte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(short))
{
result = new(0, 0, 0, float.CreateSaturating((short)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ushort))
{
result = new(0, 0, 0, float.CreateSaturating((ushort)(object)value!));
return true;
}
if (typeof(TOther) == typeof(int))
{
result = new(0, 0, 0, float.CreateSaturating((int)(object)value!));
return true;
}
if (typeof(TOther) == typeof(uint))
{
result = new(0, 0, 0, float.CreateSaturating((uint)(object)value!));
return true;
}
if (typeof(TOther) == typeof(long))
{
result = new(0, 0, 0, float.CreateSaturating((long)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ulong))
{
result = new(0, 0, 0, float.CreateSaturating((ulong)(object)value!));
return true;
}
if (typeof(TOther) == typeof(float))
{
result = new(0, 0, 0, float.CreateSaturating((float)(object)value!));
return true;
}
if (typeof(TOther) == typeof(double))
{
result = new(0, 0, 0, float.CreateSaturating((double)(object)value!));
return true;
}
result = default;
return false;
}
static bool INumberBase.TryConvertToSaturating(floatQuat value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase.TryConvertFromTruncating(TOther value, out floatQuat result)
{
if (typeof(TOther) == typeof(floatQuat))
{
result = (floatQuat)(object)value!;
return true;
}
if (typeof(TOther) == typeof(doubleQuat))
{
var other = (doubleQuat)(object)value!;
result = new(float.CreateTruncating(other.X), float.CreateTruncating(other.Y), float.CreateTruncating(other.Z), float.CreateTruncating(other.W));
return true;
}
if (typeof(TOther) == typeof(sbyte))
{
result = new(0, 0, 0, float.CreateTruncating((sbyte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(byte))
{
result = new(0, 0, 0, float.CreateTruncating((byte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(short))
{
result = new(0, 0, 0, float.CreateTruncating((short)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ushort))
{
result = new(0, 0, 0, float.CreateTruncating((ushort)(object)value!));
return true;
}
if (typeof(TOther) == typeof(int))
{
result = new(0, 0, 0, float.CreateTruncating((int)(object)value!));
return true;
}
if (typeof(TOther) == typeof(uint))
{
result = new(0, 0, 0, float.CreateTruncating((uint)(object)value!));
return true;
}
if (typeof(TOther) == typeof(long))
{
result = new(0, 0, 0, float.CreateTruncating((long)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ulong))
{
result = new(0, 0, 0, float.CreateTruncating((ulong)(object)value!));
return true;
}
if (typeof(TOther) == typeof(float))
{
result = new(0, 0, 0, float.CreateTruncating((float)(object)value!));
return true;
}
if (typeof(TOther) == typeof(double))
{
result = new(0, 0, 0, float.CreateTruncating((double)(object)value!));
return true;
}
result = default;
return false;
}
static bool INumberBase.TryConvertToTruncating(floatQuat value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat Add(floatQuat a, floatQuat b)
{
return a + b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat Divide(floatQuat a, floatQuat b)
{
return a / b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat Multiply(floatQuat a, floatQuat b)
{
return a * b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat Negate(floatQuat value)
{
return -value;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat Subtract(floatQuat a, floatQuat b)
{
return a - b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat operator +(floatQuat a, floatQuat b)
{
return Unsafe.BitCast, floatQuat>(Unsafe.BitCast>(a) + Unsafe.BitCast>(b));
}
///
public static floatQuat operator /(floatQuat a, floatQuat b)
{
floatQuat ans;
var ax = a.X;
var ay = a.Y;
var az = a.Z;
var aw = a.W;
//-------------------------------------
// Inverse part.
var ls = b.X * b.X + b.Y * b.Y + b.Z * b.Z + b.W * b.W;
var invNorm = 1 / ls;
var bx = -b.X * invNorm;
var by = -b.Y * invNorm;
var bz = -b.Z * invNorm;
var bw = b.W * invNorm;
//-------------------------------------
// Multiply part.
// cross(av, bv)
var cx = ay * bz - az * by;
var cy = az * bx - ax * bz;
var cz = ax * by - ay * bx;
var dot = ax * bx + ay * by + az * bz;
ans.X = ax * bw + bx * aw + cx;
ans.Y = ay * bw + by * aw + cy;
ans.Z = az * bw + bz * aw + cz;
ans.W = aw * bw - dot;
return ans;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat operator *(floatQuat a, floatQuat b)
{
var left = Unsafe.BitCast>(a);
var right = Unsafe.BitCast>(b);
var result = right * left.GetElement(3);
result = Vector128.MultiplyAddEstimate(Vector128.ShuffleNative(right, Vector128.Create(3, 2, 1, 0)) * left.GetElement(0), Vector128.Create(+1.0f, -1.0f, +1.0f, -1.0f), result);
result = Vector128.MultiplyAddEstimate(Vector128.ShuffleNative(right, Vector128.Create(2, 3, 0, 1)) * left.GetElement(1), Vector128.Create(+1.0f, +1.0f, -1.0f, -1.0f), result);
result = Vector128.MultiplyAddEstimate(Vector128.ShuffleNative(right, Vector128.Create(1, 0, 3, 2)) * left.GetElement(2), Vector128.Create(-1.0f, +1.0f, +1.0f, -1.0f), result);
return Unsafe.BitCast, floatQuat>(result);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat operator -(floatQuat a, floatQuat b)
{
return Unsafe.BitCast, floatQuat>(Unsafe.BitCast>(a) - Unsafe.BitCast>(b));
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat operator -(floatQuat value)
{
return Zero - value;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat operator +(floatQuat value)
{
return value;
}
}
// IMultiplyOperators, IDivisionOperators
public partial struct floatQuat
: IMultiplyOperators
, IDivisionOperators
{
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat Multiply(floatQuat a, float b)
{
return a * b;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat operator *(floatQuat a, float b)
{
return Unsafe.BitCast, floatQuat>(Unsafe.BitCast>(a) * Vector128.Create(b));
}
/// Divides a quaternion by a scalar, componentwise.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static floatQuat operator /(floatQuat a, float b)
{
return Unsafe.BitCast, floatQuat>(Unsafe.BitCast>(a) / Vector128.Create(b));
}
}