Adding tests for the ParallelBitDeposit and ParallelBitExtract Bmi2 HWIntrinsics
authorTanner Gooding <tagoo@outlook.com>
Fri, 6 Jul 2018 16:29:33 +0000 (09:29 -0700)
committerTanner Gooding <tagoo@outlook.com>
Mon, 9 Jul 2018 23:21:18 +0000 (16:21 -0700)
tests/src/JIT/HardwareIntrinsics/X86/Bmi2/Bmi2_r.csproj [new file with mode: 0644]
tests/src/JIT/HardwareIntrinsics/X86/Bmi2/Bmi2_ro.csproj [new file with mode: 0644]
tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitDeposit.UInt32.cs [new file with mode: 0644]
tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitDeposit.UInt64.cs [new file with mode: 0644]
tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitExtract.UInt32.cs [new file with mode: 0644]
tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitExtract.UInt64.cs [new file with mode: 0644]
tests/src/JIT/HardwareIntrinsics/X86/Bmi2/Program.Bmi2.cs [new file with mode: 0644]
tests/src/JIT/HardwareIntrinsics/X86/Shared/GenerateTests.csx

diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/Bmi2_r.csproj b/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/Bmi2_r.csproj
new file mode 100644 (file)
index 0000000..007f097
--- /dev/null
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType>None</DebugType>
+    <Optimize></Optimize>
+  </PropertyGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="ParallelBitDeposit.UInt32.cs" />
+    <Compile Include="ParallelBitDeposit.UInt64.cs" />
+    <Compile Include="ParallelBitExtract.UInt32.cs" />
+    <Compile Include="ParallelBitExtract.UInt64.cs" />
+    <Compile Include="Program.Bmi2.cs" />
+    <Compile Include="..\Shared\Program.cs" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup>
+</Project>
diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/Bmi2_ro.csproj b/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/Bmi2_ro.csproj
new file mode 100644 (file)
index 0000000..d0ad1a1
--- /dev/null
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType>None</DebugType>
+    <Optimize>True</Optimize>
+  </PropertyGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="ParallelBitDeposit.UInt32.cs" />
+    <Compile Include="ParallelBitDeposit.UInt64.cs" />
+    <Compile Include="ParallelBitExtract.UInt32.cs" />
+    <Compile Include="ParallelBitExtract.UInt64.cs" />
+    <Compile Include="Program.Bmi2.cs" />
+    <Compile Include="..\Shared\Program.cs" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup>
+</Project>
diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitDeposit.UInt32.cs b/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitDeposit.UInt32.cs
new file mode 100644 (file)
index 0000000..66e5a59
--- /dev/null
@@ -0,0 +1,200 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/******************************************************************************
+ * This file is auto-generated from a template file by the GenerateTests.csx  *
+ * script in tests\src\JIT\HardwareIntrinsics\X86\Shared. In order to make    *
+ * changes, please update the corresponding template and run according to the *
+ * directions listed in the file.                                             *
+ ******************************************************************************/
+
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Runtime.Intrinsics;
+using System.Runtime.Intrinsics.X86;
+
+namespace JIT.HardwareIntrinsics.X86
+{
+    public static partial class Program
+    {
+        private static void ParallelBitDepositUInt32()
+        {
+            var test = new ScalarBinaryOpTest__ParallelBitDepositUInt32();
+
+            if (test.IsSupported)
+            {
+                // Validates basic functionality works, using Unsafe.ReadUnaligned
+                test.RunBasicScenario_UnsafeRead();
+
+                // Validates calling via reflection works, using Unsafe.ReadUnaligned
+                test.RunReflectionScenario_UnsafeRead();
+
+                // Validates passing a static member works
+                test.RunClsVarScenario();
+
+                // Validates passing a local works, using Unsafe.ReadUnaligned
+                test.RunLclVarScenario_UnsafeRead();
+
+                // Validates passing the field of a local works
+                test.RunLclFldScenario();
+
+                // Validates passing an instance member works
+                test.RunFldScenario();
+            }
+            else
+            {
+                // Validates we throw on unsupported hardware
+                test.RunUnsupportedScenario();
+            }
+
+            if (!test.Succeeded)
+            {
+                throw new Exception("One or more scenarios did not complete as expected.");
+            }
+        }
+    }
+
+    public sealed unsafe class ScalarBinaryOpTest__ParallelBitDepositUInt32
+    {
+        private static UInt32 _data1;
+        private static UInt32 _data2;
+
+        private static UInt32 _clsVar1;
+        private static UInt32 _clsVar2;
+
+        private UInt32 _fld1;
+        private UInt32 _fld2;
+
+        static ScalarBinaryOpTest__ParallelBitDepositUInt32()
+        {
+            var random = new Random();
+            _clsVar1 = (uint)(random.Next(0, int.MaxValue));
+            _clsVar2 = (uint)(random.Next(0, int.MaxValue));
+        }
+
+        public ScalarBinaryOpTest__ParallelBitDepositUInt32()
+        {
+            Succeeded = true;
+
+            var random = new Random();
+            
+            _fld1 = (uint)(random.Next(0, int.MaxValue));
+            _fld2 = (uint)(random.Next(0, int.MaxValue));
+
+            _data1 = (uint)(random.Next(0, int.MaxValue));
+            _data2 = (uint)(random.Next(0, int.MaxValue));
+        }
+
+        public bool IsSupported => Bmi2.IsSupported && (Environment.Is64BitProcess || ((typeof(UInt32) != typeof(long)) && (typeof(UInt32) != typeof(ulong))));
+
+        public bool Succeeded { get; set; }
+
+        public void RunBasicScenario_UnsafeRead()
+        {
+            var result = Bmi2.ParallelBitDeposit(
+                Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data1)),
+                Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data2))
+            );
+
+            ValidateResult(_data1, _data2, result);
+        }
+
+        public void RunReflectionScenario_UnsafeRead()
+        {
+            var result = typeof(Bmi2).GetMethod(nameof(Bmi2.ParallelBitDeposit), new Type[] { typeof(UInt32), typeof(UInt32) })
+                                     .Invoke(null, new object[] {
+                                        Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data1)),
+                                        Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data2))
+                                     });
+
+            ValidateResult(_data1, _data2, (UInt32)result);
+        }
+
+        public void RunClsVarScenario()
+        {
+            var result = Bmi2.ParallelBitDeposit(
+                _clsVar1,
+                _clsVar2
+            );
+
+            ValidateResult(_clsVar1, _clsVar2, result);
+        }
+
+        public void RunLclVarScenario_UnsafeRead()
+        {
+            var data1 = Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data1));
+            var data2 = Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data2));
+            var result = Bmi2.ParallelBitDeposit(data1, data2);
+
+            ValidateResult(data1, data2, result);
+        }
+
+        public void RunLclFldScenario()
+        {
+            var test = new ScalarBinaryOpTest__ParallelBitDepositUInt32();
+            var result = Bmi2.ParallelBitDeposit(test._fld1, test._fld2);
+
+            ValidateResult(test._fld1, test._fld2, result);
+        }
+
+        public void RunFldScenario()
+        {
+            var result = Bmi2.ParallelBitDeposit(_fld1, _fld2);
+            ValidateResult(_fld1, _fld2, result);
+        }
+
+        public void RunUnsupportedScenario()
+        {
+            Succeeded = false;
+
+            try
+            {
+                RunBasicScenario_UnsafeRead();
+            }
+            catch (PlatformNotSupportedException)
+            {
+                Succeeded = true;
+            }
+        }
+
+        private void ValidateResult(UInt32 left, UInt32 right, UInt32 result, [CallerMemberName] string method = "")
+        {
+            var isUnexpectedResult = false;
+
+            
+// The validation logic defined here for Bmi2.ParallelBitDeposit and Bmi2.ParallelBitExtract is
+// based on the 'Operation' pseudo-code defined for the pdep and pext instruction in the 'Intel®
+// 64 and IA-32 Architectures Software Developer’s Manual; Volume 2 (2A, 2B, 2C & 2D): Instruction
+// Set Reference, A-Z'
+
+uint temp = left;
+uint mask = right;
+uint dest = 0;
+byte m = 0, k = 0;
+
+while (m < 32)
+{
+    if (((mask >> m) & 1) == 1) // Extract bit at index m of mask
+    {
+        dest |= (((temp >> k) & 1) << m); // Extract bit at index k of temp and insert to index m of dest
+        k++;
+    }
+    m++;
+}
+
+isUnexpectedResult = (dest != result);
+
+
+            if (isUnexpectedResult)
+            {
+                Console.WriteLine($"{nameof(Bmi2)}.{nameof(Bmi2.ParallelBitDeposit)}<UInt32>(UInt32, UInt32): ParallelBitDeposit failed:");
+                Console.WriteLine($"    left: {left}");
+                Console.WriteLine($"   right: {right}");
+                Console.WriteLine($"  result: {result}");
+                Console.WriteLine();
+            }
+        }
+    }
+}
diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitDeposit.UInt64.cs b/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitDeposit.UInt64.cs
new file mode 100644 (file)
index 0000000..91ed8bd
--- /dev/null
@@ -0,0 +1,200 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/******************************************************************************
+ * This file is auto-generated from a template file by the GenerateTests.csx  *
+ * script in tests\src\JIT\HardwareIntrinsics\X86\Shared. In order to make    *
+ * changes, please update the corresponding template and run according to the *
+ * directions listed in the file.                                             *
+ ******************************************************************************/
+
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Runtime.Intrinsics;
+using System.Runtime.Intrinsics.X86;
+
+namespace JIT.HardwareIntrinsics.X86
+{
+    public static partial class Program
+    {
+        private static void ParallelBitDepositUInt64()
+        {
+            var test = new ScalarBinaryOpTest__ParallelBitDepositUInt64();
+
+            if (test.IsSupported)
+            {
+                // Validates basic functionality works, using Unsafe.ReadUnaligned
+                test.RunBasicScenario_UnsafeRead();
+
+                // Validates calling via reflection works, using Unsafe.ReadUnaligned
+                test.RunReflectionScenario_UnsafeRead();
+
+                // Validates passing a static member works
+                test.RunClsVarScenario();
+
+                // Validates passing a local works, using Unsafe.ReadUnaligned
+                test.RunLclVarScenario_UnsafeRead();
+
+                // Validates passing the field of a local works
+                test.RunLclFldScenario();
+
+                // Validates passing an instance member works
+                test.RunFldScenario();
+            }
+            else
+            {
+                // Validates we throw on unsupported hardware
+                test.RunUnsupportedScenario();
+            }
+
+            if (!test.Succeeded)
+            {
+                throw new Exception("One or more scenarios did not complete as expected.");
+            }
+        }
+    }
+
+    public sealed unsafe class ScalarBinaryOpTest__ParallelBitDepositUInt64
+    {
+        private static UInt64 _data1;
+        private static UInt64 _data2;
+
+        private static UInt64 _clsVar1;
+        private static UInt64 _clsVar2;
+
+        private UInt64 _fld1;
+        private UInt64 _fld2;
+
+        static ScalarBinaryOpTest__ParallelBitDepositUInt64()
+        {
+            var random = new Random();
+            _clsVar1 = (ulong)(random.Next(0, int.MaxValue));
+            _clsVar2 = (ulong)(random.Next(0, int.MaxValue));
+        }
+
+        public ScalarBinaryOpTest__ParallelBitDepositUInt64()
+        {
+            Succeeded = true;
+
+            var random = new Random();
+            
+            _fld1 = (ulong)(random.Next(0, int.MaxValue));
+            _fld2 = (ulong)(random.Next(0, int.MaxValue));
+
+            _data1 = (ulong)(random.Next(0, int.MaxValue));
+            _data2 = (ulong)(random.Next(0, int.MaxValue));
+        }
+
+        public bool IsSupported => Bmi2.IsSupported && (Environment.Is64BitProcess || ((typeof(UInt64) != typeof(long)) && (typeof(UInt64) != typeof(ulong))));
+
+        public bool Succeeded { get; set; }
+
+        public void RunBasicScenario_UnsafeRead()
+        {
+            var result = Bmi2.ParallelBitDeposit(
+                Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data1)),
+                Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data2))
+            );
+
+            ValidateResult(_data1, _data2, result);
+        }
+
+        public void RunReflectionScenario_UnsafeRead()
+        {
+            var result = typeof(Bmi2).GetMethod(nameof(Bmi2.ParallelBitDeposit), new Type[] { typeof(UInt64), typeof(UInt64) })
+                                     .Invoke(null, new object[] {
+                                        Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data1)),
+                                        Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data2))
+                                     });
+
+            ValidateResult(_data1, _data2, (UInt64)result);
+        }
+
+        public void RunClsVarScenario()
+        {
+            var result = Bmi2.ParallelBitDeposit(
+                _clsVar1,
+                _clsVar2
+            );
+
+            ValidateResult(_clsVar1, _clsVar2, result);
+        }
+
+        public void RunLclVarScenario_UnsafeRead()
+        {
+            var data1 = Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data1));
+            var data2 = Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data2));
+            var result = Bmi2.ParallelBitDeposit(data1, data2);
+
+            ValidateResult(data1, data2, result);
+        }
+
+        public void RunLclFldScenario()
+        {
+            var test = new ScalarBinaryOpTest__ParallelBitDepositUInt64();
+            var result = Bmi2.ParallelBitDeposit(test._fld1, test._fld2);
+
+            ValidateResult(test._fld1, test._fld2, result);
+        }
+
+        public void RunFldScenario()
+        {
+            var result = Bmi2.ParallelBitDeposit(_fld1, _fld2);
+            ValidateResult(_fld1, _fld2, result);
+        }
+
+        public void RunUnsupportedScenario()
+        {
+            Succeeded = false;
+
+            try
+            {
+                RunBasicScenario_UnsafeRead();
+            }
+            catch (PlatformNotSupportedException)
+            {
+                Succeeded = true;
+            }
+        }
+
+        private void ValidateResult(UInt64 left, UInt64 right, UInt64 result, [CallerMemberName] string method = "")
+        {
+            var isUnexpectedResult = false;
+
+            
+// The validation logic defined here for Bmi2.ParallelBitDeposit and Bmi2.ParallelBitExtract is
+// based on the 'Operation' pseudo-code defined for the pdep and pext instruction in the 'Intel®
+// 64 and IA-32 Architectures Software Developer’s Manual; Volume 2 (2A, 2B, 2C & 2D): Instruction
+// Set Reference, A-Z'
+
+ulong temp = left;
+ulong mask = right;
+ulong dest = 0;
+byte m = 0, k = 0;
+
+while (m < 64)
+{
+    if (((mask >> m) & 1) == 1) // Extract bit at index m of mask
+    {
+        dest |= (((temp >> k) & 1) << m); // Extract bit at index k of temp and insert to index m of dest
+        k++;
+    }
+    m++;
+}
+
+isUnexpectedResult = (dest != result);
+
+
+            if (isUnexpectedResult)
+            {
+                Console.WriteLine($"{nameof(Bmi2)}.{nameof(Bmi2.ParallelBitDeposit)}<UInt64>(UInt64, UInt64): ParallelBitDeposit failed:");
+                Console.WriteLine($"    left: {left}");
+                Console.WriteLine($"   right: {right}");
+                Console.WriteLine($"  result: {result}");
+                Console.WriteLine();
+            }
+        }
+    }
+}
diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitExtract.UInt32.cs b/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitExtract.UInt32.cs
new file mode 100644 (file)
index 0000000..67258ee
--- /dev/null
@@ -0,0 +1,200 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/******************************************************************************
+ * This file is auto-generated from a template file by the GenerateTests.csx  *
+ * script in tests\src\JIT\HardwareIntrinsics\X86\Shared. In order to make    *
+ * changes, please update the corresponding template and run according to the *
+ * directions listed in the file.                                             *
+ ******************************************************************************/
+
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Runtime.Intrinsics;
+using System.Runtime.Intrinsics.X86;
+
+namespace JIT.HardwareIntrinsics.X86
+{
+    public static partial class Program
+    {
+        private static void ParallelBitExtractUInt32()
+        {
+            var test = new ScalarBinaryOpTest__ParallelBitExtractUInt32();
+
+            if (test.IsSupported)
+            {
+                // Validates basic functionality works, using Unsafe.ReadUnaligned
+                test.RunBasicScenario_UnsafeRead();
+
+                // Validates calling via reflection works, using Unsafe.ReadUnaligned
+                test.RunReflectionScenario_UnsafeRead();
+
+                // Validates passing a static member works
+                test.RunClsVarScenario();
+
+                // Validates passing a local works, using Unsafe.ReadUnaligned
+                test.RunLclVarScenario_UnsafeRead();
+
+                // Validates passing the field of a local works
+                test.RunLclFldScenario();
+
+                // Validates passing an instance member works
+                test.RunFldScenario();
+            }
+            else
+            {
+                // Validates we throw on unsupported hardware
+                test.RunUnsupportedScenario();
+            }
+
+            if (!test.Succeeded)
+            {
+                throw new Exception("One or more scenarios did not complete as expected.");
+            }
+        }
+    }
+
+    public sealed unsafe class ScalarBinaryOpTest__ParallelBitExtractUInt32
+    {
+        private static UInt32 _data1;
+        private static UInt32 _data2;
+
+        private static UInt32 _clsVar1;
+        private static UInt32 _clsVar2;
+
+        private UInt32 _fld1;
+        private UInt32 _fld2;
+
+        static ScalarBinaryOpTest__ParallelBitExtractUInt32()
+        {
+            var random = new Random();
+            _clsVar1 = (uint)(random.Next(0, int.MaxValue));
+            _clsVar2 = (uint)(random.Next(0, int.MaxValue));
+        }
+
+        public ScalarBinaryOpTest__ParallelBitExtractUInt32()
+        {
+            Succeeded = true;
+
+            var random = new Random();
+            
+            _fld1 = (uint)(random.Next(0, int.MaxValue));
+            _fld2 = (uint)(random.Next(0, int.MaxValue));
+
+            _data1 = (uint)(random.Next(0, int.MaxValue));
+            _data2 = (uint)(random.Next(0, int.MaxValue));
+        }
+
+        public bool IsSupported => Bmi2.IsSupported && (Environment.Is64BitProcess || ((typeof(UInt32) != typeof(long)) && (typeof(UInt32) != typeof(ulong))));
+
+        public bool Succeeded { get; set; }
+
+        public void RunBasicScenario_UnsafeRead()
+        {
+            var result = Bmi2.ParallelBitExtract(
+                Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data1)),
+                Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data2))
+            );
+
+            ValidateResult(_data1, _data2, result);
+        }
+
+        public void RunReflectionScenario_UnsafeRead()
+        {
+            var result = typeof(Bmi2).GetMethod(nameof(Bmi2.ParallelBitExtract), new Type[] { typeof(UInt32), typeof(UInt32) })
+                                     .Invoke(null, new object[] {
+                                        Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data1)),
+                                        Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data2))
+                                     });
+
+            ValidateResult(_data1, _data2, (UInt32)result);
+        }
+
+        public void RunClsVarScenario()
+        {
+            var result = Bmi2.ParallelBitExtract(
+                _clsVar1,
+                _clsVar2
+            );
+
+            ValidateResult(_clsVar1, _clsVar2, result);
+        }
+
+        public void RunLclVarScenario_UnsafeRead()
+        {
+            var data1 = Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data1));
+            var data2 = Unsafe.ReadUnaligned<UInt32>(ref Unsafe.As<UInt32, byte>(ref _data2));
+            var result = Bmi2.ParallelBitExtract(data1, data2);
+
+            ValidateResult(data1, data2, result);
+        }
+
+        public void RunLclFldScenario()
+        {
+            var test = new ScalarBinaryOpTest__ParallelBitExtractUInt32();
+            var result = Bmi2.ParallelBitExtract(test._fld1, test._fld2);
+
+            ValidateResult(test._fld1, test._fld2, result);
+        }
+
+        public void RunFldScenario()
+        {
+            var result = Bmi2.ParallelBitExtract(_fld1, _fld2);
+            ValidateResult(_fld1, _fld2, result);
+        }
+
+        public void RunUnsupportedScenario()
+        {
+            Succeeded = false;
+
+            try
+            {
+                RunBasicScenario_UnsafeRead();
+            }
+            catch (PlatformNotSupportedException)
+            {
+                Succeeded = true;
+            }
+        }
+
+        private void ValidateResult(UInt32 left, UInt32 right, UInt32 result, [CallerMemberName] string method = "")
+        {
+            var isUnexpectedResult = false;
+
+            
+// The validation logic defined here for Bmi2.ParallelBitDeposit and Bmi2.ParallelBitExtract is
+// based on the 'Operation' pseudo-code defined for the pdep and pext instruction in the 'Intel®
+// 64 and IA-32 Architectures Software Developer’s Manual; Volume 2 (2A, 2B, 2C & 2D): Instruction
+// Set Reference, A-Z'
+
+uint temp = left;
+uint mask = right;
+uint dest = 0;
+byte m = 0, k = 0;
+
+while (m < 32)
+{
+    if (((mask >> m) & 1) == 1) // Extract bit at index m of mask
+    {
+        dest |= (((temp >> m) & 1) << k); // Extract bit at index m of temp and insert to index k of dest
+        k++;
+    }
+    m++;
+}
+
+isUnexpectedResult = (dest != result);
+
+
+            if (isUnexpectedResult)
+            {
+                Console.WriteLine($"{nameof(Bmi2)}.{nameof(Bmi2.ParallelBitExtract)}<UInt32>(UInt32, UInt32): ParallelBitExtract failed:");
+                Console.WriteLine($"    left: {left}");
+                Console.WriteLine($"   right: {right}");
+                Console.WriteLine($"  result: {result}");
+                Console.WriteLine();
+            }
+        }
+    }
+}
diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitExtract.UInt64.cs b/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/ParallelBitExtract.UInt64.cs
new file mode 100644 (file)
index 0000000..6bcf7c5
--- /dev/null
@@ -0,0 +1,200 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/******************************************************************************
+ * This file is auto-generated from a template file by the GenerateTests.csx  *
+ * script in tests\src\JIT\HardwareIntrinsics\X86\Shared. In order to make    *
+ * changes, please update the corresponding template and run according to the *
+ * directions listed in the file.                                             *
+ ******************************************************************************/
+
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Runtime.Intrinsics;
+using System.Runtime.Intrinsics.X86;
+
+namespace JIT.HardwareIntrinsics.X86
+{
+    public static partial class Program
+    {
+        private static void ParallelBitExtractUInt64()
+        {
+            var test = new ScalarBinaryOpTest__ParallelBitExtractUInt64();
+
+            if (test.IsSupported)
+            {
+                // Validates basic functionality works, using Unsafe.ReadUnaligned
+                test.RunBasicScenario_UnsafeRead();
+
+                // Validates calling via reflection works, using Unsafe.ReadUnaligned
+                test.RunReflectionScenario_UnsafeRead();
+
+                // Validates passing a static member works
+                test.RunClsVarScenario();
+
+                // Validates passing a local works, using Unsafe.ReadUnaligned
+                test.RunLclVarScenario_UnsafeRead();
+
+                // Validates passing the field of a local works
+                test.RunLclFldScenario();
+
+                // Validates passing an instance member works
+                test.RunFldScenario();
+            }
+            else
+            {
+                // Validates we throw on unsupported hardware
+                test.RunUnsupportedScenario();
+            }
+
+            if (!test.Succeeded)
+            {
+                throw new Exception("One or more scenarios did not complete as expected.");
+            }
+        }
+    }
+
+    public sealed unsafe class ScalarBinaryOpTest__ParallelBitExtractUInt64
+    {
+        private static UInt64 _data1;
+        private static UInt64 _data2;
+
+        private static UInt64 _clsVar1;
+        private static UInt64 _clsVar2;
+
+        private UInt64 _fld1;
+        private UInt64 _fld2;
+
+        static ScalarBinaryOpTest__ParallelBitExtractUInt64()
+        {
+            var random = new Random();
+            _clsVar1 = (ulong)(random.Next(0, int.MaxValue));
+            _clsVar2 = (ulong)(random.Next(0, int.MaxValue));
+        }
+
+        public ScalarBinaryOpTest__ParallelBitExtractUInt64()
+        {
+            Succeeded = true;
+
+            var random = new Random();
+            
+            _fld1 = (ulong)(random.Next(0, int.MaxValue));
+            _fld2 = (ulong)(random.Next(0, int.MaxValue));
+
+            _data1 = (ulong)(random.Next(0, int.MaxValue));
+            _data2 = (ulong)(random.Next(0, int.MaxValue));
+        }
+
+        public bool IsSupported => Bmi2.IsSupported && (Environment.Is64BitProcess || ((typeof(UInt64) != typeof(long)) && (typeof(UInt64) != typeof(ulong))));
+
+        public bool Succeeded { get; set; }
+
+        public void RunBasicScenario_UnsafeRead()
+        {
+            var result = Bmi2.ParallelBitExtract(
+                Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data1)),
+                Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data2))
+            );
+
+            ValidateResult(_data1, _data2, result);
+        }
+
+        public void RunReflectionScenario_UnsafeRead()
+        {
+            var result = typeof(Bmi2).GetMethod(nameof(Bmi2.ParallelBitExtract), new Type[] { typeof(UInt64), typeof(UInt64) })
+                                     .Invoke(null, new object[] {
+                                        Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data1)),
+                                        Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data2))
+                                     });
+
+            ValidateResult(_data1, _data2, (UInt64)result);
+        }
+
+        public void RunClsVarScenario()
+        {
+            var result = Bmi2.ParallelBitExtract(
+                _clsVar1,
+                _clsVar2
+            );
+
+            ValidateResult(_clsVar1, _clsVar2, result);
+        }
+
+        public void RunLclVarScenario_UnsafeRead()
+        {
+            var data1 = Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data1));
+            var data2 = Unsafe.ReadUnaligned<UInt64>(ref Unsafe.As<UInt64, byte>(ref _data2));
+            var result = Bmi2.ParallelBitExtract(data1, data2);
+
+            ValidateResult(data1, data2, result);
+        }
+
+        public void RunLclFldScenario()
+        {
+            var test = new ScalarBinaryOpTest__ParallelBitExtractUInt64();
+            var result = Bmi2.ParallelBitExtract(test._fld1, test._fld2);
+
+            ValidateResult(test._fld1, test._fld2, result);
+        }
+
+        public void RunFldScenario()
+        {
+            var result = Bmi2.ParallelBitExtract(_fld1, _fld2);
+            ValidateResult(_fld1, _fld2, result);
+        }
+
+        public void RunUnsupportedScenario()
+        {
+            Succeeded = false;
+
+            try
+            {
+                RunBasicScenario_UnsafeRead();
+            }
+            catch (PlatformNotSupportedException)
+            {
+                Succeeded = true;
+            }
+        }
+
+        private void ValidateResult(UInt64 left, UInt64 right, UInt64 result, [CallerMemberName] string method = "")
+        {
+            var isUnexpectedResult = false;
+
+            
+// The validation logic defined here for Bmi2.ParallelBitDeposit and Bmi2.ParallelBitExtract is
+// based on the 'Operation' pseudo-code defined for the pdep and pext instruction in the 'Intel®
+// 64 and IA-32 Architectures Software Developer’s Manual; Volume 2 (2A, 2B, 2C & 2D): Instruction
+// Set Reference, A-Z'
+
+ulong temp = left;
+ulong mask = right;
+ulong dest = 0;
+byte m = 0, k = 0;
+
+while (m < 64)
+{
+    if (((mask >> m) & 1) == 1) // Extract bit at index m of mask
+    {
+        dest |= (((temp >> m) & 1) << k); // Extract bit at index m of temp and insert to index k of dest
+        k++;
+    }
+    m++;
+}
+
+isUnexpectedResult = (dest != result);
+
+
+            if (isUnexpectedResult)
+            {
+                Console.WriteLine($"{nameof(Bmi2)}.{nameof(Bmi2.ParallelBitExtract)}<UInt64>(UInt64, UInt64): ParallelBitExtract failed:");
+                Console.WriteLine($"    left: {left}");
+                Console.WriteLine($"   right: {right}");
+                Console.WriteLine($"  result: {result}");
+                Console.WriteLine();
+            }
+        }
+    }
+}
diff --git a/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/Program.Bmi2.cs b/tests/src/JIT/HardwareIntrinsics/X86/Bmi2/Program.Bmi2.cs
new file mode 100644 (file)
index 0000000..1f65f58
--- /dev/null
@@ -0,0 +1,22 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Collections.Generic;
+
+namespace JIT.HardwareIntrinsics.X86
+{
+    public static partial class Program
+    {
+        static Program()
+        {
+            TestList = new Dictionary<string, Action>() {
+                ["ParallelBitDeposit.UInt32"] = ParallelBitDepositUInt32,
+                ["ParallelBitDeposit.UInt64"] = ParallelBitDepositUInt64,
+                ["ParallelBitExtract.UInt32"] = ParallelBitExtractUInt32,
+                ["ParallelBitExtract.UInt64"] = ParallelBitExtractUInt64,
+            };
+        }
+    }
+}
index 54e3328..4f2ccde 100644 (file)
@@ -800,6 +800,97 @@ private static readonly (string templateFileName, Dictionary<string, string> tem
     ("ScalarUnOpTest.template",  new Dictionary<string, string> { ["Isa"] = "Bmi1", ["Method"] = "TrailingZeroCount",       ["RetBaseType"] = "UInt64", ["Op1BaseType"] = "UInt64",                             ["NextValueOp1"] = "(ulong)(random.Next(0, int.MaxValue))",                                                             ["ValidateResult"] = "ulong expectedResult = 0; for (int index = 0; ((data >> index) & 1) == 0; index++) { expectedResult++; } isUnexpectedResult = (expectedResult != result);" }),
 };
 
