aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Tests/Cpu/CpuTestSimdImm.cs
blob: 27e3b41a06db0f3dc4ead11966576680d7b2eaa3 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#define SimdImm

using ARMeilleure.State;
using NUnit.Framework;
using System.Collections.Generic;

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

        #region "Helper methods"
        // abcdefgh -> aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffffgggggggghhhhhhhh
        private static ulong ExpandImm8(byte imm8)
        {
            ulong imm64 = 0ul;

            for (int i = 0, j = 0; i < 8; i++, j += 8)
            {
                if (((imm8 >> i) & 0b1) != 0)
                {
                    imm64 |= 0b11111111ul << j;
                }
            }

            return imm64;
        }

        // aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffffgggggggghhhhhhhh -> abcdefgh
        private static byte ShrinkImm64(ulong imm64)
        {
            byte imm8 = 0;

            for (int i = 0, j = 0; i < 8; i++, j += 8)
            {
                if (((imm64 >> j) & 0b11111111ul) != 0ul) // Note: no format check.
                {
                    imm8 |= (byte)(0b1 << i);
                }
            }

            return imm8;
        }
        #endregion

        #region "ValueSource (Types)"
        private static ulong[] _2S_()
        {
            return new[] {
                0x0000000000000000ul, 0x7FFFFFFF7FFFFFFFul,
                0x8000000080000000ul, 0xFFFFFFFFFFFFFFFFul,
            };
        }

        private static ulong[] _4H_()
        {
            return new[] {
                0x0000000000000000ul, 0x7FFF7FFF7FFF7FFFul,
                0x8000800080008000ul, 0xFFFFFFFFFFFFFFFFul,
            };
        }

        private static IEnumerable<byte> _8BIT_IMM_()
        {
            yield return 0x00;
            yield return 0x7F;
            yield return 0x80;
            yield return 0xFF;

            for (int cnt = 1; cnt <= RndCntImm8; cnt++)
            {
                byte imm8 = TestContext.CurrentContext.Random.NextByte();

                yield return imm8;
            }
        }

        private static IEnumerable<ulong> _64BIT_IMM_()
        {
            yield return ExpandImm8(0x00);
            yield return ExpandImm8(0x7F);
            yield return ExpandImm8(0x80);
            yield return ExpandImm8(0xFF);

            for (int cnt = 1; cnt <= RndCntImm64; cnt++)
            {
                byte imm8 = TestContext.CurrentContext.Random.NextByte();

                yield return ExpandImm8(imm8);
            }
        }
        #endregion

        #region "ValueSource (Opcodes)"
        private static uint[] _Bic_Orr_Vi_16bit_()
        {
            return new[]
            {
                0x2F009400u, // BIC V0.4H, #0
                0x0F009400u, // ORR V0.4H, #0
            };
        }

        private static uint[] _Bic_Orr_Vi_32bit_()
        {
            return new[]
            {
                0x2F001400u, // BIC V0.2S, #0
                0x0F001400u, // ORR V0.2S, #0
            };
        }

        private static uint[] _F_Mov_Vi_2S_()
        {
            return new[]
            {
                0x0F00F400u, // FMOV V0.2S, #2.0
            };
        }

        private static uint[] _F_Mov_Vi_4S_()
        {
            return new[]
            {
                0x4F00F400u, // FMOV V0.4S, #2.0
            };
        }

        private static uint[] _F_Mov_Vi_2D_()
        {
            return new[]
            {
                0x6F00F400u, // FMOV V0.2D, #2.0
            };
        }

        private static uint[] _Movi_V_8bit_()
        {
            return new[]
            {
                0x0F00E400u, // MOVI V0.8B, #0
            };
        }

        private static uint[] _Movi_Mvni_V_16bit_shifted_imm_()
        {
            return new[]
            {
                0x0F008400u, // MOVI V0.4H, #0
                0x2F008400u, // MVNI V0.4H, #0
            };
        }

        private static uint[] _Movi_Mvni_V_32bit_shifted_imm_()
        {
            return new[]
            {
                0x0F000400u, // MOVI V0.2S, #0
                0x2F000400u, // MVNI V0.2S, #0
            };
        }

        private static uint[] _Movi_Mvni_V_32bit_shifting_ones_()
        {
            return new[]
            {
                0x0F00C400u, // MOVI V0.2S, #0, MSL #8
                0x2F00C400u, // MVNI V0.2S, #0, MSL #8
            };
        }

        private static uint[] _Movi_V_64bit_scalar_()
        {
            return new[]
            {
                0x2F00E400u, // MOVI D0, #0
            };
        }

        private static uint[] _Movi_V_64bit_vector_()
        {
            return new[]
            {
                0x6F00E400u, // MOVI V0.2D, #0
            };
        }
        #endregion

        private const int RndCntImm8 = 2;
        private const int RndCntImm64 = 2;

        [Test, Pairwise]
        public void Bic_Orr_Vi_16bit([ValueSource(nameof(_Bic_Orr_Vi_16bit_))] uint opcodes,
                                     [ValueSource(nameof(_4H_))] ulong z,
                                     [ValueSource(nameof(_8BIT_IMM_))] byte imm8,
                                     [Values(0b0u, 0b1u)] uint amount, // <0, 8>
                                     [Values(0b0u, 0b1u)] uint q)      // <4H, 8H>
        {
            uint abc = (imm8 & 0xE0u) >> 5;
            uint defgh = (imm8 & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);
            opcodes |= ((amount & 1) << 13);
            opcodes |= ((q & 1) << 30);

            V128 v0 = MakeVectorE0E1(z, z);

            SingleOpcode(opcodes, v0: v0);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        public void Bic_Orr_Vi_32bit([ValueSource(nameof(_Bic_Orr_Vi_32bit_))] uint opcodes,
                                     [ValueSource(nameof(_2S_))] ulong z,
                                     [ValueSource(nameof(_8BIT_IMM_))] byte imm8,
                                     [Values(0b00u, 0b01u, 0b10u, 0b11u)] uint amount, // <0, 8, 16, 24>
                                     [Values(0b0u, 0b1u)] uint q)                      // <2S, 4S>
        {
            uint abc = (imm8 & 0xE0u) >> 5;
            uint defgh = (imm8 & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);
            opcodes |= ((amount & 3) << 13);
            opcodes |= ((q & 1) << 30);

            V128 v0 = MakeVectorE0E1(z, z);

            SingleOpcode(opcodes, v0: v0);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        [Explicit]
        public void F_Mov_Vi_2S([ValueSource(nameof(_F_Mov_Vi_2S_))] uint opcodes,
                                [Range(0u, 255u, 1u)] uint abcdefgh)
        {
            uint abc = (abcdefgh & 0xE0u) >> 5;
            uint defgh = (abcdefgh & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);

            ulong z = TestContext.CurrentContext.Random.NextULong();
            V128 v0 = MakeVectorE1(z);

            SingleOpcode(opcodes, v0: v0);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        [Explicit]
        public void F_Mov_Vi_4S([ValueSource(nameof(_F_Mov_Vi_4S_))] uint opcodes,
                                [Range(0u, 255u, 1u)] uint abcdefgh)
        {
            uint abc = (abcdefgh & 0xE0u) >> 5;
            uint defgh = (abcdefgh & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);

            SingleOpcode(opcodes);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        [Explicit]
        public void F_Mov_Vi_2D([ValueSource(nameof(_F_Mov_Vi_2D_))] uint opcodes,
                                [Range(0u, 255u, 1u)] uint abcdefgh)
        {
            uint abc = (abcdefgh & 0xE0u) >> 5;
            uint defgh = (abcdefgh & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);

            SingleOpcode(opcodes);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        public void Movi_V_8bit([ValueSource(nameof(_Movi_V_8bit_))] uint opcodes,
                                [ValueSource(nameof(_8BIT_IMM_))] byte imm8,
                                [Values(0b0u, 0b1u)] uint q) // <8B, 16B>
        {
            uint abc = (imm8 & 0xE0u) >> 5;
            uint defgh = (imm8 & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);
            opcodes |= ((q & 1) << 30);

            ulong z = TestContext.CurrentContext.Random.NextULong();
            V128 v0 = MakeVectorE1(q == 0u ? z : 0ul);

            SingleOpcode(opcodes, v0: v0);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        public void Movi_Mvni_V_16bit_shifted_imm([ValueSource(nameof(_Movi_Mvni_V_16bit_shifted_imm_))] uint opcodes,
                                                  [ValueSource(nameof(_8BIT_IMM_))] byte imm8,
                                                  [Values(0b0u, 0b1u)] uint amount, // <0, 8>
                                                  [Values(0b0u, 0b1u)] uint q)      // <4H, 8H>
        {
            uint abc = (imm8 & 0xE0u) >> 5;
            uint defgh = (imm8 & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);
            opcodes |= ((amount & 1) << 13);
            opcodes |= ((q & 1) << 30);

            ulong z = TestContext.CurrentContext.Random.NextULong();
            V128 v0 = MakeVectorE1(q == 0u ? z : 0ul);

            SingleOpcode(opcodes, v0: v0);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        public void Movi_Mvni_V_32bit_shifted_imm([ValueSource(nameof(_Movi_Mvni_V_32bit_shifted_imm_))] uint opcodes,
                                                  [ValueSource(nameof(_8BIT_IMM_))] byte imm8,
                                                  [Values(0b00u, 0b01u, 0b10u, 0b11u)] uint amount, // <0, 8, 16, 24>
                                                  [Values(0b0u, 0b1u)] uint q)                      // <2S, 4S>
        {
            uint abc = (imm8 & 0xE0u) >> 5;
            uint defgh = (imm8 & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);
            opcodes |= ((amount & 3) << 13);
            opcodes |= ((q & 1) << 30);

            ulong z = TestContext.CurrentContext.Random.NextULong();
            V128 v0 = MakeVectorE1(q == 0u ? z : 0ul);

            SingleOpcode(opcodes, v0: v0);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        public void Movi_Mvni_V_32bit_shifting_ones([ValueSource(nameof(_Movi_Mvni_V_32bit_shifting_ones_))] uint opcodes,
                                                    [ValueSource(nameof(_8BIT_IMM_))] byte imm8,
                                                    [Values(0b0u, 0b1u)] uint amount, // <8, 16>
                                                    [Values(0b0u, 0b1u)] uint q)      // <2S, 4S>
        {
            uint abc = (imm8 & 0xE0u) >> 5;
            uint defgh = (imm8 & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);
            opcodes |= ((amount & 1) << 12);
            opcodes |= ((q & 1) << 30);

            ulong z = TestContext.CurrentContext.Random.NextULong();
            V128 v0 = MakeVectorE1(q == 0u ? z : 0ul);

            SingleOpcode(opcodes, v0: v0);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        public void Movi_V_64bit_scalar([ValueSource(nameof(_Movi_V_64bit_scalar_))] uint opcodes,
                                        [ValueSource(nameof(_64BIT_IMM_))] ulong imm)
        {
            byte imm8 = ShrinkImm64(imm);

            uint abc = (imm8 & 0xE0u) >> 5;
            uint defgh = (imm8 & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);

            ulong z = TestContext.CurrentContext.Random.NextULong();
            V128 v0 = MakeVectorE1(z);

            SingleOpcode(opcodes, v0: v0);

            CompareAgainstUnicorn();
        }

        [Test, Pairwise]
        public void Movi_V_64bit_vector([ValueSource(nameof(_Movi_V_64bit_vector_))] uint opcodes,
                                        [ValueSource(nameof(_64BIT_IMM_))] ulong imm)
        {
            byte imm8 = ShrinkImm64(imm);

            uint abc = (imm8 & 0xE0u) >> 5;
            uint defgh = (imm8 & 0x1Fu);

            opcodes |= (abc << 16) | (defgh << 5);

            SingleOpcode(opcodes);

            CompareAgainstUnicorn();
        }
#endif
    }
}