Remove random.NextDouble() from JIT/HardwareIntrinsics tests (#18838)
[platform/upstream/coreclr.git] / tests / src / JIT / HardwareIntrinsics / X86 / Sse / CompareLessThanUnorderedScalar.Boolean.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 CompareLessThanUnorderedScalarBoolean()
23         {
24             var test = new BooleanComparisonOpTest__CompareLessThanUnorderedScalarBoolean();
25
26             if (test.IsSupported)
27             {
28                 // Validates basic functionality works, using Unsafe.Read
29                 test.RunBasicScenario_UnsafeRead();
30
31                 if (Sse.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 (Sse.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 (Sse.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 class works
68                 test.RunClassLclFldScenario();
69
70                 // Validates passing an instance member of a class works
71                 test.RunClassFldScenario();
72
73                 // Validates passing the field of a local struct works
74                 test.RunStructLclFldScenario();
75
76                 // Validates passing an instance member of a struct works
77                 test.RunStructFldScenario();
78             }
79             else
80             {
81                 // Validates we throw on unsupported hardware
82                 test.RunUnsupportedScenario();
83             }
84
85             if (!test.Succeeded)
86             {
87                 throw new Exception("One or more scenarios did not complete as expected.");
88             }
89         }
90     }
91
92     public sealed unsafe class BooleanComparisonOpTest__CompareLessThanUnorderedScalarBoolean
93     {
94         private struct TestStruct
95         {
96             public Vector128<Single> _fld1;
97             public Vector128<Single> _fld2;
98
99             public static TestStruct Create()
100             {
101                 var testStruct = new TestStruct();
102                 var random = new Random();
103
104                 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = TestLibrary.Generator.GetSingle(); }
105                 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref testStruct._fld1), ref Unsafe.As<Single, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
106                 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = TestLibrary.Generator.GetSingle(); }
107                 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref testStruct._fld2), ref Unsafe.As<Single, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
108
109                 return testStruct;
110             }
111
112             public void RunStructFldScenario(BooleanComparisonOpTest__CompareLessThanUnorderedScalarBoolean testClass)
113             {
114                 var result = Sse.CompareLessThanUnorderedScalar(_fld1, _fld2);
115                 testClass.ValidateResult(_fld1, _fld2, result);
116             }
117         }
118
119         private static readonly int LargestVectorSize = 16;
120
121         private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
122         private static readonly int Op2ElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
123
124         private static Single[] _data1 = new Single[Op1ElementCount];
125         private static Single[] _data2 = new Single[Op2ElementCount];
126
127         private static Vector128<Single> _clsVar1;
128         private static Vector128<Single> _clsVar2;
129
130         private Vector128<Single> _fld1;
131         private Vector128<Single> _fld2;
132
133         private BooleanComparisonOpTest__DataTable<Single, Single> _dataTable;
134
135         static BooleanComparisonOpTest__CompareLessThanUnorderedScalarBoolean()
136         {
137             var random = new Random();
138
139             for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = TestLibrary.Generator.GetSingle(); }
140             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar1), ref Unsafe.As<Single, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
141             for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = TestLibrary.Generator.GetSingle(); }
142             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar2), ref Unsafe.As<Single, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
143         }
144
145         public BooleanComparisonOpTest__CompareLessThanUnorderedScalarBoolean()
146         {
147             Succeeded = true;
148
149             var random = new Random();
150
151             for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = TestLibrary.Generator.GetSingle(); }
152             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld1), ref Unsafe.As<Single, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
153             for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = TestLibrary.Generator.GetSingle(); }
154             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld2), ref Unsafe.As<Single, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
155
156             for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = TestLibrary.Generator.GetSingle(); }
157             for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = TestLibrary.Generator.GetSingle(); }
158             _dataTable = new BooleanComparisonOpTest__DataTable<Single, Single>(_data1, _data2, LargestVectorSize);
159         }
160
161         public bool IsSupported => Sse.IsSupported;
162
163         public bool Succeeded { get; set; }
164
165         public void RunBasicScenario_UnsafeRead()
166         {
167             var result = Sse.CompareLessThanUnorderedScalar(
168                 Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr),
169                 Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr)
170             );
171
172             ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, result);
173         }
174
175         public void RunBasicScenario_Load()
176         {
177             var result = Sse.CompareLessThanUnorderedScalar(
178                 Sse.LoadVector128((Single*)(_dataTable.inArray1Ptr)),
179                 Sse.LoadVector128((Single*)(_dataTable.inArray2Ptr))
180             );
181
182             ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, result);
183         }
184
185         public void RunBasicScenario_LoadAligned()
186         {
187             var result = Sse.CompareLessThanUnorderedScalar(
188                 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray1Ptr)),
189                 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray2Ptr))
190             );
191
192             ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, result);
193         }
194
195         public void RunReflectionScenario_UnsafeRead()
196         {
197             var result = typeof(Sse).GetMethod(nameof(Sse.CompareLessThanUnorderedScalar), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
198                                      .Invoke(null, new object[] {
199                                         Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr),
200                                         Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr)
201                                      });
202
203             ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, (bool)(result));
204         }
205
206         public void RunReflectionScenario_Load()
207         {
208             var result = typeof(Sse).GetMethod(nameof(Sse.CompareLessThanUnorderedScalar), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
209                                      .Invoke(null, new object[] {
210                                         Sse.LoadVector128((Single*)(_dataTable.inArray1Ptr)),
211                                         Sse.LoadVector128((Single*)(_dataTable.inArray2Ptr))
212                                      });
213
214             ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, (bool)(result));
215         }
216
217         public void RunReflectionScenario_LoadAligned()
218         {
219             var result = typeof(Sse).GetMethod(nameof(Sse.CompareLessThanUnorderedScalar), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
220                                      .Invoke(null, new object[] {
221                                         Sse.LoadAlignedVector128((Single*)(_dataTable.inArray1Ptr)),
222                                         Sse.LoadAlignedVector128((Single*)(_dataTable.inArray2Ptr))
223                                      });
224
225             ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, (bool)(result));
226         }
227
228         public void RunClsVarScenario()
229         {
230             var result = Sse.CompareLessThanUnorderedScalar(
231                 _clsVar1,
232                 _clsVar2
233             );
234
235             ValidateResult(_clsVar1, _clsVar2, result);
236         }
237
238         public void RunLclVarScenario_UnsafeRead()
239         {
240             var left = Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr);
241             var right = Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr);
242             var result = Sse.CompareLessThanUnorderedScalar(left, right);
243
244             ValidateResult(left, right, result);
245         }
246
247         public void RunLclVarScenario_Load()
248         {
249             var left = Sse.LoadVector128((Single*)(_dataTable.inArray1Ptr));
250             var right = Sse.LoadVector128((Single*)(_dataTable.inArray2Ptr));
251             var result = Sse.CompareLessThanUnorderedScalar(left, right);
252
253             ValidateResult(left, right, result);
254         }
255
256         public void RunLclVarScenario_LoadAligned()
257         {
258             var left = Sse.LoadAlignedVector128((Single*)(_dataTable.inArray1Ptr));
259             var right = Sse.LoadAlignedVector128((Single*)(_dataTable.inArray2Ptr));
260             var result = Sse.CompareLessThanUnorderedScalar(left, right);
261
262             ValidateResult(left, right, result);
263         }
264
265         public void RunClassLclFldScenario()
266         {
267             var test = new BooleanComparisonOpTest__CompareLessThanUnorderedScalarBoolean();
268             var result = Sse.CompareLessThanUnorderedScalar(test._fld1, test._fld2);
269
270             ValidateResult(test._fld1, test._fld2, result);
271         }
272
273         public void RunClassFldScenario()
274         {
275             var result = Sse.CompareLessThanUnorderedScalar(_fld1, _fld2);
276
277             ValidateResult(_fld1, _fld2, result);
278         }
279
280         public void RunStructLclFldScenario()
281         {
282             var test = TestStruct.Create();
283             var result = Sse.CompareLessThanUnorderedScalar(test._fld1, test._fld2);
284             ValidateResult(test._fld1, test._fld2, result);
285         }
286
287         public void RunStructFldScenario()
288         {
289             var test = TestStruct.Create();
290             test.RunStructFldScenario(this);
291         }
292
293         public void RunUnsupportedScenario()
294         {
295             Succeeded = false;
296
297             try
298             {
299                 RunBasicScenario_UnsafeRead();
300             }
301             catch (PlatformNotSupportedException)
302             {
303                 Succeeded = true;
304             }
305         }
306
307         private void ValidateResult(Vector128<Single> left, Vector128<Single> right, bool result, [CallerMemberName] string method = "")
308         {
309             Single[] inArray1 = new Single[Op1ElementCount];
310             Single[] inArray2 = new Single[Op2ElementCount];
311
312             Unsafe.WriteUnaligned(ref Unsafe.As<Single, byte>(ref inArray1[0]), left);
313             Unsafe.WriteUnaligned(ref Unsafe.As<Single, byte>(ref inArray2[0]), right);
314
315             ValidateResult(inArray1, inArray2, result, method);
316         }
317
318         private void ValidateResult(void* left, void* right, bool result, [CallerMemberName] string method = "")
319         {
320             Single[] inArray1 = new Single[Op1ElementCount];
321             Single[] inArray2 = new Single[Op2ElementCount];
322
323             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref inArray1[0]), ref Unsafe.AsRef<byte>(left), (uint)Unsafe.SizeOf<Vector128<Single>>());
324             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref inArray2[0]), ref Unsafe.AsRef<byte>(right), (uint)Unsafe.SizeOf<Vector128<Single>>());
325
326             ValidateResult(inArray1, inArray2, result, method);
327         }
328
329         private void ValidateResult(Single[] left, Single[] right, bool result, [CallerMemberName] string method = "")
330         {
331             if ((left[0] < right[0]) != result)
332             {
333                 Succeeded = false;
334
335                 Console.WriteLine($"{nameof(Sse)}.{nameof(Sse.CompareLessThanUnorderedScalar)}<Boolean>(Vector128<Single>, Vector128<Single>): {method} failed:");
336                 Console.WriteLine($"    left: ({string.Join(", ", left)})");
337                 Console.WriteLine($"   right: ({string.Join(", ", right)})");
338                 Console.WriteLine($"  result: ({string.Join(", ", result)})");
339                 Console.WriteLine();
340             }
341         }
342     }
343 }