// ----------------------------------------------------------------------------- // file="Vec4" // Project: StgSharp // Copyright (c) Nitload. // SPDX-License-Identifier: MIT // ----------------------------------------------------------------------------- using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; namespace Nitload.Mathematics.Numeric.Graphics { public static class Vec4 { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vec4 ConvertToDouble( Vec4 source ) { return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Unsafe())); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vec4 ConvertToDouble( Vec4 source ) { return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Unsafe())); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vec4 ConvertToSingle( Vec4 source ) { return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Unsafe())); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vec4 ConvertToSingle( Vec4 source ) { return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Unsafe())); } public static Vec4 FromSpan( ReadOnlySpan span ) where T : unmanaged, INumber { if (span.Length < 4) { throw new ArgumentException("Span length must be at least 4.", nameof(span)); } Unsafe.SkipInit(out Vec4 result); Span target = MemoryMarshal.CreateSpan(ref Unsafe.As, T>(ref result), 4); span[..4].CopyTo(target); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vector128 AsVector128Unsafe( this Vec4 source ) where T : unmanaged, INumber { Unsafe.SkipInit(out Vector128 result); Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vector256 AsVector256Unsafe( this Vec4 source ) where T : unmanaged, INumber { Unsafe.SkipInit(out Vector256 result); Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vec4 FromVector128Unsafe( Vector128 source ) where T : unmanaged, INumber { return Unsafe.As, Vec4>(ref source); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Vec4 FromVector256Unsafe( Vector256 source ) where T : unmanaged, INumber { return Unsafe.As, Vec4>(ref source); } } }