From b3c051bbec58c667e0e9037c57300791775d84ea Mon Sep 17 00:00:00 2001
From: Ficture Seven <FICTURE7@gmail.com>
Date: Thu, 30 Jul 2020 09:52:26 +0400
Subject: [PATCH] Use movd,movq for i32/64 VectorExtract %x, 0x0 (#1439)

* Use movd,movq for i32/64 VectorExtract %x, 0x0

* Increment PPTC interval version

* Use else-if instead

- Address gdkchan's feedback.
- Clean up Debug.Assert calls

* Inline `count` expression into Debug.Assert

Apparently the CoreCLR JIT will not eliminate this. :(
---
 ARMeilleure/CodeGen/X86/CodeGenerator.cs | 56 ++++++++++--------------
 ARMeilleure/Translation/PTC/Ptc.cs       |  2 +-
 2 files changed, 24 insertions(+), 34 deletions(-)

diff --git a/ARMeilleure/CodeGen/X86/CodeGenerator.cs b/ARMeilleure/CodeGen/X86/CodeGenerator.cs
index e217a665..5c9fcd89 100644
--- a/ARMeilleure/CodeGen/X86/CodeGenerator.cs
+++ b/ARMeilleure/CodeGen/X86/CodeGenerator.cs
@@ -1148,62 +1148,52 @@ namespace ARMeilleure.CodeGen.X86
 
             byte index = src2.AsByte();
 
+            Debug.Assert(index < OperandType.V128.GetSizeInBytes() / dest.Type.GetSizeInBytes());
+
             if (dest.Type == OperandType.I32)
             {
-                Debug.Assert(index < 4);
-
-                if (HardwareCapabilities.SupportsSse41)
+                if (index == 0)
+                {
+                    context.Assembler.Movd(dest, src1);
+                }
+                else if (HardwareCapabilities.SupportsSse41)
                 {
                     context.Assembler.Pextrd(dest, src1, index);
                 }
                 else
                 {
-                    if (index != 0)
-                    {
-                        int mask0 = 0b11_10_01_00;
-                        int mask1 = 0b11_10_01_00;
+                    int mask0 = 0b11_10_01_00;
+                    int mask1 = 0b11_10_01_00;
 
-                        mask0 = BitUtils.RotateRight(mask0,     index * 2, 8);
-                        mask1 = BitUtils.RotateRight(mask1, 8 - index * 2, 8);
+                    mask0 = BitUtils.RotateRight(mask0, index * 2, 8);
+                    mask1 = BitUtils.RotateRight(mask1, 8 - index * 2, 8);
 
-                        context.Assembler.Pshufd(src1, src1, (byte)mask0);
-                        context.Assembler.Movd  (dest, src1);
-                        context.Assembler.Pshufd(src1, src1, (byte)mask1);
-                    }
-                    else
-                    {
-                        context.Assembler.Movd(dest, src1);
-                    }
+                    context.Assembler.Pshufd(src1, src1, (byte)mask0);
+                    context.Assembler.Movd  (dest, src1);
+                    context.Assembler.Pshufd(src1, src1, (byte)mask1);
                 }
             }
             else if (dest.Type == OperandType.I64)
             {
-                Debug.Assert(index < 2);
-
-                if (HardwareCapabilities.SupportsSse41)
+                if (index == 0)
+                {
+                    context.Assembler.Movq(dest, src1);
+                }
+                else if (HardwareCapabilities.SupportsSse41)
                 {
                     context.Assembler.Pextrq(dest, src1, index);
                 }
                 else
                 {
-                    if (index != 0)
-                    {
-                        const byte mask = 0b01_00_11_10;
+                    const byte mask = 0b01_00_11_10;
 
-                        context.Assembler.Pshufd(src1, src1, mask);
-                        context.Assembler.Movq  (dest, src1);
-                        context.Assembler.Pshufd(src1, src1, mask);
-                    }
-                    else
-                    {
-                        context.Assembler.Movq(dest, src1);
-                    }
+                    context.Assembler.Pshufd(src1, src1, mask);
+                    context.Assembler.Movq  (dest, src1);
+                    context.Assembler.Pshufd(src1, src1, mask);
                 }
             }
             else
             {
-                Debug.Assert(index < (dest.Type == OperandType.FP32 ? 4 : 2));
-
                 // Floating-point types.
                 if ((index >= 2 && dest.Type == OperandType.FP32) ||
                     (index == 1 && dest.Type == OperandType.FP64))
diff --git a/ARMeilleure/Translation/PTC/Ptc.cs b/ARMeilleure/Translation/PTC/Ptc.cs
index ae92d4f3..1affa427 100644
--- a/ARMeilleure/Translation/PTC/Ptc.cs
+++ b/ARMeilleure/Translation/PTC/Ptc.cs
@@ -20,7 +20,7 @@ namespace ARMeilleure.Translation.PTC
     {
         private const string HeaderMagic = "PTChd";
 
-        private const int InternalVersion = 14; //! To be incremented manually for each change to the ARMeilleure project.
+        private const int InternalVersion = 17; //! To be incremented manually for each change to the ARMeilleure project.
 
         private const string BaseDir = "Ryujinx";