aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics/Shader/Instructions/InstEmitConversion.cs
blob: f5e9af0365af8fedb4809fa30679d7c9540b41c9 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation;

using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
using static Ryujinx.Graphics.Shader.Instructions.InstEmitAluHelper;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;

namespace Ryujinx.Graphics.Shader.Instructions
{
    static partial class InstEmit
    {
        public static void F2F(EmitterContext context)
        {
            OpCodeFArith op = (OpCodeFArith)context.CurrOp;

            FPType srcType = (FPType)op.RawOpCode.Extract(8,  2);
            FPType dstType = (FPType)op.RawOpCode.Extract(10, 2);

            bool pass      = op.RawOpCode.Extract(40);
            bool negateB   = op.RawOpCode.Extract(45);
            bool absoluteB = op.RawOpCode.Extract(49);

            pass &= op.RoundingMode == RoundingMode.TowardsNegativeInfinity;

            Operand srcB = context.FPAbsNeg(GetSrcB(context, srcType), absoluteB, negateB);

            if (!pass)
            {
                switch (op.RoundingMode)
                {
                    case RoundingMode.TowardsNegativeInfinity:
                        srcB = context.FPFloor(srcB);
                        break;

                    case RoundingMode.TowardsPositiveInfinity:
                        srcB = context.FPCeiling(srcB);
                        break;

                    case RoundingMode.TowardsZero:
                        srcB = context.FPTruncate(srcB);
                        break;
                }
            }

            srcB = context.FPSaturate(srcB, op.Saturate);

            WriteFP(context, dstType, srcB);

            //TODO: CC.
        }

        public static void F2I(EmitterContext context)
        {
            OpCodeFArith op = (OpCodeFArith)context.CurrOp;

            IntegerType intType = (IntegerType)op.RawOpCode.Extract(8, 2);

            bool isSmallInt = intType <= IntegerType.U16;

            FPType floatType = (FPType)op.RawOpCode.Extract(10, 2);

            bool isSignedInt = op.RawOpCode.Extract(12);
            bool negateB     = op.RawOpCode.Extract(45);
            bool absoluteB   = op.RawOpCode.Extract(49);

            if (isSignedInt)
            {
                intType |= IntegerType.S8;
            }

            Operand srcB = context.FPAbsNeg(GetSrcB(context, floatType), absoluteB, negateB);

            switch (op.RoundingMode)
            {
                case RoundingMode.TowardsNegativeInfinity:
                    srcB = context.FPFloor(srcB);
                    break;

                case RoundingMode.TowardsPositiveInfinity:
                    srcB = context.FPCeiling(srcB);
                    break;

                case RoundingMode.TowardsZero:
                    srcB = context.FPTruncate(srcB);
                    break;
            }

            srcB = context.FPConvertToS32(srcB);

            //TODO: S/U64, conversion overflow handling.
            if (intType != IntegerType.S32)
            {
                int min = GetIntMin(intType);
                int max = GetIntMax(intType);

                srcB = isSignedInt
                    ? context.IClampS32(srcB, Const(min), Const(max))
                    : context.IClampU32(srcB, Const(min), Const(max));
            }

            Operand dest = GetDest(context);

            context.Copy(dest, srcB);

            //TODO: CC.
        }

        public static void I2F(EmitterContext context)
        {
            OpCodeAlu op = (OpCodeAlu)context.CurrOp;

            FPType dstType = (FPType)op.RawOpCode.Extract(8, 2);

            IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);

            bool isSmallInt = srcType <= IntegerType.U16;

            bool isSignedInt = op.RawOpCode.Extract(13);
            bool negateB     = op.RawOpCode.Extract(45);
            bool absoluteB   = op.RawOpCode.Extract(49);

            Operand srcB = context.IAbsNeg(GetSrcB(context), absoluteB, negateB);

            if (isSmallInt)
            {
                int size = srcType == IntegerType.U16 ? 16 : 8;

                srcB = isSignedInt
                    ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
                    : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
            }

            srcB = isSignedInt
                ? context.IConvertS32ToFP(srcB)
                : context.IConvertU32ToFP(srcB);

            WriteFP(context, dstType, srcB);

            //TODO: CC.
        }

        public static void I2I(EmitterContext context)
        {
            OpCodeAlu op = (OpCodeAlu)context.CurrOp;

            IntegerType dstType = (IntegerType)op.RawOpCode.Extract(8,  2);
            IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);

            if (srcType == IntegerType.U64 || dstType == IntegerType.U64)
            {
                //TODO: Warning. This instruction doesn't support 64-bits integers
            }

            bool srcIsSmallInt = srcType <= IntegerType.U16;

            bool dstIsSignedInt = op.RawOpCode.Extract(12);
            bool srcIsSignedInt = op.RawOpCode.Extract(13);
            bool negateB        = op.RawOpCode.Extract(45);
            bool absoluteB      = op.RawOpCode.Extract(49);

            Operand srcB = GetSrcB(context);

            if (srcIsSmallInt)
            {
                int size = srcType == IntegerType.U16 ? 16 : 8;

                srcB = srcIsSignedInt
                    ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
                    : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
            }

            srcB = context.IAbsNeg(srcB, absoluteB, negateB);

            if (op.Saturate)
            {
                if (dstIsSignedInt)
                {
                    dstType |= IntegerType.S8;
                }

                int min = GetIntMin(dstType);
                int max = GetIntMax(dstType);

                srcB = dstIsSignedInt
                    ? context.IClampS32(srcB, Const(min), Const(max))
                    : context.IClampU32(srcB, Const(min), Const(max));
            }

            context.Copy(GetDest(context), srcB);

            //TODO: CC.
        }

        private static void WriteFP(EmitterContext context, FPType type, Operand srcB)
        {
            Operand dest = GetDest(context);

            if (type == FPType.FP32)
            {
                context.Copy(dest, srcB);
            }
            else if (type == FPType.FP16)
            {
                context.Copy(dest, context.PackHalf2x16(srcB, ConstF(0)));
            }
            else
            {
                //TODO.
            }
        }
    }
}