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