d53b365b33914419aa4660994d6feba5d4255447
[platform/upstream/coreclr.git] / tests / src / JIT / HardwareIntrinsics / X86 / Avx / RoundToPositiveInfinity.Double.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 RoundToPositiveInfinityDouble()
23         {
24             var test = new SimpleUnaryOpTest__RoundToPositiveInfinityDouble();
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__RoundToPositiveInfinityDouble
87     {
88         private const int VectorSize = 32;
89
90         private const int Op1ElementCount = VectorSize / sizeof(Double);
91         private const int RetElementCount = VectorSize / sizeof(Double);
92
93         private static Double[] _data = new Double[Op1ElementCount];
94
95         private static Vector256<Double> _clsVar;
96
97         private Vector256<Double> _fld;
98
99         private SimpleUnaryOpTest__DataTable<Double, Double> _dataTable;
100
101         static SimpleUnaryOpTest__RoundToPositiveInfinityDouble()
102         {
103             var random = new Random();
104
105             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (double)(random.NextDouble()); }
106             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Double>, byte>(ref _clsVar), ref Unsafe.As<Double, byte>(ref _data[0]), VectorSize);
107         }
108
109         public SimpleUnaryOpTest__RoundToPositiveInfinityDouble()
110         {
111             Succeeded = true;
112
113             var random = new Random();
114
115             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (double)(random.NextDouble()); }
116             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Double>, byte>(ref _fld), ref Unsafe.As<Double, byte>(ref _data[0]), VectorSize);
117
118             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (double)(random.NextDouble()); }
119             _dataTable = new SimpleUnaryOpTest__DataTable<Double, Double>(_data, new Double[RetElementCount], VectorSize);
120         }
121
122         public bool IsSupported => Avx.IsSupported;
123
124         public bool Succeeded { get; set; }
125
126         public void RunBasicScenario_UnsafeRead()
127         {
128             var result = Avx.RoundToPositiveInfinity(
129                 Unsafe.Read<Vector256<Double>>(_dataTable.inArrayPtr)
130             );
131
132             Unsafe.Write(_dataTable.outArrayPtr, result);
133             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
134         }
135
136         public void RunBasicScenario_Load()
137         {
138             var result = Avx.RoundToPositiveInfinity(
139                 Avx.LoadVector256((Double*)(_dataTable.inArrayPtr))
140             );
141
142             Unsafe.Write(_dataTable.outArrayPtr, result);
143             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
144         }
145
146         public void RunBasicScenario_LoadAligned()
147         {
148             var result = Avx.RoundToPositiveInfinity(
149                 Avx.LoadAlignedVector256((Double*)(_dataTable.inArrayPtr))
150             );
151
152             Unsafe.Write(_dataTable.outArrayPtr, result);
153             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
154         }
155
156         public void RunReflectionScenario_UnsafeRead()
157         {
158             var result = typeof(Avx).GetMethod(nameof(Avx.RoundToPositiveInfinity), new Type[] { typeof(Vector256<Double>) })
159                                      .Invoke(null, new object[] {
160                                         Unsafe.Read<Vector256<Double>>(_dataTable.inArrayPtr)
161                                      });
162
163             Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Double>)(result));
164             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
165         }
166
167         public void RunReflectionScenario_Load()
168         {
169             var result = typeof(Avx).GetMethod(nameof(Avx.RoundToPositiveInfinity), new Type[] { typeof(Vector256<Double>) })
170                                      .Invoke(null, new object[] {
171                                         Avx.LoadVector256((Double*)(_dataTable.inArrayPtr))
172                                      });
173
174             Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Double>)(result));
175             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
176         }
177
178         public void RunReflectionScenario_LoadAligned()
179         {
180             var result = typeof(Avx).GetMethod(nameof(Avx.RoundToPositiveInfinity), new Type[] { typeof(Vector256<Double>) })
181                                      .Invoke(null, new object[] {
182                                         Avx.LoadAlignedVector256((Double*)(_dataTable.inArrayPtr))
183                                      });
184
185             Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Double>)(result));
186             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
187         }
188
189         public void RunClsVarScenario()
190         {
191             var result = Avx.RoundToPositiveInfinity(
192                 _clsVar
193             );
194
195             Unsafe.Write(_dataTable.outArrayPtr, result);
196             ValidateResult(_clsVar, _dataTable.outArrayPtr);
197         }
198
199         public void RunLclVarScenario_UnsafeRead()
200         {
201             var firstOp = Unsafe.Read<Vector256<Double>>(_dataTable.inArrayPtr);
202             var result = Avx.RoundToPositiveInfinity(firstOp);
203
204             Unsafe.Write(_dataTable.outArrayPtr, result);
205             ValidateResult(firstOp, _dataTable.outArrayPtr);
206         }
207
208         public void RunLclVarScenario_Load()
209         {
210             var firstOp = Avx.LoadVector256((Double*)(_dataTable.inArrayPtr));
211             var result = Avx.RoundToPositiveInfinity(firstOp);
212
213             Unsafe.Write(_dataTable.outArrayPtr, result);
214             ValidateResult(firstOp, _dataTable.outArrayPtr);
215         }
216
217         public void RunLclVarScenario_LoadAligned()
218         {
219             var firstOp = Avx.LoadAlignedVector256((Double*)(_dataTable.inArrayPtr));
220             var result = Avx.RoundToPositiveInfinity(firstOp);
221
222             Unsafe.Write(_dataTable.outArrayPtr, result);
223             ValidateResult(firstOp, _dataTable.outArrayPtr);
224         }
225
226         public void RunLclFldScenario()
227         {
228             var test = new SimpleUnaryOpTest__RoundToPositiveInfinityDouble();
229             var result = Avx.RoundToPositiveInfinity(test._fld);
230
231             Unsafe.Write(_dataTable.outArrayPtr, result);
232             ValidateResult(test._fld, _dataTable.outArrayPtr);
233         }
234
235         public void RunFldScenario()
236         {
237             var result = Avx.RoundToPositiveInfinity(_fld);
238
239             Unsafe.Write(_dataTable.outArrayPtr, result);
240             ValidateResult(_fld, _dataTable.outArrayPtr);
241         }
242
243         public void RunUnsupportedScenario()
244         {
245             Succeeded = false;
246
247             try
248             {
249                 RunBasicScenario_UnsafeRead();
250             }
251             catch (PlatformNotSupportedException)
252             {
253                 Succeeded = true;
254             }
255         }
256
257         private void ValidateResult(Vector256<Double> firstOp, void* result, [CallerMemberName] string method = "")
258         {
259             Double[] inArray = new Double[Op1ElementCount];
260             Double[] outArray = new Double[RetElementCount];
261
262             Unsafe.Write(Unsafe.AsPointer(ref inArray[0]), firstOp);
263             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Double, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
264
265             ValidateResult(inArray, outArray, method);
266         }
267
268         private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
269         {
270             Double[] inArray = new Double[Op1ElementCount];
271             Double[] outArray = new Double[RetElementCount];
272
273             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Double, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), VectorSize);
274             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Double, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
275
276             ValidateResult(inArray, outArray, method);
277         }
278
279         private void ValidateResult(Double[] firstOp, Double[] result, [CallerMemberName] string method = "")
280         {
281             if (BitConverter.DoubleToInt64Bits(result[0]) != BitConverter.DoubleToInt64Bits(Math.Ceiling(firstOp[0])))
282             {
283                 Succeeded = false;
284             }
285             else
286             {
287                 for (var i = 1; i < RetElementCount; i++)
288                 {
289                     if (BitConverter.DoubleToInt64Bits(result[i]) != BitConverter.DoubleToInt64Bits(Math.Ceiling(firstOp[i])))
290                     {
291                         Succeeded = false;
292                         break;
293                     }
294                 }
295             }
296
297             if (!Succeeded)
298             {
299                 Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.RoundToPositiveInfinity)}<Double>(Vector256<Double>): {method} failed:");
300                 Console.WriteLine($"  firstOp: ({string.Join(", ", firstOp)})");
301                 Console.WriteLine($"   result: ({string.Join(", ", result)})");
302                 Console.WriteLine();
303             }
304         }
305     }
306 }