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 ExtendToVector256Single()
24 var test = new GenericUnaryOpTest__ExtendToVector256Single();
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 class works
68 test.RunClassLclFldScenario();
70 // Validates passing an instance member of a class works
71 test.RunClassFldScenario();
73 // Validates passing the field of a local struct works
74 test.RunStructLclFldScenario();
76 // Validates passing an instance member of a struct works
77 test.RunStructFldScenario();
81 // Validates we throw on unsupported hardware
82 test.RunUnsupportedScenario();
87 throw new Exception("One or more scenarios did not complete as expected.");
92 public sealed unsafe class GenericUnaryOpTest__ExtendToVector256Single
94 private struct TestStruct
96 public Vector128<Single> _fld;
98 public static TestStruct Create()
100 var testStruct = new TestStruct();
101 var random = new Random();
103 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = TestLibrary.Generator.GetSingle(); }
104 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref testStruct._fld), ref Unsafe.As<Single, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
109 public void RunStructFldScenario(GenericUnaryOpTest__ExtendToVector256Single testClass)
111 var result = Avx.ExtendToVector256<Single>(_fld);
113 Unsafe.Write(testClass._dataTable.outArrayPtr, result);
114 testClass.ValidateResult(_fld, testClass._dataTable.outArrayPtr);
118 private static readonly int LargestVectorSize = 32;
120 private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector128<Single>>() / sizeof(Single);
121 private static readonly int RetElementCount = Unsafe.SizeOf<Vector256<Single>>() / sizeof(Single);
123 private static Single[] _data = new Single[Op1ElementCount];
125 private static Vector128<Single> _clsVar;
127 private Vector128<Single> _fld;
129 private SimpleUnaryOpTest__DataTable<Single, Single> _dataTable;
131 static GenericUnaryOpTest__ExtendToVector256Single()
133 var random = new Random();
135 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = TestLibrary.Generator.GetSingle(); }
136 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _clsVar), ref Unsafe.As<Single, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
139 public GenericUnaryOpTest__ExtendToVector256Single()
143 var random = new Random();
145 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = TestLibrary.Generator.GetSingle(); }
146 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<Single>, byte>(ref _fld), ref Unsafe.As<Single, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector128<Single>>());
148 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = TestLibrary.Generator.GetSingle(); }
149 _dataTable = new SimpleUnaryOpTest__DataTable<Single, Single>(_data, new Single[RetElementCount], LargestVectorSize);
152 public bool IsSupported => Avx.IsSupported;
154 public bool Succeeded { get; set; }
156 public void RunBasicScenario_UnsafeRead()
158 var result = Avx.ExtendToVector256<Single>(
159 Unsafe.Read<Vector128<Single>>(_dataTable.inArrayPtr)
162 Unsafe.Write(_dataTable.outArrayPtr, result);
163 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
166 public void RunBasicScenario_Load()
168 var result = Avx.ExtendToVector256<Single>(
169 Sse.LoadVector128((Single*)(_dataTable.inArrayPtr))
172 Unsafe.Write(_dataTable.outArrayPtr, result);
173 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
176 public void RunBasicScenario_LoadAligned()
178 var result = Avx.ExtendToVector256<Single>(
179 Sse.LoadAlignedVector128((Single*)(_dataTable.inArrayPtr))
182 Unsafe.Write(_dataTable.outArrayPtr, result);
183 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
186 public void RunReflectionScenario_UnsafeRead()
188 var result = typeof(Avx).GetMethod(nameof(Avx.ExtendToVector256))
189 .MakeGenericMethod( new Type[] { typeof(Single) })
190 .Invoke(null, new object[] {
191 Unsafe.Read<Vector128<Single>>(_dataTable.inArrayPtr)
194 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Single>)(result));
195 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
198 public void RunReflectionScenario_Load()
200 var result = typeof(Avx).GetMethod(nameof(Avx.ExtendToVector256))
201 .MakeGenericMethod( new Type[] { typeof(Single) })
202 .Invoke(null, new object[] {
203 Sse.LoadVector128((Single*)(_dataTable.inArrayPtr))
206 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Single>)(result));
207 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
210 public void RunReflectionScenario_LoadAligned()
212 var result = typeof(Avx).GetMethod(nameof(Avx.ExtendToVector256))
213 .MakeGenericMethod( new Type[] { typeof(Single) })
214 .Invoke(null, new object[] {
215 Sse.LoadAlignedVector128((Single*)(_dataTable.inArrayPtr))
218 Unsafe.Write(_dataTable.outArrayPtr, (Vector256<Single>)(result));
219 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
222 public void RunClsVarScenario()
224 var result = Avx.ExtendToVector256<Single>(
228 Unsafe.Write(_dataTable.outArrayPtr, result);
229 ValidateResult(_clsVar, _dataTable.outArrayPtr);
232 public void RunLclVarScenario_UnsafeRead()
234 var firstOp = Unsafe.Read<Vector128<Single>>(_dataTable.inArrayPtr);
235 var result = Avx.ExtendToVector256<Single>(firstOp);
237 Unsafe.Write(_dataTable.outArrayPtr, result);
238 ValidateResult(firstOp, _dataTable.outArrayPtr);
241 public void RunLclVarScenario_Load()
243 var firstOp = Sse.LoadVector128((Single*)(_dataTable.inArrayPtr));
244 var result = Avx.ExtendToVector256<Single>(firstOp);
246 Unsafe.Write(_dataTable.outArrayPtr, result);
247 ValidateResult(firstOp, _dataTable.outArrayPtr);
250 public void RunLclVarScenario_LoadAligned()
252 var firstOp = Sse.LoadAlignedVector128((Single*)(_dataTable.inArrayPtr));
253 var result = Avx.ExtendToVector256<Single>(firstOp);
255 Unsafe.Write(_dataTable.outArrayPtr, result);
256 ValidateResult(firstOp, _dataTable.outArrayPtr);
259 public void RunClassLclFldScenario()
261 var test = new GenericUnaryOpTest__ExtendToVector256Single();
262 var result = Avx.ExtendToVector256<Single>(test._fld);
264 Unsafe.Write(_dataTable.outArrayPtr, result);
265 ValidateResult(test._fld, _dataTable.outArrayPtr);
268 public void RunClassFldScenario()
270 var result = Avx.ExtendToVector256<Single>(_fld);
272 Unsafe.Write(_dataTable.outArrayPtr, result);
273 ValidateResult(_fld, _dataTable.outArrayPtr);
276 public void RunStructLclFldScenario()
278 var test = TestStruct.Create();
279 var result = Avx.ExtendToVector256(test._fld);
281 Unsafe.Write(_dataTable.outArrayPtr, result);
282 ValidateResult(test._fld, _dataTable.outArrayPtr);
285 public void RunStructFldScenario()
287 var test = TestStruct.Create();
288 test.RunStructFldScenario(this);
291 public void RunUnsupportedScenario()
297 RunBasicScenario_UnsafeRead();
299 catch (PlatformNotSupportedException)
305 private void ValidateResult(Vector128<Single> firstOp, void* result, [CallerMemberName] string method = "")
307 Single[] inArray = new Single[Op1ElementCount];
308 Single[] outArray = new Single[RetElementCount];
310 Unsafe.WriteUnaligned(ref Unsafe.As<Single, byte>(ref inArray[0]), firstOp);
311 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<Single>>());
313 ValidateResult(inArray, outArray, method);
316 private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
318 Single[] inArray = new Single[Op1ElementCount];
319 Single[] outArray = new Single[RetElementCount];
321 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), (uint)Unsafe.SizeOf<Vector128<Single>>());
322 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Single, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector256<Single>>());
324 ValidateResult(inArray, outArray, method);
327 private void ValidateResult(Single[] firstOp, Single[] result, [CallerMemberName] string method = "")
329 if (firstOp[0] != result[0])
335 for (var i = 1; i < RetElementCount; i++)
337 if (result[i] != ((i < (RetElementCount / 2)) ? firstOp[i] : 0))
347 Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.ExtendToVector256)}<Single>(Vector128<Single>): {method} failed:");
348 Console.WriteLine($" firstOp: ({string.Join(", ", firstOp)})");
349 Console.WriteLine($" result: ({string.Join(", ", result)})");