From e10b11a5d06de6efd1ccb39a0ed6bb602761df6d Mon Sep 17 00:00:00 2001
From: Yuri Kunde Schlesner <yuriks@yuriks.net>
Date: Sun, 12 Feb 2017 13:29:14 -0800
Subject: [PATCH] video_core/shader: Document sanitized MUL operation

---
 src/video_core/shader/shader_jit_x64_compiler.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/video_core/shader/shader_jit_x64_compiler.cpp b/src/video_core/shader/shader_jit_x64_compiler.cpp
index 92b35dbc0..2dbc8b147 100644
--- a/src/video_core/shader/shader_jit_x64_compiler.cpp
+++ b/src/video_core/shader/shader_jit_x64_compiler.cpp
@@ -295,14 +295,22 @@ void JitShader::Compile_DestEnable(Instruction instr, Xmm src) {
 }
 
 void JitShader::Compile_SanitizedMul(Xmm src1, Xmm src2, Xmm scratch) {
+    // 0 * inf and inf * 0 in the PICA should return 0 instead of NaN. This can be implemented by
+    // checking for NaNs before and after the multiplication.  If the multiplication result is NaN
+    // where neither source was, this NaN was generated by a 0 * inf multiplication, and so the
+    // result should be transformed to 0 to match PICA fp rules.
+
+    // Set scratch to mask of (src1 != NaN and src2 != NaN)
     movaps(scratch, src1);
     cmpordps(scratch, src2);
 
     mulps(src1, src2);
 
+    // Set src2 to mask of (result == NaN)
     movaps(src2, src1);
     cmpunordps(src2, src2);
 
+    // Clear components where scratch != src2 (i.e. if result is NaN where neither source was NaN)
     xorps(scratch, src2);
     andps(src1, scratch);
 }