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 TestMixOnesZerosUInt32()
24 var test = new BooleanTwoComparisonOpTest__TestMixOnesZerosUInt32();
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 BooleanTwoComparisonOpTest__TestMixOnesZerosUInt32
88 private const int VectorSize = 16;
90 private const int Op1ElementCount = VectorSize / sizeof(UInt32);
91 private const int Op2ElementCount = VectorSize / sizeof(UInt32);
93 private static UInt32[] _data1 = new UInt32[Op1ElementCount];
94 private static UInt32[] _data2 = new UInt32[Op2ElementCount];
96 private static Vector128<UInt32> _clsVar1;
97 private static Vector128<UInt32> _clsVar2;
99 private Vector128<UInt32> _fld1;
100 private Vector128<UInt32> _fld2;
102 private BooleanTwoComparisonOpTest__DataTable<UInt32, UInt32> _dataTable;
104 static BooleanTwoComparisonOpTest__TestMixOnesZerosUInt32()
106 var random = new Random();
108 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (uint)(random.Next(0, int.MaxValue)); }
109 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt32>, byte>(ref _clsVar1), ref Unsafe.As<UInt32, byte>(ref _data1[0]), VectorSize);
110 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (uint)(random.Next(0, int.MaxValue)); }
111 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt32>, byte>(ref _clsVar2), ref Unsafe.As<UInt32, byte>(ref _data2[0]), VectorSize);
114 public BooleanTwoComparisonOpTest__TestMixOnesZerosUInt32()
118 var random = new Random();
120 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (uint)(random.Next(0, int.MaxValue)); }
121 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt32>, byte>(ref _fld1), ref Unsafe.As<UInt32, byte>(ref _data1[0]), VectorSize);
122 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (uint)(random.Next(0, int.MaxValue)); }
123 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt32>, byte>(ref _fld2), ref Unsafe.As<UInt32, byte>(ref _data2[0]), VectorSize);
125 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (uint)(random.Next(0, int.MaxValue)); }
126 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (uint)(random.Next(0, int.MaxValue)); }
127 _dataTable = new BooleanTwoComparisonOpTest__DataTable<UInt32, UInt32>(_data1, _data2, VectorSize);
130 public bool IsSupported => Sse41.IsSupported;
132 public bool Succeeded { get; set; }
134 public void RunBasicScenario_UnsafeRead()
136 var result = Sse41.TestMixOnesZeros(
137 Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray1Ptr),
138 Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray2Ptr)
141 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, result);
144 public void RunBasicScenario_Load()
146 var result = Sse41.TestMixOnesZeros(
147 Sse2.LoadVector128((UInt32*)(_dataTable.inArray1Ptr)),
148 Sse2.LoadVector128((UInt32*)(_dataTable.inArray2Ptr))
151 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, result);
154 public void RunBasicScenario_LoadAligned()
156 var result = Sse41.TestMixOnesZeros(
157 Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray1Ptr)),
158 Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray2Ptr))
161 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, result);
164 public void RunReflectionScenario_UnsafeRead()
166 var method = typeof(Sse41).GetMethod(nameof(Sse41.TestMixOnesZeros), new Type[] { typeof(Vector128<UInt32>), typeof(Vector128<UInt32>) });
170 var result = method.Invoke(null, new object[] {
171 Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray1Ptr),
172 Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray2Ptr)
175 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, (bool)(result));
179 public void RunReflectionScenario_Load()
181 var method = typeof(Sse41).GetMethod(nameof(Sse41.TestMixOnesZeros), new Type[] { typeof(Vector128<UInt32>), typeof(Vector128<UInt32>) });
185 var result = method.Invoke(null, new object[] {
186 Sse2.LoadVector128((UInt32*)(_dataTable.inArray1Ptr)),
187 Sse2.LoadVector128((UInt32*)(_dataTable.inArray2Ptr))
190 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, (bool)(result));
194 public void RunReflectionScenario_LoadAligned()
195 {var method = typeof(Sse41).GetMethod(nameof(Sse41.TestMixOnesZeros), new Type[] { typeof(Vector128<UInt32>), typeof(Vector128<UInt32>) });
199 var result = method.Invoke(null, new object[] {
200 Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray1Ptr)),
201 Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray2Ptr))
204 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, (bool)(result));
208 public void RunClsVarScenario()
210 var result = Sse41.TestMixOnesZeros(
215 ValidateResult(_clsVar1, _clsVar2, result);
218 public void RunLclVarScenario_UnsafeRead()
220 var left = Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray1Ptr);
221 var right = Unsafe.Read<Vector128<UInt32>>(_dataTable.inArray2Ptr);
222 var result = Sse41.TestMixOnesZeros(left, right);
224 ValidateResult(left, right, result);
227 public void RunLclVarScenario_Load()
229 var left = Sse2.LoadVector128((UInt32*)(_dataTable.inArray1Ptr));
230 var right = Sse2.LoadVector128((UInt32*)(_dataTable.inArray2Ptr));
231 var result = Sse41.TestMixOnesZeros(left, right);
233 ValidateResult(left, right, result);
236 public void RunLclVarScenario_LoadAligned()
238 var left = Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray1Ptr));
239 var right = Sse2.LoadAlignedVector128((UInt32*)(_dataTable.inArray2Ptr));
240 var result = Sse41.TestMixOnesZeros(left, right);
242 ValidateResult(left, right, result);
245 public void RunLclFldScenario()
247 var test = new BooleanTwoComparisonOpTest__TestMixOnesZerosUInt32();
248 var result = Sse41.TestMixOnesZeros(test._fld1, test._fld2);
250 ValidateResult(test._fld1, test._fld2, result);
253 public void RunFldScenario()
255 var result = Sse41.TestMixOnesZeros(_fld1, _fld2);
257 ValidateResult(_fld1, _fld2, result);
260 public void RunUnsupportedScenario()
266 RunBasicScenario_UnsafeRead();
268 catch (PlatformNotSupportedException)
274 private void ValidateResult(Vector128<UInt32> left, Vector128<UInt32> right, bool result, [CallerMemberName] string method = "")
276 UInt32[] inArray1 = new UInt32[Op1ElementCount];
277 UInt32[] inArray2 = new UInt32[Op2ElementCount];
279 Unsafe.WriteUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray1[0]), left);
280 Unsafe.WriteUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray2[0]), right);
282 ValidateResult(inArray1, inArray2, result, method);
285 private void ValidateResult(void* left, void* right, bool result, [CallerMemberName] string method = "")
287 UInt32[] inArray1 = new UInt32[Op1ElementCount];
288 UInt32[] inArray2 = new UInt32[Op2ElementCount];
290 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray1[0]), ref Unsafe.AsRef<byte>(left), VectorSize);
291 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray2[0]), ref Unsafe.AsRef<byte>(right), VectorSize);
293 ValidateResult(inArray1, inArray2, result, method);
296 private void ValidateResult(UInt32[] left, UInt32[] right, bool result, [CallerMemberName] string method = "")
298 var expectedResult1 = true;
300 for (var i = 0; i < Op1ElementCount; i++)
302 expectedResult1 &= (((left[i] & right[i]) == 0));
305 var expectedResult2 = true;
307 for (var i = 0; i < Op1ElementCount; i++)
309 expectedResult2 &= (((~left[i] & right[i]) == 0));
312 if (((expectedResult1 == false) && (expectedResult2 == false)) != result)
316 Console.WriteLine($"{nameof(Sse41)}.{nameof(Sse41.TestMixOnesZeros)}<UInt32>(Vector128<UInt32>, Vector128<UInt32>): {method} failed:");
317 Console.WriteLine($" left: ({string.Join(", ", left)})");
318 Console.WriteLine($" right: ({string.Join(", ", right)})");
319 Console.WriteLine($" result: ({string.Join(", ", result)})");