|
| 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] { float.NaN, float.NaN, float.NaN, float.NaN, float.NaN, float.NaN, float.NaN, float.NaN })) |
| 26 | + { |
| 27 | + var vf1 = Avx.SetAllVector256<float>(-5); |
| 28 | + Unsafe.Write(floatTable.outArrayPtr, vf1); |
| 29 | + |
| 30 | + if (!floatTable.CheckResult((x) => (x == -5))) |
| 31 | + { |
| 32 | + Console.WriteLine("AVX SetAllVector256 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] { double.NaN, double.NaN, double.NaN, double.NaN })) |
| 43 | + { |
| 44 | + var vf1 = Avx.SetAllVector256<double>(3); |
| 45 | + Unsafe.Write(doubleTable.outArrayPtr, vf1); |
| 46 | + |
| 47 | + if (!doubleTable.CheckResult((x) => (x == 3))) |
| 48 | + { |
| 49 | + Console.WriteLine("AVX SetAllVector256 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 | + using (TestTable<sbyte> sbyteTable = new TestTable<sbyte>(new sbyte[32] { sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, |
| 60 | + sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, |
| 61 | + sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, |
| 62 | + sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue, sbyte.MaxValue })) |
| 63 | + { |
| 64 | + var vf1 = Avx.SetAllVector256<sbyte>(100); |
| 65 | + Unsafe.Write(sbyteTable.outArrayPtr, vf1); |
| 66 | + |
| 67 | + if (!sbyteTable.CheckResult((x) => (x == 100))) |
| 68 | + { |
| 69 | + Console.WriteLine("AVX SetAllVector256 failed on sbyte:"); |
| 70 | + foreach (var item in sbyteTable.outArray) |
| 71 | + { |
| 72 | + Console.Write(item + ", "); |
| 73 | + } |
| 74 | + Console.WriteLine(); |
| 75 | + testResult = Fail; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + using (TestTable<byte> byteTable = new TestTable<byte>(new byte[32] { byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, |
| 80 | + byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, |
| 81 | + byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, |
| 82 | + byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue })) |
| 83 | + { |
| 84 | + Vector256<byte> vf1 = Avx.SetAllVector256<byte>(4); |
| 85 | + Unsafe.Write(byteTable.outArrayPtr, vf1); |
| 86 | + |
| 87 | + if (!byteTable.CheckResult((x) => (x == 4))) |
| 88 | + { |
| 89 | + Console.WriteLine("AVX SetAllVector256 failed on byte:"); |
| 90 | + foreach (var item in byteTable.outArray) |
| 91 | + { |
| 92 | + Console.Write(item + ", "); |
| 93 | + } |
| 94 | + Console.WriteLine(); |
| 95 | + testResult = Fail; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + using (TestTable<short> shortTable = new TestTable<short>(new short[16] { short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue, |
| 100 | + short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue, short.MaxValue })) |
| 101 | + { |
| 102 | + var vf1 = Avx.SetAllVector256<short>(-5); |
| 103 | + Unsafe.Write(shortTable.outArrayPtr, vf1); |
| 104 | + |
| 105 | + if (!shortTable.CheckResult((x) => (x == -5))) |
| 106 | + { |
| 107 | + Console.WriteLine("AVX SetAllVector256 failed on short:"); |
| 108 | + foreach (var item in shortTable.outArray) |
| 109 | + { |
| 110 | + Console.Write(item + ", "); |
| 111 | + } |
| 112 | + Console.WriteLine(); |
| 113 | + testResult = Fail; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + using (TestTable<ushort> ushortTable = new TestTable<ushort>(new ushort[16] { ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, |
| 118 | + ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue })) |
| 119 | + { |
| 120 | + Vector256<ushort> vf1 = Avx.SetAllVector256<ushort>(2); |
| 121 | + Unsafe.Write(ushortTable.outArrayPtr, vf1); |
| 122 | + |
| 123 | + if (!ushortTable.CheckResult((x) => (x == 2))) |
| 124 | + { |
| 125 | + Console.WriteLine("AVX SetAllVector256 failed on ushort:"); |
| 126 | + foreach (var item in ushortTable.outArray) |
| 127 | + { |
| 128 | + Console.Write(item + ", "); |
| 129 | + } |
| 130 | + Console.WriteLine(); |
| 131 | + testResult = Fail; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + using (TestTable<int> intTable = new TestTable<int>(new int[8] { int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue })) |
| 136 | + { |
| 137 | + var vf1 = Avx.SetAllVector256<int>(-5); |
| 138 | + Unsafe.Write(intTable.outArrayPtr, vf1); |
| 139 | + |
| 140 | + if (!intTable.CheckResult((x) => (x == -5))) |
| 141 | + { |
| 142 | + Console.WriteLine("AVX SetAllVector256 failed on int:"); |
| 143 | + foreach (var item in intTable.outArray) |
| 144 | + { |
| 145 | + Console.Write(item + ", "); |
| 146 | + } |
| 147 | + Console.WriteLine(); |
| 148 | + testResult = Fail; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + using (TestTable<uint> uintTable = new TestTable<uint>(new uint[8] { uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue })) |
| 153 | + { |
| 154 | + Vector256<uint> vf1 = Avx.SetAllVector256<uint>(3); |
| 155 | + Unsafe.Write(uintTable.outArrayPtr, vf1); |
| 156 | + |
| 157 | + if (!uintTable.CheckResult((x) => (x == 3))) |
| 158 | + { |
| 159 | + Console.WriteLine("AVX SetAllVector256 failed on uint:"); |
| 160 | + foreach (var item in uintTable.outArray) |
| 161 | + { |
| 162 | + Console.Write(item + ", "); |
| 163 | + } |
| 164 | + Console.WriteLine(); |
| 165 | + testResult = Fail; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + using (TestTable<long> longTable = new TestTable<long>(new long[4] { long.MaxValue, long.MaxValue, long.MaxValue, long.MaxValue })) |
| 170 | + { |
| 171 | + var vf1 = Avx.SetAllVector256<long>(-199); |
| 172 | + Unsafe.Write(longTable.outArrayPtr, vf1); |
| 173 | + |
| 174 | + if (!longTable.CheckResult((x) => (x == -199))) |
| 175 | + { |
| 176 | + Console.WriteLine("AVX SetAllVector256 failed on long:"); |
| 177 | + foreach (var item in longTable.outArray) |
| 178 | + { |
| 179 | + Console.Write(item + ", "); |
| 180 | + } |
| 181 | + Console.WriteLine(); |
| 182 | + testResult = Fail; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + using (TestTable<ulong> ulongTable = new TestTable<ulong>(new ulong[4] { ulong.MaxValue, ulong.MaxValue, ulong.MaxValue, ulong.MaxValue })) |
| 187 | + { |
| 188 | + Vector256<ulong> vf1 = Avx.SetAllVector256<ulong>(34); |
| 189 | + Unsafe.Write(ulongTable.outArrayPtr, vf1); |
| 190 | + |
| 191 | + if (!ulongTable.CheckResult((x) => (x == 34))) |
| 192 | + { |
| 193 | + Console.WriteLine("AVX SetAllVector256 failed on ulong:"); |
| 194 | + foreach (var item in ulongTable.outArray) |
| 195 | + { |
| 196 | + Console.Write(item + ", "); |
| 197 | + } |
| 198 | + Console.WriteLine(); |
| 199 | + testResult = Fail; |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return testResult; |
| 205 | + } |
| 206 | + |
| 207 | + public unsafe struct TestTable<T> : IDisposable where T : struct |
| 208 | + { |
| 209 | + public T[] outArray; |
| 210 | + |
| 211 | + public void* outArrayPtr => outHandle.AddrOfPinnedObject().ToPointer(); |
| 212 | + |
| 213 | + GCHandle outHandle; |
| 214 | + public TestTable(T[] a) |
| 215 | + { |
| 216 | + this.outArray = a; |
| 217 | + |
| 218 | + outHandle = GCHandle.Alloc(outArray, GCHandleType.Pinned); |
| 219 | + } |
| 220 | + public bool CheckResult(Func<T, bool> check) |
| 221 | + { |
| 222 | + foreach (var item in outArray) |
| 223 | + { |
| 224 | + if (!check(item)) |
| 225 | + { |
| 226 | + return false; |
| 227 | + } |
| 228 | + } |
| 229 | + return true; |
| 230 | + } |
| 231 | + |
| 232 | + public void Dispose() |
| 233 | + { |
| 234 | + outHandle.Free(); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments