// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. // using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics.X86; using System.Runtime.Intrinsics; namespace IntelHardwareIntrinsicTest { class Program { const int Pass = 100; const int Fail = 0; static unsafe int Main(string[] args) { int testResult = Pass; if (Avx2.IsSupported) { using (TestTable intTable = new TestTable(new int[8] { 1, -5, 100, 0, 1, 2, 3, 4 }, new int[8])) { var vf = Avx2.BroadcastVector128ToVector256((int*)(intTable.inArrayPtr)); Unsafe.Write(intTable.outArrayPtr, vf); if (!intTable.CheckResult((x, y) => x == y)) { Console.WriteLine("Avx2 BroadcastVector128ToVector256 failed on int:"); foreach (var item in intTable.outArray) { Console.Write(item + ", "); } Console.WriteLine(); testResult = Fail; } } using (TestTable uintTable = new TestTable(new uint[8] { 1, 5, 100, 0, 1, 2, 3, 4 }, new uint[8])) { var vf = Avx2.BroadcastVector128ToVector256((uint*)(uintTable.inArrayPtr)); Unsafe.Write(uintTable.outArrayPtr, vf); if (!uintTable.CheckResult((x, y) => x == y)) { Console.WriteLine("Avx2 BroadcastVector128ToVector256 failed on uint:"); foreach (var item in uintTable.outArray) { Console.Write(item + ", "); } Console.WriteLine(); testResult = Fail; } } using (TestTable longTable = new TestTable(new long[4] { 1, -5, 100, 0}, new long[4])) { var vf = Avx2.BroadcastVector128ToVector256((long*)(longTable.inArrayPtr)); Unsafe.Write(longTable.outArrayPtr, vf); if (!longTable.CheckResult((x, y) => x == y)) { Console.WriteLine("Avx2 BroadcastVector128ToVector256 failed on long:"); foreach (var item in longTable.outArray) { Console.Write(item + ", "); } Console.WriteLine(); testResult = Fail; } } using (TestTable ulongTable = new TestTable(new ulong[4] { 1, 5, 100, 0}, new ulong[4])) { var vf = Avx2.BroadcastVector128ToVector256((ulong*)(ulongTable.inArrayPtr)); Unsafe.Write(ulongTable.outArrayPtr, vf); if (!ulongTable.CheckResult((x, y) => x == y)) { Console.WriteLine("Avx2 BroadcastVector128ToVector256 failed on ulong:"); foreach (var item in ulongTable.outArray) { Console.Write(item + ", "); } Console.WriteLine(); testResult = Fail; } } } return testResult; } public unsafe struct TestTable : IDisposable where T : struct { public T[] inArray; public T[] outArray; public void* inArrayPtr => inHandle.AddrOfPinnedObject().ToPointer(); public void* outArrayPtr => outHandle.AddrOfPinnedObject().ToPointer(); GCHandle inHandle; GCHandle outHandle; public TestTable(T[] a, T[] b) { this.inArray = a; this.outArray = b; inHandle = GCHandle.Alloc(inArray, GCHandleType.Pinned); outHandle = GCHandle.Alloc(outArray, GCHandleType.Pinned); } public bool CheckResult(Func check) { for (int i = 0; i < outArray.Length/2; i++) { if (!check(inArray[i], outArray[i])) { return false; } } for (int i = outArray.Length/2; i < outArray.Length; i++) { if (!check(inArray[i - outArray.Length/2], outArray[i])) { return false; } } return true; } public void Dispose() { inHandle.Free(); outHandle.Free(); } } } }