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 ExtractUInt16129()
24 var test = new SimpleUnaryOpTest__ExtractUInt16129();
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__ExtractUInt16129
95 private const int VectorSize = 16;
97 private const int Op1ElementCount = VectorSize / sizeof(UInt16);
98 private const int RetElementCount = VectorSize / sizeof(UInt16);
100 private static UInt16[] _data = new UInt16[Op1ElementCount];
102 private static Vector128<UInt16> _clsVar;
104 private Vector128<UInt16> _fld;
106 private SimpleUnaryOpTest__DataTable<UInt16, UInt16> _dataTable;
108 static SimpleUnaryOpTest__ExtractUInt16129()
110 var random = new Random();
112 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, int.MaxValue)); }
113 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt16>, byte>(ref _clsVar), ref Unsafe.As<UInt16, byte>(ref _data[0]), VectorSize);
116 public SimpleUnaryOpTest__ExtractUInt16129()
120 var random = new Random();
122 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, int.MaxValue)); }
123 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt16>, byte>(ref _fld), ref Unsafe.As<UInt16, byte>(ref _data[0]), VectorSize);
125 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, int.MaxValue)); }
126 _dataTable = new SimpleUnaryOpTest__DataTable<UInt16, UInt16>(_data, new UInt16[RetElementCount], VectorSize);
129 public bool IsSupported => Sse2.IsSupported;
131 public bool Succeeded { get; set; }
133 public void RunBasicScenario_UnsafeRead()
135 var result = Sse2.Extract(
136 Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr),
140 Unsafe.Write(_dataTable.outArrayPtr, result);
141 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
144 public void RunBasicScenario_Load()
146 var result = Sse2.Extract(
147 Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr)),
151 Unsafe.Write(_dataTable.outArrayPtr, result);
152 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
155 public void RunBasicScenario_LoadAligned()
157 var result = Sse2.Extract(
158 Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr)),
162 Unsafe.Write(_dataTable.outArrayPtr, result);
163 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
166 public void RunReflectionScenario_UnsafeRead()
168 var result = typeof(Sse2).GetMethod(nameof(Sse2.Extract), new Type[] { typeof(Vector128<UInt16>), typeof(byte) })
169 .Invoke(null, new object[] {
170 Unsafe.Read<Vector128<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(Sse2).GetMethod(nameof(Sse2.Extract), new Type[] { typeof(Vector128<UInt16>), typeof(byte) })
181 .Invoke(null, new object[] {
182 Sse2.LoadVector128((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(Sse2).GetMethod(nameof(Sse2.Extract), new Type[] { typeof(Vector128<UInt16>), typeof(byte) })
193 .Invoke(null, new object[] {
194 Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr)),
198 Unsafe.Write(_dataTable.outArrayPtr, (UInt16)(result));
199 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
202 public void RunClsVarScenario()
204 var result = Sse2.Extract(
209 Unsafe.Write(_dataTable.outArrayPtr, result);
210 ValidateResult(_clsVar, _dataTable.outArrayPtr);
213 public void RunLclVarScenario_UnsafeRead()
215 var firstOp = Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr);
216 var result = Sse2.Extract(firstOp, 129);
218 Unsafe.Write(_dataTable.outArrayPtr, result);
219 ValidateResult(firstOp, _dataTable.outArrayPtr);
222 public void RunLclVarScenario_Load()
224 var firstOp = Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr));
225 var result = Sse2.Extract(firstOp, 129);
227 Unsafe.Write(_dataTable.outArrayPtr, result);
228 ValidateResult(firstOp, _dataTable.outArrayPtr);
231 public void RunLclVarScenario_LoadAligned()
233 var firstOp = Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr));
234 var result = Sse2.Extract(firstOp, 129);
236 Unsafe.Write(_dataTable.outArrayPtr, result);
237 ValidateResult(firstOp, _dataTable.outArrayPtr);
240 public void RunLclFldScenario()
242 var test = new SimpleUnaryOpTest__ExtractUInt16129();
243 var result = Sse2.Extract(test._fld, 129);
245 Unsafe.Write(_dataTable.outArrayPtr, result);
246 ValidateResult(test._fld, _dataTable.outArrayPtr);
249 public void RunFldScenario()
251 var result = Sse2.Extract(_fld, 129);
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(Vector128<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), VectorSize);
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), VectorSize);
288 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
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(Sse2)}.{nameof(Sse2.Extract)}<UInt16>(Vector128<UInt16><9>): {method} failed:");
303 Console.WriteLine($" firstOp: ({string.Join(", ", firstOp)})");
304 Console.WriteLine($" result: ({string.Join(", ", result)})");