aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Tests/Cpu/CpuTestAlu.cs
blob: 0c4aa3b0e393af5a34dbb9106ac4de16b6aa6392 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#define Alu

using NUnit.Framework;

using System.Collections.Generic;

namespace Ryujinx.Tests.Cpu
{
    [Category("Alu")]
    public sealed class CpuTestAlu : CpuTest
    {
#if Alu

#region "Helper methods"
        private static uint GenLeadingSignsMinus32(int cnt) // 0 <= cnt <= 31
        {
            return ~GenLeadingZeros32(cnt + 1);
        }

        private static ulong GenLeadingSignsMinus64(int cnt) // 0 <= cnt <= 63
        {
            return ~GenLeadingZeros64(cnt + 1);
        }

        private static uint GenLeadingSignsPlus32(int cnt) // 0 <= cnt <= 31
        {
            return GenLeadingZeros32(cnt + 1);
        }

        private static ulong GenLeadingSignsPlus64(int cnt) // 0 <= cnt <= 63
        {
            return GenLeadingZeros64(cnt + 1);
        }

        private static uint GenLeadingZeros32(int cnt) // 0 <= cnt <= 32
        {
            if (cnt == 32) return 0u;
            if (cnt == 31) return 1u;

            uint rnd  = TestContext.CurrentContext.Random.NextUInt();
            int  mask = int.MinValue;

            return (rnd >> (cnt + 1)) | ((uint)mask >> cnt);
        }

        private static ulong GenLeadingZeros64(int cnt) // 0 <= cnt <= 64
        {
            if (cnt == 64) return 0ul;
            if (cnt == 63) return 1ul;

            ulong rnd  = TestContext.CurrentContext.Random.NextULong();
            long  mask = long.MinValue;

            return (rnd >> (cnt + 1)) | ((ulong)mask >> cnt);
        }
#endregion

#region "ValueSource (Types)"
        private static IEnumerable<ulong> _GenLeadingSignsX_()
        {
            for (int cnt = 0; cnt <= 63; cnt++)
            {
                yield return GenLeadingSignsMinus64(cnt);
                yield return GenLeadingSignsPlus64(cnt);
            }
        }

        private static IEnumerable<uint> _GenLeadingSignsW_()
        {
            for (int cnt = 0; cnt <= 31; cnt++)
            {
                yield return GenLeadingSignsMinus32(cnt);
                yield return GenLeadingSignsPlus32(cnt);
            }
        }

        private static IEnumerable<ulong> _GenLeadingZerosX_()
        {
            for (int cnt = 0; cnt <= 64; cnt++)
            {
                yield return GenLeadingZeros64(cnt);
            }
        }

        private static IEnumerable<uint> _GenLeadingZerosW_()
        {
            for (int cnt = 0; cnt <= 32; cnt++)
            {
                yield return GenLeadingZeros32(cnt);
            }
        }
#endregion

        private const int RndCnt = 2;

