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 Permute2x128UInt322()
24 var test = new SimpleBinaryOpTest__Permute2x128UInt322();
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__Permute2x128UInt322
88 private const int VectorSize = 32;
90 private const int Op1ElementCount = VectorSize / sizeof(UInt32);
91 private const int Op2ElementCount = VectorSize / sizeof(UInt32);
92 private const int RetElementCount = VectorSize / sizeof(UInt32);
94 private static UInt32[] _data1 = new UInt32[Op1ElementCount];
95 private static UInt32[] _data2 = new UInt32[Op2ElementCount];
97 private static Vector256<UInt32> _clsVar1;
98 private static Vector256<UInt32> _clsVar2;
100 private Vector256<UInt32> _fld1;
101 private Vector256<UInt32> _fld2;
103 private SimpleBinaryOpTest__DataTable<UInt32, UInt32, UInt32> _dataTable;
105 static SimpleBinaryOpTest__Permute2x128UInt322()
107 var random = new Random();
109 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (uint)(random.Next(0, int.MaxValue)); }
110 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<UInt32>, byte>(ref _clsVar1), ref Unsafe.As<UInt32, byte>(ref _data1[0]), VectorSize);
111 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (uint)(random.Next(0, int.MaxValue)); }
112 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<UInt32>, byte>(ref _clsVar2), ref Unsafe.As<UInt32, byte>(ref _data2[0]), VectorSize);
115 public SimpleBinaryOpTest__Permute2x128UInt322()
119 var random = new Random();
121 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (uint)(random.Next(0, int.MaxValue)); }
122 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<UInt32>, byte>(ref _fld1), ref Unsafe.As<UInt32, byte>(ref _data1[0]), VectorSize);
123 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (uint)(random.Next(0, int.MaxValue)); }
124 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<UInt32>, byte>(ref _fld2), ref Unsafe.As<UInt32, byte>(ref _data2[0]), VectorSize);
126 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (uint)(random.Next(0, int.MaxValue)); }
127 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (uint)(random.Next(0, int.MaxValue)); }
128 _dataTable = new SimpleBinaryOpTest__DataTable<UInt32, UInt32, UInt32>(_data1, _data2, new UInt32[RetElementCount], VectorSize);
131 public bool IsSupported => Avx.IsSupported;
133 public bool Succeeded { get; set; }
135 public void RunBasicScenario_UnsafeRead()
137 var result = Avx.Permute2x128(
138 Unsafe.Read<Vector256<UInt32>>(_dataTable.inArray1Ptr),
139 Unsafe.Read<Vector256<UInt32>>(_dataTable.inArray2Ptr),
143 Unsafe.Write(_dataTable.outArrayPtr, result);
144 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
147 public void RunBasicScenario_Load()
149 var result = Avx.Permute2x128(
150 Avx.LoadVector256((UInt32*)(_dataTable.inArray1Ptr)),
151 Avx.LoadVector256((UInt32*)(_dataTable.inArray2Ptr)),
155 Unsafe.Write(_dataTable.outArrayPtr, result);
156 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
159 public void RunBasicScenario_LoadAligned()
161 var result = Avx.Permute2x128(
162 Avx.LoadAlignedVector256((UInt32*)(_dataTable.inArray1Ptr)),
163 Avx.LoadAlignedVector256((UInt32*)(_dataTable.inArray2Ptr)),
167 Unsafe.Write(_dataTable.outArrayPtr, result);
168 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
171 public void RunReflectionScenario_UnsafeRead()
173 var result = typeof(Avx).GetMethod(nameof(Avx.Permute2x128)).MakeGenericMethod( new Type[] { typeof(UInt32) })
174 .Invoke(null, new object[] {
175 Unsafe.Read<Vector256<UInt32>>(_dataTable.inArray1Ptr),
176 Unsafe.Read<Vector256<UInt32>>(_dataTable.inArray2Ptr),
180 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<UInt32>)(result));
181 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
184 public void RunReflectionScenario_Load()
186 var result = typeof(Avx).GetMethod(nameof(Avx.Permute2x128)).MakeGenericMethod( new Type[] { typeof(UInt32) })
187 .Invoke(null, new object[] {
188 Avx.LoadVector256((UInt32*)(_dataTable.inArray1Ptr)),
189 Avx.LoadVector256((UInt32*)(_dataTable.inArray2Ptr)),
193 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<UInt32>)(result));
194 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
197 public void RunReflectionScenario_LoadAligned()
199 var result = typeof(Avx).GetMethod(nameof(Avx.Permute2x128)).MakeGenericMethod( new Type[] { typeof(UInt32) })
200 .Invoke(null, new object[] {
201 Avx.LoadAlignedVector256((UInt32*)(_dataTable.inArray1Ptr)),
202 Avx.LoadAlignedVector256((UInt32*)(_dataTable.inArray2Ptr)),
206 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<UInt32>)(result));
207 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
210 public void RunClsVarScenario()
212 var result = Avx.Permute2x128(
218 Unsafe.Write(_dataTable.outArrayPtr, result);
219 ValidateResult(_clsVar1, _clsVar2, _dataTable.outArrayPtr);
222 public void RunLclVarScenario_UnsafeRead()
224 var left = Unsafe.Read<Vector256<UInt32>>(_dataTable.inArray1Ptr);
225 var right = Unsafe.Read<Vector256<UInt32>>(_dataTable.inArray2Ptr);
226 var result = Avx.Permute2x128(left, right, 2);
228 Unsafe.Write(_dataTable.outArrayPtr, result);
229 ValidateResult(left, right, _dataTable.outArrayPtr);
232 public void RunLclVarScenario_Load()
234 var left = Avx.LoadVector256((UInt32*)(_dataTable.inArray1Ptr));
235 var right = Avx.LoadVector256((UInt32*)(_dataTable.inArray2Ptr));
236 var result = Avx.Permute2x128(left, right, 2);
238 Unsafe.Write(_dataTable.outArrayPtr, result);
239 ValidateResult(left, right, _dataTable.outArrayPtr);
242 public void RunLclVarScenario_LoadAligned()
244 var left = Avx.LoadAlignedVector256((UInt32*)(_dataTable.inArray1Ptr));
245 var right = Avx.LoadAlignedVector256((UInt32*)(_dataTable.inArray2Ptr));
246 var result = Avx.Permute2x128(left, right, 2);
248 Unsafe.Write(_dataTable.outArrayPtr, result);
249 ValidateResult(left, right, _dataTable.outArrayPtr);
252 public void RunLclFldScenario()
254 var test = new SimpleBinaryOpTest__Permute2x128UInt322();
255 var result = Avx.Permute2x128(test._fld1, test._fld2, 2);
257 Unsafe.Write(_dataTable.outArrayPtr, result);
258 ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
261 public void RunFldScenario()
263 var result = Avx.Permute2x128(_fld1, _fld2, 2);
265 Unsafe.Write(_dataTable.outArrayPtr, result);
266 ValidateResult(_fld1, _fld2, _dataTable.outArrayPtr);
269 public void RunUnsupportedScenario()
275 RunBasicScenario_UnsafeRead();
277 catch (PlatformNotSupportedException)
283 private void ValidateResult(Vector256<UInt32> left, Vector256<UInt32> right, void* result, [CallerMemberName] string method = "")
285 UInt32[] inArray1 = new UInt32[Op1ElementCount];
286 UInt32[] inArray2 = new UInt32[Op2ElementCount];
287 UInt32[] outArray = new UInt32[RetElementCount];
289 Unsafe.WriteUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray1[0]), left);
290 Unsafe.WriteUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray2[0]), right);
291 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
293 ValidateResult(inArray1, inArray2, outArray, method);
296 private void ValidateResult(void* left, void* right, void* result, [CallerMemberName] string method = "")
298 UInt32[] inArray1 = new UInt32[Op1ElementCount];
299 UInt32[] inArray2 = new UInt32[Op2ElementCount];
300 UInt32[] outArray = new UInt32[RetElementCount];
302 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray1[0]), ref Unsafe.AsRef<byte>(left), VectorSize);
303 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray2[0]), ref Unsafe.AsRef<byte>(right), VectorSize);
304 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
306 ValidateResult(inArray1, inArray2, outArray, method);
309 private void ValidateResult(UInt32[] left, UInt32[] right, UInt32[] result, [CallerMemberName] string method = "")
311 if (result[0] != right[0])
317 for (var i = 1; i < RetElementCount; i++)
319 if (i > 3 ? (result[i] != left[i - 4]) : (result[i] != right[i]))
329 Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.Permute2x128)}<UInt32>(Vector256<UInt32>.2, Vector256<UInt32>): {method} failed:");
330 Console.WriteLine($" left: ({string.Join(", ", left)})");
331 Console.WriteLine($" right: ({string.Join(", ", right)})");
332 Console.WriteLine($" result: ({string.Join(", ", result)})");