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 {Method}{RetBaseType}{Imm}()
24 var test = new SimpleUnaryOpTest__{Method}{RetBaseType}{Imm}();
28 // Validates basic functionality works, using Unsafe.Read
29 test.RunBasicScenario_UnsafeRead();
31 if ({LoadIsa}.IsSupported)
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();
43 if ({LoadIsa}.IsSupported)
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();
58 if ({LoadIsa}.IsSupported)
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__{Method}{RetBaseType}{Imm}
88 private static readonly int LargestVectorSize = {LargestVectorSize};
90 private static readonly int Op1ElementCount = Unsafe.SizeOf<{Op1VectorType}<{Op1BaseType}>>() / sizeof({Op1BaseType});
91 private static readonly int RetElementCount = Unsafe.SizeOf<{RetVectorType}<{RetBaseType}>>() / sizeof({RetBaseType});
93 private static {Op1BaseType}[] _data = new {Op1BaseType}[Op1ElementCount];
95 private static {Op1VectorType}<{Op1BaseType}> _clsVar;
97 private {Op1VectorType}<{Op1BaseType}> _fld;
99 private SimpleUnaryOpTest__DataTable<{RetBaseType}, {Op1BaseType}> _dataTable;
101 static SimpleUnaryOpTest__{Method}{RetBaseType}{Imm}()
103 var random = new Random();
105 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = {NextValueOp1}; }
106 Unsafe.CopyBlockUnaligned(ref Unsafe.As<{Op1VectorType}<{Op1BaseType}>, byte>(ref _clsVar), ref Unsafe.As<{Op1BaseType}, byte>(ref _data[0]), (uint)Unsafe.SizeOf<{Op1VectorType}<{Op1BaseType}>>());
109 public SimpleUnaryOpTest__{Method}{RetBaseType}{Imm}()
113 var random = new Random();
115 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = {NextValueOp1}; }
116 Unsafe.CopyBlockUnaligned(ref Unsafe.As<{Op1VectorType}<{Op1BaseType}>, byte>(ref _fld), ref Unsafe.As<{Op1BaseType}, byte>(ref _data[0]), (uint)Unsafe.SizeOf<{Op1VectorType}<{Op1BaseType}>>());
118 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = {NextValueOp1}; }
119 _dataTable = new SimpleUnaryOpTest__DataTable<{RetBaseType}, {Op1BaseType}>(_data, new {RetBaseType}[RetElementCount], LargestVectorSize);
122 public bool IsSupported => {Isa}.IsSupported && (Environment.Is64BitProcess || ((typeof({RetBaseType}) != typeof(long)) && (typeof({RetBaseType}) != typeof(ulong))));
124 public bool Succeeded { get; set; }
126 public void RunBasicScenario_UnsafeRead()
128 var result = {Isa}.{Method}(
129 Unsafe.Read<{Op1VectorType}<{Op1BaseType}>>(_dataTable.inArrayPtr),
134 Unsafe.Write(_dataTable.outArrayPtr, result);
135 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
138 public void RunBasicScenario_Load()
140 var result = {Isa}.{Method}(
141 {LoadIsa}.Load{Op1VectorType}(({Op1BaseType}*)(_dataTable.inArrayPtr)),
146 Unsafe.Write(_dataTable.outArrayPtr, result);
147 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
150 public void RunBasicScenario_LoadAligned()
152 var result = {Isa}.{Method}(
153 {LoadIsa}.LoadAligned{Op1VectorType}(({Op1BaseType}*)(_dataTable.inArrayPtr)),
158 Unsafe.Write(_dataTable.outArrayPtr, result);
159 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
162 public void RunReflectionScenario_UnsafeRead()
164 var result = typeof({Isa}).GetMethod(nameof({Isa}.{Method}), new Type[] { typeof({Op1VectorType}<{Op1BaseType}>), typeof({RetBaseType}), typeof(byte) })
165 .Invoke(null, new object[] {
166 Unsafe.Read<{Op1VectorType}<{Op1BaseType}>>(_dataTable.inArrayPtr),
171 Unsafe.Write(_dataTable.outArrayPtr, ({RetVectorType}<{RetBaseType}>)(result));
172 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
175 public void RunReflectionScenario_Load()
177 var result = typeof({Isa}).GetMethod(nameof({Isa}.{Method}), new Type[] { typeof({Op1VectorType}<{Op1BaseType}>), typeof({RetBaseType}), typeof(byte) })
178 .Invoke(null, new object[] {
179 {LoadIsa}.Load{Op1VectorType}(({Op1BaseType}*)(_dataTable.inArrayPtr)),
184 Unsafe.Write(_dataTable.outArrayPtr, ({RetVectorType}<{RetBaseType}>)(result));
185 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
188 public void RunReflectionScenario_LoadAligned()
190 var result = typeof({Isa}).GetMethod(nameof({Isa}.{Method}), new Type[] { typeof({Op1VectorType}<{Op1BaseType}>), typeof({RetBaseType}), typeof(byte) })
191 .Invoke(null, new object[] {
192 {LoadIsa}.LoadAligned{Op1VectorType}(({Op1BaseType}*)(_dataTable.inArrayPtr)),
197 Unsafe.Write(_dataTable.outArrayPtr, ({RetVectorType}<{RetBaseType}>)(result));
198 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
201 public void RunClsVarScenario()
203 var result = {Isa}.{Method}(
209 Unsafe.Write(_dataTable.outArrayPtr, result);
210 ValidateResult(_clsVar, _dataTable.outArrayPtr);
213 public void RunLclVarScenario_UnsafeRead()
215 var firstOp = Unsafe.Read<{Op1VectorType}<{Op1BaseType}>>(_dataTable.inArrayPtr);
216 var result = {Isa}.{Method}(firstOp, {Data}, {Imm});
218 Unsafe.Write(_dataTable.outArrayPtr, result);
219 ValidateResult(firstOp, _dataTable.outArrayPtr);
222 public void RunLclVarScenario_Load()
224 var firstOp = {LoadIsa}.Load{Op1VectorType}(({Op1BaseType}*)(_dataTable.inArrayPtr));
225 var result = {Isa}.{Method}(firstOp, {Data}, {Imm});
227 Unsafe.Write(_dataTable.outArrayPtr, result);
228 ValidateResult(firstOp, _dataTable.outArrayPtr);
231 public void RunLclVarScenario_LoadAligned()
233 var firstOp = {LoadIsa}.LoadAligned{Op1VectorType}(({Op1BaseType}*)(_dataTable.inArrayPtr));
234 var result = {Isa}.{Method}(firstOp, {Data}, {Imm});
236 Unsafe.Write(_dataTable.outArrayPtr, result);
237 ValidateResult(firstOp, _dataTable.outArrayPtr);
240 public void RunLclFldScenario()
242 var test = new SimpleUnaryOpTest__{Method}{RetBaseType}{Imm}();
243 var result = {Isa}.{Method}(test._fld, {Data}, {Imm});
245 Unsafe.Write(_dataTable.outArrayPtr, result);
246 ValidateResult(test._fld, _dataTable.outArrayPtr);
249 public void RunFldScenario()
251 var result = {Isa}.{Method}(_fld, {Data}, {Imm});
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({Op1VectorType}<{Op1BaseType}> firstOp, void* result, [CallerMemberName] string method = "")
273 {Op1BaseType}[] inArray = new {Op1BaseType}[Op1ElementCount];
274 {RetBaseType}[] outArray = new {RetBaseType}[RetElementCount];
276 Unsafe.WriteUnaligned(ref Unsafe.As<{Op1BaseType}, byte>(ref inArray[0]), firstOp);
277 Unsafe.CopyBlockUnaligned(ref Unsafe.As<{RetBaseType}, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<{RetVectorType}<{RetBaseType}>>());
279 ValidateResult(inArray, outArray, method);
282 private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
284 {Op1BaseType}[] inArray = new {Op1BaseType}[Op1ElementCount];
285 {RetBaseType}[] outArray = new {RetBaseType}[RetElementCount];
287 Unsafe.CopyBlockUnaligned(ref Unsafe.As<{Op1BaseType}, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), (uint)Unsafe.SizeOf<{Op1VectorType}<{Op1BaseType}>>());
288 Unsafe.CopyBlockUnaligned(ref Unsafe.As<{RetBaseType}, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<{RetVectorType}<{RetBaseType}>>());
290 ValidateResult(inArray, outArray, method);
293 private void ValidateResult({Op1BaseType}[] firstOp, {RetBaseType}[] result, [CallerMemberName] string method = "")
296 for (var i = 0; i < RetElementCount; i++)
298 if ({ValidateFirstResult})
307 Console.WriteLine($"{nameof({Isa})}.{nameof({Isa}.{Method})}<{RetBaseType}>({Op1VectorType}<{Op1BaseType}><9>): {method} failed:");
308 Console.WriteLine($" firstOp: ({string.Join(", ", firstOp)})");
309 Console.WriteLine($" result: ({string.Join(", ", result)})");