21890aa77152102a4fbf5335bf4eb099da0d405e
[platform/upstream/coreclr.git] / tests / src / JIT / HardwareIntrinsics / X86 / Sse2 / SetAllVector128.UInt32.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 SetAllVector128UInt32()
23         {
24             bool skipIf32Bit = typeof(UInt32) == typeof(Int64) ? true :
25                                      typeof(UInt32) == typeof(UInt64) ? true : false;
26
27             if (skipIf32Bit && !Environment.Is64BitProcess)
28             {
29                 return;
30             }
31
32             var test = new SimpleScalarUnaryOpTest__SetAllVector128UInt32();
33
34             if (test.IsSupported)
35             {
36                 // Validates basic functionality works, using Unsafe.Read
37                 test.RunBasicScenario_UnsafeRead();
38
39                 // Validates calling via reflection works, using Unsafe.Read
40                 test.RunReflectionScenario_UnsafeRead();
41
42                 if (Sse2.IsSupported)
43                 {
44                     // Validates calling via reflection works, using Load
45                     test.RunReflectionScenario();
46                 }
47
48                 // Validates passing a static member works
49                 test.RunClsVarScenario();
50
51                 // Validates passing a local works, using Unsafe.Read
52                 test.RunLclVarScenario_UnsafeRead();
53
54                 // Validates passing the field of a local works
55                 test.RunLclFldScenario();
56
57                 // Validates passing an instance member works
58                 test.RunFldScenario();
59             }
60             else
61             {
62                 // Validates we throw on unsupported hardware
63                 test.RunUnsupportedScenario();
64             }
65
66             if (!test.Succeeded)
67             {
68                 throw new Exception("One or more scenarios did not complete as expected.");
69             }
70         }
71     }
72
73     public sealed unsafe class SimpleScalarUnaryOpTest__SetAllVector128UInt32
74     {
75         private const int VectorSize = 16;
76
77         private const int Op1ElementCount = 2;
78         private const int RetElementCount = VectorSize / sizeof(UInt32);
79
80         private static UInt32[] _data = new UInt32[Op1ElementCount];
81
82         private static UInt32 _clsVar;
83
84         private UInt32 _fld;
85
86         private SimpleScalarUnaryOpTest__DataTable<UInt32, UInt32> _dataTable;
87
88         static SimpleScalarUnaryOpTest__SetAllVector128UInt32()
89         {
90             var random = new Random();
91
92             for (int i = 0; i < Op1ElementCount; i++)
93             {
94                 _data[i] = (uint)(random.Next(0, int.MaxValue));
95             }
96
97             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref _clsVar), ref Unsafe.As<UInt32, byte>(ref _data[0]), (uint)Marshal.SizeOf<UInt32>());
98         }
99
100         public SimpleScalarUnaryOpTest__SetAllVector128UInt32()
101         {
102             Succeeded = true;
103
104             var random = new Random();
105
106             for (var i = 0; i < Op1ElementCount; i++)
107             {
108                 _data[i] = (uint)(random.Next(0, int.MaxValue));
109             }
110
111             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref _fld), ref Unsafe.As<UInt32, byte>(ref _data[0]), (uint)Marshal.SizeOf<UInt32>());
112
113             for (var i = 0; i < Op1ElementCount; i++)
114             {
115                 _data[i] = (uint)(random.Next(0, int.MaxValue));
116             }
117
118             _dataTable = new SimpleScalarUnaryOpTest__DataTable<UInt32, UInt32>(_data, new UInt32[RetElementCount], VectorSize);
119         }
120
121         public bool IsSupported => Sse2.IsSupported;
122
123         public bool Succeeded { get; set; }
124
125         public void RunBasicScenario_UnsafeRead()
126         {
127             var result = Sse2.SetAllVector128(
128                 Unsafe.Read<UInt32>(_dataTable.inArrayPtr)
129             );
130
131             Unsafe.Write(_dataTable.outArrayPtr, result);
132             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
133         }
134
135         public void RunReflectionScenario_UnsafeRead()
136         {
137             var method = typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), new Type[] { typeof(UInt32) });
138             var result = method.Invoke(null, new object[] { Unsafe.Read<UInt32>(_dataTable.inArrayPtr)});
139
140             Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt32>)(result));
141             ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
142         }
143
144         public void RunReflectionScenario()
145         {
146             var method = typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), new Type[] { typeof(UInt32) });
147             UInt32 parameter = (UInt32) _dataTable.inArray[0];
148             var result = method.Invoke(null, new object[] { parameter });
149
150             Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt32>)(result));
151             ValidateResult(parameter, _dataTable.outArrayPtr);
152         }
153
154         public void RunClsVarScenario()
155         {
156             var result = Sse2.SetAllVector128(
157                 _clsVar
158             );
159
160             Unsafe.Write(_dataTable.outArrayPtr, result);
161             ValidateResult(_clsVar, _dataTable.outArrayPtr);
162         }
163
164         public void RunLclVarScenario_UnsafeRead()
165         {
166             var firstOp = Unsafe.Read<UInt32>(_dataTable.inArrayPtr);
167             var result = Sse2.SetAllVector128(firstOp);
168
169             Unsafe.Write(_dataTable.outArrayPtr, result);
170             ValidateResult(firstOp, _dataTable.outArrayPtr);
171         }
172
173         public void RunLclFldScenario()
174         {
175             var test = new SimpleScalarUnaryOpTest__SetAllVector128UInt32();
176             var result = Sse2.SetAllVector128(test._fld);
177
178             Unsafe.Write(_dataTable.outArrayPtr, result);
179             ValidateResult(test._fld, _dataTable.outArrayPtr);
180         }
181
182         public void RunFldScenario()
183         {
184             var result = Sse2.SetAllVector128(_fld);
185
186             Unsafe.Write(_dataTable.outArrayPtr, result);
187             ValidateResult(_fld, _dataTable.outArrayPtr);
188         }
189
190         public void RunUnsupportedScenario()
191         {
192             Succeeded = false;
193
194             try
195             {
196                 RunBasicScenario_UnsafeRead();
197             }
198             catch (PlatformNotSupportedException)
199             {
200                 Succeeded = true;
201             }
202         }
203
204         private void ValidateResult(UInt32 firstOp, void* result, [CallerMemberName] string method = "")
205         {
206             UInt32[] inArray = new UInt32[Op1ElementCount];
207             UInt32[] outArray = new UInt32[RetElementCount];
208
209             Unsafe.WriteUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray[0]), firstOp);
210             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
211
212             ValidateResult(inArray, outArray, method);
213         }
214
215         private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
216         {
217             UInt32[] inArray = new UInt32[Op1ElementCount];
218             UInt32[] outArray = new UInt32[RetElementCount];
219
220             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), VectorSize);
221             Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize);
222
223             ValidateResult(inArray, outArray, method);
224         }
225
226         private void ValidateResult(UInt32[] firstOp, UInt32[] result, [CallerMemberName] string method = "")
227         {
228             if (result[0] != firstOp[0])
229             {
230                 Succeeded = false;
231             }
232             else
233             {
234                 for (var i = 1; i < RetElementCount; i++)
235                 {
236                     if (result[i] != firstOp[0])
237                     {
238                         Succeeded = false;
239                         break;
240                     }
241                 }
242             }
243
244             if (!Succeeded)
245             {
246                 Console.WriteLine($"{nameof(Sse2)}.{nameof(Sse2.SetAllVector128)}<UInt32>(Vector128<UInt32>): {method} failed:");
247                 Console.WriteLine($"  firstOp: ({string.Join(", ", firstOp)})");
248                 Console.WriteLine($"   result: ({string.Join(", ", result)})");
249                 Console.WriteLine();
250             }
251         }
252     }
253 }