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 ShiftLeftLogicalUInt161()
24 var test = new SimpleUnaryOpTest__ShiftLeftLogicalUInt161();
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__ShiftLeftLogicalUInt161
88 private const int VectorSize = 16;
90 private const int Op1ElementCount = VectorSize / sizeof(UInt16);
91 private const int RetElementCount = VectorSize / sizeof(UInt16);
93 private static UInt16[] _data = new UInt16[Op1ElementCount];
95 private static Vector128<UInt16> _clsVar;
97 private Vector128<UInt16> _fld;
99 private SimpleUnaryOpTest__DataTable<UInt16, UInt16> _dataTable;
101 static SimpleUnaryOpTest__ShiftLeftLogicalUInt161()
103 var random = new Random();
105 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, ushort.MaxValue)); }
106 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt16>, byte>(ref _clsVar), ref Unsafe.As<UInt16, byte>(ref _data[0]), VectorSize);
109 public SimpleUnaryOpTest__ShiftLeftLogicalUInt161()
113 var random = new Random();
115 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, ushort.MaxValue)); }
116 Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt16>, byte>(ref _fld), ref Unsafe.As<UInt16, byte>(ref _data[0]), VectorSize);
118 for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, ushort.MaxValue)); }
119 _dataTable = new SimpleUnaryOpTest__DataTable<UInt16, UInt16>(_data, new UInt16[RetElementCount], VectorSize);
122 public bool IsSupported => Sse2.IsSupported;
124 public bool Succeeded { get; set; }
126 public void RunBasicScenario_UnsafeRead()
128 var result = Sse2.ShiftLeftLogical(
129 Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr),
133 Unsafe.Write(_dataTable.outArrayPtr, result);
134 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
137 public void RunBasicScenario_Load()
139 var result = Sse2.ShiftLeftLogical(
140 Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr)),
144 Unsafe.Write(_dataTable.outArrayPtr, result);
145 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
148 public void RunBasicScenario_LoadAligned()
150 var result = Sse2.ShiftLeftLogical(
151 Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr)),
155 Unsafe.Write(_dataTable.outArrayPtr, result);
156 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
159 public void RunReflectionScenario_UnsafeRead()
161 var result = typeof(Sse2).GetMethod(nameof(Sse2.ShiftLeftLogical), new Type[] { typeof(Vector128<UInt16>), typeof(byte) })
162 .Invoke(null, new object[] {
163 Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr),
167 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt16>)(result));
168 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
171 public void RunReflectionScenario_Load()
173 var result = typeof(Sse2).GetMethod(nameof(Sse2.ShiftLeftLogical), new Type[] { typeof(Vector128<UInt16>), typeof(byte) })
174 .Invoke(null, new object[] {
175 Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr)),
179 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt16>)(result));
180 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
183 public void RunReflectionScenario_LoadAligned()
185 var result = typeof(Sse2).GetMethod(nameof(Sse2.ShiftLeftLogical), new Type[] { typeof(Vector128<UInt16>), typeof(byte) })
186 .Invoke(null, new object[] {
187 Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr)),
191 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt16>)(result));
192 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
195 public void RunClsVarScenario()
197 var result = Sse2.ShiftLeftLogical(
202 Unsafe.Write(_dataTable.outArrayPtr, result);
203 ValidateResult(_clsVar, _dataTable.outArrayPtr);
206 public void RunLclVarScenario_UnsafeRead()
208 var firstOp = Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr);
209 var result = Sse2.ShiftLeftLogical(firstOp, 1);
211 Unsafe.Write(_dataTable.outArrayPtr, result);
212 ValidateResult(firstOp, _dataTable.outArrayPtr);
215 public void RunLclVarScenario_Load()
217 var firstOp = Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr));
218 var result = Sse2.ShiftLeftLogical(firstOp, 1);
220 Unsafe.Write(_dataTable.outArrayPtr, result);
221 ValidateResult(firstOp, _dataTable.outArrayPtr);
224 public void RunLclVarScenario_LoadAligned()
226 var firstOp = Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr));
227 var result = Sse2.ShiftLeftLogical(firstOp, 1);
229 Unsafe.Write(_dataTable.outArrayPtr, result);
230 ValidateResult(firstOp, _dataTable.outArrayPtr);
233 public void RunLclFldScenario()
235 var test = new SimpleUnaryOpTest__ShiftLeftLogicalUInt161();
236 var result = Sse2.ShiftLeftLogical(test._fld, 1);
238 Unsafe.Write(_dataTable.outArrayPtr, result);
239 ValidateResult(test._fld, _dataTable.outArrayPtr);
242 public void RunFldScenario()
244 var result = Sse2.ShiftLeftLogical(_fld, 1);
246 Unsafe.Write(_dataTable.outArrayPtr, result);
247 ValidateResult(_fld, _dataTable.outArrayPtr);
250 public void RunUnsupportedScenario()
256 RunBasicScenario_UnsafeRead();
258 catch (PlatformNotSupportedException)
264 private void ValidateResult(Vector128<UInt16> firstOp, void* result, [CallerMemberName] string method = "")
266 UInt16[] inArray = new UInt16[Op1ElementCount];
267 UInt16[] outArray = new UInt16[RetElementCount];
269 Unsafe.WriteUnaligned(ref Unsafe.As<UInt16, byte>(ref inArray[0]), firstOp);
270 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
272 ValidateResult(inArray, outArray, method);
275 private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
277 UInt16[] inArray = new UInt16[Op1ElementCount];
278 UInt16[] outArray = new UInt16[RetElementCount];
280 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), VectorSize);
281 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
283 ValidateResult(inArray, outArray, method);
286 private void ValidateResult(UInt16[] firstOp, UInt16[] result, [CallerMemberName] string method = "")
288 if ((ushort)(firstOp[0] << 1) != result[0])
294 for (var i = 1; i < RetElementCount; i++)
296 if ((ushort)(firstOp[i] << 1) != result[i])
306 Console.WriteLine($"{nameof(Sse2)}.{nameof(Sse2.ShiftLeftLogical)}<UInt16>(Vector128<UInt16><9>): {method} failed:");
307 Console.WriteLine($" firstOp: ({string.Join(", ", firstOp)})");
308 Console.WriteLine($" result: ({string.Join(", ", result)})");