From 16f39aa452baee9048b24545392455c1af0e873f Mon Sep 17 00:00:00 2001
From: aroulin <andy.roulin@epfl.ch>
Date: Sun, 23 Aug 2015 15:37:57 +0200
Subject: [PATCH 1/3] x64-emitter: add RCPSS SSE instruction

---
 src/common/x64/emitter.cpp | 1 +
 src/common/x64/emitter.h   | 1 +
 2 files changed, 2 insertions(+)

diff --git a/src/common/x64/emitter.cpp b/src/common/x64/emitter.cpp
index 749a75b72..939df210e 100644
--- a/src/common/x64/emitter.cpp
+++ b/src/common/x64/emitter.cpp
@@ -1535,6 +1535,7 @@ void XEmitter::MAXSS(X64Reg regOp, const OpArg& arg)   {WriteSSEOp(0xF3, sseMAX,
 void XEmitter::MAXSD(X64Reg regOp, const OpArg& arg)   {WriteSSEOp(0xF2, sseMAX, regOp, arg);}
 void XEmitter::SQRTSS(X64Reg regOp, const OpArg& arg)  {WriteSSEOp(0xF3, sseSQRT, regOp, arg);}
 void XEmitter::SQRTSD(X64Reg regOp, const OpArg& arg)  {WriteSSEOp(0xF2, sseSQRT, regOp, arg);}
+void XEmitter::RCPSS(X64Reg regOp, const OpArg& arg)   {WriteSSEOp(0xF3, sseRCP, regOp, arg);}
 void XEmitter::RSQRTSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseRSQRT, regOp, arg);}
 
 void XEmitter::ADDPS(X64Reg regOp, const OpArg& arg)   {WriteSSEOp(0x00, sseADD, regOp, arg);}
diff --git a/src/common/x64/emitter.h b/src/common/x64/emitter.h
index 3d6eeb564..a49cd2cf1 100644
--- a/src/common/x64/emitter.h
+++ b/src/common/x64/emitter.h
@@ -586,6 +586,7 @@ public:
     void MAXSD(X64Reg regOp, const OpArg& arg);
     void SQRTSS(X64Reg regOp, const OpArg& arg);
     void SQRTSD(X64Reg regOp, const OpArg& arg);
+    void RCPSS(X64Reg regOp, const OpArg& arg);
     void RSQRTSS(X64Reg regOp, const OpArg& arg);
 
     // SSE/SSE2: Floating point bitwise (yes)

From fa552f11ef1f96afab715c9653c705e3fbbe2a74 Mon Sep 17 00:00:00 2001
From: aroulin <andy.roulin@epfl.ch>
Date: Sun, 23 Aug 2015 15:13:36 +0200
Subject: [PATCH 2/3] Shader: RCP and RSQ computes only the 1st component

