Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit eaeb4d3

Browse files
committed
Updating the existing HWIntrinsic tests to also test indirect calling via reflection.
1 parent a71f4bc commit eaeb4d3

File tree

7 files changed

+473
-86
lines changed

7 files changed

+473
-86
lines changed

tests/src/JIT/HardwareIntrinsics/X86/Avx/Add.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,53 @@ static unsafe int Main(string[] args)
3030
var vf3 = Avx.Add(vf1, vf2);
3131
Unsafe.Write(floatTable.outArrayPtr, vf3);
3232

33+
if (!floatTable.CheckResult((x, y, z) => x + y == z))
34+
{
35+
Console.WriteLine("AVX Add failed on float:");
36+
foreach (var item in floatTable.outArray)
37+
{
38+
Console.Write(item + ", ");
39+
}
40+
Console.WriteLine();
41+
testResult = Fail;
42+
}
43+
44+
vf3 = (Vector256<float>)typeof(Avx).GetMethod(nameof(Avx.Add), new Type[] { vf1.GetType(), vf2.GetType() }).Invoke(null, new object[] { vf1, vf2 });
45+
Unsafe.Write(floatTable.outArrayPtr, vf3);
46+
47+
if (!floatTable.CheckResult((x, y, z) => x + y == z))
48+
{
49+
Console.WriteLine("AVX Add failed via reflection on float:");
50+
foreach (var item in floatTable.outArray)
51+
{
52+
Console.Write(item + ", ");
53+
}
54+
Console.WriteLine();
55+
testResult = Fail;
56+
}
57+
3358
var vd1 = Unsafe.Read<Vector256<double>>(doubleTable.inArray1Ptr);
3459
var vd2 = Unsafe.Read<Vector256<double>>(doubleTable.inArray2Ptr);
3560
var vd3 = Avx.Add(vd1, vd2);
3661
Unsafe.Write(doubleTable.outArrayPtr, vd3);
3762

38-
if (!floatTable.CheckResult((x, y, z) => x + y == z))
63+
if (!doubleTable.CheckResult((x, y, z) => x + y == z))
3964
{
40-
Console.WriteLine("AVX Add failed on float:");
41-
foreach (var item in floatTable.outArray)
65+
Console.WriteLine("AVX Add failed on double:");
66+
foreach (var item in doubleTable.outArray)
4267
{
4368
Console.Write(item + ", ");
4469
}
4570
Console.WriteLine();
4671
testResult = Fail;
4772
}
4873

