namespace Brutal.Numerics; /* One shared body for the vectors' INumberBase TryConvertFrom* protocol - the per-type explicit * implementations are one-line forwards into TryConvertFrom, with the overflow policy carried as * a generic marker struct. Deduplicated for assembly size: the typeof dispatch below exists once * instead of once per vector type x policy (it was ~27% of the library's IL as generated code), * and still JIT-folds to the single live branch per instantiation - the BCL's own pattern. * * Semantics (also documented in Vector.numerics.tt): scalars embed along One - a broadcast; a * sibling vector converts per lane when the arities match (the lane-count check inside FromLanes * rejects a cross-arity pair). Quaternions embed scalars on the real axis instead, so they keep * their own generated protocol. */ internal static class NumberConversion { /// The overflow policy of one TryConvertFrom flavor, as a JIT-foldable marker struct. internal interface IPolicy { static abstract TComponent Convert(TOther value) where TComponent : INumberBase where TOther : INumberBase; } internal readonly struct Checked : IPolicy { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TComponent Convert(TOther value) where TComponent : INumberBase where TOther : INumberBase { return TComponent.CreateChecked(value); } } internal readonly struct Saturating : IPolicy { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TComponent Convert(TOther value) where TComponent : INumberBase where TOther : INumberBase { return TComponent.CreateSaturating(value); } } internal readonly struct Truncating : IPolicy { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TComponent Convert(TOther value) where TComponent : INumberBase where TOther : INumberBase { return TComponent.CreateTruncating(value); } } // the supported-set dispatch: self, then the 8 scalar component types, then every non-bool // vector type (cross-arity pairs fall out via the lane-count check) internal static bool TryConvertFrom(TOther value, out TVector result) where TVector : unmanaged, IVector where TComponent : unmanaged, INumberBase where TOther : INumberBase where TPolicy : IPolicy { if (typeof(TOther) == typeof(TVector)) { result = (TVector)(object)value!; return true; } if (typeof(TOther) == typeof(sbyte)) return Broadcast((sbyte)(object)value!, out result); if (typeof(TOther) == typeof(byte)) return Broadcast((byte)(object)value!, out result); if (typeof(TOther) == typeof(short)) return Broadcast((short)(object)value!, out result); if (typeof(TOther) == typeof(ushort)) return Broadcast((ushort)(object)value!, out result); if (typeof(TOther) == typeof(int)) return Broadcast((int)(object)value!, out result); if (typeof(TOther) == typeof(uint)) return Broadcast((uint)(object)value!, out result); if (typeof(TOther) == typeof(long)) return Broadcast((long)(object)value!, out result); if (typeof(TOther) == typeof(ulong)) return Broadcast((ulong)(object)value!, out result); if (typeof(TOther) == typeof(float)) return Broadcast((float)(object)value!, out result); if (typeof(TOther) == typeof(double)) return Broadcast((double)(object)value!, out result); if (typeof(TOther) == typeof(sbyte2)) { var other = (sbyte2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(sbyte3)) { var other = (sbyte3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(sbyte4)) { var other = (sbyte4)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(byte2)) { var other = (byte2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(byte3)) { var other = (byte3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(byte4)) { var other = (byte4)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(short2)) { var other = (short2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(short3)) { var other = (short3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(short4)) { var other = (short4)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(ushort2)) { var other = (ushort2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(ushort3)) { var other = (ushort3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(ushort4)) { var other = (ushort4)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(int2)) { var other = (int2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(int3)) { var other = (int3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(int4)) { var other = (int4)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(uint2)) { var other = (uint2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(uint3)) { var other = (uint3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(uint4)) { var other = (uint4)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(long2)) { var other = (long2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(long3)) { var other = (long3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(long4)) { var other = (long4)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(ulong2)) { var other = (ulong2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(ulong3)) { var other = (ulong3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(ulong4)) { var other = (ulong4)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(float2)) { var other = (float2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(float3)) { var other = (float3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(float4)) { var other = (float4)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(double2)) { var other = (double2)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(double3)) { var other = (double3)(object)value!; return FromLanes(other.AsSpan(), out result); } if (typeof(TOther) == typeof(double4)) { var other = (double4)(object)value!; return FromLanes(other.AsSpan(), out result); } result = default; return false; } private static bool Broadcast(TSource value, out TVector result) where TVector : unmanaged, IVector where TComponent : unmanaged, INumberBase where TSource : INumberBase where TPolicy : IPolicy { var converted = TPolicy.Convert(value); Span lanes = stackalloc TComponent[TVector.ComponentCount]; lanes.Fill(converted); result = TVector.Create(lanes); return true; } private static bool FromLanes( ReadOnlySpan source, out TVector result ) where TVector : unmanaged, IVector where TComponent : unmanaged, INumberBase where TSource : INumberBase where TPolicy : IPolicy { if (source.Length != TVector.ComponentCount) { result = default; return false; } Span lanes = stackalloc TComponent[source.Length]; for (var i = 0; i < source.Length; i++) lanes[i] = TPolicy.Convert(source[i]); result = TVector.Create(lanes); return true; } }