---
 src/video_core/shader/shader_interpreter.cpp | 10 ++++------
 src/video_core/shader/shader_jit_x64.cpp     | 10 ++++++----
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/video_core/shader/shader_interpreter.cpp b/src/video_core/shader/shader_interpreter.cpp
index 063cc38f0..51fee6f97 100644
--- a/src/video_core/shader/shader_interpreter.cpp
+++ b/src/video_core/shader/shader_interpreter.cpp
@@ -221,13 +221,12 @@ void RunInterpreter(UnitState<Debug>& state) {
             {
                 Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
                 Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
+                float24 rcp_res = float24::FromFloat32(1.0f / src1[0].ToFloat32());
                 for (int i = 0; i < 4; ++i) {
                     if (!swizzle.DestComponentEnabled(i))
                         continue;
 
-                    // TODO: Be stable against division by zero!
-                    // TODO: I think this might be wrong... we should only use one component here
-                    dest[i] = float24::FromFloat32(1.0f / src1[i].ToFloat32());
+                    dest[i] = rcp_res;
                 }
                 Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
                 break;
@@ -238,13 +237,12 @@ void RunInterpreter(UnitState<Debug>& state) {
             {
                 Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
                 Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
+                float24 rsq_res = float24::FromFloat32(1.0f / sqrt(src1[0].ToFloat32()));
                 for (int i = 0; i < 4; ++i) {
                     if (!swizzle.DestComponentEnabled(i))
                         continue;
 
-                    // TODO: Be stable against division by zero!
-                    // TODO: I think this might be wrong... we should only use one component here
-                    dest[i] = float24::FromFloat32(1.0f / sqrt(src1[i].ToFloat32()));
+                    dest[i] = rsq_res;
                 }
                 Record<DebugDataRecord::DEST_OUT>(state.debug, iteration, dest);
                 break;
diff --git a/src/video_core/shader/shader_jit_x64.cpp b/src/video_core/shader/shader_jit_x64.cpp
index a1bdd8456..e52fe43fb 100644
--- a/src/video_core/shader/shader_jit_x64.cpp
+++ b/src/video_core/shader/shader_jit_x64.cpp
@@ -496,9 +496,10 @@ void JitCompiler::Compile_MOV(Instruction instr) {
 void JitCompiler::Compile_RCP(Instruction instr) {
     Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
 
-    // TODO(bunnei): RCPPS is a pretty rough approximation, this might cause problems if Pica
+    // TODO(bunnei): RCPSS is a pretty rough approximation, this might cause problems if Pica
     // performs this operation more accurately. This should be checked on hardware.
-    RCPPS(SRC1, R(SRC1));
+    RCPSS(SRC1, R(SRC1));
+    SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0)); // XYWZ -> XXXX
 
     Compile_DestEnable(instr, SRC1);
 }
@@ -506,9 +507,10 @@ void JitCompiler::Compile_RCP(Instruction instr) {
 void JitCompiler::Compile_RSQ(Instruction instr) {
     Compile_SwizzleSrc(instr, 1, instr.common.src1, SRC1);
 
-    // TODO(bunnei): RSQRTPS is a pretty rough approximation, this might cause problems if Pica
+    // TODO(bunnei): RSQRTSS is a pretty rough approximation, this might cause problems if Pica
     // performs this operation more accurately. This should be checked on hardware.
-    RSQRTPS(SRC1, R(SRC1));
+    RSQRTSS(SRC1, R(SRC1));
+    SHUFPS(SRC1, R(SRC1), _MM_SHUFFLE(0, 0, 0, 0)); // XYWZ -> XXXX
 
     Compile_DestEnable(instr, SRC1);
 }

From 03c5cfead4a6ad75097e736062b25f9a7e6082cd Mon Sep 17 00:00:00 2001
From: aroulin <andy.roulin@epfl.ch>
Date: Sun, 23 Aug 2015 22:03:07 +0200
Subject: [PATCH 3/3] Shader: Use std::sqrt for float instead of sqrt

---
 src/video_core/shader/shader_interpreter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/video_core/shader/shader_interpreter.cpp b/src/video_core/shader/shader_interpreter.cpp
index 51fee6f97..76fda95f3 100644
--- a/src/video_core/shader/shader_interpreter.cpp
+++ b/src/video_core/shader/shader_interpreter.cpp
@@ -237,7 +237,7 @@ void RunInterpreter(UnitState<Debug>& state) {
             {
                 Record<DebugDataRecord::SRC1>(state.debug, iteration, src1);
                 Record<DebugDataRecord::DEST_IN>(state.debug, iteration, dest);
-                float24 rsq_res = float24::FromFloat32(1.0f / sqrt(src1[0].ToFloat32()));
+                float24 rsq_res = float24::FromFloat32(1.0f / std::sqrt(src1[0].ToFloat32()));
                 for (int i = 0; i < 4; ++i) {
                     if (!swizzle.DestComponentEnabled(i))
                         continue;