Merge pull request #16703 from BruceForstall/GeneralizeRuntestExclusionFiles
[platform/upstream/coreclr.git] / tests / src / JIT / HardwareIntrinsics / X86 / Avx2 / ShiftRightLogical128BitLane.UInt64.1.cs
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  * This file is auto-generated from a template file by the GenerateTests.csx  *
7  * script in tests\src\JIT\HardwareIntrinsics\X86\Shared. In order to make    *
8  * changes, please update the corresponding template and run according to the *
9  * directions listed in the file.                                             *
10  ******************************************************************************/
11
12 using System;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
15 using System.Runtime.Intrinsics;
16 using System.Runtime.Intrinsics.X86;
17
18 namespace JIT.HardwareIntrinsics.X86
19 {
20     public static partial class Program
21     {
22         private static void ShiftRightLogical128BitLaneUInt641()
23         {
24             var test = new SimpleUnaryOpTest__ShiftRightLogical128BitLaneUInt641();
25
26             if (test.IsSupported)
27             {
28                 // Validates basic functionality works, using Unsafe.Read
29                 test.RunBasicScenario_UnsafeRead();
30
31                 if (Avx.IsSupported)
32                 {
33                     // Validates basic functionality works, using Load
34                     test.RunBasicScenario_Load();
35
36                     // Validates basic functionality works, using LoadAligned
37                     test.RunBasicScenario_LoadAligned();
38                 }
39
40                 // Validates calling via reflection works, using Unsafe.Read
41                 test.RunReflectionScenario_UnsafeRead();
42
43                 if (Avx.IsSupported)
44                 {
45                     // Validates calling via reflection works, using Load
46                     test.RunReflectionScenario_Load();
47
48                     // Validates calling via reflection works, using LoadAligned
49                     test.RunReflectionScenario_LoadAligned();
50                 }
51
52                 // Validates passing a static member works
53                 test.RunClsVarScenario();
54
55                 // Validates passing a local works, using Unsafe.Read
56                 test.RunLclVarScenario_UnsafeRead();
57
58                 if (Avx.IsSupported)
59                 {
60                     // Validates passing a local works, using Load
61                     test.RunLclVarScenario_Load();
62
63                     // Validates passing a local works, using LoadAligned
64                     test.RunLclVarScenario_LoadAligned();
65                 }
66
67                 // Validates passing the field of a local works
68                 test.RunLclFldScenario();
69
70                 // Validates passing an instance member works
71                 test.RunFldScenario();
72             }
73             else
74             {
75                 // Validates we throw on unsupported hardware
76                 test.RunUnsupportedScenario();
77             }
78
79             if (!test.Succeeded)
80             {
81                 throw new Exception("One or more scenarios did not complete as expected.");
82             }
83         }
84     }
85
86     public sealed unsafe class SimpleUnaryOpTest__ShiftRightLogical128BitLaneUInt641
87     {
88         private const int VectorSize = 32;
89
90         private const int Op1ElementCount = VectorSize / sizeof(UInt64);
91         private const int RetElementCount = VectorSize / sizeof(UInt64);
92
93         private static UInt64[] _data = new UInt64[Op1ElementCount];
94
95         private static Vector256<UInt64> _clsVar;
96
97         private Vector256<UInt64> _fld;
98
99         private SimpleUnaryOpTest__DataTable<UInt64, UInt64> _dataTable;
100
101         static SimpleUnaryOpTest__ShiftRightLogical128BitLaneUInt641()
102         {
103             var random = new Random();
104
105             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ulong)8; }
106             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<UInt64>, byte>(ref _clsVar), ref Unsafe.As<UInt64, byte>(ref _data[0]), VectorSize);
107         }
108
109         public SimpleUnaryOpTest__ShiftRightLogical128BitLaneUInt641()
110         {
111             Succeeded = true;
112
113             var random = new Random();
114
115             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ulong)8; }
116             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<UInt64>, byte>(ref _fld), ref Unsafe.As<UInt64, byte>(ref _data[0]), VectorSize);
117
118             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ulong)8; }
119             _dataTable = new SimpleUnaryOpTest__DataTable<UInt64, UInt64>(_data, new UInt64[RetElementCount], VectorSize);
120         }
121
122         public bool IsSupported => Avx2.IsSupported;
123
124         public bool Succeeded { get; set; }
125
126         public void RunBasicScenario_UnsafeRead()
127         {
128             var result = Avx2.ShiftRightLogical128BitLane(
129                 Unsafe.Read<Vector256<UInt64>>(_dataTable.inArrayPtr),
130                 1
131             );
132
133             Unsafe.Write(_dataTable.outArrayPtr, result);
134             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
135         }
136
137         public void RunBasicScenario_Load()
138         {
139             var result = Avx2.ShiftRightLogical128BitLane(
140                 Avx.LoadVector256((UInt64*)(_dataTable.inArrayPtr)),
141                 1
142             );
143
144             Unsafe.Write(_dataTable.outArrayPtr, result);
145             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
146         }
147
148         public void RunBasicScenario_LoadAligned()
149         {
150             var result = Avx2.ShiftRightLogical128BitLane(
151                 Avx.LoadAlignedVector256((UInt64*)(_dataTable.inArrayPtr)),
152                 1
153             );
154
155             Unsafe.Write(_dataTable.outArrayPtr, result);
156             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
157         }
158
159         public void RunReflectionScenario_UnsafeRead()
160         {
161             var result = typeof(Avx2).GetMethod(nameof(Avx2.ShiftRightLogical128BitLane), new Type[] { typeof(Vector256<UInt64>), typeof(byte) })
162                                      .Invoke(null, new object[] {
163                                         Unsafe.Read<Vector256<UInt64>>(_dataTable.inArrayPtr),
164                                         (byte)1
165                                      });
166
167             Unsafe.Write(_dataTable.outArrayPtr, (Vector256<UInt64>)(result));
168             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
169         }
170
171         public void RunReflectionScenario_Load()
172         {
173             var result = typeof(Avx2).GetMethod(nameof(Avx2.ShiftRightLogical128BitLane), new Type[] { typeof(Vector256<UInt64>), typeof(byte) })
174                                      .Invoke(null, new object[] {
175                                         Avx.LoadVector256((UInt64*)(_dataTable.inArrayPtr)),
176                                         (byte)1
177                                      });
178
179             Unsafe.Write(_dataTable.outArrayPtr, (Vector256<UInt64>)(result));
180             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
181         }
182
183         public void RunReflectionScenario_LoadAligned()
184         {
185             var result = typeof(Avx2).GetMethod(nameof(Avx2.ShiftRightLogical128BitLane), new Type[] { typeof(Vector256<UInt64>), typeof(byte) })
186                                      .Invoke(null, new object[] {
187                                         Avx.LoadAlignedVector256((UInt64*)(_dataTable.inArrayPtr)),
188                                         (byte)1
189                                      });
190
191             Unsafe.Write(_dataTable.outArrayPtr, (Vector256<UInt64>)(result));
192             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
193         }
194
195         public void RunClsVarScenario()
196         {
197             var result = Avx2.ShiftRightLogical128BitLane(
198                 _clsVar,
199                 1
200             );
201
202             Unsafe.Write(_dataTable.outArrayPtr, result);
203             ValidateResult(_clsVar, _dataTable.outArrayPtr);
204         }
205
206         public void RunLclVarScenario_UnsafeRead()
207         {
208             var firstOp = Unsafe.Read<Vector256<UInt64>>(_dataTable.inArrayPtr);
209             var result = Avx2.ShiftRightLogical128BitLane(firstOp, 1);
210
211             Unsafe.Write(_dataTable.outArrayPtr, result);
212             ValidateResult(firstOp, _dataTable.outArrayPtr);
213         }
214
215         public void RunLclVarScenario_Load()
216         {
217             var firstOp = Avx.LoadVector256((UInt64*)(_dataTable.inArrayPtr));
218             var result = Avx2.ShiftRightLogical128BitLane(firstOp, 1);
219
220             Unsafe.Write(_dataTable.outArrayPtr, result);
221             ValidateResult(firstOp, _dataTable.outArrayPtr);
222         }
223
224         public void RunLclVarScenario_LoadAligned()
225         {
226             var firstOp = Avx.LoadAlignedVector256((UInt64*)(_dataTable.inArrayPtr));
227             var result = Avx2.ShiftRightLogical128BitLane(firstOp, 1);
228
229             Unsafe.Write(_dataTable.outArrayPtr, result);
230             ValidateResult(firstOp, _dataTable.outArrayPtr);
231         }
232
233         public void RunLclFldScenario()
234         {
235             var test = new SimpleUnaryOpTest__ShiftRightLogical128BitLaneUInt641();
236             var result = Avx2.ShiftRightLogical128BitLane(test._fld, 1);
237
238             Unsafe.Write(_dataTable.outArrayPtr, result);
239             ValidateResult(test._fld, _dataTable.outArrayPtr);
240         }
241
242         public void RunFldScenario()
243         {
244             var result = Avx2.ShiftRightLogical128BitLane(_fld, 1);
245
246             Unsafe.Write(_dataTable.outArrayPtr, result);
247             ValidateResult(_fld, _dataTable.outArrayPtr);
248         }
249
250         public void RunUnsupportedScenario()
251         {
252             Succeeded = false;
253
254             try
255             {
256                 RunBasicScenario_UnsafeRead();
257             }
258             catch (PlatformNotSupportedException)
259             {
260                 Succeeded = true;
261             }
262         }
263
264         private void ValidateResult(Vector256<UInt64> firstOp, void* result, [CallerMemberName] string method = "")
265         {
266             UInt64[] inArray = new UInt64[Op1ElementCount];
267             UInt64[] outArray = new UInt64[RetElementCount];
268
269             Unsafe.Write(Unsafe.AsPointer(ref inArray[0]), firstOp);
270             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt64, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
271
272             ValidateResult(inArray, outArray, method);
273         }
274
275         private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
276         {
277             UInt64[] inArray = new UInt64[Op1ElementCount];
278             UInt64[] outArray = new UInt64[RetElementCount];
279
280             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt64, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), VectorSize);
281             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt64, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
282
283             ValidateResult(inArray, outArray, method);
284         }
285
286         private void ValidateResult(UInt64[] firstOp, UInt64[] result, [CallerMemberName] string method = "")
287         {
288             if (result[0] != 576460752303423488UL)
289             {
290                 Succeeded = false;
291             }
292             else
293             {
294                 for (var i = 1; i < RetElementCount; i++)
295                 {
296                     if ((i == 2 ? result[i] != 576460752303423488UL : result[i] != 0))
297                     {
298                         Succeeded = false;
299                         break;
300                     }
301                 }
302             }
303
304             if (!Succeeded)
305             {
306                 Console.WriteLine($"{nameof(Avx2)}.{nameof(Avx2.ShiftRightLogical128BitLane)}<UInt64>(Vector256<UInt64><9>): {method} failed:");
307                 Console.WriteLine($"  firstOp: ({string.Join(", ", firstOp)})");
308                 Console.WriteLine($"   result: ({string.Join(", ", result)})");
309                 Console.WriteLine();
310             }
311         }
312     }
313 }