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 PermuteSingle1()
24 var test = new ImmUnaryOpTest__PermuteSingle1();
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 ImmUnaryOpTest__PermuteSingle1
94 private struct TestStruct
96 public Vector256<Single> _fld;
98 public static TestStruct Create()
100 var testStruct = new TestStruct();
101 var random = new Random();
103 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (float)(random.NextDouble()); }
104 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Single>, byte>(ref testStruct._fld), ref Unsafe.As<Single, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector256<Single>>());
109 public void RunStructFldScenario(ImmUnaryOpTest__PermuteSingle1 testClass)
111 var result = Avx.Permute(_fld, 1);
113 Unsafe.Write(testClass._dataTable.outArrayPtr, result);
114 testClass.ValidateResult(_fld, testClass._dataTable.outArrayPtr);
118 private static readonly int LargestVectorSize = 32;
120 private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector256<Single>>() / sizeof(Single);
121 private static readonly int RetElementCount = Unsafe.SizeOf<Vector256<Single>>() / sizeof(Single);
123 private static Single[] _data = new Single[Op1ElementCount];
125 private static Vector256<Single> _clsVar;
127 private Vector256<Single> _fld;
129 private SimpleUnaryOpTest__DataTable<Single, Single> _dataTable;
131 static ImmUnaryOpTest__PermuteSingle1()
133 var random = new Random();
135 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (float)(random.NextDouble()); }
136 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Single>, byte>(ref _clsVar), ref Unsafe.As<Single, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector256<Single>>());
139 public ImmUnaryOpTest__PermuteSingle1()
143 var random = new Random();
145 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (float)(random.NextDouble()); }
146 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Single>, byte>(ref _fld), ref Unsafe.As<Single, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector256<Single>>());
148 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (float)(random.NextDouble()); }
149 _dataTable = new SimpleUnaryOpTest__DataTable<Single, Single>(_data, new Single[RetElementCount], LargestVectorSize);
152 public bool IsSupported => Avx.IsSupported;
154 public bool Succeeded { get; set; }
156 public void RunBasicScenario_UnsafeRead()
158 var result = Avx.Permute(
159 Unsafe.Read<Vector256<Single>>(_dataTable.inArrayPtr),
163 Unsafe.Write(_dataTable.outArrayPtr, result);
164 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
167 public void RunBasicScenario_Load()
169 var result = Avx.Permute(
170 Avx.LoadVector256((Single*)(_dataTable.inArrayPtr)),
174 Unsafe.Write(_dataTable.outArrayPtr, result);
175 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
178 public void RunBasicScenario_LoadAligned()
180 var result = Avx.Permute(
181 Avx.LoadAlignedVector256((Single*)(_dataTable.inArrayPtr)),
185 Unsafe.Write(_dataTable.outArrayPtr, result);
186 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
189 public void RunReflectionScenario_UnsafeRead()
191 var result = typeof(Avx).GetMethod(nameof(Avx.Permute), new Type[] { typeof(Vector256<Single>), typeof(byte) })
192 .Invoke(null, new object[] {
193 Unsafe.Read<Vector256<Single>>(_dataTable.inArrayPtr),
197 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Single>)(result));
198 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
201 public void RunReflectionScenario_Load()
203 var result = typeof(Avx).GetMethod(nameof(Avx.Permute), new Type[] { typeof(Vector256<Single>), typeof(byte) })
204 .Invoke(null, new object[] {
205 Avx.LoadVector256((Single*)(_dataTable.inArrayPtr)),
209 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Single>)(result));
210 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
213 public void RunReflectionScenario_LoadAligned()
215 var result = typeof(Avx).GetMethod(nameof(Avx.Permute), new Type[] { typeof(Vector256<Single>), typeof(byte) })
216 .Invoke(null, new object[] {
217 Avx.LoadAlignedVector256((Single*)(_dataTable.inArrayPtr)),
221 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Single>)(result));
222 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
225 public void RunClsVarScenario()
227 var result = Avx.Permute(
232 Unsafe.Write(_dataTable.outArrayPtr, result);
233 ValidateResult(_clsVar, _dataTable.outArrayPtr);
236 public void RunLclVarScenario_UnsafeRead()
238 var firstOp = Unsafe.Read<Vector256<Single>>(_dataTable.inArrayPtr);
239 var result = Avx.Permute(firstOp, 1);
241 Unsafe.Write(_dataTable.outArrayPtr, result);
242 ValidateResult(firstOp, _dataTable.outArrayPtr);
245 public void RunLclVarScenario_Load()
247 var firstOp = Avx.LoadVector256((Single*)(_dataTable.inArrayPtr));
248 var result = Avx.Permute(firstOp, 1);
250 Unsafe.Write(_dataTable.outArrayPtr, result);
251 ValidateResult(firstOp, _dataTable.outArrayPtr);
254 public void RunLclVarScenario_LoadAligned()
256 var firstOp = Avx.LoadAlignedVector256((Single*)(_dataTable.inArrayPtr));
257 var result = Avx.Permute(firstOp, 1);
259 Unsafe.Write(_dataTable.outArrayPtr, result);
260 ValidateResult(firstOp, _dataTable.outArrayPtr);
263 public void RunClassLclFldScenario()
265 var test = new ImmUnaryOpTest__PermuteSingle1();
266 var result = Avx.Permute(test._fld, 1);
268 Unsafe.Write(_dataTable.outArrayPtr, result);
269 ValidateResult(test._fld, _dataTable.outArrayPtr);
272 public void RunClassFldScenario()
274 var result = Avx.Permute(_fld, 1);
276 Unsafe.Write(_dataTable.outArrayPtr, result);
277 ValidateResult(_fld, _dataTable.outArrayPtr);
280 public void RunStructLclFldScenario()
282 var test = TestStruct.Create();
283 var result = Avx.Permute(test._fld, 1);
285 Unsafe.Write(_dataTable.outArrayPtr, result);
286 ValidateResult(test._fld, _dataTable.outArrayPtr);
289 public void RunStructFldScenario()
291 var test = TestStruct.Create();
292 test.RunStructFldScenario(this);
295 public void RunUnsupportedScenario()
301 RunBasicScenario_UnsafeRead();
303 catch (PlatformNotSupportedException)
309 private void ValidateResult(Vector256<Single> firstOp, void* result, [CallerMemberName] string method = "")
311 Single[] inArray = new Single[Op1ElementCount];
312 Single[] outArray = new Single[RetElementCount];
314 Unsafe.WriteUnaligned(ref Unsafe.As<Single, byte>(ref inArray[0]), firstOp);
315 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<Single>>());
317 ValidateResult(inArray, outArray, method);
320 private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
322 Single[] inArray = new Single[Op1ElementCount];
323 Single[] outArray = new Single[RetElementCount];
325 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), (uint)Unsafe.SizeOf<Vector256<Single>>());
326 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<Single>>());
328 ValidateResult(inArray, outArray, method);
331 private void ValidateResult(Single[] firstOp, Single[] result, [CallerMemberName] string method = "")
333 if (BitConverter.SingleToInt32Bits(result[0]) != BitConverter.SingleToInt32Bits(firstOp[1]))
339 for (var i = 1; i < RetElementCount; i++)
341 if (BitConverter.SingleToInt32Bits(result[4]) != BitConverter.SingleToInt32Bits(firstOp[5]))
351 Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.Permute)}<Single>(Vector256<Single><9>): {method} failed:");
352 Console.WriteLine($" firstOp: ({string.Join(", ", firstOp)})");
353 Console.WriteLine($" result: ({string.Join(", ", result)})");