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 AndSingle()
24 var test = new SimpleBinaryOpTest__AndSingle();
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 class works
68 test.RunClassLclFldScenario();
70 // Validates passing an instance member of a class works
71 test.RunClassFldScenario();
73 // Validates passing the field of a local struct works
74 test.RunStructLclFldScenario();
76 // Validates passing an instance member of a struct works
77 test.RunStructFldScenario();
81 // Validates we throw on unsupported hardware
82 test.RunUnsupportedScenario();
87 throw new Exception("One or more scenarios did not complete as expected.");
92 public sealed unsafe class SimpleBinaryOpTest__AndSingle
94 private struct TestStruct
96 public Vector128<Single> _fld1;
97 public Vector128<Single> _fld2;
99 public static TestStruct Create()
101 var testStruct = new TestStruct();
102 var random = new Random();
104 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); }
105 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref testStruct._fld1), ref Unsafe.As<Single, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
106 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (float)(random.NextDouble()); }
107 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref testStruct._fld2), ref Unsafe.As<Single, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
112 public void RunStructFldScenario(SimpleBinaryOpTest__AndSingle testClass)
114 var result = Sse.And(_fld1, _fld2);
116 Unsafe.Write(testClass._dataTable.outArrayPtr, result);
117 testClass.ValidateResult(_fld1, _fld2, testClass._dataTable.outArrayPtr);
121 private static readonly int LargestVectorSize = 16;
123 private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
124 private static readonly int Op2ElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
125 private static readonly int RetElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
127 private static Single[] _data1 = new Single[Op1ElementCount];
128 private static Single[] _data2 = new Single[Op2ElementCount];
130 private static Vector128<Single> _clsVar1;
131 private static Vector128<Single> _clsVar2;
133 private Vector128<Single> _fld1;
134 private Vector128<Single> _fld2;
136 private SimpleBinaryOpTest__DataTable<Single, Single, Single> _dataTable;
138 static SimpleBinaryOpTest__AndSingle()
140 var random = new Random();
142 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); }
143 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar1), ref Unsafe.As<Single, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
144 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (float)(random.NextDouble()); }
145 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar2), ref Unsafe.As<Single, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
148 public SimpleBinaryOpTest__AndSingle()
152 var random = new Random();
154 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); }
155 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld1), ref Unsafe.As<Single, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
156 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (float)(random.NextDouble()); }
157 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld2), ref Unsafe.As<Single, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
159 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (float)(random.NextDouble()); }
160 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (float)(random.NextDouble()); }
161 _dataTable = new SimpleBinaryOpTest__DataTable<Single, Single, Single>(_data1, _data2, new Single[RetElementCount], LargestVectorSize);
164 public bool IsSupported => Sse.IsSupported;
166 public bool Succeeded { get; set; }
168 public void RunBasicScenario_UnsafeRead()
170 var result = Sse.And(
171 Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr),
172 Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr)
175 Unsafe.Write(_dataTable.outArrayPtr, result);
176 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
179 public void RunBasicScenario_Load()
181 var result = Sse.And(
182 Sse.LoadVector128((Single*)(_dataTable.inArray1Ptr)),
183 Sse.LoadVector128((Single*)(_dataTable.inArray2Ptr))
186 Unsafe.Write(_dataTable.outArrayPtr, result);
187 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
190 public void RunBasicScenario_LoadAligned()
192 var result = Sse.And(
193 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray1Ptr)),
194 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray2Ptr))
197 Unsafe.Write(_dataTable.outArrayPtr, result);
198 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
201 public void RunReflectionScenario_UnsafeRead()
203 var result = typeof(Sse).GetMethod(nameof(Sse.And), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
204 .Invoke(null, new object[] {
205 Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr),
206 Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr)
209 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Single>)(result));
210 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
213 public void RunReflectionScenario_Load()
215 var result = typeof(Sse).GetMethod(nameof(Sse.And), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
216 .Invoke(null, new object[] {
217 Sse.LoadVector128((Single*)(_dataTable.inArray1Ptr)),
218 Sse.LoadVector128((Single*)(_dataTable.inArray2Ptr))
221 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Single>)(result));
222 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
225 public void RunReflectionScenario_LoadAligned()
227 var result = typeof(Sse).GetMethod(nameof(Sse.And), new Type[] { typeof(Vector128<Single>), typeof(Vector128<Single>) })
228 .Invoke(null, new object[] {
229 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray1Ptr)),
230 Sse.LoadAlignedVector128((Single*)(_dataTable.inArray2Ptr))
233 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Single>)(result));
234 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
237 public void RunClsVarScenario()
239 var result = Sse.And(
244 Unsafe.Write(_dataTable.outArrayPtr, result);
245 ValidateResult(_clsVar1, _clsVar2, _dataTable.outArrayPtr);
248 public void RunLclVarScenario_UnsafeRead()
250 var left = Unsafe.Read<Vector128<Single>>(_dataTable.inArray1Ptr);
251 var right = Unsafe.Read<Vector128<Single>>(_dataTable.inArray2Ptr);
252 var result = Sse.And(left, right);
254 Unsafe.Write(_dataTable.outArrayPtr, result);
255 ValidateResult(left, right, _dataTable.outArrayPtr);
258 public void RunLclVarScenario_Load()
260 var left = Sse.LoadVector128((Single*)(_dataTable.inArray1Ptr));
261 var right = Sse.LoadVector128((Single*)(_dataTable.inArray2Ptr));
262 var result = Sse.And(left, right);
264 Unsafe.Write(_dataTable.outArrayPtr, result);
265 ValidateResult(left, right, _dataTable.outArrayPtr);
268 public void RunLclVarScenario_LoadAligned()
270 var left = Sse.LoadAlignedVector128((Single*)(_dataTable.inArray1Ptr));
271 var right = Sse.LoadAlignedVector128((Single*)(_dataTable.inArray2Ptr));
272 var result = Sse.And(left, right);
274 Unsafe.Write(_dataTable.outArrayPtr, result);
275 ValidateResult(left, right, _dataTable.outArrayPtr);
278 public void RunClassLclFldScenario()
280 var test = new SimpleBinaryOpTest__AndSingle();
281 var result = Sse.And(test._fld1, test._fld2);
283 Unsafe.Write(_dataTable.outArrayPtr, result);
284 ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
287 public void RunClassFldScenario()
289 var result = Sse.And(_fld1, _fld2);
291 Unsafe.Write(_dataTable.outArrayPtr, result);
292 ValidateResult(_fld1, _fld2, _dataTable.outArrayPtr);
295 public void RunStructLclFldScenario()
297 var test = TestStruct.Create();
298 var result = Sse.And(test._fld1, test._fld2);
300 Unsafe.Write(_dataTable.outArrayPtr, result);
301 ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
304 public void RunStructFldScenario()
306 var test = TestStruct.Create();
307 test.RunStructFldScenario(this);
310 public void RunUnsupportedScenario()
316 RunBasicScenario_UnsafeRead();
318 catch (PlatformNotSupportedException)
324 private void ValidateResult(Vector128<Single> left, Vector128<Single> right, void* result, [CallerMemberName] string method = "")
326 Single[] inArray1 = new Single[Op1ElementCount];
327 Single[] inArray2 = new Single[Op2ElementCount];
328 Single[] outArray = new Single[RetElementCount];
330 Unsafe.WriteUnaligned(ref Unsafe.As<Single, byte>(ref inArray1[0]), left);
331 Unsafe.WriteUnaligned(ref Unsafe.As<Single, byte>(ref inArray2[0]), right);
332 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector128<Single>>());
334 ValidateResult(inArray1, inArray2, outArray, method);
337 private void ValidateResult(void* left, void* right, void* result, [CallerMemberName] string method = "")
339 Single[] inArray1 = new Single[Op1ElementCount];
340 Single[] inArray2 = new Single[Op2ElementCount];
341 Single[] outArray = new Single[RetElementCount];
343 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref inArray1[0]), ref Unsafe.AsRef<byte>(left), (uint)Unsafe.SizeOf<Vector128<Single>>());
344 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref inArray2[0]), ref Unsafe.AsRef<byte>(right), (uint)Unsafe.SizeOf<Vector128<Single>>());
345 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector128<Single>>());
347 ValidateResult(inArray1, inArray2, outArray, method);
350 private void ValidateResult(Single[] left, Single[] right, Single[] result, [CallerMemberName] string method = "")
352 if ((BitConverter.SingleToInt32Bits(left[0]) & BitConverter.SingleToInt32Bits(right[0])) != BitConverter.SingleToInt32Bits(result[0]))
358 for (var i = 1; i < RetElementCount; i++)
360 if ((BitConverter.SingleToInt32Bits(left[0]) & BitConverter.SingleToInt32Bits(right[0])) != BitConverter.SingleToInt32Bits(result[0]))
370 Console.WriteLine($"{nameof(Sse)}.{nameof(Sse.And)}<Single>(Vector128<Single>, Vector128<Single>): {method} failed:");
371 Console.WriteLine($" left: ({string.Join(", ", left)})");
372 Console.WriteLine($" right: ({string.Join(", ", right)})");
373 Console.WriteLine($" result: ({string.Join(", ", result)})");