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.
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 ******************************************************************************/
13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
15 using System.Runtime.Intrinsics;
16 using System.Runtime.Intrinsics.X86;
18 namespace JIT.HardwareIntrinsics.X86
20 public static partial class Program
22 private static void RoundToPositiveInfinityDouble()
24 var test = new SimpleUnaryOpTest__RoundToPositiveInfinityDouble();
28 // Validates basic functionality works, using Unsafe.Read
29 test.RunBasicScenario_UnsafeRead();
33 // Validates basic functionality works, using Load
34 test.RunBasicScenario_Load();
36 // Validates basic functionality works, using LoadAligned
37 test.RunBasicScenario_LoadAligned();
40 // Validates calling via reflection works, using Unsafe.Read
41 test.RunReflectionScenario_UnsafeRead();
45 // Validates calling via reflection works, using Load
46 test.RunReflectionScenario_Load();
48 // Validates calling via reflection works, using LoadAligned
49 test.RunReflectionScenario_LoadAligned();
52 // Validates passing a static member works
53 test.RunClsVarScenario();
55 // Validates passing a local works, using Unsafe.Read
56 test.RunLclVarScenario_UnsafeRead();
60 // Validates passing a local works, using Load
61 test.RunLclVarScenario_Load();
63 // Validates passing a local works, using LoadAligned
64 test.RunLclVarScenario_LoadAligned();
67 // Validates passing the field of a local works
68 test.RunLclFldScenario();
70 // Validates passing an instance member works
71 test.RunFldScenario();
75 // Validates we throw on unsupported hardware
76 test.RunUnsupportedScenario();
81 throw new Exception("One or more scenarios did not complete as expected.");
86 public sealed unsafe class SimpleUnaryOpTest__RoundToPositiveInfinityDouble
88 private const int VectorSize = 32;
90 private const int Op1ElementCount = VectorSize / sizeof(Double);
91 private const int RetElementCount = VectorSize / sizeof(Double);
93 private static Double[] _data = new Double[Op1ElementCount];
95 private static Vector256<Double> _clsVar;
97 private Vector256<Double> _fld;
99 private SimpleUnaryOpTest__DataTable<Double, Double> _dataTable;
101 static SimpleUnaryOpTest__RoundToPositiveInfinityDouble()
103 var random = new Random();
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);
109 public SimpleUnaryOpTest__RoundToPositiveInfinityDouble()
113 var random = new Random();
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);
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);
122 public bool IsSupported => Avx.IsSupported;
124 public bool Succeeded { get; set; }
126 public void RunBasicScenario_UnsafeRead()
128 var result = Avx.RoundToPositiveInfinity(
129 Unsafe.Read<Vector256<Double>>(_dataTable.inArrayPtr)
132 Unsafe.Write(_dataTable.outArrayPtr, result);
133 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
136 public void RunBasicScenario_Load()
138 var result = Avx.RoundToPositiveInfinity(
139 Avx.LoadVector256((Double*)(_dataTable.inArrayPtr))
142 Unsafe.Write(_dataTable.outArrayPtr, result);
143 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
146 public void RunBasicScenario_LoadAligned()
148 var result = Avx.RoundToPositiveInfinity(
149 Avx.LoadAlignedVector256((Double*)(_dataTable.inArrayPtr))
152 Unsafe.Write(_dataTable.outArrayPtr, result);
153 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
156 public void RunReflectionScenario_UnsafeRead()
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)
163 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Double>)(result));
164 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
167 public void RunReflectionScenario_Load()
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))
174 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Double>)(result));
175 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
178 public void RunReflectionScenario_LoadAligned()
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))
185 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Double>)(result));
186 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
189 public void RunClsVarScenario()
191 var result = Avx.RoundToPositiveInfinity(
195 Unsafe.Write(_dataTable.outArrayPtr, result);
196 ValidateResult(_clsVar, _dataTable.outArrayPtr);
199 public void RunLclVarScenario_UnsafeRead()
201 var firstOp = Unsafe.Read<Vector256<Double>>(_dataTable.inArrayPtr);
202 var result = Avx.RoundToPositiveInfinity(firstOp);
204 Unsafe.Write(_dataTable.outArrayPtr, result);
205 ValidateResult(firstOp, _dataTable.outArrayPtr);
208 public void RunLclVarScenario_Load()
210 var firstOp = Avx.LoadVector256((Double*)(_dataTable.inArrayPtr));
211 var result = Avx.RoundToPositiveInfinity(firstOp);
213 Unsafe.Write(_dataTable.outArrayPtr, result);
214 ValidateResult(firstOp, _dataTable.outArrayPtr);
217 public void RunLclVarScenario_LoadAligned()
219 var firstOp = Avx.LoadAlignedVector256((Double*)(_dataTable.inArrayPtr));
220 var result = Avx.RoundToPositiveInfinity(firstOp);
222 Unsafe.Write(_dataTable.outArrayPtr, result);
223 ValidateResult(firstOp, _dataTable.outArrayPtr);
226 public void RunLclFldScenario()
228 var test = new SimpleUnaryOpTest__RoundToPositiveInfinityDouble();
229 var result = Avx.RoundToPositiveInfinity(test._fld);
231 Unsafe.Write(_dataTable.outArrayPtr, result);
232 ValidateResult(test._fld, _dataTable.outArrayPtr);
235 public void RunFldScenario()
237 var result = Avx.RoundToPositiveInfinity(_fld);
239 Unsafe.Write(_dataTable.outArrayPtr, result);
240 ValidateResult(_fld, _dataTable.outArrayPtr);
243 public void RunUnsupportedScenario()
249 RunBasicScenario_UnsafeRead();
251 catch (PlatformNotSupportedException)
257 private void ValidateResult(Vector256<Double> firstOp, void* result, [CallerMemberName] string method = "")
259 Double[] inArray = new Double[Op1ElementCount];
260 Double[] outArray = new Double[RetElementCount];
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);
265 ValidateResult(inArray, outArray, method);
268 private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
270 Double[] inArray = new Double[Op1ElementCount];
271 Double[] outArray = new Double[RetElementCount];
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);
276 ValidateResult(inArray, outArray, method);
279 private void ValidateResult(Double[] firstOp, Double[] result, [CallerMemberName] string method = "")
281 if (BitConverter.DoubleToInt64Bits(result[0]) != BitConverter.DoubleToInt64Bits(Math.Ceiling(firstOp[0])))
287 for (var i = 1; i < RetElementCount; i++)
289 if (BitConverter.DoubleToInt64Bits(result[i]) != BitConverter.DoubleToInt64Bits(Math.Ceiling(firstOp[i])))
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)})");