1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
7 using System.Runtime.CompilerServices;
8 using System.Runtime.InteropServices;
9 using System.Runtime.Intrinsics.X86;
10 using System.Runtime.Intrinsics;
12 namespace IntelHardwareIntrinsicTest
14 // that it is intentionally designed to be a struct type that meets
15 // the generic constraint but is not supported by any intrinsics
26 static unsafe int Main(string[] args)
28 int testResult = Pass;
32 using (TestTable<float, int> floatTable = new TestTable<float, int>(new float[8] { 1, float.NaN, float.PositiveInfinity, float.NegativeInfinity, float.NegativeInfinity, float.PositiveInfinity, float.NaN, 1 }, new int[8]))
34 var vf1 = Unsafe.Read<Vector256<float>>(floatTable.inArrayPtr);
35 var vf2 = Avx.StaticCast<float, int>(vf1);
36 Unsafe.Write(floatTable.outArrayPtr, vf2);
38 if (!floatTable.CheckResult((x, y) => BitConverter.SingleToInt32Bits(x) == y))
40 Console.WriteLine("Avx StaticCast failed on float:");
41 foreach (var item in floatTable.outArray)
43 Console.Write(item + ", ");
49 // the successful path is the one that catches the exception, so that does nothing,
50 // it is the fall-through path that's the error path
53 var v = Avx.StaticCast<float, Num>(vf1);
54 Unsafe.Write(floatTable.outArrayPtr, v);
55 Console.WriteLine("Avx StaticCast failed on target type test:");
58 catch (System.NotSupportedException)
62 // the successful path is the one that catches the exception, so that does nothing,
63 // it is the fall-through path that's the error path
66 var v = TestSrcType();
67 Unsafe.Write(floatTable.outArrayPtr, v);
68 Console.WriteLine("Avx StaticCast failed on source type test:");
71 catch (System.NotSupportedException)
80 [MethodImpl(MethodImplOptions.NoInlining)]
81 static Vector256<int> TestSrcType()
83 Vector256<Num> v1 = new Vector256<Num>();
84 Vector256<Num> v2 = new Vector256<Num>();
85 return Avx2.Add(Avx.StaticCast<Num, int>(v1), Avx.StaticCast<Num, int>(v2));
88 public unsafe struct TestTable<T, U> : IDisposable where T : struct where U : struct
93 public void* inArrayPtr => inHandle.AddrOfPinnedObject().ToPointer();
94 public void* outArrayPtr => outHandle.AddrOfPinnedObject().ToPointer();
99 public TestTable(T[] a, U[] b)
104 inHandle = GCHandle.Alloc(inArray, GCHandleType.Pinned);
105 outHandle = GCHandle.Alloc(outArray, GCHandleType.Pinned);
108 public bool CheckResult(Func<T, U, bool> check)
110 for (int i = 0; i < inArray.Length; i++)
112 if (!check(inArray[i], outArray[i]))
120 public void Dispose()