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 SetAllVector128UInt32()
24 bool skipIf32Bit = typeof(UInt32) == typeof(Int64) ? true :
25 typeof(UInt32) == typeof(UInt64) ? true : false;
27 if (skipIf32Bit && !Environment.Is64BitProcess)
32 var test = new SimpleScalarUnaryOpTest__SetAllVector128UInt32();
36 // Validates basic functionality works, using Unsafe.Read
37 test.RunBasicScenario_UnsafeRead();
39 // Validates calling via reflection works, using Unsafe.Read
40 test.RunReflectionScenario_UnsafeRead();
44 // Validates calling via reflection works, using Load
45 test.RunReflectionScenario();
48 // Validates passing a static member works
49 test.RunClsVarScenario();
51 // Validates passing a local works, using Unsafe.Read
52 test.RunLclVarScenario_UnsafeRead();
54 // Validates passing the field of a local works
55 test.RunLclFldScenario();
57 // Validates passing an instance member works
58 test.RunFldScenario();
62 // Validates we throw on unsupported hardware
63 test.RunUnsupportedScenario();
68 throw new Exception("One or more scenarios did not complete as expected.");
73 public sealed unsafe class SimpleScalarUnaryOpTest__SetAllVector128UInt32
75 private const int VectorSize = 16;
77 private const int Op1ElementCount = 2;
78 private const int RetElementCount = VectorSize / sizeof(UInt32);
80 private static UInt32[] _data = new UInt32[Op1ElementCount];
82 private static UInt32 _clsVar;
86 private SimpleScalarUnaryOpTest__DataTable<UInt32, UInt32> _dataTable;
88 static SimpleScalarUnaryOpTest__SetAllVector128UInt32()
90 var random = new Random();
92 for (int i = 0; i < Op1ElementCount; i++)
94 _data[i] = (uint)(random.Next(0, int.MaxValue));
97 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref _clsVar), ref Unsafe.As<UInt32, byte>(ref _data[0]), (uint)Marshal.SizeOf<UInt32>());
100 public SimpleScalarUnaryOpTest__SetAllVector128UInt32()
104 var random = new Random();
106 for (var i = 0; i < Op1ElementCount; i++)
108 _data[i] = (uint)(random.Next(0, int.MaxValue));
111 Unsafe.CopyBlockUnaligned(ref Unsafe.As<UInt32, byte>(ref _fld), ref Unsafe.As<UInt32, byte>(ref _data[0]), (uint)Marshal.SizeOf<UInt32>());
113 for (var i = 0; i < Op1ElementCount; i++)
115 _data[i] = (uint)(random.Next(0, int.MaxValue));
118 _dataTable = new SimpleScalarUnaryOpTest__DataTable<UInt32, UInt32>(_data, new UInt32[RetElementCount], VectorSize);
121 public bool IsSupported => Sse2.IsSupported;
123 public bool Succeeded { get; set; }
125 public void RunBasicScenario_UnsafeRead()
127 var result = Sse2.SetAllVector128(
128 Unsafe.Read<UInt32>(_dataTable.inArrayPtr)
131 Unsafe.Write(_dataTable.outArrayPtr, result);
132 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
135 public void RunReflectionScenario_UnsafeRead()
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)});
140 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt32>)(result));
141 ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr);
144 public void RunReflectionScenario()
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 });
150 Unsafe.Write(_dataTable.outArrayPtr, (Vector128<UInt32>)(result));
151 ValidateResult(parameter, _dataTable.outArrayPtr);
154 public void RunClsVarScenario()
156 var result = Sse2.SetAllVector128(
160 Unsafe.Write(_dataTable.outArrayPtr, result);
161 ValidateResult(_clsVar, _dataTable.outArrayPtr);
164 public void RunLclVarScenario_UnsafeRead()
166 var firstOp = Unsafe.Read<UInt32>(_dataTable.inArrayPtr);
167 var result = Sse2.SetAllVector128(firstOp);
169 Unsafe.Write(_dataTable.outArrayPtr, result);
170 ValidateResult(firstOp, _dataTable.outArrayPtr);
173 public void RunLclFldScenario()
175 var test = new SimpleScalarUnaryOpTest__SetAllVector128UInt32();
176 var result = Sse2.SetAllVector128(test._fld);
178 Unsafe.Write(_dataTable.outArrayPtr, result);
179 ValidateResult(test._fld, _dataTable.outArrayPtr);
182 public void RunFldScenario()
184 var result = Sse2.SetAllVector128(_fld);
186 Unsafe.Write(_dataTable.outArrayPtr, result);
187 ValidateResult(_fld, _dataTable.outArrayPtr);
190 public void RunUnsupportedScenario()
196 RunBasicScenario_UnsafeRead();
198 catch (PlatformNotSupportedException)
204 private void ValidateResult(UInt32 firstOp, void* result, [CallerMemberName] string method = "")
206 UInt32[] inArray = new UInt32[Op1ElementCount];
207 UInt32[] outArray = new UInt32[RetElementCount];
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);
212 ValidateResult(inArray, outArray, method);
215 private void ValidateResult(void* firstOp, void* result, [CallerMemberName] string method = "")
217 UInt32[] inArray = new UInt32[Op1ElementCount];
218 UInt32[] outArray = new UInt32[RetElementCount];
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);
223 ValidateResult(inArray, outArray, method);
226 private void ValidateResult(UInt32[] firstOp, UInt32[] result, [CallerMemberName] string method = "")
228 if (result[0] != firstOp[0])
234 for (var i = 1; i < RetElementCount; i++)
236 if (result[i] != firstOp[0])
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)})");