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
29 test.RunBasicScenario();
31 // Validates calling via reflection works
32 test.RunReflectionScenario();
34 // Validates passing a static member works
35 test.RunClsVarScenario();
37 // Validates passing a local works
38 test.RunLclVarScenario();
40 // Validates passing the field of a local works
41 test.RunLclFldScenario();
43 // Validates passing an instance member works
44 test.RunFldScenario();
48 // Validates we throw on unsupported hardware
49 test.RunUnsupportedScenario();
54 throw new Exception("One or more scenarios did not complete as expected.");
59 public sealed unsafe class SimpleBinaryOpTest__CompareOrderedSingle
61 private const int VectorSize = 16;
62 private const int ElementCount = VectorSize / sizeof(Single);
64 private static Single[] _data1 = new Single[ElementCount];
65 private static Single[] _data2 = new Single[ElementCount];
67 private static Vector128<Single> _clsVar1;
68 private static Vector128<Single> _clsVar2;
70 private Vector128<Single> _fld1;
71 private Vector128<Single> _fld2;
73 private SimpleBinaryOpTest__DataTable<Single> _dataTable;
75 static SimpleBinaryOpTest__CompareOrderedSingle()
77 var random = new Random();
79 for (var i = 0; i < ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); _data2[i] = (float)(random.NextDouble()); }
80 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar1), ref Unsafe.As<Single, byte>(ref _data2[0]), VectorSize);
81 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar2), ref Unsafe.As<Single, byte>(ref _data1[0]), VectorSize);
84 public SimpleBinaryOpTest__CompareOrderedSingle()
88 var random = new Random();
90 for (var i = 0; i < ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); _data2[i] = (float)(random.NextDouble()); }
91 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld1), ref Unsafe.As<Single, byte>(ref _data1[0]), VectorSize);
92 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld2), ref Unsafe.As<Single, byte>(ref _data2[0]), VectorSize);
94 for (var i = 0; i < ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); _data2[i] = (float)(random.NextDouble()); }
95 _dataTable = new SimpleBinaryOpTest__DataTable<Single>(_data1, _data2, new Single[ElementCount]);
98 public bool IsSupported => Sse.IsSupported;
100 public bool Succeeded { get; set; }
102 public void RunBasicScenario()
104 var result = Sse.CompareOrdered(
105 Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr),
106 Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr)
109 Unsafe.Write(_dataTable.outArrayPtr, result);
110 ValidateResult(_dataTable.inArray1, _dataTable.inArray2, _dataTable.outArray);
113 public void RunReflectionScenario()
115 var result = typeof(Sse).GetMethod(nameof(Sse.CompareOrdered), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
116 .Invoke(null, new object[] {
117 Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr),
118 Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr)
121 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Single>)(result));
122 ValidateResult(_dataTable.inArray1, _dataTable.inArray2, _dataTable.outArray);
125 public void RunClsVarScenario()
127 var result = Sse.CompareOrdered(
132 Unsafe.Write(_dataTable.outArrayPtr, result);
133 ValidateResult(_clsVar1, _clsVar2, _dataTable.outArray);
136 public void RunLclVarScenario()
138 var left = Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr);
139 var right = Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr);
140 var result = Sse.CompareOrdered(left, right);
142 Unsafe.Write(_dataTable.outArrayPtr, result);
143 ValidateResult(left, right, _dataTable.outArray);
146 public void RunLclFldScenario()
148 var test = new SimpleBinaryOpTest__CompareOrderedSingle();
149 var result = Sse.CompareOrdered(test._fld1, test._fld2);
151 Unsafe.Write(_dataTable.outArrayPtr, result);
152 ValidateResult(test._fld1, test._fld2, _dataTable.outArray);
155 public void RunFldScenario()
157 var result = Sse.CompareOrdered(_fld1, _fld2);
159 Unsafe.Write(_dataTable.outArrayPtr, result);
160 ValidateResult(_fld1, _fld2, _dataTable.outArray);
163 public void RunUnsupportedScenario()
171 catch (PlatformNotSupportedException)
177 private void ValidateResult(Vector128<Single> left, Vector128<Single> right, Single[] result, [CallerMemberName] string method = "")
179 Single[] inArray1 = new Single[ElementCount];
180 Single[] inArray2 = new Single[ElementCount];
182 Unsafe.Write(Unsafe.AsPointer(ref inArray1[0]), left);
183 Unsafe.Write(Unsafe.AsPointer(ref inArray2[0]), right);
185 ValidateResult(inArray1, inArray2, result, method);
188 private void ValidateResult(Single[] left, Single[] right, Single[] result, [CallerMemberName] string method = "")
190 if (BitConverter.SingleToInt32Bits(result[0]) != ((!float.IsNaN(left[0]) && !float.IsNaN(right[0])) ? -1 : 0))
196 for (var i = 1; i < left.Length; i++)
198 if (BitConverter.SingleToInt32Bits(result[i]) != ((!float.IsNaN(left[i]) && !float.IsNaN(right[i])) ? -1 : 0))
208 Console.WriteLine($"{nameof(Sse)}.{nameof(Sse.CompareOrdered)}<Single>: {method} failed:");
209 Console.WriteLine($" left: ({string.Join(", ", left)})");
210 Console.WriteLine($" right: ({string.Join(", ", right)})");
211 Console.WriteLine($" result: ({string.Join(", ", result)})");