74+
vd3 = (Vector256<double>)typeof(Avx).GetMethod(nameof(Avx.Add), new Type[] { vd1.GetType(), vd2.GetType() }).Invoke(null, new object[] { vd1, vd2 });
75+
Unsafe.Write(doubleTable.outArrayPtr, vd3);
76+
4977
if (!doubleTable.CheckResult((x, y, z) => x + y == z))
5078
{
51-
Console.WriteLine("AVX Add failed on double:");
79+
Console.WriteLine("AVX Add failed via reflection on double:");
5280
foreach (var item in doubleTable.outArray)
5381
{
5482
Console.Write(item + ", ");

tests/src/JIT/HardwareIntrinsics/X86/Avx2/Add.cs

Lines changed: 146 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,44 +37,23 @@ static unsafe int Main(string[] args)
3737
var vi3 = Avx2.Add(vi1, vi2);
3838
Unsafe.Write(intTable.outArrayPtr, vi3);
3939

40-
var vl1 = Unsafe.Read<Vector256<long>>(longTable.inArray1Ptr);
41-
var vl2 = Unsafe.Read<Vector256<long>>(longTable.inArray2Ptr);
42-
var vl3 = Avx2.Add(vl1, vl2);
43-
Unsafe.Write(longTable.outArrayPtr, vl3);
44-
45-
var vui1 = Unsafe.Read<Vector256<uint>>(uintTable.inArray1Ptr);
46-
var vui2 = Unsafe.Read<Vector256<uint>>(uintTable.inArray2Ptr);
47-
var vui3 = Avx2.Add(vui1, vui2);
48-
Unsafe.Write(uintTable.outArrayPtr, vui3);
49-
50-
var vul1 = Unsafe.Read<Vector256<ulong>>(ulongTable.inArray1Ptr);
51-
var vul2 = Unsafe.Read<Vector256<ulong>>(ulongTable.inArray2Ptr);
52-
var vul3 = Avx2.Add(vul1, vul2);
53-
Unsafe.Write(ulongTable.outArrayPtr, vul3);
54-
55-
var vs1 = Unsafe.Read<Vector256<short>>(shortTable.inArray1Ptr);
56-
var vs2 = Unsafe.Read<Vector256<short>>(shortTable.inArray2Ptr);
57-
var vs3 = Avx2.Add(vs1, vs2);
58-
Unsafe.Write(shortTable.outArrayPtr, vs3);
59-
60-
var vus1 = Unsafe.Read<Vector256<ushort>>(ushortTable.inArray1Ptr);
61-
var vus2 = Unsafe.Read<Vector256<ushort>>(ushortTable.inArray2Ptr);
62-
var vus3 = Avx2.Add(vus1, vus2);
63-
Unsafe.Write(ushortTable.outArrayPtr, vus3);
64-
65-
var vsb1 = Unsafe.Read<Vector256<sbyte>>(sbyteTable.inArray1Ptr);
66-
var vsb2 = Unsafe.Read<Vector256<sbyte>>(sbyteTable.inArray2Ptr);
67-
var vsb3 = Avx2.Add(vsb1, vsb2);
68-
Unsafe.Write(sbyteTable.outArrayPtr, vsb3);
40+
if (!intTable.CheckResult((x, y, z) => x + y == z))
41+
{
42+
Console.WriteLine("AVX2 Add failed on int:");
43+
foreach (var item in intTable.outArray)
44+
{
45+
Console.Write(item + ", ");
46+
}
47+
Console.WriteLine();
48+
testResult = Fail;
49+
}
6950

70-
var vb1 = Unsafe.Read<Vector256<byte>>(byteTable.inArray1Ptr);
71-
var vb2 = Unsafe.Read<Vector256<byte>>(byteTable.inArray2Ptr);
72-
var vb3 = Avx2.Add(vb1, vb2);
73-
Unsafe.Write(byteTable.outArrayPtr, vb3);
51+
vi3 = (Vector256<int>)typeof(Avx2).GetMethod(nameof(Avx2.Add), new Type[] { vi1.GetType(), vi2.GetType() }).Invoke(null, new object[] { vi1, vi2 });
52+
Unsafe.Write(intTable.outArrayPtr, vi3);
7453

7554
if (!intTable.CheckResult((x, y, z) => x + y == z))
7655
{
77-
Console.WriteLine("AVX2 Add failed on int:");
56+
Console.WriteLine("AVX2 Add failed via reflection on int:");
7857
foreach (var item in intTable.outArray)
7958
{
8059
Console.Write(item + ", ");
@@ -83,6 +62,11 @@ static unsafe int Main(string[] args)
8362
testResult = Fail;
8463
}
8564

65+
var vl1 = Unsafe.Read<Vector256<long>>(longTable.inArray1Ptr);
66+
var vl2 = Unsafe.Read<Vector256<long>>(longTable.inArray2Ptr);
67+
var vl3 = Avx2.Add(vl1, vl2);
68+
Unsafe.Write(longTable.outArrayPtr, vl3);
69+
8670
if (!longTable.CheckResult((x, y, z) => x + y == z))
8771
{
8872
Console.WriteLine("AVX2 Add failed on long:");
@@ -94,6 +78,25 @@ static unsafe int Main(string[] args)
9478
testResult = Fail;
9579
}
9680

81+
vl3 = (Vector256<long>)typeof(Avx2).GetMethod(nameof(Avx2.Add), new Type[] { vl1.GetType(), vl2.GetType() }).Invoke(null, new object[] { vl1, vl2 });
82+
Unsafe.Write(longTable.outArrayPtr, vl3);
83+
84+
if (!longTable.CheckResult((x, y, z) => x + y == z))
85+
{
86+
Console.WriteLine("AVX2 Add failed via reflection on long:");
87+
foreach (var item in longTable.outArray)
88+
{
89+
Console.Write(item + ", ");
90+
}
91+
Console.WriteLine();
92+
testResult = Fail;
93+
}
94+
95+
var vui1 = Unsafe.Read<Vector256<uint>>(uintTable.inArray1Ptr);
96+
var vui2 = Unsafe.Read<Vector256<uint>>(uintTable.inArray2Ptr);
97+
var vui3 = Avx2.Add(vui1, vui2);
98+
Unsafe.Write(uintTable.outArrayPtr, vui3);
99+
97100
if (!uintTable.CheckResult((x, y, z) => x + y == z))
98101
{
99102
Console.WriteLine("AVX2 Add failed on uint:");
@@ -105,6 +108,25 @@ static unsafe int Main(string[] args)
105108
testResult = Fail;
106109
}
107110

111+
vui3 = (Vector256<uint>)typeof(Avx2).GetMethod(nameof(Avx2.Add), new Type[] { vui1.GetType(), vui2.GetType() }).Invoke(null, new object[] { vui1, vui2 });
112+
Unsafe.Write(uintTable.outArrayPtr, vui3);
113+
114+
if (!uintTable.CheckResult((x, y, z) => x + y == z))
115+
{
116+
Console.WriteLine("AVX2 Add failed via reflection on uint:");
117+
foreach (var item in uintTable.outArray)
118+
{
119+
Console.Write(item + ", ");
120+
}
121+
Console.WriteLine();
122+
testResult = Fail;
123+
}
124+
125+
var vul1 = Unsafe.Read<Vector256<ulong>>(ulongTable.inArray1Ptr);
126+
var vul2 = Unsafe.Read<Vector256<ulong>>(ulongTable.inArray2Ptr);
127+
var vul3 = Avx2.Add(vul1, vul2);
128+
Unsafe.Write(ulongTable.outArrayPtr, vul3);
129+
108130
if (!ulongTable.CheckResult((x, y, z) => x + y == z))
109131
{
110132
Console.WriteLine("AVX2 Add failed on ulong:");
@@ -116,6 +138,25 @@ static unsafe int Main(string[] args)
116138
testResult = Fail;
117139
}
118140

141+
vul3 = (Vector256<ulong>)typeof(Avx2).GetMethod(nameof(Avx2.Add), new Type[] { vul1.GetType(), vul2.GetType() }).Invoke(null, new object[] { vul1, vul2 });
142+
Unsafe.Write(ulongTable.outArrayPtr, vul3);
143+
144+
if (!ulongTable.CheckResult((x, y, z) => x + y == z))
145+
{
146+
Console.WriteLine("AVX2 Add failed via reflection on ulong:");
147+
foreach (var item in ulongTable.outArray)
148+
{
149+
Console.Write(item + ", ");
150+
}
151+
Console.WriteLine();
152+
testResult = Fail;
153+
}
154+
155+
var vs1 = Unsafe.Read<Vector256<short>>(shortTable.inArray1Ptr);
156+
var vs2 = Unsafe.Read<Vector256<short>>(shortTable.inArray2Ptr);
157+
var vs3 = Avx2.Add(vs1, vs2);
158+
Unsafe.Write(shortTable.outArrayPtr, vs3);
159+
119160
if (!shortTable.CheckResult((x, y, z) => x + y == z))
120161
{
121162
Console.WriteLine("AVX2 Add failed on short:");
@@ -127,6 +168,25 @@ static unsafe int Main(string[] args)
127168
testResult = Fail;
128169
}
129170

171+
vs3 = (Vector256<short>)typeof(Avx2).GetMethod(nameof(Avx2.Add), new Type[] { vs1.GetType(), vs2.GetType() }).Invoke(null, new object[] { vs1, vs2 });
172+
Unsafe.Write(shortTable.outArrayPtr, vs3);
173+
174+
if (!shortTable.CheckResult((x, y, z) => x + y == z))
175+
{
176+
Console.WriteLine("AVX2 Add failed via reflection on short:");
177+
foreach (var item in shortTable.outArray)
178+
{
179+
Console.Write(item + ", ");
180+
}
181+
Console.WriteLine();
182+
testResult = Fail;
183+
}
184+
185+
var vus1 = Unsafe.Read<Vector256<ushort>>(ushortTable.inArray1Ptr);
186+
var vus2 = Unsafe.Read<Vector256<ushort>>(ushortTable.inArray2Ptr);
187+
var vus3 = Avx2.Add(vus1, vus2);
188+
Unsafe.Write(ushortTable.outArrayPtr, vus3);
189+
130190
if (!ushortTable.CheckResult((x, y, z) => x + y == z))
131191
{
132192
Console.WriteLine("AVX2 Add failed on ushort:");
@@ -138,6 +198,25 @@ static unsafe int Main(string[] args)
138198
testResult = Fail;
139199
}
140200

201+
vus3 = (Vector256<ushort>)typeof(Avx2).GetMethod(nameof(Avx2.Add), new Type[] { vus1.GetType(), vus2.GetType() }).Invoke(null, new object[] { vus1, vus2 });
202+
Unsafe.Write(ushortTable.outArrayPtr, vus3);
203+
204+
if (!ushortTable.CheckResult((x, y, z) => x + y == z))
205+
{
206+
Console.WriteLine("AVX2 Add failed via reflection on ushort:");
207+
foreach (var item in ushortTable.outArray)
208+
{
209+
Console.Write(item + ", ");
210+
}
211+
Console.WriteLine();
212+
testResult = Fail;
213+
}
214+
215+
var vsb1 = Unsafe.Read<Vector256<sbyte>>(sbyteTable.inArray1Ptr);
216+
var vsb2 = Unsafe.Read<Vector256<sbyte>>(sbyteTable.inArray2Ptr);
217+
var vsb3 = Avx2.Add(vsb1, vsb2);
218+
Unsafe.Write(sbyteTable.outArrayPtr, vsb3);
219+
141220
if (!sbyteTable.CheckResult((x, y, z) => x + y == z))
142221
{
143222
Console.WriteLine("AVX2 Add failed on sbyte:");
@@ -149,6 +228,25 @@ static unsafe int Main(string[] args)
149228
testResult = Fail;
150229
}
151230

231+
vsb3 = (Vector256<sbyte>)typeof(Avx2).GetMethod(nameof(Avx2.Add), new Type[] { vsb1.GetType(), vsb2.GetType() }).Invoke(null, new object[] { vsb1, vsb2 });
232+
Unsafe.Write(sbyteTable.outArrayPtr, vsb3);
233+
234+
if (!sbyteTable.CheckResult((x, y, z) => x + y == z))
235+
{
236+
Console.WriteLine("AVX2 Add failed via reflection on sbyte:");
237+
foreach (var item in sbyteTable.outArray)
238+
{
239+
Console.Write(item + ", ");
240+
}
241+
Console.WriteLine();
242+
testResult = Fail;
243+
}
244+
245+
var vb1 = Unsafe.Read<Vector256<byte>>(byteTable.inArray1Ptr);
246+
var vb2 = Unsafe.Read<Vector256<byte>>(byteTable.inArray2Ptr);
247+
var vb3 = Avx2.Add(vb1, vb2);
248+
Unsafe.Write(byteTable.outArrayPtr, vb3);
249+
152250
if (!byteTable.CheckResult((x, y, z) => x + y == z))
153251
{
154252
Console.WriteLine("AVX2 Add failed on byte:");
@@ -159,8 +257,21 @@ static unsafe int Main(string[] args)
159257
Console.WriteLine();
160258
testResult = Fail;
161259
}
162-
}
163260

261+
vb3 = (Vector256<byte>)typeof(Avx2).GetMethod(nameof(Avx2.Add), new Type[] { vb1.GetType(), vb2.GetType() }).Invoke(null, new object[] { vb1, vb2 });
262+
Unsafe.Write(byteTable.outArrayPtr, vb3);
263+
264+
if (!byteTable.CheckResult((x, y, z) => x + y == z))
265+
{
266+
Console.WriteLine("AVX2 Add failed via reflection on byte:");
267+
foreach (var item in byteTable.outArray)
268+
{
269+
Console.Write(item + ", ");
270+
}
271+
Console.WriteLine();
272+
testResult = Fail;
273+
}
274+
}
164275
}
165276

