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.Reflection;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
17 using System.Runtime.Intrinsics;
18 using System.Runtime.Intrinsics.X86;
20 namespace JIT.HardwareIntrinsics.X86
22 public static partial class Program
24 private static void ExtractVector128Byte1()
26 var test = new SimpleUnaryOpTest__ExtractVector128Byte1();
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();
83 throw new Exception("One or more scenarios did not complete as expected.");
88 public sealed unsafe class SimpleUnaryOpTest__ExtractVector128Byte1
90 private static readonly int LargestVectorSize = 32;
92 private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector256<Byte>>() / sizeof(Byte);
93 private static readonly int RetElementCount = Unsafe.SizeOf<Vector128<Byte>>() / sizeof(Byte);
95 private static Byte[] _data = new Byte[Op1ElementCount];
97 private static Vector256<Byte> _clsVar;
99 private Vector256<Byte> _fld;
101 private SimpleUnaryOpTest__DataTable<Byte, Byte> _dataTable;
103 static SimpleUnaryOpTest__ExtractVector128Byte1()
105 var random = new Random();
107 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (byte)(random.Next(0, byte.MaxValue)); }
108 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Byte>, byte>(ref _clsVar), ref Unsafe.As<Byte, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector256<Byte>>());
111 public SimpleUnaryOpTest__ExtractVector128Byte1()
115 var random = new Random();
117 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (byte)(random.Next(0, byte.MaxValue)); }
118 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Byte>, byte>(ref _fld), ref Unsafe.As<Byte, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector256<Byte>>());
120 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (byte)(random.Next(0, byte.MaxValue)); }
121 _dataTable = new SimpleUnaryOpTest__DataTable<Byte, Byte>(_data, new Byte[RetElementCount], LargestVectorSize);
124 public bool IsSupported => Avx.IsSupported;
126 public bool Succeeded { get; set; }
128 public void RunBasicScenario_UnsafeRead()
130 var result = Avx.ExtractVector128(
131 Unsafe.Read<Vector256<Byte>>(_dataTable.inArrayPtr),
135 Unsafe.Write(_dataTable.outArrayPtr, result);
136 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
139 public void RunBasicScenario_Load()
141 var result = Avx.ExtractVector128(
142 Avx.LoadVector256((Byte*)(_dataTable.inArrayPtr)),
146 Unsafe.Write(_dataTable.outArrayPtr, result);
147 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
150 public void RunBasicScenario_LoadAligned()
152 var result = Avx.ExtractVector128(
153 Avx.LoadAlignedVector256((Byte*)(_dataTable.inArrayPtr)),
157 Unsafe.Write(_dataTable.outArrayPtr, result);
158 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
161 public void RunReflectionScenario_UnsafeRead()
163 var result = typeof(Avx).GetMethods(BindingFlags.Public | BindingFlags.Static)
164 .FirstOrDefault( mi => mi.Name == nameof(Avx.ExtractVector128) && mi.IsGenericMethod)
165 .MakeGenericMethod(new[] { typeof(Byte) })
166 .Invoke(null, new object[] {
167 Unsafe.Read<Vector256<Byte>>(_dataTable.inArrayPtr),
171 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Byte>)(result));
172 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
175 public void RunReflectionScenario_Load()
177 var result = typeof(Avx).GetMethods(BindingFlags.Public | BindingFlags.Static)
178 .FirstOrDefault( mi => mi.Name == nameof(Avx.ExtractVector128) && mi.IsGenericMethod)
179 .MakeGenericMethod(new[] { typeof(Byte) })
180 .Invoke(null, new object[] {
181 Avx.LoadVector256((Byte*)(_dataTable.inArrayPtr)),
185 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Byte>)(result));
186 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
189 public void RunReflectionScenario_LoadAligned()
191 var result = typeof(Avx).GetMethods(BindingFlags.Public | BindingFlags.Static)
192 .FirstOrDefault( mi => mi.Name == nameof(Avx.ExtractVector128) && mi.IsGenericMethod)
193 .MakeGenericMethod(new[] { typeof(Byte) })
194 .Invoke(null, new object[] {
195 Avx.LoadAlignedVector256((Byte*)(_dataTable.inArrayPtr)),
199 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Byte>)(result));
200 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
203 public void RunClsVarScenario()
205 var result = Avx.ExtractVector128(
210 Unsafe.Write(_dataTable.outArrayPtr, result);
211 ValidateResult(_clsVar, _dataTable.outArrayPtr);
214 public void RunLclVarScenario_UnsafeRead()
216 var firstOp = Unsafe.Read<Vector256<Byte>>(_dataTable.inArrayPtr);
217 var result = Avx.ExtractVector128(firstOp, 1);
219 Unsafe.Write(_dataTable.outArrayPtr, result);
220 ValidateResult(firstOp, _dataTable.outArrayPtr);
223 public void RunLclVarScenario_Load()
225 var firstOp = Avx.LoadVector256((Byte*)(_dataTable.inArrayPtr));
226 var result = Avx.ExtractVector128(firstOp, 1);
228 Unsafe.Write(_dataTable.outArrayPtr, result);
229 ValidateResult(firstOp, _dataTable.outArrayPtr);
232 public void RunLclVarScenario_LoadAligned()
234 var firstOp = Avx.LoadAlignedVector256((Byte*)(_dataTable.inArrayPtr));
235 var result = Avx.ExtractVector128(firstOp, 1);
237 Unsafe.Write(_dataTable.outArrayPtr, result);
238 ValidateResult(firstOp, _dataTable.outArrayPtr);
241 public void RunLclFldScenario()
243 var test = new SimpleUnaryOpTest__ExtractVector128Byte1();
244 var result = Avx.ExtractVector128(test._fld, 1);
246 Unsafe.Write(_dataTable.outArrayPtr, result);
247 ValidateResult(test._fld, _dataTable.outArrayPtr);
250 public void RunFldScenario()
252 var result = Avx.ExtractVector128(_fld, 1);
254 Unsafe.Write(_dataTable.outArrayPtr, result);
255 ValidateResult(_fld, _dataTable.outArrayPtr);
258 public void RunUnsupportedScenario()
264 RunBasicScenario_UnsafeRead();
266 catch (PlatformNotSupportedException)
272 private void ValidateResult(Vector256<Byte> firstOp, void* result, [CallerMemberName] string method = "")
274 Byte[] inArray = new Byte[Op1ElementCount];
275 Byte[] outArray = new Byte[RetElementCount];
277 Unsafe.WriteUnaligned(ref Unsafe.As<Byte, byte>(ref inArray[0]), firstOp);
278 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Byte, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector128<Byte>>());
280 ValidateResult(inArray, outArray, method);
283 private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
285 Byte[] inArray = new Byte[Op1ElementCount];
286 Byte[] outArray = new Byte[RetElementCount];
288 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Byte, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), (uint)Unsafe.SizeOf<Vector256<Byte>>());
289 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Byte, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector128<Byte>>());
291 ValidateResult(inArray, outArray, method);
294 private void ValidateResult(Byte[] firstOp, Byte[] result, [CallerMemberName] string method = "")
296 if (result[0] != firstOp[16])
302 for (var i = 1; i < RetElementCount; i++)
304 if ((result[i] != firstOp[i + 16]))
314 Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.ExtractVector128)}<Byte>(Vector256<Byte><9>): {method} failed:");
315 Console.WriteLine($" firstOp: ({string.Join(", ", firstOp)})");
316 Console.WriteLine($" result: ({string.Join(", ", result)})");