+private const string ValidateBmi2ParallelBitComment = @"
+// The validation logic defined here for Bmi2.ParallelBitDeposit and Bmi2.ParallelBitExtract is
+// based on the 'Operation' pseudo-code defined for the pdep and pext instruction in the 'Intel®
+// 64 and IA-32 Architectures Software Developer’s Manual; Volume 2 (2A, 2B, 2C & 2D): Instruction
+// Set Reference, A-Z'
+";
+
+private const string ValidateBmi2ParallelBitDepositUInt32 = ValidateBmi2ParallelBitComment + @"
+uint temp = left;
+uint mask = right;
+uint dest = 0;
+byte m = 0, k = 0;
+
+while (m < 32)
+{
+    if (((mask >> m) & 1) == 1) // Extract bit at index m of mask
+    {
+        dest |= (((temp >> k) & 1) << m); // Extract bit at index k of temp and insert to index m of dest
+        k++;
+    }
+    m++;
+}
+
+isUnexpectedResult = (dest != result);
+";
+
+private const string ValidateBmi2ParallelBitDepositUInt64 = ValidateBmi2ParallelBitComment + @"
+ulong temp = left;
+ulong mask = right;
+ulong dest = 0;
+byte m = 0, k = 0;
+
+while (m < 64)
+{
+    if (((mask >> m) & 1) == 1) // Extract bit at index m of mask
+    {
+        dest |= (((temp >> k) & 1) << m); // Extract bit at index k of temp and insert to index m of dest
+        k++;
+    }
+    m++;
+}
+
+isUnexpectedResult = (dest != result);
+";
+
+private const string ValidateBmi2ParallelBitExtractUInt32 = ValidateBmi2ParallelBitComment + @"
+uint temp = left;
+uint mask = right;
+uint dest = 0;
+byte m = 0, k = 0;
+
+while (m < 32)
+{
+    if (((mask >> m) & 1) == 1) // Extract bit at index m of mask
+    {
+        dest |= (((temp >> m) & 1) << k); // Extract bit at index m of temp and insert to index k of dest
+        k++;
+    }
+    m++;
+}
+
+isUnexpectedResult = (dest != result);
+";
+
+private const string ValidateBmi2ParallelBitExtractUInt64 = ValidateBmi2ParallelBitComment + @"
+ulong temp = left;
+ulong mask = right;
+ulong dest = 0;
+byte m = 0, k = 0;
+
+while (m < 64)
+{
+    if (((mask >> m) & 1) == 1) // Extract bit at index m of mask
+    {
+        dest |= (((temp >> m) & 1) << k); // Extract bit at index m of temp and insert to index k of dest
+        k++;
+    }
+    m++;
+}
+
+isUnexpectedResult = (dest != result);
+";
+
+private static readonly (string templateFileName, Dictionary<string, string> templateData)[] Bmi2Inputs = new []
+{
+    ("ScalarBinOpTest.template", new Dictionary<string, string> { ["Isa"] = "Bmi2", ["Method"] = "ParallelBitDeposit", ["RetBaseType"] = "UInt32", ["Op1BaseType"] = "UInt32", ["Op2BaseType"] = "UInt32", ["NextValueOp1"] = "(uint)(random.Next(0, int.MaxValue))",  ["NextValueOp2"] = "(uint)(random.Next(0, int.MaxValue))",  ["ValidateResult"] = ValidateBmi2ParallelBitDepositUInt32 }),
+    ("ScalarBinOpTest.template", new Dictionary<string, string> { ["Isa"] = "Bmi2", ["Method"] = "ParallelBitDeposit", ["RetBaseType"] = "UInt64", ["Op1BaseType"] = "UInt64", ["Op2BaseType"] = "UInt64", ["NextValueOp1"] = "(ulong)(random.Next(0, int.MaxValue))", ["NextValueOp2"] = "(ulong)(random.Next(0, int.MaxValue))", ["ValidateResult"] = ValidateBmi2ParallelBitDepositUInt64 }),
+    ("ScalarBinOpTest.template", new Dictionary<string, string> { ["Isa"] = "Bmi2", ["Method"] = "ParallelBitExtract", ["RetBaseType"] = "UInt32", ["Op1BaseType"] = "UInt32", ["Op2BaseType"] = "UInt32", ["NextValueOp1"] = "(uint)(random.Next(0, int.MaxValue))",  ["NextValueOp2"] = "(uint)(random.Next(0, int.MaxValue))",  ["ValidateResult"] = ValidateBmi2ParallelBitExtractUInt32 }),
+    ("ScalarBinOpTest.template", new Dictionary<string, string> { ["Isa"] = "Bmi2", ["Method"] = "ParallelBitExtract", ["RetBaseType"] = "UInt64", ["Op1BaseType"] = "UInt64", ["Op2BaseType"] = "UInt64", ["NextValueOp1"] = "(ulong)(random.Next(0, int.MaxValue))", ["NextValueOp2"] = "(ulong)(random.Next(0, int.MaxValue))", ["ValidateResult"] = ValidateBmi2ParallelBitExtractUInt64 }),
+};
+
 private static void ProcessInputs(string groupName, (string templateFileName, Dictionary<string, string> templateData)[] inputs)
 {
     var testListFileName = Path.Combine("..", groupName, $"Program.{groupName}.cs");
@@ -891,3 +982,4 @@ ProcessInputs("Avx2", Avx2Inputs);
 ProcessInputs("Fma_Vector128", Fma_Vector128Inputs);
 ProcessInputs("Fma_Vector256", Fma_Vector256Inputs);
 ProcessInputs("Bmi1", Bmi1Inputs);
+ProcessInputs("Bmi2", Bmi2Inputs);