166277
return testResult;

tests/src/JIT/HardwareIntrinsics/X86/Lzcnt/Lzcnt.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
using System;
7+
using System.Reflection;
78
using System.Runtime.Intrinsics.X86;
89

910
namespace IntelHardwareIntrinsicTest
@@ -26,11 +27,22 @@ static int Main(string[] args)
2627
Console.WriteLine("Intrinsic Lzcnt.LeadingZeroCount is called on non-supported hardware.");
2728
Console.WriteLine("Lzcnt.IsSupported " + Lzcnt.IsSupported);
2829
Console.WriteLine("Environment.Is64BitProcess " + Environment.Is64BitProcess);
29-
return Fail;
30+
testResult = Fail;
3031
}
3132
catch (PlatformNotSupportedException)
3233
{
33-
testResult = Pass;
34+
}
35+
36+
try
37+
{
38+
resl = Convert.ToUInt64(typeof(Lzcnt).GetMethod(nameof(Lzcnt.LeadingZeroCount), new Type[] { sl.GetType() }).Invoke(null, new object[] { sl }));
39+
Console.WriteLine("Intrinsic Lzcnt.LeadingZeroCount is called via reflection on non-supported hardware.");
40+
Console.WriteLine("Lzcnt.IsSupported " + Lzcnt.IsSupported);
41+
Console.WriteLine("Environment.Is64BitProcess " + Environment.Is64BitProcess);
42+
testResult = Fail;
43+
}
44+
catch (TargetInvocationException e) when (e.InnerException is PlatformNotSupportedException)
45+
{
3446
}
3547
}
3648

