Remove random.NextDouble() from JIT/HardwareIntrinsics tests (#18838)
[platform/upstream/coreclr.git] / tests / src / JIT / HardwareIntrinsics / X86 / Avx / ExtendToVector256.Single.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 ExtendToVector256Single()
23         {
24             var test = new GenericUnaryOpTest__ExtendToVector256Single();
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 GenericUnaryOpTest__ExtendToVector256Single
93     {
94         private struct TestStruct
95         {
96             public Vector128<Single> _fld;
97
98             public static TestStruct Create()
99             {
100                 var testStruct = new TestStruct();
101                 var random = new Random();
102
103                 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = TestLibrary.Generator.GetSingle(); }
104                 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref testStruct._fld), ref Unsafe.As<Single, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
105
106                 return testStruct;
107             }
108
109             public void RunStructFldScenario(GenericUnaryOpTest__ExtendToVector256Single testClass)
110             {
111                 var result = Avx.ExtendToVector256<Single>(_fld);
112
113                 Unsafe.Write(testClass._dataTable.outArrayPtr, result);
114                 testClass.ValidateResult(_fld, testClass._dataTable.outArrayPtr);
115             }
116         }
117
118         private static readonly int LargestVectorSize = 32;
119
120         private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
121         private static readonly int RetElementCount = Unsafe.SizeOf<Vector256<Single>>() / sizeof(Single);
122
123         private static Single[] _data = new Single[Op1ElementCount];
124
125         private static Vector128<Single> _clsVar;
126
127         private Vector128<Single> _fld;
128
129         private SimpleUnaryOpTest__DataTable<Single, Single> _dataTable;
130
131         static GenericUnaryOpTest__ExtendToVector256Single()
132         {
133             var random = new Random();
134
135             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = TestLibrary.Generator.GetSingle(); }
136             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar), ref Unsafe.As<Single, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
137         }
138
139         public GenericUnaryOpTest__ExtendToVector256Single()
140         {
141             Succeeded = true;
142
143             var random = new Random();
144
145             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = TestLibrary.Generator.GetSingle(); }
146             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld), ref Unsafe.As<Single, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
147
148             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = TestLibrary.Generator.GetSingle(); }
149             _dataTable = new SimpleUnaryOpTest__DataTable<Single, Single>(_data, new Single[RetElementCount], LargestVectorSize);
150         }
151
152         public bool IsSupported => Avx.IsSupported;
153
154         public bool Succeeded { get; set; }
155
156         public void RunBasicScenario_UnsafeRead()
157         {
158             var result = Avx.ExtendToVector256<Single>(
159                 Unsafe.Read<Vector128<Single>>(_dataTable.inArrayPtr)
160             );
161
162             Unsafe.Write(_dataTable.outArrayPtr, result);
163             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
164         }
165
166         public void RunBasicScenario_Load()
167         {
168             var result = Avx.ExtendToVector256<Single>(
169                 Sse.LoadVector128((Single*)(_dataTable.inArrayPtr))
170             );
171
172             Unsafe.Write(_dataTable.outArrayPtr, result);
173             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
174         }
175
176         public void RunBasicScenario_LoadAligned()
177         {
178             var result = Avx.ExtendToVector256<Single>(
179                 Sse.LoadAlignedVector128((Single*)(_dataTable.inArrayPtr))
180             );
181
182             Unsafe.Write(_dataTable.outArrayPtr, result);
183             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
184         }
185
186         public void RunReflectionScenario_UnsafeRead()
187         {
188             var result = typeof(Avx).GetMethod(nameof(Avx.ExtendToVector256))
189                                      .MakeGenericMethod( new Type[] { typeof(Single) })
190                                      .Invoke(null, new object[] {
191                                         Unsafe.Read<Vector128<Single>>(_dataTable.inArrayPtr)
192                                      });
193
194             Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Single>)(result));
195             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
196         }
197
198         public void RunReflectionScenario_Load()
199         {
200             var result = typeof(Avx).GetMethod(nameof(Avx.ExtendToVector256))
201                                      .MakeGenericMethod( new Type[] { typeof(Single) })
202                                      .Invoke(null, new object[] {
203                                         Sse.LoadVector128((Single*)(_dataTable.inArrayPtr))
204                                      });
205
206             Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Single>)(result));
207             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
208         }
209
210         public void RunReflectionScenario_LoadAligned()
211         {
212             var result = typeof(Avx).GetMethod(nameof(Avx.ExtendToVector256))
213                                      .MakeGenericMethod( new Type[] { typeof(Single) })
214                                      .Invoke(null, new object[] {
215                                         Sse.LoadAlignedVector128((Single*)(_dataTable.inArrayPtr))
216                                      });
217
218             Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Single>)(result));
219             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
220         }
221
222         public void RunClsVarScenario()
223         {
224             var result = Avx.ExtendToVector256<Single>(
225                 _clsVar
226             );
227
228             Unsafe.Write(_dataTable.outArrayPtr, result);
229             ValidateResult(_clsVar, _dataTable.outArrayPtr);
230         }
231
232         public void RunLclVarScenario_UnsafeRead()
233         {
234             var firstOp = Unsafe.Read<Vector128<Single>>(_dataTable.inArrayPtr);
235             var result = Avx.ExtendToVector256<Single>(firstOp);
236
237             Unsafe.Write(_dataTable.outArrayPtr, result);
238             ValidateResult(firstOp, _dataTable.outArrayPtr);
239         }
240
241         public void RunLclVarScenario_Load()
242         {
243             var firstOp = Sse.LoadVector128((Single*)(_dataTable.inArrayPtr));
244             var result = Avx.ExtendToVector256<Single>(firstOp);
245
246             Unsafe.Write(_dataTable.outArrayPtr, result);
247             ValidateResult(firstOp, _dataTable.outArrayPtr);
248         }
249
250         public void RunLclVarScenario_LoadAligned()
251         {
252             var firstOp = Sse.LoadAlignedVector128((Single*)(_dataTable.inArrayPtr));
253             var result = Avx.ExtendToVector256<Single>(firstOp);
254
255             Unsafe.Write(_dataTable.outArrayPtr, result);
256             ValidateResult(firstOp, _dataTable.outArrayPtr);
257         }
258
259         public void RunClassLclFldScenario()
260         {
261             var test = new GenericUnaryOpTest__ExtendToVector256Single();
262             var result = Avx.ExtendToVector256<Single>(test._fld);
263
264             Unsafe.Write(_dataTable.outArrayPtr, result);
265             ValidateResult(test._fld, _dataTable.outArrayPtr);
266         }
267
268         public void RunClassFldScenario()
269         {
270             var result = Avx.ExtendToVector256<Single>(_fld);
271
272             Unsafe.Write(_dataTable.outArrayPtr, result);
273             ValidateResult(_fld, _dataTable.outArrayPtr);
274         }
275
276         public void RunStructLclFldScenario()
277         {
278             var test = TestStruct.Create();
279             var result = Avx.ExtendToVector256(test._fld);
280
281             Unsafe.Write(_dataTable.outArrayPtr, result);
282             ValidateResult(test._fld, _dataTable.outArrayPtr);
283         }
284
285         public void RunStructFldScenario()
286         {
287             var test = TestStruct.Create();
288             test.RunStructFldScenario(this);
289         }
290
291         public void RunUnsupportedScenario()
292         {
293             Succeeded = false;
294
295             try
296             {
297                 RunBasicScenario_UnsafeRead();
298             }
299             catch (PlatformNotSupportedException)
300             {
301                 Succeeded = true;
302             }
303         }
304
305         private void ValidateResult(Vector128<Single> firstOp, void* result, [CallerMemberName] string method = "")
306         {
307             Single[] inArray = new Single[Op1ElementCount];
308             Single[] outArray = new Single[RetElementCount];
309
310             Unsafe.WriteUnaligned(ref Unsafe.As<Single, byte>(ref inArray[0]), firstOp);
311             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<Single>>());
312
313             ValidateResult(inArray, outArray, method);
314         }
315
316         private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
317         {
318             Single[] inArray = new Single[Op1ElementCount];
319             Single[] outArray = new Single[RetElementCount];
320
321             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), (uint)Unsafe.SizeOf<Vector128<Single>>());
322             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<Single>>());
323
324             ValidateResult(inArray, outArray, method);
325         }
326
327         private void ValidateResult(Single[] firstOp, Single[] result, [CallerMemberName] string method = "")
328         {
329             if (firstOp[0] != result[0])
330             {
331                 Succeeded = false;
332             }
333             else
334             {
335                 for (var i = 1; i < RetElementCount; i++)
336                 {
337                     if (result[i] != ((i < (RetElementCount / 2)) ? firstOp[i] : 0))
338                     {
339                         Succeeded = false;
340                         break;
341                     }
342                 }
343             }
344
345             if (!Succeeded)
346             {
347                 Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.ExtendToVector256)}<Single>(Vector128<Single>): {method} failed:");
348                 Console.WriteLine($"  firstOp: ({string.Join(", ", firstOp)})");
349                 Console.WriteLine($"   result: ({string.Join(", ", result)})");
350                 Console.WriteLine();
351             }
352         }
353     }
354 }