Regenerating the x86 HWIntrinsics tests to no longer have a GC hole
[platform/upstream/coreclr.git] / tests / src / JIT / HardwareIntrinsics / X86 / Sse41 / TestNotZAndNotC.UInt32.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 TestNotZAndNotCUInt32()
23         {
24             var test = new BooleanTwoComparisonOpTest__TestNotZAndNotCUInt32();
25
26             if (test.IsSupported)
27             {
28                 // Validates basic functionality works, using Unsafe.Read
29                 test.RunBasicScenario_UnsafeRead();
30
31                 if (Sse2.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 (Sse2.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 (Sse2.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 BooleanTwoComparisonOpTest__TestNotZAndNotCUInt32
87     {
88         private const int VectorSize = 16;
89
90         private const int Op1ElementCount = VectorSize / sizeof(UInt32);
91         private const int Op2ElementCount = VectorSize / sizeof(UInt32);
92
93         private static UInt32[] _data1 = new UInt32[Op1ElementCount];
94         private static UInt32[] _data2 = new UInt32[Op2ElementCount];
95
96         private static Vector128<UInt32> _clsVar1;
97         private static Vector128<UInt32> _clsVar2;
98
99         private Vector128<UInt32> _fld1;
100         private Vector128<UInt32> _fld2;
101
102         private BooleanTwoComparisonOpTest__DataTable<UInt32, UInt32> _dataTable;
103
104         static BooleanTwoComparisonOpTest__TestNotZAndNotCUInt32()
105         {
106             var random = new Random();
107
108             for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (uint)(random.Next(0, int.MaxValue)); }
109             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt32>, byte>(ref _clsVar1), ref Unsafe.As<UInt32, byte>(ref _data1[0]), VectorSize);
110             for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (uint)(random.Next(0, int.MaxValue)); }
111             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt32>, byte>(ref _clsVar2), ref Unsafe.As<UInt32, byte>(ref _data2[0]), VectorSize);
112         }
113
114         public BooleanTwoComparisonOpTest__TestNotZAndNotCUInt32()
115         {
116             Succeeded = true;
117
118             var random = new Random();
119
120             for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (uint)(random.Next(0, int.MaxValue)); }
121             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt32>, byte>(ref _fld1), ref Unsafe.As<UInt32, byte>(ref _data1[0]), VectorSize);
122             for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (uint)(random.Next(0, int.MaxValue)); }
123             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt32>, byte>(ref _fld2), ref Unsafe.As<UInt32, byte>(ref _data2[0]), VectorSize);
124
125             for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (uint)(random.Next(0, int.MaxValue)); }
126             for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (uint)(random.Next(0, int.MaxValue)); }
127             _dataTable = new BooleanTwoComparisonOpTest__DataTable<UInt32, UInt32>(_data1, _data2, VectorSize);
128         }
129
130         public bool IsSupported => Sse41.IsSupported;
131
132         public bool Succeeded { get; set; }
133
134         public void RunBasicScenario_UnsafeRead()
135         {
136             var result = Sse41.TestNotZAndNotC(
137                 Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray1Ptr),
138                 Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray2Ptr)
139             );
140
141             ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, result);
142         }
143
144         public void RunBasicScenario_Load()
145         {
146             var result = Sse41.TestNotZAndNotC(
147                 Sse2.LoadVector128((UInt32*)(_dataTable.inArray1Ptr)),
148                 Sse2.LoadVector128((UInt32*)(_dataTable.inArray2Ptr))
149             );
150
151             ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, result);
152         }
153
154         public void RunBasicScenario_LoadAligned()
155         {
156             var result = Sse41.TestNotZAndNotC(
157                 Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray1Ptr)),
158                 Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray2Ptr))
159             );
160
161             ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, result);
162         }
163
164         public void RunReflectionScenario_UnsafeRead()
165         {
166             var method = typeof(Sse41).GetMethod(nameof(Sse41.TestNotZAndNotC), new Type[] { typeof(Vector128<UInt32>), typeof(Vector128<UInt32>) });
167             
168             if (method != null)
169             {
170                 var result = method.Invoke(null, new object[] {
171                                         Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray1Ptr),
172                                         Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray2Ptr)
173                                      });
174
175                 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, (bool)(result));
176             }
177         }
178
179         public void RunReflectionScenario_Load()
180         {
181             var method = typeof(Sse41).GetMethod(nameof(Sse41.TestNotZAndNotC), new Type[] { typeof(Vector128<UInt32>), typeof(Vector128<UInt32>) });
182             
183             if (method != null)
184             {
185                 var result = method.Invoke(null, new object[] {
186                                         Sse2.LoadVector128((UInt32*)(_dataTable.inArray1Ptr)),
187                                         Sse2.LoadVector128((UInt32*)(_dataTable.inArray2Ptr))
188                                      });
189
190                 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, (bool)(result));
191             }
192         }
193
194         public void RunReflectionScenario_LoadAligned()
195         {var method = typeof(Sse41).GetMethod(nameof(Sse41.TestNotZAndNotC), new Type[] { typeof(Vector128<UInt32>), typeof(Vector128<UInt32>) });
196             
197             if (method != null)
198             {
199                 var result = method.Invoke(null, new object[] {
200                                         Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray1Ptr)),
201                                         Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray2Ptr))
202                                      });
203
204                 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, (bool)(result));
205             }
206         }
207
208         public void RunClsVarScenario()
209         {
210             var result = Sse41.TestNotZAndNotC(
211                 _clsVar1,
212                 _clsVar2
213             );
214
215             ValidateResult(_clsVar1, _clsVar2, result);
216         }
217
218         public void RunLclVarScenario_UnsafeRead()
219         {
220             var left = Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray1Ptr);
221             var right = Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray2Ptr);
222             var result = Sse41.TestNotZAndNotC(left, right);
223
224             ValidateResult(left, right, result);
225         }
226
227         public void RunLclVarScenario_Load()
228         {
229             var left = Sse2.LoadVector128((UInt32*)(_dataTable.inArray1Ptr));
230             var right = Sse2.LoadVector128((UInt32*)(_dataTable.inArray2Ptr));
231             var result = Sse41.TestNotZAndNotC(left, right);
232
233             ValidateResult(left, right, result);
234         }
235
236         public void RunLclVarScenario_LoadAligned()
237         {
238             var left = Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray1Ptr));
239             var right = Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray2Ptr));
240             var result = Sse41.TestNotZAndNotC(left, right);
241
242             ValidateResult(left, right, result);
243         }
244
245         public void RunLclFldScenario()
246         {
247             var test = new BooleanTwoComparisonOpTest__TestNotZAndNotCUInt32();
248             var result = Sse41.TestNotZAndNotC(test._fld1, test._fld2);
249
250             ValidateResult(test._fld1, test._fld2, result);
251         }
252
253         public void RunFldScenario()
254         {
255             var result = Sse41.TestNotZAndNotC(_fld1, _fld2);
256
257             ValidateResult(_fld1, _fld2, result);
258         }
259
260         public void RunUnsupportedScenario()
261         {
262             Succeeded = false;
263
264             try
265             {
266                 RunBasicScenario_UnsafeRead();
267             }
268             catch (PlatformNotSupportedException)
269             {
270                 Succeeded = true;
271             }
272         }
273
274         private void ValidateResult(Vector128<UInt32> left, Vector128<UInt32> right, bool result, [CallerMemberName] string method = "")
275         {
276             UInt32[] inArray1 = new UInt32[Op1ElementCount];
277             UInt32[] inArray2 = new UInt32[Op2ElementCount];
278
279             Unsafe.WriteUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray1[0]), left);
280             Unsafe.WriteUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray2[0]), right);
281
282             ValidateResult(inArray1, inArray2, result, method);
283         }
284
285         private void ValidateResult(void* left, void* right, bool result, [CallerMemberName] string method = "")
286         {
287             UInt32[] inArray1 = new UInt32[Op1ElementCount];
288             UInt32[] inArray2 = new UInt32[Op2ElementCount];
289
290             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray1[0]), ref Unsafe.AsRef<byte>(left), VectorSize);
291             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray2[0]), ref Unsafe.AsRef<byte>(right), VectorSize);
292
293             ValidateResult(inArray1, inArray2, result, method);
294         }
295
296         private void ValidateResult(UInt32[] left, UInt32[] right, bool result, [CallerMemberName] string method = "")
297         {
298             var expectedResult1 = true;
299
300             for (var i = 0; i < Op1ElementCount; i++)
301             {
302                 expectedResult1 &= (((left[i] & right[i]) == 0));
303             }
304
305             var expectedResult2 = true;
306
307             for (var i = 0; i < Op1ElementCount; i++)
308             {
309                 expectedResult2 &= (((~left[i] & right[i]) == 0));
310             }
311
312             if (((expectedResult1 == false) && (expectedResult2 == false)) != result)
313             {
314                 Succeeded = false;
315
316                 Console.WriteLine($"{nameof(Sse41)}.{nameof(Sse41.TestNotZAndNotC)}<UInt32>(Vector128<UInt32>, Vector128<UInt32>): {method} failed:");
317                 Console.WriteLine($"    left: ({string.Join(", ", left)})");
318                 Console.WriteLine($"   right: ({string.Join(", ", right)})");
319                 Console.WriteLine($"  result: ({string.Join(", ", result)})");
320                 Console.WriteLine();
321             }
322         }
323     }
324 }