1#if NETCOREAPP3_0_OR_GREATER
5using System.Runtime.InteropServices;
6using System.Runtime.Intrinsics;
14 static class Vector128Util
23 [MethodImpl(MethodImplOptions.AggressiveInlining)]
24 public static void CopyTo<T>(
this Vector128<T> vector, Span<T> destination) where T :
struct
27 Vector128.CopyTo(vector, destination);
29 if (destination.Length < Vector128<T>.Count)
30 throw new ArgumentException(
null, nameof(destination));
32 Unsafe.WriteUnaligned(
ref Unsafe.As<T,
byte>(
ref MemoryMarshal.GetReference(destination)), vector);
42 [MethodImpl(MethodImplOptions.AggressiveInlining)]
43 public static Vector128<T> Create<T>(ReadOnlySpan<T> values) where T :
struct
46 return Vector128.Create(values);
48 if (values.Length < Vector128<T>.Count)
49 throw new ArgumentOutOfRangeException(nameof(values));
51 return Unsafe.ReadUnaligned<Vector128<T>>(
ref Unsafe.As<T,
byte>(
ref MemoryMarshal.GetReference(values)));
61 [MethodImpl(MethodImplOptions.AggressiveInlining)]
62 public static (Vector128<T> V1, Vector128<T> V2) Load2xUnsafe<T>(
ref readonly T source) where T :
struct
65 LoadUnsafe(in source, 0 * (nuint)Vector128<T>.Count),
66 LoadUnsafe(in source, 1 * (nuint)Vector128<T>.Count)
76 [MethodImpl(MethodImplOptions.AggressiveInlining)]
77 public static (Vector128<T> V1, Vector128<T> V2, Vector128<T> V3) Load3xUnsafe<T>(
ref readonly T source) where T :
struct
80 LoadUnsafe(in source, 0 * (nuint)Vector128<T>.Count),
81 LoadUnsafe(in source, 1 * (nuint)Vector128<T>.Count),
82 LoadUnsafe(in source, 2 * (nuint)Vector128<T>.Count)
92 [MethodImpl(MethodImplOptions.AggressiveInlining)]
93 public static (Vector128<T> V1, Vector128<T> V2, Vector128<T> V3, Vector128<T> V4) Load4xUnsafe<T>(
ref readonly T source) where T :
struct
96 LoadUnsafe(in source, 0 * (nuint)Vector128<T>.Count),
97 LoadUnsafe(in source, 1 * (nuint)Vector128<T>.Count),
98 LoadUnsafe(in source, 2 * (nuint)Vector128<T>.Count),
99 LoadUnsafe(in source, 3 * (nuint)Vector128<T>.Count)
109 [MethodImpl(MethodImplOptions.AggressiveInlining)]
110 public static Vector128<T> LoadUnsafe<T>(
ref readonly T source) where T :
struct
113 return Vector128.LoadUnsafe(in source);
117 ref byte address =
ref Unsafe.As<T,
byte>(
ref Unsafe.AsRef(in source));
118 return Unsafe.ReadUnaligned<Vector128<T>>(
ref address);
129 [MethodImpl(MethodImplOptions.AggressiveInlining)]
130 public static Vector128<T> LoadUnsafe<T>(
ref readonly T source, nuint elementOffset) where T :
struct
133 return Vector128.LoadUnsafe(in source, elementOffset);
137 ref byte address =
ref Unsafe.As<T,
byte>(
ref Unsafe.Add(
ref Unsafe.AsRef(in source), (nint)elementOffset));
138 return Unsafe.ReadUnaligned<Vector128<T>>(
ref address);
148 [MethodImpl(MethodImplOptions.AggressiveInlining)]
149 public static void StoreUnsafe<T>(
this Vector128<T> source,
ref T destination) where T :
struct
152 Vector128.StoreUnsafe(source,
ref destination);
156 ref byte address =
ref Unsafe.As<T,
byte>(
ref destination);
157 Unsafe.WriteUnaligned(
ref address, source);
168 [MethodImpl(MethodImplOptions.AggressiveInlining)]
169 public static void StoreUnsafe<T>(
this Vector128<T> source,
ref T destination, nuint elementOffset) where T :
struct
172 Vector128.StoreUnsafe(source,
ref destination, elementOffset);
176 destination =
ref Unsafe.Add(
ref destination, (nint)elementOffset);
177 Unsafe.WriteUnaligned(
ref Unsafe.As<T,
byte>(
ref destination), source);