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