|
| 1 | +using System.Numerics; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using System.Runtime.Intrinsics; |
| 4 | + |
| 5 | +namespace SmartVectorDotNet; |
| 6 | + |
| 7 | +public unsafe partial class InternalHelpersTest |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void CreateVector_Span() |
| 11 | + { |
| 12 | + var elements = Enumerable |
| 13 | + .Range(1, Vector<int>.Count) |
| 14 | + .ToArray(); |
| 15 | + var vector = InternalHelpers.CreateVector((Span<int>)elements); |
| 16 | + for(int i = 0; i < Vector<int>.Count; i++) |
| 17 | + { |
| 18 | + Assert.Equal(elements[i], vector[i]); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void CreateVector_ROSpan() |
| 24 | + { |
| 25 | + var elements = Enumerable |
| 26 | + .Range(1, Vector<int>.Count) |
| 27 | + .ToArray(); |
| 28 | + var vector = InternalHelpers.CreateVector((ReadOnlySpan<int>)elements); |
| 29 | + for (int i = 0; i < Vector<int>.Count; i++) |
| 30 | + { |
| 31 | + Assert.Equal(elements[i], vector[i]); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void CreateVector128_Span() |
| 37 | + { |
| 38 | + var elements = Enumerable |
| 39 | + .Range(1, Vector128<int>.Count) |
| 40 | + .ToArray(); |
| 41 | + var vector = InternalHelpers.CreateVector128((Span<int>)elements); |
| 42 | + for (int i = 0; i < Vector128<int>.Count; i++) |
| 43 | + { |
| 44 | + Assert.Equal(elements[i], vector[i]); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void CreateVector128_ROSpan() |
| 50 | + { |
| 51 | + var elements = Enumerable |
| 52 | + .Range(1, Vector128<int>.Count) |
| 53 | + .ToArray(); |
| 54 | + var vector = InternalHelpers.CreateVector128((ReadOnlySpan<int>)elements); |
| 55 | + for (int i = 0; i < Vector128<int>.Count; i++) |
| 56 | + { |
| 57 | + Assert.Equal(elements[i], vector[i]); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void CreateVector256_Span() |
| 63 | + { |
| 64 | + var elements = Enumerable |
| 65 | + .Range(1, Vector256<int>.Count) |
| 66 | + .ToArray(); |
| 67 | + var vector = InternalHelpers.CreateVector256((Span<int>)elements); |
| 68 | + for (int i = 0; i < Vector256<int>.Count; i++) |
| 69 | + { |
| 70 | + Assert.Equal(elements[i], vector[i]); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void CreateVector256_ROSpan() |
| 76 | + { |
| 77 | + var elements = Enumerable |
| 78 | + .Range(1, Vector256<int>.Count) |
| 79 | + .ToArray(); |
| 80 | + var vector = InternalHelpers.CreateVector((ReadOnlySpan<int>)elements); |
| 81 | + for (int i = 0; i < Vector256<int>.Count; i++) |
| 82 | + { |
| 83 | + Assert.Equal(elements[i], vector[i]); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void Reinterpret_Single() |
| 89 | + { |
| 90 | + int x = 42; |
| 91 | + ref readonly float y = ref InternalHelpers.Reinterpret<int, float>(in x); |
| 92 | + Assert.Equal(Unsafe.BitCast<int, float>(x), y); |
| 93 | + var ptrX = (nint)Unsafe.AsPointer(ref x); |
| 94 | + var ptrY = (nint)Unsafe.AsPointer(ref Unsafe.AsRef(in y)); |
| 95 | + Assert.Equal(ptrX, ptrY); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void ReinterpretMutable_Single() |
| 100 | + { |
| 101 | + int x = 42; |
| 102 | + ref float y = ref InternalHelpers.ReinterpretMutable<int, float>(ref x); |
| 103 | + Assert.Equal(Unsafe.BitCast<int, float>(x), y); |
| 104 | + var ptrX = (nint)Unsafe.AsPointer(ref x); |
| 105 | + var ptrY = (nint)Unsafe.AsPointer(ref y); |
| 106 | + Assert.Equal(ptrX, ptrY); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public void Reinterpret_Vector() |
| 111 | + { |
| 112 | + Vector<int> x = new(42); |
| 113 | + ref readonly Vector<float> y = ref InternalHelpers.Reinterpret<int, float>(in x); |
| 114 | + Assert.Equal(Vector.AsVectorSingle(x), y); |
| 115 | + var ptrX = (nint)Unsafe.AsPointer(ref x); |
| 116 | + var ptrY = (nint)Unsafe.AsPointer(ref Unsafe.AsRef(in y)); |
| 117 | + Assert.Equal(ptrX, ptrY); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void ReinterpretMutable_Vector() |
| 122 | + { |
| 123 | + Vector<int> x = new(42); |
| 124 | + ref Vector<float> y = ref InternalHelpers.ReinterpretMutable<int, float>(ref x); |
| 125 | + Assert.Equal(Vector.AsVectorSingle(x), y); |
| 126 | + var ptrX = new nint(Unsafe.AsPointer(ref x)); |
| 127 | + var ptrY = new nint(Unsafe.AsPointer(ref y)); |
| 128 | + Assert.Equal(ptrX, ptrY); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void Reinterpret_Vector256() |
| 133 | + { |
| 134 | + Vector256<int> x = Vector256.Create(42); |
| 135 | + ref readonly Vector256<float> y = ref InternalHelpers.Reinterpret<int, float>(in x); |
| 136 | + Assert.Equal(Vector256.AsSingle(x), y); |
| 137 | + var ptrX = (nint)Unsafe.AsPointer(ref x); |
| 138 | + var ptrY = (nint)Unsafe.AsPointer(ref Unsafe.AsRef(in y)); |
| 139 | + Assert.Equal(ptrX, ptrY); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void Reinterpret_Vector128() |
| 144 | + { |
| 145 | + Vector128<int> x = Vector128.Create(42); |
| 146 | + ref readonly Vector128<float> y = ref InternalHelpers.Reinterpret<int, float>(in x); |
| 147 | + Assert.Equal(Vector128.AsSingle(x), y); |
| 148 | + var ptrX = (nint)Unsafe.AsPointer(ref x); |
| 149 | + var ptrY = (nint)Unsafe.AsPointer(ref Unsafe.AsRef(in y)); |
| 150 | + Assert.Equal(ptrX, ptrY); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void ReinterpretVArray() |
| 155 | + { |
| 156 | + Vector<int>[] x = [ |
| 157 | + .. Enumerable |
| 158 | + .Range(1, 16) |
| 159 | + .Select(i => new Vector<int>(i)), |
| 160 | + ]; |
| 161 | + core<int>(x); |
| 162 | + |
| 163 | + static void core<T>(Vector<int>[] x) |
| 164 | + where T : unmanaged |
| 165 | + { |
| 166 | + ref readonly Vector<T>[] y = ref InternalHelpers.ReinterpretVArray<int, T>(in x); |
| 167 | + for(var i = 0; i < x.Length; i++) |
| 168 | + { |
| 169 | + Assert.Equal(Unsafe.As<Vector<int>, Vector<T>>(ref x[i]), y[i]); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + [Fact] |
| 175 | + public void AsUnsigned08U() |
| 176 | + { |
| 177 | + Vector<byte> x = new (byte.MaxValue); |
| 178 | + ref readonly Vector<byte> y = ref InternalHelpers.AsUnsigned(in x); |
| 179 | + Assert.Equal(x[0], y[0]); |
| 180 | + } |
| 181 | + |
| 182 | + [Fact] |
| 183 | + public void AsUnsigned16U() |
| 184 | + { |
| 185 | + Vector<ushort> x = new(ushort.MaxValue); |
| 186 | + ref readonly Vector<ushort> y = ref InternalHelpers.AsUnsigned(in x); |
| 187 | + Assert.Equal(x[0], y[0]); |
| 188 | + } |
| 189 | + |
| 190 | + [Fact] |
| 191 | + public void AsUnsigned32U() |
| 192 | + { |
| 193 | + Vector<uint> x = new(uint.MaxValue); |
| 194 | + ref readonly Vector<uint> y = ref InternalHelpers.AsUnsigned(in x); |
| 195 | + Assert.Equal(x[0], y[0]); |
| 196 | + } |
| 197 | + |
| 198 | + [Fact] |
| 199 | + public void AsUnsigned64U() |
| 200 | + { |
| 201 | + Vector<ulong> x = new(ulong.MaxValue); |
| 202 | + ref readonly Vector<ulong> y = ref InternalHelpers.AsUnsigned(in x); |
| 203 | + Assert.Equal(x[0], y[0]); |
| 204 | + } |
| 205 | + |
| 206 | + [Fact] |
| 207 | + public void AsUnsignedNU() |
| 208 | + { |
| 209 | + Vector<nuint> x = new(nuint.MaxValue); |
| 210 | + ref readonly Vector<nuint> y = ref InternalHelpers.AsUnsigned(in x); |
| 211 | + Assert.Equal(x[0], y[0]); |
| 212 | + } |
| 213 | + |
| 214 | + [Fact] |
| 215 | + public void AsUnsigned08I() |
| 216 | + { |
| 217 | + Vector<sbyte> x = new(sbyte.MaxValue); |
| 218 | + ref readonly Vector<byte> y = ref InternalHelpers.AsUnsigned(in x); |
| 219 | + Assert.Equal((byte)x[0], y[0]); |
| 220 | + } |
| 221 | + |
| 222 | + [Fact] |
| 223 | + public void AsUnsigned16I() |
| 224 | + { |
| 225 | + Vector<short> x = new(short.MaxValue); |
| 226 | + ref readonly Vector<ushort> y = ref InternalHelpers.AsUnsigned(in x); |
| 227 | + Assert.Equal((ushort)x[0], y[0]); |
| 228 | + } |
| 229 | + |
| 230 | + [Fact] |
| 231 | + public void AsUnsigned32I() |
| 232 | + { |
| 233 | + Vector<int> x = new(int.MaxValue); |
| 234 | + ref readonly Vector<uint> y = ref InternalHelpers.AsUnsigned(in x); |
| 235 | + Assert.Equal((uint)x[0], y[0]); |
| 236 | + } |
| 237 | + |
| 238 | + [Fact] |
| 239 | + public void AsUnsigned64I() |
| 240 | + { |
| 241 | + Vector<long> x = new(long.MaxValue); |
| 242 | + ref readonly Vector<ulong> y = ref InternalHelpers.AsUnsigned(in x); |
| 243 | + Assert.Equal((ulong)x[0], y[0]); |
| 244 | + } |
| 245 | + |
| 246 | + [Fact] |
| 247 | + public void AsUnsignedNI() |
| 248 | + { |
| 249 | + Vector<nint> x = new(nint.MaxValue); |
| 250 | + ref readonly Vector<nuint> y = ref InternalHelpers.AsUnsigned(in x); |
| 251 | + Assert.Equal((nuint)x[0], y[0]); |
| 252 | + } |
| 253 | + |
| 254 | + [Fact] |
| 255 | + public void AsSigned08U() |
| 256 | + { |
| 257 | + Vector<byte> x = new(byte.MaxValue); |
| 258 | + ref readonly Vector<sbyte> y = ref InternalHelpers.AsSigned(in x); |
| 259 | + Assert.Equal((sbyte)x[0], y[0]); |
| 260 | + } |
| 261 | + |
| 262 | + [Fact] |
| 263 | + public void AsSigned16U() |
| 264 | + { |
| 265 | + Vector<ushort> x = new(ushort.MaxValue); |
| 266 | + ref readonly Vector<short> y = ref InternalHelpers.AsSigned(in x); |
| 267 | + Assert.Equal((short)x[0], y[0]); |
| 268 | + } |
| 269 | + |
| 270 | + [Fact] |
| 271 | + public void AsSigned32U() |
| 272 | + { |
| 273 | + Vector<uint> x = new(uint.MaxValue); |
| 274 | + ref readonly Vector<int> y = ref InternalHelpers.AsSigned(in x); |
| 275 | + Assert.Equal((int)x[0], y[0]); |
| 276 | + } |
| 277 | + |
| 278 | + [Fact] |
| 279 | + public void AsSigned64U() |
| 280 | + { |
| 281 | + Vector<ulong> x = new(ulong.MaxValue); |
| 282 | + ref readonly Vector<long> y = ref InternalHelpers.AsSigned(in x); |
| 283 | + Assert.Equal((long)x[0], y[0]); |
| 284 | + } |
| 285 | + |
| 286 | + [Fact] |
| 287 | + public void AsSignedNU() |
| 288 | + { |
| 289 | + Vector<nuint> x = new(nuint.MaxValue); |
| 290 | + ref readonly Vector<nint> y = ref InternalHelpers.AsSigned(in x); |
| 291 | + Assert.Equal((nint)x[0], y[0]); |
| 292 | + } |
| 293 | + |
| 294 | + [Fact] |
| 295 | + public void AsSigned08I() |
| 296 | + { |
| 297 | + Vector<sbyte> x = new(sbyte.MaxValue); |
| 298 | + ref readonly Vector<sbyte> y = ref InternalHelpers.AsSigned(in x); |
| 299 | + Assert.Equal(x[0], y[0]); |
| 300 | + } |
| 301 | + |
| 302 | + [Fact] |
| 303 | + public void AsSigned16I() |
| 304 | + { |
| 305 | + Vector<short> x = new(short.MaxValue); |
| 306 | + ref readonly Vector<short> y = ref InternalHelpers.AsSigned(in x); |
| 307 | + Assert.Equal(x[0], y[0]); |
| 308 | + } |
| 309 | + |
| 310 | + [Fact] |
| 311 | + public void AsSigned32I() |
| 312 | + { |
| 313 | + Vector<int> x = new(int.MaxValue); |
| 314 | + ref readonly Vector<int> y = ref InternalHelpers.AsSigned(in x); |
| 315 | + Assert.Equal(x[0], y[0]); |
| 316 | + } |
| 317 | + |
| 318 | + [Fact] |
| 319 | + public void AsSigned64I() |
| 320 | + { |
| 321 | + Vector<long> x = new(long.MaxValue); |
| 322 | + ref readonly Vector<long> y = ref InternalHelpers.AsSigned(in x); |
| 323 | + Assert.Equal(x[0], y[0]); |
| 324 | + } |
| 325 | + |
| 326 | + [Fact] |
| 327 | + public void AsSignedNI() |
| 328 | + { |
| 329 | + Vector<nint> x = new(nint.MaxValue); |
| 330 | + ref readonly Vector<nint> y = ref InternalHelpers.AsSigned(in x); |
| 331 | + Assert.Equal(x[0], y[0]); |
| 332 | + } |
| 333 | +} |
0 commit comments