Regenerating the x86 HWIntrinsics tests to no longer have a GC hole
[platform/upstream/coreclr.git] / tests / src / JIT / HardwareIntrinsics / X86 / Sse2 / Extract.UInt16.129.cs
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.
4
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  ******************************************************************************/
11
12 using System;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
15 using System.Runtime.Intrinsics;
16 using System.Runtime.Intrinsics.X86;
17
18 namespace JIT.HardwareIntrinsics.X86
19 {
20     public static partial class Program
21     {
22         private static void ExtractUInt16129()
23         {
24             var test = new SimpleUnaryOpTest__ExtractUInt16129();
25             
26             try
27             {
28             if (test.IsSupported)
29             {
30                 // Validates basic functionality works, using Unsafe.Read
31                 test.RunBasicScenario_UnsafeRead();
32
33                 if (Sse2.IsSupported)
34                 {
35                     // Validates basic functionality works, using Load
36                     test.RunBasicScenario_Load();
37
38                     // Validates basic functionality works, using LoadAligned
39                     test.RunBasicScenario_LoadAligned();
40                 }
41
42                 // Validates calling via reflection works, using Unsafe.Read
43                 test.RunReflectionScenario_UnsafeRead();
44
45                 if (Sse2.IsSupported)
46                 {
47                     // Validates calling via reflection works, using Load
48                     test.RunReflectionScenario_Load();
49
50                     // Validates calling via reflection works, using LoadAligned
51                     test.RunReflectionScenario_LoadAligned();
52                 }
53
54                 // Validates passing a static member works
55                 test.RunClsVarScenario();
56
57                 // Validates passing a local works, using Unsafe.Read
58                 test.RunLclVarScenario_UnsafeRead();
59
60                 if (Sse2.IsSupported)
61                 {
62                     // Validates passing a local works, using Load
63                     test.RunLclVarScenario_Load();
64
65                     // Validates passing a local works, using LoadAligned
66                     test.RunLclVarScenario_LoadAligned();
67                 }
68
69                 // Validates passing the field of a local works
70                 test.RunLclFldScenario();
71
72                 // Validates passing an instance member works
73                 test.RunFldScenario();
74             }
75             else
76             {
77                 // Validates we throw on unsupported hardware
78                 test.RunUnsupportedScenario();
79             }
80             }
81             catch (PlatformNotSupportedException)
82             {
83                 test.Succeeded = true;
84             }
85
86             if (!test.Succeeded)
87             {
88                 throw new Exception("One or more scenarios did not complete as expected.");
89             }
90         }
91     }
92
93     public sealed unsafe class SimpleUnaryOpTest__ExtractUInt16129
94     {
95         private const int VectorSize = 16;
96
97         private const int Op1ElementCount = VectorSize / sizeof(UInt16);
98         private const int RetElementCount = VectorSize / sizeof(UInt16);
99
100         private static UInt16[] _data = new UInt16[Op1ElementCount];
101
102         private static Vector128<UInt16> _clsVar;
103
104         private Vector128<UInt16> _fld;
105
106         private SimpleUnaryOpTest__DataTable<UInt16, UInt16> _dataTable;
107
108         static SimpleUnaryOpTest__ExtractUInt16129()
109         {
110             var random = new Random();
111
112             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, int.MaxValue)); }
113             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt16>, byte>(ref _clsVar), ref Unsafe.As<UInt16, byte>(ref _data[0]), VectorSize);
114         }
115
116         public SimpleUnaryOpTest__ExtractUInt16129()
117         {
118             Succeeded = true;
119
120             var random = new Random();
121
122             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, int.MaxValue)); }
123             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt16>, byte>(ref _fld), ref Unsafe.As<UInt16, byte>(ref _data[0]), VectorSize);
124
125             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)(random.Next(0, int.MaxValue)); }
126             _dataTable = new SimpleUnaryOpTest__DataTable<UInt16, UInt16>(_data, new UInt16[RetElementCount], VectorSize);
127         }
128
129         public bool IsSupported => Sse2.IsSupported;
130
131         public bool Succeeded { get; set; }
132
133         public void RunBasicScenario_UnsafeRead()
134         {
135             var result = Sse2.Extract(
136                 Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr),
137                 129
138             );
139
140             Unsafe.Write(_dataTable.outArrayPtr, result);
141             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
142         }
143
144         public void RunBasicScenario_Load()
145         {
146             var result = Sse2.Extract(
147                 Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr)),
148                 129
149             );
150
151             Unsafe.Write(_dataTable.outArrayPtr, result);
152             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
153         }
154
155         public void RunBasicScenario_LoadAligned()
156         {
157             var result = Sse2.Extract(
158                 Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr)),
159                 129
160             );
161
162             Unsafe.Write(_dataTable.outArrayPtr, result);
163             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
164         }
165
166         public void RunReflectionScenario_UnsafeRead()
167         {
168             var result = typeof(Sse2).GetMethod(nameof(Sse2.Extract), new Type[] { typeof(Vector128<UInt16>), typeof(byte) })
169                                      .Invoke(null, new object[] {
170                                         Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr),
171                                         (byte)129
172                                      });
173
174             Unsafe.Write(_dataTable.outArrayPtr, (UInt16)(result));
175             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
176         }
177
178         public void RunReflectionScenario_Load()
179         {
180             var result = typeof(Sse2).GetMethod(nameof(Sse2.Extract), new Type[] { typeof(Vector128<UInt16>), typeof(byte) })
181                                      .Invoke(null, new object[] {
182                                         Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr)),
183                                         (byte)129
184                                      });
185
186             Unsafe.Write(_dataTable.outArrayPtr, (UInt16)(result));
187             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
188         }
189
190         public void RunReflectionScenario_LoadAligned()
191         {
192             var result = typeof(Sse2).GetMethod(nameof(Sse2.Extract), new Type[] { typeof(Vector128<UInt16>), typeof(byte) })
193                                      .Invoke(null, new object[] {
194                                         Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr)),
195                                         (byte)129
196                                      });
197
198             Unsafe.Write(_dataTable.outArrayPtr, (UInt16)(result));
199             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
200         }
201
202         public void RunClsVarScenario()
203         {
204             var result = Sse2.Extract(
205                 _clsVar,
206                 129
207             );
208
209             Unsafe.Write(_dataTable.outArrayPtr, result);
210             ValidateResult(_clsVar, _dataTable.outArrayPtr);
211         }
212
213         public void RunLclVarScenario_UnsafeRead()
214         {
215             var firstOp = Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr);
216             var result = Sse2.Extract(firstOp, 129);
217
218             Unsafe.Write(_dataTable.outArrayPtr, result);
219             ValidateResult(firstOp, _dataTable.outArrayPtr);
220         }
221
222         public void RunLclVarScenario_Load()
223         {
224             var firstOp = Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr));
225             var result = Sse2.Extract(firstOp, 129);
226
227             Unsafe.Write(_dataTable.outArrayPtr, result);
228             ValidateResult(firstOp, _dataTable.outArrayPtr);
229         }
230
231         public void RunLclVarScenario_LoadAligned()
232         {
233             var firstOp = Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr));
234             var result = Sse2.Extract(firstOp, 129);
235
236             Unsafe.Write(_dataTable.outArrayPtr, result);
237             ValidateResult(firstOp, _dataTable.outArrayPtr);
238         }
239
240         public void RunLclFldScenario()
241         {
242             var test = new SimpleUnaryOpTest__ExtractUInt16129();
243             var result = Sse2.Extract(test._fld, 129);
244
245             Unsafe.Write(_dataTable.outArrayPtr, result);
246             ValidateResult(test._fld, _dataTable.outArrayPtr);
247         }
248
249         public void RunFldScenario()
250         {
251             var result = Sse2.Extract(_fld, 129);
252
253             Unsafe.Write(_dataTable.outArrayPtr, result);
254             ValidateResult(_fld, _dataTable.outArrayPtr);
255         }
256
257         public void RunUnsupportedScenario()
258         {
259             Succeeded = false;
260
261             try
262             {
263                 RunBasicScenario_UnsafeRead();
264             }
265             catch (PlatformNotSupportedException)
266             {
267                 Succeeded = true;
268             }
269         }
270
271         private void ValidateResult(Vector128<UInt16> firstOp, void* result, [CallerMemberName] string method = "")
272         {
273             UInt16[] inArray = new UInt16[Op1ElementCount];
274             UInt16[] outArray = new UInt16[RetElementCount];
275
276             Unsafe.WriteUnaligned(ref Unsafe.As<UInt16, byte>(ref inArray[0]), firstOp);
277             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
278
279             ValidateResult(inArray, outArray, method);
280         }
281
282         private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
283         {
284             UInt16[] inArray = new UInt16[Op1ElementCount];
285             UInt16[] outArray = new UInt16[RetElementCount];
286
287             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), VectorSize);
288             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
289
290             ValidateResult(inArray, outArray, method);
291         }
292
293         private void ValidateResult(UInt16[] firstOp, UInt16[] result, [CallerMemberName] string method = "")
294         {
295             if ((result[0] != firstOp[1]))
296             {
297                 Succeeded = false;
298             }
299
300             if (!Succeeded)
301             {
302                 Console.WriteLine($"{nameof(Sse2)}.{nameof(Sse2.Extract)}<UInt16>(Vector128<UInt16><9>): {method} failed:");
303                 Console.WriteLine($"  firstOp: ({string.Join(", ", firstOp)})");
304                 Console.WriteLine($"   result: ({string.Join(", ", result)})");
305                 Console.WriteLine();
306             }
307         }
308     }
309 }