aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Shader/Translation/Optimizations/HalfConversion.cs
blob: 960602726a0832eb892e00d0fba9e12d2b637302 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
        }
    }
}