|
| 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 ExtractByte52() |
| 23 | + { |
| 24 | + var test = new SimpleUnaryOpTest__ExtractByte52(); |
| 25 | + |
| 26 | + try |
| 27 | + { |
| 28 | + if (test.IsSupported) |
| 29 | + { |
| 30 | + // Validates basic functionality works, using Unsafe.Read |
| 31 | + test.RunBasicScenario_UnsafeRead(); |
| 32 | + |
| 33 | + if (Avx.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 (Avx.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 (Avx.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__ExtractByte52 |
| 94 | + { |
| 95 | + private const int VectorSize = 32; |
| 96 | + |
| 97 | + private const int Op1ElementCount = VectorSize / sizeof(Byte); |
| 98 | + private const int RetElementCount = VectorSize / sizeof(Byte); |
| 99 | + |
| 100 | + private static Byte[] _data = new Byte[Op1ElementCount]; |
| 101 | + |
| 102 | + private static Vector256<Byte> _clsVar; |
| 103 | + |
| 104 | + private Vector256<Byte> _fld; |
| 105 | + |
| 106 | + private SimpleUnaryOpTest__DataTable<Byte, Byte> _dataTable; |
| 107 | + |
| 108 | + static SimpleUnaryOpTest__ExtractByte52() |
| 109 | + { |
| 110 | + var random = new Random(); |
| 111 | + |
| 112 | + for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (byte)(random.Next(0, byte.MaxValue)); } |
| 113 | + Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Byte>, byte>(ref _clsVar), ref Unsafe.As<Byte, byte>(ref _data[0]), VectorSize); |
| 114 | + } |
| 115 | + |
| 116 | + public SimpleUnaryOpTest__ExtractByte52() |
| 117 | + { |
| 118 | + Succeeded = true; |
| 119 | + |
| 120 | + var random = new Random(); |
| 121 | + |
| 122 | + for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (byte)(random.Next(0, byte.MaxValue)); } |
| 123 | + Unsafe.CopyBlockUnaligned(ref Unsafe.As<Vector256<Byte>, byte>(ref _fld), ref Unsafe.As<Byte, byte>(ref _data[0]), VectorSize); |
| 124 | + |
| 125 | + for (var i = 0; i < Op1ElementCount; i++) { _data[i] = (byte)(random.Next(0, byte.MaxValue)); } |
| 126 | + _dataTable = new SimpleUnaryOpTest__DataTable<Byte, Byte>(_data, new Byte[RetElementCount], VectorSize); |
| 127 | + } |
| 128 | + |
| 129 | + public bool IsSupported => Avx.IsSupported; |
| 130 | + |
| 131 | + public bool Succeeded { get; set; } |
| 132 | + |
| 133 | + public void RunBasicScenario_UnsafeRead() |
| 134 | + { |
| 135 | + var result = Avx.Extract( |
| 136 | + Unsafe.Read<Vector256<Byte>>(_dataTable.inArrayPtr), |
| 137 | + 52 |
| 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 = Avx.Extract( |
| 147 | + Avx.LoadVector256((Byte*)(_dataTable.inArrayPtr)), |
| 148 | + 52 |
| 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 = Avx.Extract( |
| 158 | + Avx.LoadAlignedVector256((Byte*)(_dataTable.inArrayPtr)), |
| 159 | + 52 |
| 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(Avx).GetMethod(nameof(Avx.Extract), new Type[] { typeof(Vector256<Byte>), typeof(byte) }) |
| 169 | + .Invoke(null, new object[] { |
| 170 | + Unsafe.Read<Vector256<Byte>>(_dataTable.inArrayPtr), |
| 171 | + (byte)52 |
| 172 | + }); |
| 173 | + |
| 174 | + Unsafe.Write(_dataTable.outArrayPtr, (Byte)(result)); |
| 175 | + ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr); |
| 176 | + } |
| 177 | + |
| 178 | + public void RunReflectionScenario_Load() |
| 179 | + { |
| 180 | + var result = typeof(Avx).GetMethod(nameof(Avx.Extract), new Type[] { typeof(Vector256<Byte>), typeof(byte) }) |
| 181 | + .Invoke(null, new object[] { |
| 182 | + Avx.LoadVector256((Byte*)(_dataTable.inArrayPtr)), |
| 183 | + (byte)52 |
| 184 | + }); |
| 185 | + |
| 186 | + Unsafe.Write(_dataTable.outArrayPtr, (Byte)(result)); |
| 187 | + ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr); |
| 188 | + } |
| 189 | + |
| 190 | + public void RunReflectionScenario_LoadAligned() |
| 191 | + { |
| 192 | + var result = typeof(Avx).GetMethod(nameof(Avx.Extract), new Type[] { typeof(Vector256<Byte>), typeof(byte) }) |
| 193 | + .Invoke(null, new object[] { |
| 194 | + Avx.LoadAlignedVector256((Byte*)(_dataTable.inArrayPtr)), |
| 195 | + (byte)52 |
| 196 | + }); |
| 197 | + |
| 198 | + Unsafe.Write(_dataTable.outArrayPtr, (Byte)(result)); |
| 199 | + ValidateResult(_dataTable.inArrayPtr, _dataTable.outArrayPtr); |
| 200 | + } |
| 201 | + |
| 202 | + public void RunClsVarScenario() |
| 203 | + { |
| 204 | + var result = Avx.Extract( |
| 205 | + _clsVar, |
| 206 | + 52 |
| 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<Vector256<Byte>>(_dataTable.inArrayPtr); |
| 216 | + var result = Avx.Extract(firstOp, 52); |
| 217 | + |
| 218 | + Unsafe.Write(_dataTable.outArrayPtr, result); |
| 219 | + ValidateResult(firstOp, _dataTable.outArrayPtr); |
| 220 | + } |
| 221 | + |
| 222 | + public void RunLclVarScenario_Load() |
| 223 | + { |
| 224 | + var firstOp = Avx.LoadVector256((Byte*)(_dataTable.inArrayPtr)); |
| 225 | + var result = Avx.Extract(firstOp, 52); |
| 226 | + |
| 227 | + Unsafe.Write(_dataTable.outArrayPtr, result); |
| 228 | + ValidateResult(firstOp, _dataTable.outArrayPtr); |
| 229 | + } |
| 230 | + |
| 231 | + public void RunLclVarScenario_LoadAligned() |
| 232 | + { |
| 233 | + var firstOp = Avx.LoadAlignedVector256((Byte*)(_dataTable.inArrayPtr)); |
| 234 | + var result = Avx.Extract(firstOp, 52); |
| 235 | + |
| 236 | + Unsafe.Write(_dataTable.outArrayPtr, result); |
| 237 | + ValidateResult(firstOp, _dataTable.outArrayPtr); |
| 238 | + } |
| 239 | + |
| 240 | + public void RunLclFldScenario() |
| 241 | + { |
| 242 | + var test = new SimpleUnaryOpTest__ExtractByte52(); |
| 243 | + var result = Avx.Extract(test._fld, 52); |
| 244 | + |
| 245 | + Unsafe.Write(_dataTable.outArrayPtr, result); |
| 246 | + ValidateResult(test._fld, _dataTable.outArrayPtr); |
| 247 | + } |
| 248 | + |
| 249 | + public void RunFldScenario() |
| 250 | + { |
| 251 | + var result = Avx.Extract(_fld, 52); |
| 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(Vector256<Byte> firstOp, void* result, [CallerMemberName] string method = "") |
| 272 | + { |
| 273 | + Byte[] inArray = new Byte[Op1ElementCount]; |
| 274 | + Byte[] outArray = new Byte[RetElementCount]; |
| 275 | + |
| 276 | + Unsafe.Write(Unsafe.AsPointer(ref inArray[0]), firstOp); |
| 277 | + Unsafe.CopyBlockUnaligned(ref Unsafe.As<Byte, 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 | + Byte[] inArray = new Byte[Op1ElementCount]; |
| 285 | + Byte[] outArray = new Byte[RetElementCount]; |
| 286 | + |
| 287 | + Unsafe.CopyBlockUnaligned(ref Unsafe.As<Byte, byte>(ref inArray[0]), ref Unsafe.AsRef<byte>(firstOp), VectorSize); |
| 288 | + Unsafe.CopyBlockUnaligned(ref Unsafe.As<Byte, byte>(ref outArray[0]), ref Unsafe.AsRef<byte>(result), VectorSize); |
| 289 | + |
| 290 | + ValidateResult(inArray, outArray, method); |
| 291 | + } |
| 292 | + |
| 293 | + private void ValidateResult(Byte[] firstOp, Byte[] result, [CallerMemberName] string method = "") |
| 294 | + { |
| 295 | + if ((result[0] != firstOp[20])) |
| 296 | + { |
| 297 | + Succeeded = false; |
| 298 | + } |
| 299 | + |
| 300 | + if (!Succeeded) |
| 301 | + { |
| 302 | + Console.WriteLine($"{nameof(Avx)}.{nameof(Avx.Extract)}<Byte>(Vector256<Byte><9>): {method} failed:"); |
| 303 | + Console.WriteLine($" firstOp: ({string.Join(", ", firstOp)})"); |
| 304 | + Console.WriteLine($" result: ({string.Join(", ", result)})"); |
| 305 | + Console.WriteLine(); |
| 306 | + } |
| 307 | + } |
| 308 | + } |
| 309 | +} |
0 commit comments