|
| 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. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using System.Runtime.Intrinsics.X86; |
| 10 | +using System.Runtime.Intrinsics; |
| 11 | + |
| 12 | +namespace IntelHardwareIntrinsicTest |
| 13 | +{ |
| 14 | + class Program |
| 15 | + { |
| 16 | + const int Pass = 100; |
| 17 | + const int Fail = 0; |
| 18 | + |
| 19 | + static unsafe int Main(string[] args) |
| 20 | + { |
| 21 | + int testResult = Pass; |
| 22 | + |
| 23 | + if (Avx.IsSupported) |
| 24 | + { |
| 25 | + using (TestTable<float> floatTable = new TestTable<float>(new float[8] { 1, -5, 100, 0, 1, 2, 3, 4 }, new float[8])) |
| 26 | + { |
| 27 | + var vf = Avx.BroadcastVector128ToVector256((float*)(floatTable.inArrayPtr)); |
| 28 | + Unsafe.Write(floatTable.outArrayPtr, vf); |
| 29 | + |
| 30 | + if (!floatTable.CheckResult((x, y) => BitConverter.SingleToInt32Bits(x) == BitConverter.SingleToInt32Bits(y))) |
| 31 | + { |
| 32 | + Console.WriteLine("AVX BroadcastVector128ToVector256 failed on float:"); |
| 33 | + foreach (var item in floatTable.outArray) |
| 34 | + { |
| 35 | + Console.Write(item + ", "); |
| 36 | + } |
| 37 | + Console.WriteLine(); |
| 38 | + testResult = Fail; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + using (TestTable<double> doubleTable = new TestTable<double>(new double[4] { 1, -5, 100, 0}, new double[4])) |
| 43 | + { |
| 44 | + var vf = Avx.BroadcastVector128ToVector256((double*)(doubleTable.inArrayPtr)); |
| 45 | + Unsafe.Write(doubleTable.outArrayPtr, vf); |
| 46 | + |
| 47 | + if (!doubleTable.CheckResult((x, y) => BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(y))) |
| 48 | + { |
| 49 | + Console.WriteLine("AVX BroadcastVector128ToVector256 failed on double:"); |
| 50 | + foreach (var item in doubleTable.outArray) |
| 51 | + { |
| 52 | + Console.Write(item + ", "); |
| 53 | + } |
| 54 | + Console.WriteLine(); |
| 55 | + testResult = Fail; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return testResult; |
| 60 | + } |
| 61 | + |
| 62 | + public unsafe struct TestTable<T> : IDisposable where T : struct |
| 63 | + { |
| 64 | + public T[] inArray; |
| 65 | + public T[] outArray; |
| 66 | + |
| 67 | + public void* inArrayPtr => inHandle.AddrOfPinnedObject().ToPointer(); |
| 68 | + public void* outArrayPtr => outHandle.AddrOfPinnedObject().ToPointer(); |
| 69 | + |
| 70 | + GCHandle inHandle; |
| 71 | + GCHandle outHandle; |
| 72 | + public TestTable(T[] a, T[] b) |
| 73 | + { |
| 74 | + this.inArray = a; |
| 75 | + this.outArray = b; |
| 76 | + |
| 77 | + inHandle = GCHandle.Alloc(inArray, GCHandleType.Pinned); |
| 78 | + outHandle = GCHandle.Alloc(outArray, GCHandleType.Pinned); |
| 79 | + } |
| 80 | + public bool CheckResult(Func<T, T, bool> check) |
| 81 | + { |
| 82 | + for (int i = 0; i < outArray.Length/2; i++) |
| 83 | + { |
| 84 | + if (!check(inArray[i], outArray[i])) |
| 85 | + { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + for (int i = outArray.Length/2; i < outArray.Length; i++) |
| 90 | + { |
| 91 | + if (!check(inArray[i - outArray.Length/2], outArray[i])) |
| 92 | + { |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + public void Dispose() |
| 100 | + { |
| 101 | + inHandle.Free(); |
| 102 | + outHandle.Free(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | +} |
0 commit comments