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 CompareOrderedSingle()
24 var test = new SimpleBinaryOpTest__CompareOrderedSingle();
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 SimpleBinaryOpTest__CompareOrderedSingle
88 private static readonly int LargestVectorSize = 16;
90 private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
91 private static readonly int Op2ElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
92 private static readonly int RetElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
94 private static Single[] _data1 = new Single[Op1ElementCount];
95 private static Single[] _data2 = new Single[Op2ElementCount];
97 private static Vector128<Single> _clsVar1;
98 private static Vector128<Single> _clsVar2;
100 private Vector128<Single> _fld1;
101 private Vector128<Single> _fld2;
103 private SimpleBinaryOpTest__DataTable<Single, Single, Single> _dataTable;
105 static SimpleBinaryOpTest__CompareOrderedSingle()
107 var random = new Random();
109 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); }
110 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar1), ref Unsafe.As<Single, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
111 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (float)(random.NextDouble()); }
112 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar2), ref Unsafe.As<Single, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
115 public SimpleBinaryOpTest__CompareOrderedSingle()
119 var random = new Random();
121 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); }
122 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld1), ref Unsafe.As<Single, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
123 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (float)(random.NextDouble()); }
124 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld2), ref Unsafe.As<Single, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
126 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); }
127 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (float)(random.NextDouble()); }
128 _dataTable = new SimpleBinaryOpTest__DataTable<Single, Single, Single>(_data1, _data2, new Single[RetElementCount], LargestVectorSize);
131 public bool IsSupported => Sse.IsSupported;
133 public bool Succeeded { get; set; }
135 public void RunBasicScenario_UnsafeRead()
137 var result = Sse.CompareOrdered(
138 Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr),
139 Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr)
142 Unsafe.Write(_dataTable.outArrayPtr, result);
143 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
146 public void RunBasicScenario_Load()
148 var result = Sse.CompareOrdered(
149 Sse.LoadVector128((Single*)(_dataTable.inArray1Ptr)),
150 Sse.LoadVector128((Single*)(_dataTable.inArray2Ptr))
153 Unsafe.Write(_dataTable.outArrayPtr, result);
154 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
157 public void RunBasicScenario_LoadAligned()
159 var result = Sse.CompareOrdered(
160 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray1Ptr)),
161 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray2Ptr))
164 Unsafe.Write(_dataTable.outArrayPtr, result);
165 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
168 public void RunReflectionScenario_UnsafeRead()
170 var result = typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
171 .Invoke(null, new object[] {
172 Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr),
173 Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr)
176 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Single>)(result));
177 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
180 public void RunReflectionScenario_Load()
182 var result = typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
183 .Invoke(null, new object[] {
184 Sse.LoadVector128((Single*)(_dataTable.inArray1Ptr)),
185 Sse.LoadVector128((Single*)(_dataTable.inArray2Ptr))
188 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Single>)(result));
189 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
192 public void RunReflectionScenario_LoadAligned()
194 var result = typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
195 .Invoke(null, new object[] {
196 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray1Ptr)),
197 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray2Ptr))
200 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Single>)(result));
201 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
204 public void RunClsVarScenario()
206 var result = Sse.CompareOrdered(
211 Unsafe.Write(_dataTable.outArrayPtr, result);
212 ValidateResult(_clsVar1, _clsVar2, _dataTable.outArrayPtr);
215 public void RunLclVarScenario_UnsafeRead()
217 var left = Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr);
218 var right = Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr);
219 var result = Sse.CompareOrdered(left, right);
221 Unsafe.Write(_dataTable.outArrayPtr, result);
222 ValidateResult(left, right, _dataTable.outArrayPtr);
225 public void RunLclVarScenario_Load()
227 var left = Sse.LoadVector128((Single*)(_dataTable.inArray1Ptr));
228 var right = Sse.LoadVector128((Single*)(_dataTable.inArray2Ptr));
229 var result = Sse.CompareOrdered(left, right);
231 Unsafe.Write(_dataTable.outArrayPtr, result);
232 ValidateResult(left, right, _dataTable.outArrayPtr);
235 public void RunLclVarScenario_LoadAligned()
237 var left = Sse.LoadAlignedVector128((Single*)(_dataTable.inArray1Ptr));
238 var right = Sse.LoadAlignedVector128((Single*)(_dataTable.inArray2Ptr));
239 var result = Sse.CompareOrdered(left, right);
241 Unsafe.Write(_dataTable.outArrayPtr, result);
242 ValidateResult(left, right, _dataTable.outArrayPtr);
245 public void RunLclFldScenario()
247 var test = new SimpleBinaryOpTest__CompareOrderedSingle();
248 var result = Sse.CompareOrdered(test._fld1, test._fld2);
250 Unsafe.Write(_dataTable.outArrayPtr, result);
251 ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
254 public void RunFldScenario()
256 var result = Sse.CompareOrdered(_fld1, _fld2);
258 Unsafe.Write(_dataTable.outArrayPtr, result);
259 ValidateResult(_fld1, _fld2, _dataTable.outArrayPtr);
262 public void RunUnsupportedScenario()
268 RunBasicScenario_UnsafeRead();
270 catch (PlatformNotSupportedException)
276 private void ValidateResult(Vector128<Single> left, Vector128<Single> right, void* result, [CallerMemberName] string method = "")
278 Single[] inArray1 = new Single[Op1ElementCount];
279 Single[] inArray2 = new Single[Op2ElementCount];
280 Single[] outArray = new Single[RetElementCount];
282 Unsafe.WriteUnaligned(ref Unsafe.As<Single, byte>(ref inArray1[0]), left);
283 Unsafe.WriteUnaligned(ref Unsafe.As<Single, byte>(ref inArray2[0]), right);
284 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector128<Single>>());
286 ValidateResult(inArray1, inArray2, outArray, method);
289 private void ValidateResult(void* left, void* right, void* result, [CallerMemberName] string method = "")
291 Single[] inArray1 = new Single[Op1ElementCount];
292 Single[] inArray2 = new Single[Op2ElementCount];
293 Single[] outArray = new Single[RetElementCount];
295 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref inArray1[0]), ref Unsafe.AsRef<byte>(left), (uint)Unsafe.SizeOf<Vector128<Single>>());
296 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref inArray2[0]), ref Unsafe.AsRef<byte>(right), (uint)Unsafe.SizeOf<Vector128<Single>>());
297 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector128<Single>>());
299 ValidateResult(inArray1, inArray2, outArray, method);
302 private void ValidateResult(Single[] left, Single[] right, Single[] result, [CallerMemberName] string method = "")
304 if (BitConverter.SingleToInt32Bits(result[0]) != ((!float.IsNaN(left[0]) && !float.IsNaN(right[0])) ? -1 : 0))
310 for (var i = 1; i < RetElementCount; i++)
312 if (BitConverter.SingleToInt32Bits(result[i]) != ((!float.IsNaN(left[i]) && !float.IsNaN(right[i])) ? -1 : 0))
322 Console.WriteLine($"{nameof(Sse)}.{nameof(Sse.CompareOrdered)}<Single>(Vector128<Single>, Vector128<Single>): {method} failed:");
323 Console.WriteLine($" left: ({string.Join(", ", left)})");
324 Console.WriteLine($" right: ({string.Join(", ", right)})");
325 Console.WriteLine($" result: ({string.Join(", ", result)})");