Enable constant folding on UnpackHalf2x16 instructions

This commit is contained in:
Gabriel 2019-04-10 21:25:29 -03:00
parent 5b106a51ed
commit 543ac2791e
2 changed files with 60 additions and 0 deletions

View file

@ -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();

View file

@ -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);
}
}
}