From 543ac2791ede8c8dd520286decdaae59d2e9f3cf Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 10 Apr 2019 21:25:29 -0300 Subject: [PATCH] Enable constant folding on UnpackHalf2x16 instructions --- .../Optimizations/ConstantFolding.cs | 13 +++++ .../Optimizations/HalfConversion.cs | 47 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Ryujinx.Graphics/Shader/Translation/Optimizations/HalfConversion.cs diff --git a/Ryujinx.Graphics/Shader/Translation/Optimizations/ConstantFolding.cs b/Ryujinx.Graphics/Shader/Translation/Optimizations/ConstantFolding.cs index 3c12517139..a2e05ef120 100644 --- a/Ryujinx.Graphics/Shader/Translation/Optimizations/ConstantFolding.cs +++ b/Ryujinx.Graphics/Shader/Translation/Optimizations/ConstantFolding.cs @@ -188,6 +188,10 @@ namespace Ryujinx.Graphics.Shader.Translation.Optimizations case Instruction.Subtract: EvaluateBinary(operation, (x, y) => x - y); break; + + case Instruction.UnpackHalf2x16: + UnpackHalf2x16(operation); + break; } } @@ -229,6 +233,15 @@ namespace Ryujinx.Graphics.Shader.Translation.Optimizations return value.Extract(lsb, length); } + private static void UnpackHalf2x16(Operation operation) + { + int value = operation.GetSource(0).Value; + + value = (value >> operation.ComponentIndex * 16) & 0xffff; + + operation.TurnIntoCopy(ConstF(HalfConversion.HalfToSingle(value))); + } + private static void FPNegate(Operation operation) { float value = operation.GetSource(0).AsFloat(); diff --git a/Ryujinx.Graphics/Shader/Translation/Optimizations/HalfConversion.cs b/Ryujinx.Graphics/Shader/Translation/Optimizations/HalfConversion.cs new file mode 100644 index 0000000000..9ef35abc92 --- /dev/null +++ b/Ryujinx.Graphics/Shader/Translation/Optimizations/HalfConversion.cs @@ -0,0 +1,47 @@ +using System; + +namespace Ryujinx.Graphics.Shader.Translation.Optimizations +{ + static class HalfConversion + { + public static float HalfToSingle(int value) + { + int mantissa = (value >> 0) & 0x3ff; + int exponent = (value >> 10) & 0x1f; + int sign = (value >> 15) & 0x1; + + if (exponent == 0x1f) + { + //NaN or Infinity. + mantissa <<= 13; + exponent = 0xff; + } + else if (exponent != 0 || mantissa != 0 ) + { + if (exponent == 0) + { + //Denormal. + int e = -1; + int m = mantissa; + + do + { + e++; + m <<= 1; + } + while ((m & 0x400) == 0); + + mantissa = m & 0x3ff; + exponent = e; + } + + mantissa <<= 13; + exponent = 127 - 15 + exponent; + } + + int output = (sign << 31) | (exponent << 23) | mantissa; + + return BitConverter.Int32BitsToSingle(output); + } + } +} \ No newline at end of file