        [Test, Pairwise, Description("CLS <Xd>, <Xn>")]
        public void Cls_64bit([Values(0u, 31u)] uint rd,
                              [Values(1u, 31u)] uint rn,
                              [ValueSource("_GenLeadingSignsX_")] [Random(RndCnt)] ulong xn)
        {
            uint opcode = 0xDAC01400; // CLS X0, X0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            ulong x31 = TestContext.CurrentContext.Random.NextULong();

            SingleOpcode(opcode, x1: xn, x31: x31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("CLS <Wd>, <Wn>")]
        public void Cls_32bit([Values(0u, 31u)] uint rd,
                              [Values(1u, 31u)] uint rn,
                              [ValueSource("_GenLeadingSignsW_")] [Random(RndCnt)] uint wn)
        {
            uint opcode = 0x5AC01400; // CLS W0, W0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            uint w31 = TestContext.CurrentContext.Random.NextUInt();

            SingleOpcode(opcode, x1: wn, x31: w31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("CLZ <Xd>, <Xn>")]
        public void Clz_64bit([Values(0u, 31u)] uint rd,
                              [Values(1u, 31u)] uint rn,
                              [ValueSource("_GenLeadingZerosX_")] [Random(RndCnt)] ulong xn)
        {
            uint opcode = 0xDAC01000; // CLZ X0, X0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            ulong x31 = TestContext.CurrentContext.Random.NextULong();

            SingleOpcode(opcode, x1: xn, x31: x31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("CLZ <Wd>, <Wn>")]
        public void Clz_32bit([Values(0u, 31u)] uint rd,
                              [Values(1u, 31u)] uint rn,
                              [ValueSource("_GenLeadingZerosW_")] [Random(RndCnt)] uint wn)
        {
            uint opcode = 0x5AC01000; // CLZ W0, W0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            uint w31 = TestContext.CurrentContext.Random.NextUInt();

            SingleOpcode(opcode, x1: wn, x31: w31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("RBIT <Xd>, <Xn>")]
        public void Rbit_64bit([Values(0u, 31u)] uint rd,
                               [Values(1u, 31u)] uint rn,
                               [Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
                                       0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] [Random(RndCnt)] ulong xn)
        {
            uint opcode = 0xDAC00000; // RBIT X0, X0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            ulong x31 = TestContext.CurrentContext.Random.NextULong();

            SingleOpcode(opcode, x1: xn, x31: x31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("RBIT <Wd>, <Wn>")]
        public void Rbit_32bit([Values(0u, 31u)] uint rd,
                               [Values(1u, 31u)] uint rn,
                               [Values(0x00000000u, 0x7FFFFFFFu,
                                       0x80000000u, 0xFFFFFFFFu)] [Random(RndCnt)] uint wn)
        {
            uint opcode = 0x5AC00000; // RBIT W0, W0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            uint w31 = TestContext.CurrentContext.Random.NextUInt();

            SingleOpcode(opcode, x1: wn, x31: w31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("REV16 <Xd>, <Xn>")]
        public void Rev16_64bit([Values(0u, 31u)] uint rd,
                                [Values(1u, 31u)] uint rn,
                                [Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
                                        0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] [Random(RndCnt)] ulong xn)
        {
            uint opcode = 0xDAC00400; // REV16 X0, X0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            ulong x31 = TestContext.CurrentContext.Random.NextULong();

            SingleOpcode(opcode, x1: xn, x31: x31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("REV16 <Wd>, <Wn>")]
        public void Rev16_32bit([Values(0u, 31u)] uint rd,
                                [Values(1u, 31u)] uint rn,
                                [Values(0x00000000u, 0x7FFFFFFFu,
                                        0x80000000u, 0xFFFFFFFFu)] [Random(RndCnt)] uint wn)
        {
            uint opcode = 0x5AC00400; // REV16 W0, W0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            uint w31 = TestContext.CurrentContext.Random.NextUInt();

            SingleOpcode(opcode, x1: wn, x31: w31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("REV32 <Xd>, <Xn>")]
        public void Rev32_64bit([Values(0u, 31u)] uint rd,
                                [Values(1u, 31u)] uint rn,
                                [Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
                                        0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] [Random(RndCnt)] ulong xn)
        {
            uint opcode = 0xDAC00800; // REV32 X0, X0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            ulong x31 = TestContext.CurrentContext.Random.NextULong();

            SingleOpcode(opcode, x1: xn, x31: x31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("REV <Wd>, <Wn>")]
        public void Rev32_32bit([Values(0u, 31u)] uint rd,
                                [Values(1u, 31u)] uint rn,
                                [Values(0x00000000u, 0x7FFFFFFFu,
                                        0x80000000u, 0xFFFFFFFFu)] [Random(RndCnt)] uint wn)
        {
            uint opcode = 0x5AC00800; // REV W0, W0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            uint w31 = TestContext.CurrentContext.Random.NextUInt();

            SingleOpcode(opcode, x1: wn, x31: w31);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise, Description("REV64 <Xd>, <Xn>")]
        public void Rev64_64bit([Values(0u, 31u)] uint rd,
                                [Values(1u, 31u)] uint rn,
                                [Values(0x0000000000000000ul, 0x7FFFFFFFFFFFFFFFul,
                                        0x8000000000000000ul, 0xFFFFFFFFFFFFFFFFul)] [Random(RndCnt)] ulong xn)
        {
            uint opcode = 0xDAC00C00; // REV64 X0, X0
            opcode |= ((rn & 31) << 5) | ((rd & 31) << 0);

            ulong x31 = TestContext.CurrentContext.Random.NextULong();

            SingleOpcode(opcode, x1: xn, x31: x31);

            CompareAgainstUnicorn();
        }
#endif
    }
}