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 GetLowerHalfInt64()
24 var test = new SimpleUnaryOpTest__GetLowerHalfInt64();
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 SimpleUnaryOpTest__GetLowerHalfInt64
88 private const int VectorSize = 32;
90 private const int Op1ElementCount = VectorSize / sizeof(Int64);
91 private const int RetElementCount = VectorSize / sizeof(Int64);
93 private static Int64[] _data = new Int64[Op1ElementCount];
95 private static Vector256<Int64> _clsVar;
97 private Vector256<Int64> _fld;
99 private SimpleUnaryOpTest__DataTable<Int64, Int64> _dataTable;
101 static SimpleUnaryOpTest__GetLowerHalfInt64()
103 var random = new Random();
105 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (long)(random.Next(int.MinValue, int.MaxValue)); }
106 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Int64>, byte>(ref _clsVar), ref Unsafe.As<Int64, byte>(ref _data[0]), VectorSize);
109 public SimpleUnaryOpTest__GetLowerHalfInt64()
113 var random = new Random();
115 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (long)(random.Next(int.MinValue, int.MaxValue)); }
116 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Int64>, byte>(ref _fld), ref Unsafe.As<Int64, byte>(ref _data[0]), VectorSize);
118 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (long)(random.Next(int.MinValue, int.MaxValue)); }
119 _dataTable = new SimpleUnaryOpTest__DataTable<Int64, Int64>(_data, new Int64[RetElementCount], VectorSize);
122 public bool IsSupported => Avx.IsSupported;
124 public bool Succeeded { get; set; }
126 public void RunBasicScenario_UnsafeRead()
128 var result = Avx.GetLowerHalf<Int64>(
129 Unsafe.Read<Vector256<Int64>>(_dataTable.inArrayPtr)
132 Unsafe.Write(_dataTable.outArrayPtr, result);
133 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
136 public void RunBasicScenario_Load()
138 var result = Avx.GetLowerHalf<Int64>(
139 Avx.LoadVector256((Int64*)(_dataTable.inArrayPtr))
142 Unsafe.Write(_dataTable.outArrayPtr, result);
143 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
146 public void RunBasicScenario_LoadAligned()
148 var result = Avx.GetLowerHalf<Int64>(
149 Avx.LoadAlignedVector256((Int64*)(_dataTable.inArrayPtr))
152 Unsafe.Write(_dataTable.outArrayPtr, result);
153 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
156 public void RunReflectionScenario_UnsafeRead()
158 var result = typeof(Avx).GetMethod(nameof(Avx.GetLowerHalf))
159 .MakeGenericMethod( new Type[] { typeof(Int64) })
160 .Invoke(null, new object[] {
161 Unsafe.Read<Vector256<Int64>>(_dataTable.inArrayPtr)
164 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Int64>)(result));
165 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
168 public void RunReflectionScenario_Load()
170 var result = typeof(Avx).GetMethod(nameof(Avx.GetLowerHalf))
171 .MakeGenericMethod( new Type[] { typeof(Int64) })
172 .Invoke(null, new object[] {
173 Avx.LoadVector256((Int64*)(_dataTable.inArrayPtr))
176 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Int64>)(result));
177 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
180 public void RunReflectionScenario_LoadAligned()
182 var result = typeof(Avx).GetMethod(nameof(Avx.GetLowerHalf))
183 .MakeGenericMethod( new Type[] { typeof(Int64) })
184 .Invoke(null, new object[] {
185 Avx.LoadAlignedVector256((Int64*)(_dataTable.inArrayPtr))
188 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<Int64>)(result));
189 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
192 public void RunClsVarScenario()
194 var result = Avx.GetLowerHalf<Int64>(
198 Unsafe.Write(_dataTable.outArrayPtr, result);
199 ValidateResult(_clsVar, _dataTable.outArrayPtr);
202 public void RunLclVarScenario_UnsafeRead()
204 var firstOp = Unsafe.Read<Vector256<Int64>>(_dataTable.inArrayPtr);
205 var result = Avx.GetLowerHalf<Int64>(firstOp);
207 Unsafe.Write(_dataTable.outArrayPtr, result);
208 ValidateResult(firstOp, _dataTable.outArrayPtr);
211 public void RunLclVarScenario_Load()
213 var firstOp = Avx.LoadVector256((Int64*)(_dataTable.inArrayPtr));
214 var result = Avx.GetLowerHalf<Int64>(firstOp);
216 Unsafe.Write(_dataTable.outArrayPtr, result);
217 ValidateResult(firstOp, _dataTable.outArrayPtr);
220 public void RunLclVarScenario_LoadAligned()
222 var firstOp = Avx.LoadAlignedVector256((Int64*)(_dataTable.inArrayPtr));
223 var result = Avx.GetLowerHalf<Int64>(firstOp);
225 Unsafe.Write(_dataTable.outArrayPtr, result);
226 ValidateResult(firstOp, _dataTable.outArrayPtr);
229 public void RunLclFldScenario()
231 var test = new SimpleUnaryOpTest__GetLowerHalfInt64();
232 var result = Avx.GetLowerHalf<Int64>(test._fld);
234 Unsafe.Write(_dataTable.outArrayPtr, result);
235 ValidateResult(test._fld, _dataTable.outArrayPtr);
238 public void RunFldScenario()
240 var result = Avx.GetLowerHalf<Int64>(_fld);
242 Unsafe.Write(_dataTable.outArrayPtr, result);
243 ValidateResult(_fld, _dataTable.outArrayPtr);
246 public void RunUnsupportedScenario()
252 RunBasicScenario_UnsafeRead();
254 catch (PlatformNotSupportedException)
260 private void ValidateResult(Vector256<Int64> firstOp, void* result, [CallerMemberName] string method = "")
262 Int64[] inArray = new Int64[Op1ElementCount];
263 Int64[] outArray = new Int64[RetElementCount];
265 Unsafe.Write(Unsafe.AsPointer(ref inArray[0]), firstOp);
266 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Int64, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
268 ValidateResult(inArray, outArray, method);
271 private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
273 Int64[] inArray = new Int64[Op1ElementCount];
274 Int64[] outArray = new Int64[RetElementCount];
276 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Int64, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), VectorSize);
277 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Int64, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
279 ValidateResult(inArray, outArray, method);
282 private void ValidateResult(Int64[] firstOp, Int64[] result, [CallerMemberName] string method = "")
284 if (firstOp[0] != result[0])
290 for (var i = 1; i < RetElementCount; i++)
292 if (result[i] != ((i < (Op1ElementCount / 2)) ? firstOp[i] : 0))
302 Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.GetLowerHalf)}<Int64>(Vector256<Int64>): {method} failed:");
303 Console.WriteLine($" firstOp: ({string.Join(", ", firstOp)})");
304 Console.WriteLine($" result: ({string.Join(", ", result)})");