Merge pull request #18504 from mikedn/comp-small
[platform/upstream/coreclr.git] / tests / src / JIT / HardwareIntrinsics / X86 / Sse2 / Insert.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 InsertUInt16129()
23         {
24             var test = new SimpleUnaryOpTest__InsertUInt16129();
25
26             if (test.IsSupported)
27             {
28                 // Validates basic functionality works, using Unsafe.Read
29                 test.RunBasicScenario_UnsafeRead();
30
31                 if (Sse2.IsSupported)
32                 {
33                     // Validates basic functionality works, using Load
34                     test.RunBasicScenario_Load();
35
36                     // Validates basic functionality works, using LoadAligned
37                     test.RunBasicScenario_LoadAligned();
38                 }
39
40                 // Validates calling via reflection works, using Unsafe.Read
41                 test.RunReflectionScenario_UnsafeRead();
42
43                 if (Sse2.IsSupported)
44                 {
45                     // Validates calling via reflection works, using Load
46                     test.RunReflectionScenario_Load();
47
48                     // Validates calling via reflection works, using LoadAligned
49                     test.RunReflectionScenario_LoadAligned();
50                 }
51
52                 // Validates passing a static member works
53                 test.RunClsVarScenario();
54
55                 // Validates passing a local works, using Unsafe.Read
56                 test.RunLclVarScenario_UnsafeRead();
57
58                 if (Sse2.IsSupported)
59                 {
60                     // Validates passing a local works, using Load
61                     test.RunLclVarScenario_Load();
62
63                     // Validates passing a local works, using LoadAligned
64                     test.RunLclVarScenario_LoadAligned();
65                 }
66
67                 // Validates passing the field of a local works
68                 test.RunLclFldScenario();
69
70                 // Validates passing an instance member works
71                 test.RunFldScenario();
72             }
73             else
74             {
75                 // Validates we throw on unsupported hardware
76                 test.RunUnsupportedScenario();
77             }
78
79             if (!test.Succeeded)
80             {
81                 throw new Exception("One or more scenarios did not complete as expected.");
82             }
83         }
84     }
85
86     public sealed unsafe class SimpleUnaryOpTest__InsertUInt16129
87     {
88         private static readonly int LargestVectorSize = 16;
89
90         private static readonly int Op1ElementCount = Unsafe.SizeOf<Vector128<UInt16>>() / sizeof(UInt16);
91         private static readonly int RetElementCount = Unsafe.SizeOf<Vector128<UInt16>>() / sizeof(UInt16);
92
93         private static UInt16[] _data = new UInt16[Op1ElementCount];
94
95         private static Vector128<UInt16> _clsVar;
96
97         private Vector128<UInt16> _fld;
98
99         private SimpleUnaryOpTest__DataTable<UInt16, UInt16> _dataTable;
100
101         static SimpleUnaryOpTest__InsertUInt16129()
102         {
103             var random = new Random();
104
105             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)0; }
106             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt16>, byte>(ref _clsVar), ref Unsafe.As<UInt16, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector128<UInt16>>());
107         }
108
109         public SimpleUnaryOpTest__InsertUInt16129()
110         {
111             Succeeded = true;
112
113             var random = new Random();
114
115             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)0; }
116             Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector128<UInt16>, byte>(ref _fld), ref Unsafe.As<UInt16, byte>(ref _data[0]), (uint)Unsafe.SizeOf<Vector128<UInt16>>());
117
118             for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (ushort)0; }
119             _dataTable = new SimpleUnaryOpTest__DataTable<UInt16, UInt16>(_data, new UInt16[RetElementCount], LargestVectorSize);
120         }
121
122         public bool IsSupported => Sse2.IsSupported && (Environment.Is64BitProcess || ((typeof(UInt16) != typeof(long)) && (typeof(UInt16) != typeof(ulong))));
123
124         public bool Succeeded { get; set; }
125
126         public void RunBasicScenario_UnsafeRead()
127         {
128             var result = Sse2.Insert(
129                 Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr),
130                 (ushort)2,
131                 129
132             );
133
134             Unsafe.Write(_dataTable.outArrayPtr, result);
135             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
136         }
137
138         public void RunBasicScenario_Load()
139         {
140             var result = Sse2.Insert(
141                 Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr)),
142                 (ushort)2,
143                 129
144             );
145
146             Unsafe.Write(_dataTable.outArrayPtr, result);
147             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
148         }
149
150         public void RunBasicScenario_LoadAligned()
151         {
152             var result = Sse2.Insert(
153                 Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr)),
154                 (ushort)2,
155                 129
156             );
157
158             Unsafe.Write(_dataTable.outArrayPtr, result);
159             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
160         }
161
162         public void RunReflectionScenario_UnsafeRead()
163         {
164             var result = typeof(Sse2).GetMethod(nameof(Sse2.Insert), new Type[] { typeof(Vector128<UInt16>), typeof(UInt16), typeof(byte) })
165                                      .Invoke(null, new object[] {
166                                         Unsafe.Read<Vector128<UInt16>>(_dataTable.inArrayPtr),
167                                         (ushort)2,
168                                         (byte)129
169                                      });
170
171             Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt16>)(result));
172             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
173         }
174
175         public void RunReflectionScenario_Load()
176         {
177             var result = typeof(Sse2).GetMethod(nameof(Sse2.Insert), new Type[] { typeof(Vector128<UInt16>), typeof(UInt16), typeof(byte) })
178                                      .Invoke(null, new object[] {
179                                         Sse2.LoadVector128((UInt16*)(_dataTable.inArrayPtr)),
180                                         (ushort)2,
181                                         (byte)129
182                                      });
183
184             Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt16>)(result));
185             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
186         }
187
188         public void RunReflectionScenario_LoadAligned()
189         {
190             var result = typeof(Sse2).GetMethod(nameof(Sse2.Insert), new Type[] { typeof(Vector128<UInt16>), typeof(UInt16), typeof(byte) })
191                                      .Invoke(null, new object[] {
192                                         Sse2.LoadAlignedVector128((UInt16*)(_dataTable.inArrayPtr)),
193                                         (ushort)2,
194                                         (byte)129
195                                      });
196
197             Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt16>)(result));
198             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
199         }
200
201         public void RunClsVarScenario()
202         {
203             var result = Sse2.Insert(
204                 _clsVar,
205                 (ushort)2,
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.Insert(firstOp, (ushort)2, 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.Insert(firstOp, (ushort)2, 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.Insert(firstOp, (ushort)2, 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__InsertUInt16129();
243             var result = Sse2.Insert(test._fld, (ushort)2, 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.Insert(_fld, (ushort)2, 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), (uint)Unsafe.SizeOf<Vector128<UInt16>>());
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), (uint)Unsafe.SizeOf<Vector128<UInt16>>());
288             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt16, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), (uint)Unsafe.SizeOf<Vector128<UInt16>>());
289
290             ValidateResult(inArray, outArray, method);
291         }
292
293         private void ValidateResult(UInt16[] firstOp, UInt16[] result, [CallerMemberName] string method = "")
294         {
295
296             for (var i = 0; i < RetElementCount; i++)
297             {
298                 if ((i == 1 ? result[i] != 2 : result[i] != 0))
299                 {
300                     Succeeded = false;
301                     break;
302                 }
303             }
304
305             if (!Succeeded)
306             {
307                 Console.WriteLine($"{nameof(Sse2)}.{nameof(Sse2.Insert)}<UInt16>(Vector128<UInt16><9>): {method} failed:");
308                 Console.WriteLine($"  firstOp: ({string.Join(", ", firstOp)})");
309                 Console.WriteLine($"   result: ({string.Join(", ", result)})");
310                 Console.WriteLine();
311             }
312         }
313     }
314 }