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 ExtractUInt161()
24 var test = new SimpleUnaryOpTest__ExtractUInt161();
30 // Validates basic functionality works, using Unsafe.Read
31 test.RunBasicScenario_UnsafeRead();
35 // Validates basic functionality works, using Load
36 test.RunBasicScenario_Load();
38 // Validates basic functionality works, using LoadAligned
39 test.RunBasicScenario_LoadAligned();
42 // Validates calling via reflection works, using Unsafe.Read
43 test.RunReflectionScenario_UnsafeRead();
47 // Validates calling via reflection works, using Load
48 test.RunReflectionScenario_Load();
50 // Validates calling via reflection works, using LoadAligned
51 test.RunReflectionScenario_LoadAligned();
54 // Validates passing a static member works
55 test.RunClsVarScenario();
57 // Validates passing a local works, using Unsafe.Read
58 test.RunLclVarScenario_UnsafeRead();
62 // Validates passing a local works, using Load
63 test.RunLclVarScenario_Load();
65 // Validates passing a local works, using LoadAligned
66 test.RunLclVarScenario_LoadAligned();
69 // Validates passing the field of a local works
70 test.RunLclFldScenario();
72 // Validates passing an instance member works
73 test.RunFldScenario();
77 // Validates we throw on unsupported hardware
78 test.RunUnsupportedScenario();
81 catch (PlatformNotSupportedException)
83 test.Succeeded = true;
88 throw new Exception("One or more scenarios did not complete as expected.");
93 public sealed unsafe class SimpleUnaryOpTest__ExtractUInt161
95 private static readonly int LargestVectorSize = 32;
97 private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector256<UInt16>>() / sizeof(UInt16);
98 private static readonly int RetElementCount = Unsafe.SizeOf<Vector256<UInt16>>() / sizeof(UInt16);
100 private static UInt16[] _data = new UInt16[Op1ElementCount];
102 private static Vector256<UInt16> _clsVar;
104 private Vector256<UInt16> _fld;
106 private SimpleUnaryOpTest__DataTable<UInt16, UInt16> _dataTable;
108 static SimpleUnaryOpTest__ExtractUInt161()
110 var random = new Random();
112 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, ushort.MaxValue)); }
113 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<UInt16>, byte>(ref _clsVar), ref Unsafe.As<UInt16, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector256<UInt16>>());
116 public SimpleUnaryOpTest__ExtractUInt161()
120 var random = new Random();
122 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, ushort.MaxValue)); }
123 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<UInt16>, byte>(ref _fld), ref Unsafe.As<UInt16, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector256<UInt16>>());
125 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, ushort.MaxValue)); }
126 _dataTable = new SimpleUnaryOpTest__DataTable<UInt16, UInt16>(_data, new UInt16[RetElementCount], LargestVectorSize);
129 public bool IsSupported => Avx.IsSupported;
131 public bool Succeeded { get; set; }
133 public void RunBasicScenario_UnsafeRead()
135 var result = Avx.Extract(
136 Unsafe.Read<Vector256<UInt16>>(_dataTable.inArrayPtr),
140 Unsafe.Write(_dataTable.outArrayPtr, result);
141 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
144 public void RunBasicScenario_Load()
146 var result = Avx.Extract(
147 Avx.LoadVector256((UInt16*)(_dataTable.inArrayPtr)),
151 Unsafe.Write(_dataTable.outArrayPtr, result);
152 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
155 public void RunBasicScenario_LoadAligned()
157 var result = Avx.Extract(
158 Avx.LoadAlignedVector256((UInt16*)(_dataTable.inArrayPtr)),
162 Unsafe.Write(_dataTable.outArrayPtr, result);
163 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
166 public void RunReflectionScenario_UnsafeRead()
168 var result = typeof(Avx).GetMethod(nameof(Avx.Extract), new Type[] { typeof(Vector256<UInt16>), typeof(byte) })
169 .Invoke(null, new object[] {
170 Unsafe.Read<Vector256<UInt16>>(_dataTable.inArrayPtr),
174 Unsafe.Write(_dataTable.outArrayPtr, (UInt16)(result));
175 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
178 public void RunReflectionScenario_Load()
180 var result = typeof(Avx).GetMethod(nameof(Avx.Extract), new Type[] { typeof(Vector256<UInt16>), typeof(byte) })
181 .Invoke(null, new object[] {
182 Avx.LoadVector256((UInt16*)(_dataTable.inArrayPtr)),
186 Unsafe.Write(_dataTable.outArrayPtr, (UInt16)(result));
187 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
190 public void RunReflectionScenario_LoadAligned()
192 var result = typeof(Avx).GetMethod(nameof(Avx.Extract), new Type[] { typeof(Vector256<UInt16>), typeof(byte) })
193 .Invoke(null, new object[] {
194 Avx.LoadAlignedVector256((UInt16*)(_dataTable.inArrayPtr)),
198 Unsafe.Write(_dataTable.outArrayPtr, (UInt16)(result));
199 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
202 public void RunClsVarScenario()
204 var result = Avx.Extract(
209 Unsafe.Write(_dataTable.outArrayPtr, result);
210 ValidateResult(_clsVar, _dataTable.outArrayPtr);
213 public void RunLclVarScenario_UnsafeRead()
215 var firstOp = Unsafe.Read<Vector256<UInt16>>(_dataTable.inArrayPtr);
216 var result = Avx.Extract(firstOp, 1);
218 Unsafe.Write(_dataTable.outArrayPtr, result);
219 ValidateResult(firstOp, _dataTable.outArrayPtr);
222 public void RunLclVarScenario_Load()
224 var firstOp = Avx.LoadVector256((UInt16*)(_dataTable.inArrayPtr));
225 var result = Avx.Extract(firstOp, 1);
227 Unsafe.Write(_dataTable.outArrayPtr, result);
228 ValidateResult(firstOp, _dataTable.outArrayPtr);
231 public void RunLclVarScenario_LoadAligned()
233 var firstOp = Avx.LoadAlignedVector256((UInt16*)(_dataTable.inArrayPtr));
234 var result = Avx.Extract(firstOp, 1);
236 Unsafe.Write(_dataTable.outArrayPtr, result);
237 ValidateResult(firstOp, _dataTable.outArrayPtr);
240 public void RunLclFldScenario()
242 var test = new SimpleUnaryOpTest__ExtractUInt161();
243 var result = Avx.Extract(test._fld, 1);
245 Unsafe.Write(_dataTable.outArrayPtr, result);
246 ValidateResult(test._fld, _dataTable.outArrayPtr);
249 public void RunFldScenario()
251 var result = Avx.Extract(_fld, 1);
253 Unsafe.Write(_dataTable.outArrayPtr, result);
254 ValidateResult(_fld, _dataTable.outArrayPtr);
257 public void RunUnsupportedScenario()
263 RunBasicScenario_UnsafeRead();
265 catch (PlatformNotSupportedException)
271 private void ValidateResult(Vector256<UInt16> firstOp, void* result, [CallerMemberName] string method = "")
273 UInt16[] inArray = new UInt16[Op1ElementCount];
274 UInt16[] outArray = new UInt16[RetElementCount];
276 Unsafe.WriteUnaligned(ref Unsafe.As<UInt16, byte>(ref inArray[0]), firstOp);
277 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<UInt16>>());
279 ValidateResult(inArray, outArray, method);
282 private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
284 UInt16[] inArray = new UInt16[Op1ElementCount];
285 UInt16[] outArray = new UInt16[RetElementCount];
287 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), (uint)Unsafe.SizeOf<Vector256<UInt16>>());
288 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<UInt16>>());
290 ValidateResult(inArray, outArray, method);
293 private void ValidateResult(UInt16[] firstOp, UInt16[] result, [CallerMemberName] string method = "")
295 if ((result[0] != firstOp[1]))
302 Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.Extract)}<UInt16>(Vector256<UInt16><9>): {method} failed:");
303 Console.WriteLine($" firstOp: ({string.Join(", ", firstOp)})");
304 Console.WriteLine($" result: ({string.Join(", ", result)})");