aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/LightningJit/Arm32/Target/Arm64/InstEmitExtension.cs
blob: dafe2974e11d672290048559ddb7df86a893f1be (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
using Ryujinx.Cpu.LightningJit.CodeGen;
using Ryujinx.Cpu.LightningJit.CodeGen.Arm64;
using System;

namespace Ryujinx.Cpu.LightningJit.Arm32.Target.Arm64
{
    static class InstEmitExtension
    {
        public static void Sxtab(CodeGenContext context, uint rd, uint rn, uint rm, uint rotate)
        {
            EmitRotated(context, ArmExtensionType.Sxtb, rd, rn, rm, rotate);
        }

        public static void Sxtab16(CodeGenContext context, uint rd, uint rn, uint rm, uint rotate)
        {
            EmitExtendAccumulate8(context, rd, rn, rm, rotate, unsigned: false);
        }

        public static void Sxtah(CodeGenContext context, uint rd, uint rn, uint rm, uint rotate)
        {
            EmitRotated(context, ArmExtensionType.Sxth, rd, rn, rm, rotate);
        }

        public static void Sxtb(CodeGenContext context, uint rd, uint rm, uint rotate)
        {
            EmitRotated(context, context.Arm64Assembler.Sxtb, rd, rm, rotate);
        }

        public static void Sxtb16(CodeGenContext context, uint rd, uint rm, uint rotate)
        {
            Operand rdOperand = InstEmitCommon.GetOutputGpr(context, rd);
            Operand rmOperand = InstEmitCommon.GetInputGpr(context, rm);

            using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempGprRegisterScoped();
            using ScopedRegister tempRegister2 = context.RegisterAllocator.AllocateTempGprRegisterScoped();

            if (rotate != 0)
            {
                context.Arm64Assembler.Ror(tempRegister.Operand, rmOperand, InstEmitCommon.Const((int)rotate * 8));
                context.Arm64Assembler.And(rdOperand, tempRegister.Operand, InstEmitCommon.Const(0xff00ff));
            }
            else
            {
                context.Arm64Assembler.And(rdOperand, rmOperand, InstEmitCommon.Const(0xff00ff));
            }

            // Sign-extend by broadcasting sign bits.
            context.Arm64Assembler.And(tempRegister.Operand, rdOperand, InstEmitCommon.Const(0x800080));
            context.Arm64Assembler.Lsl(tempRegister2.Operand, tempRegister.Operand, InstEmitCommon.Const(9));
            context.Arm64Assembler.Sub(tempRegister.Operand, tempRegister2.Operand, tempRegister.Operand);
            context.Arm64Assembler.Orr(rdOperand, rdOperand, tempRegister.Operand);
        }

        public static void Sxth(CodeGenContext context, uint rd, uint rm, uint rotate)
        {
            EmitRotated(context, context.Arm64Assembler.Sxth, rd, rm, rotate);
        }

        public static void Uxtab(CodeGenContext context, uint rd, uint rn, uint rm, uint rotate)
        {
            EmitRotated(context, ArmExtensionType.Uxtb, rd, rn, rm, rotate);
        }

        public static void Uxtab16(CodeGenContext context, uint rd, uint rn, uint rm, uint rotate)
        {
            EmitExtendAccumulate8(context, rd, rn, rm, rotate, unsigned: true);
        }

        public static void Uxtah(CodeGenContext context, uint rd, uint rn, uint rm, uint rotate)
        {
            EmitRotated(context, ArmExtensionType.Uxth, rd, rn, rm, rotate);
        }

        public static void Uxtb(CodeGenContext context, uint rd, uint rm, uint rotate)
        {
            EmitRotated(context, context.Arm64Assembler.Uxtb, rd, rm, rotate);
        }

        public static void Uxtb16(CodeGenContext context, uint rd, uint rm, uint rotate)
        {
            Operand rdOperand = InstEmitCommon.GetOutputGpr(context, rd);
            Operand rmOperand = InstEmitCommon.GetInputGpr(context, rm);

            if (rotate != 0)
            {
                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempGprRegisterScoped();

                context.Arm64Assembler.Ror(tempRegister.Operand, rmOperand, InstEmitCommon.Const((int)rotate * 8));
                context.Arm64Assembler.And(rdOperand, tempRegister.Operand, InstEmitCommon.Const(0xff00ff));
            }
            else
            {
                context.Arm64Assembler.And(rdOperand, rmOperand, InstEmitCommon.Const(0xff00ff));
            }
        }

        public static void Uxth(CodeGenContext context, uint rd, uint rm, uint rotate)
        {
            EmitRotated(context, context.Arm64Assembler.Uxth, rd, rm, rotate);
        }

        private static void EmitRotated(CodeGenContext context, Action<Operand, Operand> action, uint rd, uint rm, uint rotate)
        {
            Operand rdOperand = InstEmitCommon.GetOutputGpr(context, rd);
            Operand rmOperand = InstEmitCommon.GetInputGpr(context, rm);

            if (rotate != 0)
            {
                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempGprRegisterScoped();

                context.Arm64Assembler.Ror(tempRegister.Operand, rmOperand, InstEmitCommon.Const((int)rotate * 8));
                action(rdOperand, tempRegister.Operand);
            }
            else
            {
                action(rdOperand, rmOperand);
            }
        }

        private static void EmitRotated(CodeGenContext context, ArmExtensionType extensionType, uint rd, uint rn, uint rm, uint rotate)
        {
            Operand rdOperand = InstEmitCommon.GetOutputGpr(context, rd);
            Operand rnOperand = InstEmitCommon.GetInputGpr(context, rn);
            Operand rmOperand = InstEmitCommon.GetInputGpr(context, rm);

            if (rotate != 0)
            {
                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempGprRegisterScoped();

                context.Arm64Assembler.Ror(tempRegister.Operand, rmOperand, InstEmitCommon.Const((int)rotate * 8));
                context.Arm64Assembler.Add(rdOperand, rnOperand, tempRegister.Operand, extensionType);
            }
            else
            {
                context.Arm64Assembler.Add(rdOperand, rnOperand, rmOperand, extensionType);
            }
        }

        private static void EmitExtendAccumulate8(CodeGenContext context, uint rd, uint rn, uint rm, uint rotate, bool unsigned)
        {
            Operand rdOperand = InstEmitCommon.GetOutputGpr(context, rd);
            Operand rnOperand = InstEmitCommon.GetInputGpr(context, rn);
            Operand rmOperand = InstEmitCommon.GetInputGpr(context, rm);

            if (rotate != 0)
            {
                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempGprRegisterScoped();

                context.Arm64Assembler.Ror(tempRegister.Operand, rmOperand, InstEmitCommon.Const((int)rotate * 8));

                EmitExtendAccumulate8Core(context, rdOperand, rnOperand, tempRegister.Operand, unsigned);
            }
            else
            {
                EmitExtendAccumulate8Core(context, rdOperand, rnOperand, rmOperand, unsigned);
            }
        }

        private static void EmitExtendAccumulate8Core(CodeGenContext context, Operand rd, Operand rn, Operand rm, bool unsigned)
        {
            using ScopedRegister tempD = context.RegisterAllocator.AllocateTempGprRegisterScoped();
            using ScopedRegister tempD2 = context.RegisterAllocator.AllocateTempGprRegisterScoped();
            using ScopedRegister tempN = context.RegisterAllocator.AllocateTempGprRegisterScoped();

            if (unsigned)
            {
                context.Arm64Assembler.Uxth(tempN.Operand, rn);
            }
            else
            {
                context.Arm64Assembler.Sxth(tempN.Operand, rn);
            }

            context.Arm64Assembler.Add(tempD.Operand, tempN.Operand, rm, unsigned ? ArmExtensionType.Uxtb : ArmExtensionType.Sxtb);
            context.Arm64Assembler.Uxth(tempD2.Operand, tempD.Operand);

            if (unsigned)
            {
                context.Arm64Assembler.Lsr(tempN.Operand, rn, InstEmitCommon.Const(16));
            }
            else
            {
                context.Arm64Assembler.Asr(tempN.Operand, rn, InstEmitCommon.Const(16));
            }

            context.Arm64Assembler.Lsr(tempD.Operand, rm, InstEmitCommon.Const(16));
            context.Arm64Assembler.Add(tempD.Operand, tempN.Operand, tempD.Operand, unsigned ? ArmExtensionType.Uxtb : ArmExtensionType.Sxtb);
            context.Arm64Assembler.Orr(rd, tempD2.Operand, tempD.Operand, ArmShiftType.Lsl, 16);
        }
    }
}