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;
19 using static System.Runtime.Intrinsics.X86.Sse2;
21 namespace JIT.HardwareIntrinsics.X86
23 public static partial class Program
25 private static void InsertVector128Int641()
27 var test = new SimpleBinaryOpTest__InsertVector128Int641();
31 // Validates basic functionality works, using Unsafe.Read
32 test.RunBasicScenario_UnsafeRead();
36 // Validates basic functionality works, using Load
37 test.RunBasicScenario_Load();
39 // Validates basic functionality works, using LoadAligned
40 test.RunBasicScenario_LoadAligned();
43 // Validates calling via reflection works, using Unsafe.Read
44 test.RunReflectionScenario_UnsafeRead();
48 // Validates calling via reflection works, using Load
49 test.RunReflectionScenario_Load();
51 // Validates calling via reflection works, using LoadAligned
52 test.RunReflectionScenario_LoadAligned();
55 // Validates passing a static member works
56 test.RunClsVarScenario();
58 // Validates passing a local works, using Unsafe.Read
59 test.RunLclVarScenario_UnsafeRead();
63 // Validates passing a local works, using Load
64 test.RunLclVarScenario_Load();
66 // Validates passing a local works, using LoadAligned
67 test.RunLclVarScenario_LoadAligned();
70 // Validates passing the field of a local works
71 test.RunLclFldScenario();
73 // Validates passing an instance member works
74 test.RunFldScenario();
78 // Validates we throw on unsupported hardware
79 test.RunUnsupportedScenario();
84 throw new Exception("One or more scenarios did not complete as expected.");
89 public sealed unsafe class SimpleBinaryOpTest__InsertVector128Int641
91 private static readonly int LargestVectorSize = 32;
93 private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector256<Int64>>() / sizeof(Int64);
94 private static readonly int Op2ElementCount = Unsafe.SizeOf<Vector128<Int64>>() / sizeof(Int64);
95 private static readonly int RetElementCount = Unsafe.SizeOf<Vector256<Int64>>() / sizeof(Int64);
97 private static Int64[] _data1 = new Int64[Op1ElementCount];
98 private static Int64[] _data2 = new Int64[Op2ElementCount];
100 private static Vector256<Int64> _clsVar1;
101 private static Vector128<Int64> _clsVar2;
103 private Vector256<Int64> _fld1;
104 private Vector128<Int64> _fld2;
106 private SimpleBinaryOpTest__DataTable<Int64, Int64, Int64> _dataTable;
108 static SimpleBinaryOpTest__InsertVector128Int641()
110 var random = new Random();
112 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (long)(random.Next(0, int.MaxValue)); }
113 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Int64>, byte>(ref _clsVar1), ref Unsafe.As<Int64, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector256<Int64>>());
114 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (long)(random.Next(0, int.MaxValue)); }
115 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Int64>, byte>(ref _clsVar2), ref Unsafe.As<Int64, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Int64>>());
118 public SimpleBinaryOpTest__InsertVector128Int641()
122 var random = new Random();
124 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (long)(random.Next(0, int.MaxValue)); }
125 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Int64>, byte>(ref _fld1), ref Unsafe.As<Int64, byte>(ref _data1[0]), (uint)Unsafe.SizeOf<Vector256<Int64>>());
126 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (long)(random.Next(0, int.MaxValue)); }
127 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Int64>, byte>(ref _fld2), ref Unsafe.As<Int64, byte>(ref _data2[0]), (uint)Unsafe.SizeOf<Vector128<Int64>>());
129 for (var i = 0; i < Op1ElementCount; i++) { _data1[i] = (long)(random.Next(0, int.MaxValue)); }
130 for (var i = 0; i < Op2ElementCount; i++) { _data2[i] = (long)(random.Next(0, int.MaxValue)); }
131 _dataTable = new SimpleBinaryOpTest__DataTable<Int64, Int64, Int64>(_data1, _data2, new Int64[RetElementCount], LargestVectorSize);
134 public bool IsSupported => Avx.IsSupported;
136 public bool Succeeded { get; set; }
138 public void RunBasicScenario_UnsafeRead()
140 var result = Avx.InsertVector128(
141 Unsafe.Read<Vector256<Int64>>(_dataTable.inArray1Ptr),
142 Unsafe.Read<Vector128<Int64>>(_dataTable.inArray2Ptr),
146 Unsafe.Write(_dataTable.outArrayPtr, result);
147 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
150 public void RunBasicScenario_Load()
152 var result = Avx.InsertVector128(
153 Avx.LoadVector256((Int64*)(_dataTable.inArray1Ptr)),
154 LoadVector128((Int64*)(_dataTable.inArray2Ptr)),
158 Unsafe.Write(_dataTable.outArrayPtr, result);
159 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
162 public void RunBasicScenario_LoadAligned()
164 var result = Avx.InsertVector128(
165 Avx.LoadAlignedVector256((Int64*)(_dataTable.inArray1Ptr)),
166 LoadAlignedVector128((Int64*)(_dataTable.inArray2Ptr)),
170 Unsafe.Write(_dataTable.outArrayPtr, result);
171 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
174 public void RunReflectionScenario_UnsafeRead()
176 var result = typeof(Avx).GetMethods(BindingFlags.Public | BindingFlags.Static)
177 .FirstOrDefault( mi => mi.Name == nameof(Avx.InsertVector128) && mi.IsGenericMethod)
178 .MakeGenericMethod(new[] { typeof(Int64) })
179 .Invoke(null, new object[] {
180 Unsafe.Read<Vector256<Int64>>(_dataTable.inArray1Ptr),
181 Unsafe.Read<Vector128<Int64>>(_dataTable.inArray2Ptr),
185 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Int64>)(result));
186 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
189 public void RunReflectionScenario_Load()
191 var result = typeof(Avx).GetMethods(BindingFlags.Public | BindingFlags.Static)
192 .FirstOrDefault( mi => mi.Name == nameof(Avx.InsertVector128) && mi.IsGenericMethod)
193 .MakeGenericMethod(new[] { typeof(Int64) })
194 .Invoke(null, new object[] {
195 Avx.LoadVector256((Int64*)(_dataTable.inArray1Ptr)),
196 LoadVector128((Int64*)(_dataTable.inArray2Ptr)),
200 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Int64>)(result));
201 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
204 public void RunReflectionScenario_LoadAligned()
206 var result = typeof(Avx).GetMethods(BindingFlags.Public | BindingFlags.Static)
207 .FirstOrDefault( mi => mi.Name == nameof(Avx.InsertVector128) && mi.IsGenericMethod)
208 .MakeGenericMethod(new[] { typeof(Int64) })
209 .Invoke(null, new object[] {
210 Avx.LoadAlignedVector256((Int64*)(_dataTable.inArray1Ptr)),
211 LoadAlignedVector128((Int64*)(_dataTable.inArray2Ptr)),
215 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Int64>)(result));
216 ValidateResult(_dataTable.inArray1Ptr, _dataTable.inArray2Ptr, _dataTable.outArrayPtr);
219 public void RunClsVarScenario()
221 var result = Avx.InsertVector128(
227 Unsafe.Write(_dataTable.outArrayPtr, result);
228 ValidateResult(_clsVar1, _clsVar2, _dataTable.outArrayPtr);
231 public void RunLclVarScenario_UnsafeRead()
233 var left = Unsafe.Read<Vector256<Int64>>(_dataTable.inArray1Ptr);
234 var right = Unsafe.Read<Vector128<Int64>>(_dataTable.inArray2Ptr);
235 var result = Avx.InsertVector128(left, right, 1);
237 Unsafe.Write(_dataTable.outArrayPtr, result);
238 ValidateResult(left, right, _dataTable.outArrayPtr);
241 public void RunLclVarScenario_Load()
243 var left = Avx.LoadVector256((Int64*)(_dataTable.inArray1Ptr));
244 var right = LoadVector128((Int64*)(_dataTable.inArray2Ptr));
245 var result = Avx.InsertVector128(left, right, 1);
247 Unsafe.Write(_dataTable.outArrayPtr, result);
248 ValidateResult(left, right, _dataTable.outArrayPtr);
251 public void RunLclVarScenario_LoadAligned()
253 var left = Avx.LoadAlignedVector256((Int64*)(_dataTable.inArray1Ptr));
254 var right = LoadAlignedVector128((Int64*)(_dataTable.inArray2Ptr));
255 var result = Avx.InsertVector128(left, right, 1);
257 Unsafe.Write(_dataTable.outArrayPtr, result);
258 ValidateResult(left, right, _dataTable.outArrayPtr);
261 public void RunLclFldScenario()
263 var test = new SimpleBinaryOpTest__InsertVector128Int641();
264 var result = Avx.InsertVector128(test._fld1, test._fld2, 1);
266 Unsafe.Write(_dataTable.outArrayPtr, result);
267 ValidateResult(test._fld1, test._fld2, _dataTable.outArrayPtr);
270 public void RunFldScenario()
272 var result = Avx.InsertVector128(_fld1, _fld2, 1);
274 Unsafe.Write(_dataTable.outArrayPtr, result);
275 ValidateResult(_fld1, _fld2, _dataTable.outArrayPtr);
278 public void RunUnsupportedScenario()
284 RunBasicScenario_UnsafeRead();
286 catch (PlatformNotSupportedException)
292 private void ValidateResult(Vector256<Int64> left, Vector128<Int64> right, void* result, [CallerMemberName] string method = "")
294 Int64[] inArray1 = new Int64[Op1ElementCount];
295 Int64[] inArray2 = new Int64[Op2ElementCount];
296 Int64[] outArray = new Int64[RetElementCount];
298 Unsafe.WriteUnaligned(ref Unsafe.As<Int64, byte>(ref inArray1[0]), left);
299 Unsafe.WriteUnaligned(ref Unsafe.As<Int64, byte>(ref inArray2[0]), right);
300 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Int64, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<Int64>>());
302 ValidateResult(inArray1, inArray2, outArray, method);
305 private void ValidateResult(void* left, void* right, void* result, [CallerMemberName] string method = "")
307 Int64[] inArray1 = new Int64[Op1ElementCount];
308 Int64[] inArray2 = new Int64[Op2ElementCount];
309 Int64[] outArray = new Int64[RetElementCount];
311 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Int64, byte>(ref inArray1[0]), ref Unsafe.AsRef<byte>(left), (uint)Unsafe.SizeOf<Vector256<Int64>>());
312 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Int64, byte>(ref inArray2[0]), ref Unsafe.AsRef<byte>(right), (uint)Unsafe.SizeOf<Vector128<Int64>>());
313 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Int64, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<Int64>>());
315 ValidateResult(inArray1, inArray2, outArray, method);
318 private void ValidateResult(Int64[] left, Int64[] right, Int64[] result, [CallerMemberName] string method = "")
320 if (result[0] != left[0])
326 for (var i = 1; i < RetElementCount; i++)
328 if ((i > 1 ? result[i] != right[i - 2] : result[i] != left[i]))
338 Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.InsertVector128)}<Int64>(Vector256<Int64>, Vector128<Int64>.1): {method} failed:");
339 Console.WriteLine($" left: ({string.Join(", ", left)})");
340 Console.WriteLine($" right: ({string.Join(", ", right)})");
341 Console.WriteLine($" result: ({string.Join(", ", result)})");