aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/NumberFormatter.cs
blob: 28e44c900e0dea4ab9f06cdd51cdd83b403fbfdf (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Globalization;

namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
{
    static class NumberFormatter
    {
        private const int MaxDecimal = 256;

        public static bool TryFormat(int value, AggregateType dstType, out string formatted)
        {
            if (dstType == AggregateType.FP32)
            {
                return TryFormatFloat(BitConverter.Int32BitsToSingle(value), out formatted);
            }
            else if (dstType == AggregateType.S32)
            {
                formatted = FormatInt(value);
            }
            else if (dstType == AggregateType.U32)
            {
                formatted = FormatUint((uint)value);
            }
            else if (dstType == AggregateType.Bool)
            {
                formatted = value != 0 ? "true" : "false";
            }
            else
            {
                throw new ArgumentException($"Invalid variable type \"{dstType}\".");
            }

            return true;
        }

        public static string FormatFloat(float value)
        {
            if (!TryFormatFloat(value, out string formatted))
            {
                throw new ArgumentException("Failed to convert float value to string.");
            }

            return formatted;
        }

        public static bool TryFormatFloat(float value, out string formatted)
        {
            if (float.IsNaN(value) || float.IsInfinity(value))
            {
                formatted = null;

                return false;
            }

            formatted = value.ToString("G9", CultureInfo.InvariantCulture);

            if (!(formatted.Contains('.') ||
                  formatted.Contains('e') ||
                  formatted.Contains('E')))
            {
                formatted += ".0";
            }

            return true;
        }

        public static string FormatInt(int value, AggregateType dstType)
        {
            if (dstType == AggregateType.S32)
            {
                return FormatInt(value);
            }
            else if (dstType == AggregateType.U32)
            {
                return FormatUint((uint)value);
            }
            else
            {
                throw new ArgumentException($"Invalid variable type \"{dstType}\".");
            }
        }

        public static string FormatInt(int value)
        {
            if (value <= MaxDecimal && value >= -MaxDecimal)
            {
                return value.ToString(CultureInfo.InvariantCulture);
            }

            return "0x" + value.ToString("X", CultureInfo.InvariantCulture);
        }

        public static string FormatUint(uint value)
        {
            if (value <= MaxDecimal && value >= 0)
            {
                return value.ToString(CultureInfo.InvariantCulture) + "u";
            }

            return "0x" + value.ToString("X", CultureInfo.InvariantCulture) + "u";
        }
    }
}