@@ -42,27 +54,45 @@ static int Main(string[] args)
4254
for (int i = 0; i < longLzcntTable.Length; i++)
4355
{
4456
sl = longLzcntTable[i].s;
57+
4558
resl = Lzcnt.LeadingZeroCount(sl);
4659
if (resl != longLzcntTable[i].res)
4760
{
4861
Console.WriteLine("{0}: Inputs: 0x{1,16:x} Expected: 0x{3,16:x} actual: 0x{4,16:x}",
4962
i, sl, longLzcntTable[i].res, resl);
5063
testResult = Fail;
5164
}
65+
66+
resl = Convert.ToUInt64(typeof(Lzcnt).GetMethod(nameof(Lzcnt.LeadingZeroCount), new Type[] { sl.GetType() }).Invoke(null, new object[] { sl }));
67+
if (resl != longLzcntTable[i].res)
68+
{
69+
Console.WriteLine("{0}: Inputs: 0x{1,16:x} Expected: 0x{3,16:x} actual: 0x{4,16:x} - Reflection",
70+
i, sl, longLzcntTable[i].res, resl);
71+
testResult = Fail;
72+
}
5273
}
5374
}
5475

5576
uint si, resi;
5677
for (int i = 0; i < intLzcntTable.Length; i++)
5778
{
5879
si = intLzcntTable[i].s;
80+
5981
resi = Lzcnt.LeadingZeroCount(si);
6082
if (resi != intLzcntTable[i].res)
6183
{
6284
Console.WriteLine("{0}: Inputs: 0x{1,16:x} Expected: 0x{3,16:x} actual: 0x{4,16:x}",
6385
i, si, intLzcntTable[i].res, resi);
6486
testResult = Fail;
6587
}
88+
89+
resl = Convert.ToUInt64(typeof(Lzcnt).GetMethod(nameof(Lzcnt.LeadingZeroCount), new Type[] { si.GetType() }).Invoke(null, new object[] { si }));
90+
if (resi != intLzcntTable[i].res)
91+
{
92+
Console.WriteLine("{0}: Inputs: 0x{1,16:x} Expected: 0x{3,16:x} actual: 0x{4,16:x} - Reflection",
93+
i, si, intLzcntTable[i].res, resi);
94+
testResult = Fail;
95+
}
6696
}
6797
}
6898

@@ -96,4 +126,4 @@ public LZCNT(T a, T r)
96126
new LZCNT<uint>(0x0005423fU, 13)
97127
};
98128
}
99-
}
129+
}

0 commit comments

Comments
 (0)