aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/LightningJit/Arm32/Target/Arm64/InstEmitNeonMove.cs
blob: 08a0673aaa60558cb95b5f36f193378c4c953f39 (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
using Ryujinx.Cpu.LightningJit.CodeGen;
using System;
using System.Diagnostics;
using System.Numerics;

namespace Ryujinx.Cpu.LightningJit.Arm32.Target.Arm64
{
    static class InstEmitNeonMove
    {
        public static void VdupR(CodeGenContext context, uint rd, uint rt, uint b, uint e, uint q)
        {
            uint size = 2 - (e | (b << 1));

            Debug.Assert(size < 3);

            Operand rtOperand = InstEmitCommon.GetInputGpr(context, rt);

            uint imm5 = InstEmitNeonCommon.GetImm5ForElementIndex(0, size);

            if (q == 0)
            {
                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                context.Arm64Assembler.DupGen(tempRegister.Operand, rtOperand, imm5, q);

                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rd, false);
            }
            else
            {
                Debug.Assert((rd & 1) == 0);

                Operand rdOperand = context.RegisterAllocator.RemapSimdRegister((int)(rd >> 1));

                context.Arm64Assembler.DupGen(rdOperand, rtOperand, imm5, q);
            }
        }

        public static void VdupS(CodeGenContext context, uint rd, uint rm, uint imm4, uint q)
        {
            uint size = (uint)BitOperations.TrailingZeroCount(imm4);

            Debug.Assert(size < 3);

            uint index = imm4 >> (int)(size + 1);

            Operand rmOperand = context.RegisterAllocator.RemapSimdRegister((int)(rm >> 1));

            uint imm5 = InstEmitNeonCommon.GetImm5ForElementIndex(index | ((rm & 1) << (int)(3 - size)), size);

            if (q == 0)
            {
                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                context.Arm64Assembler.DupEltVectorFromElement(tempRegister.Operand, rmOperand, imm5, q);

                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rd, false);
            }
            else
            {
                Debug.Assert((rd & 1) == 0);

                Operand rdOperand = context.RegisterAllocator.RemapSimdRegister((int)(rd >> 1));

                context.Arm64Assembler.DupEltVectorFromElement(rdOperand, rmOperand, imm5, q);
            }
        }

        public static void Vext(CodeGenContext context, uint rd, uint rn, uint rm, uint imm4, uint q)
        {
            if (q == 0)
            {
                using ScopedRegister rnReg = InstEmitNeonCommon.MoveScalarToSide(context, rn, false);
                using ScopedRegister rmReg = InstEmitNeonCommon.MoveScalarToSide(context, rm, false);

                using ScopedRegister tempRegister = InstEmitNeonCommon.PickSimdRegister(context.RegisterAllocator, rnReg, rmReg);

                context.Arm64Assembler.Ext(tempRegister.Operand, rnReg.Operand, imm4, rmReg.Operand, q);

                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rd, false);
            }
            else
            {
                Debug.Assert(((rd | rn | rm) & 1) == 0);

                Operand rdOperand = context.RegisterAllocator.RemapSimdRegister((int)(rd >> 1));
                Operand rnOperand = context.RegisterAllocator.RemapSimdRegister((int)(rn >> 1));
                Operand rmOperand = context.RegisterAllocator.RemapSimdRegister((int)(rm >> 1));

                context.Arm64Assembler.Ext(rdOperand, rnOperand, imm4, rmOperand, q);
            }
        }

        public static void Vmovl(CodeGenContext context, uint rd, uint rm, bool u, uint imm3h)
        {
            uint size = (uint)BitOperations.TrailingZeroCount(imm3h);
            Debug.Assert(size < 3);

            InstEmitNeonCommon.EmitVectorBinaryLongShift(
                context,
                rd,
                rm,
                0,
                size,
                isShl: true,
                u ? context.Arm64Assembler.Ushll : context.Arm64Assembler.Sshll);
        }

        public static void Vmovn(CodeGenContext context, uint rd, uint rm, uint size)
        {
            Debug.Assert(size < 3);

            InstEmitNeonCommon.EmitVectorUnaryNarrow(context, rd, rm, size, context.Arm64Assembler.Xtn);
        }

        public static void Vmovx(CodeGenContext context, uint rd, uint rm)
        {
            InstEmitNeonCommon.EmitScalarBinaryShift(context, rd, rm, 16, 2, isShl: false, context.Arm64Assembler.UshrS);
        }

        public static void VmovD(CodeGenContext context, uint rt, uint rt2, uint rm, bool op)
        {
            Operand rmReg = context.RegisterAllocator.RemapSimdRegister((int)(rm >> 1));

            uint top = rm & 1;
            uint ftype = top + 1;

            if (op)
            {
                Operand rtOperand = InstEmitCommon.GetOutputGpr(context, rt);
                Operand rt2Operand = InstEmitCommon.GetOutputGpr(context, rt2);

                Operand rtOperand64 = new(OperandKind.Register, OperandType.I64, rtOperand.Value);
                Operand rt2Operand64 = new(OperandKind.Register, OperandType.I64, rt2Operand.Value);

                context.Arm64Assembler.FmovFloatGen(rtOperand64, rmReg, ftype, 1, 0, top);

                context.Arm64Assembler.Lsr(rt2Operand64, rtOperand64, InstEmitCommon.Const(32));
                context.Arm64Assembler.Mov(rtOperand, rtOperand); // Zero-extend.
            }
            else
            {
                Operand rtOperand = InstEmitCommon.GetInputGpr(context, rt);
                Operand rt2Operand = InstEmitCommon.GetInputGpr(context, rt2);

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

                Operand tempRegister64 = new(OperandKind.Register, OperandType.I64, tempRegister.Operand.Value);

                context.Arm64Assembler.Lsl(tempRegister64, rt2Operand, InstEmitCommon.Const(32));
                context.Arm64Assembler.Orr(tempRegister64, tempRegister64, rtOperand);

                if (top == 0)
                {
                    // Doing FMOV on Rm directly would clear the high bits if we are moving to the bottom.

                    using ScopedRegister tempRegister2 = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                    context.Arm64Assembler.FmovFloatGen(tempRegister2.Operand, tempRegister64, ftype, 1, 1, top);

                    InstEmitNeonCommon.InsertResult(context, tempRegister2.Operand, rm, false);
                }
                else
                {
                    context.Arm64Assembler.FmovFloatGen(rmReg, tempRegister64, ftype, 1, 1, top);
                }
            }
        }

        public static void VmovH(CodeGenContext context, uint rt, uint rn, bool op)
        {
            if (op)
            {
                Operand rtOperand = InstEmitCommon.GetOutputGpr(context, rt);

                using ScopedRegister tempRegister = InstEmitNeonCommon.MoveScalarToSide(context, rn, true);

                context.Arm64Assembler.FmovFloatGen(rtOperand, tempRegister.Operand, 3, 0, 0, 0);
            }
            else
            {
                Operand rtOperand = InstEmitCommon.GetInputGpr(context, rt);

                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                context.Arm64Assembler.FmovFloatGen(tempRegister.Operand, rtOperand, 3, 0, 1, 0);

                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rn, true);
            }
        }

        public static void VmovI(CodeGenContext context, uint rd, uint op, uint cmode, uint imm8, uint q)
        {
            (uint a, uint b, uint c, uint d, uint e, uint f, uint g, uint h) = Split(imm8);

            if (q == 0)
            {
                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                context.Arm64Assembler.Movi(tempRegister.Operand, h, g, f, e, d, cmode, c, b, a, op, q);

                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rd, false);
            }
            else
            {
                Operand rdOperand = context.RegisterAllocator.RemapSimdRegister((int)(rd >> 1));

                context.Arm64Assembler.Movi(rdOperand, h, g, f, e, d, cmode, c, b, a, op, q);
            }
        }

        public static void VmovFI(CodeGenContext context, uint rd, uint imm8, uint size)
        {
            using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

            context.Arm64Assembler.FmovFloatImm(tempRegister.Operand, imm8, size ^ 2u);

            InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rd, size != 3);
        }

        public static void VmovR(CodeGenContext context, uint rd, uint rm, uint size)
        {
            bool singleRegister = size == 2;

            int shift = singleRegister ? 2 : 1;
            uint mask = singleRegister ? 3u : 1u;
            uint dstElt = rd & mask;
            uint srcElt = rm & mask;

            uint imm4 = srcElt << (singleRegister ? 2 : 3);
            uint imm5 = InstEmitNeonCommon.GetImm5ForElementIndex(dstElt, singleRegister);

            Operand rdOperand = context.RegisterAllocator.RemapSimdRegister((int)(rd >> shift));
            Operand rmOperand = context.RegisterAllocator.RemapSimdRegister((int)(rm >> shift));

            context.Arm64Assembler.InsElt(rdOperand, rmOperand, imm4, imm5);
        }

        public static void VmovRs(CodeGenContext context, uint rd, uint rt, uint opc1, uint opc2)
        {
            uint index;
            uint size;

            if ((opc1 & 2u) != 0)
            {
                index = opc2 | ((opc1 & 1u) << 2);
                size = 0;
            }
            else if ((opc2 & 1u) != 0)
            {
                index = (opc2 >> 1) | ((opc1 & 1u) << 1);
                size = 1;
            }
            else
            {
                Debug.Assert(opc1 == 0 || opc1 == 1);
                Debug.Assert(opc2 == 0);

                index = opc1 & 1u;
                size = 2;
            }

            index |= (rd & 1u) << (int)(3 - size);

            Operand rtOperand = InstEmitCommon.GetInputGpr(context, rt);

            Operand rdReg = context.RegisterAllocator.RemapSimdRegister((int)(rd >> 1));

            context.Arm64Assembler.InsGen(rdReg, rtOperand, InstEmitNeonCommon.GetImm5ForElementIndex(index, size));
        }

        public static void VmovS(CodeGenContext context, uint rt, uint rn, bool op)
        {
            if (op)
            {
                Operand rtOperand = InstEmitCommon.GetOutputGpr(context, rt);

                using ScopedRegister tempRegister = InstEmitNeonCommon.MoveScalarToSide(context, rn, true);

                context.Arm64Assembler.FmovFloatGen(rtOperand, tempRegister.Operand, 0, 0, 0, 0);
            }
            else
            {
                Operand rtOperand = InstEmitCommon.GetInputGpr(context, rt);

                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                context.Arm64Assembler.FmovFloatGen(tempRegister.Operand, rtOperand, 0, 0, 1, 0);

                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rn, true);
            }
        }

        public static void VmovSr(CodeGenContext context, uint rt, uint rn, bool u, uint opc1, uint opc2)
        {
            uint index;
            uint size;

            if ((opc1 & 2u) != 0)
            {
                index = opc2 | ((opc1 & 1u) << 2);
                size = 0;
            }
            else if ((opc2 & 1u) != 0)
            {
                index = (opc2 >> 1) | ((opc1 & 1u) << 1);
                size = 1;
            }
            else
            {
                Debug.Assert(opc1 == 0 || opc1 == 1);
                Debug.Assert(opc2 == 0);
                Debug.Assert(!u);

                index = opc1 & 1u;
                size = 2;
            }

            index |= (rn & 1u) << (int)(3 - size);

            Operand rtOperand = InstEmitCommon.GetOutputGpr(context, rt);

            Operand rnReg = context.RegisterAllocator.RemapSimdRegister((int)(rn >> 1));

            if (u || size > 1)
            {
                context.Arm64Assembler.Umov(rtOperand, rnReg, (int)index, (int)size);
            }
            else
            {
                context.Arm64Assembler.Smov(rtOperand, rnReg, (int)index, (int)size);
            }
        }

        public static void VmovSs(CodeGenContext context, uint rt, uint rt2, uint rm, bool op)
        {
            if ((rm & 1) == 0)
            {
                // If we are moving an aligned pair of single-precision registers,
                // we can just move a single double-precision register.

                VmovD(context, rt, rt2, rm >> 1, op);

                return;
            }

            if (op)
            {
                Operand rtOperand = InstEmitCommon.GetOutputGpr(context, rt);
                Operand rt2Operand = InstEmitCommon.GetOutputGpr(context, rt2);

                using ScopedRegister rmReg = InstEmitNeonCommon.MoveScalarToSide(context, rm, true);
                using ScopedRegister rmReg2 = InstEmitNeonCommon.MoveScalarToSide(context, rm + 1, true);

                context.Arm64Assembler.FmovFloatGen(rtOperand, rmReg.Operand, 0, 0, 0, 0);
                context.Arm64Assembler.FmovFloatGen(rt2Operand, rmReg2.Operand, 0, 0, 0, 0);
            }
            else
            {
                Operand rtOperand = InstEmitCommon.GetInputGpr(context, rt);
                Operand rt2Operand = InstEmitCommon.GetInputGpr(context, rt2);

                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                context.Arm64Assembler.FmovFloatGen(tempRegister.Operand, rtOperand, 0, 0, 1, 0);
                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rm, true);

                context.Arm64Assembler.FmovFloatGen(tempRegister.Operand, rt2Operand, 0, 0, 1, 0);
                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rm + 1, true);
            }
        }

        public static void VmvnI(CodeGenContext context, uint rd, uint cmode, uint imm8, uint q)
        {
            (uint a, uint b, uint c, uint d, uint e, uint f, uint g, uint h) = Split(imm8);

            if (q == 0)
            {
                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                context.Arm64Assembler.Mvni(tempRegister.Operand, h, g, f, e, d, cmode, c, b, a, q);

                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rd, false);
            }
            else
            {
                Operand rdOperand = context.RegisterAllocator.RemapSimdRegister((int)(rd >> 1));

                context.Arm64Assembler.Mvni(rdOperand, h, g, f, e, d, cmode, c, b, a, q);
            }
        }

        public static void VmvnR(CodeGenContext context, uint rd, uint rm, uint size, uint q)
        {
            InstEmitNeonCommon.EmitVectorUnary(context, rd, rm, q, context.Arm64Assembler.Not);
        }

        public static void Vswp(CodeGenContext context, uint rd, uint rm, uint q)
        {
            using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

            if (q == 0)
            {
                InstEmitNeonCommon.MoveScalarToSide(context, tempRegister.Operand, rd, false);
                using ScopedRegister rmReg = InstEmitNeonCommon.MoveScalarToSide(context, rm, false);

                InstEmitNeonCommon.InsertResult(context, rmReg.Operand, rd, false);
                InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rm, false);
            }
            else
            {
                Operand rdOperand = context.RegisterAllocator.RemapSimdRegister((int)(rd >> 1));
                Operand rmOperand = context.RegisterAllocator.RemapSimdRegister((int)(rm >> 1));

                context.Arm64Assembler.Orr(tempRegister.Operand, rdOperand, rdOperand); // Temp = Rd
                context.Arm64Assembler.Orr(rdOperand, rmOperand, rmOperand); // Rd = Rm
                context.Arm64Assembler.Orr(rmOperand, tempRegister.Operand, tempRegister.Operand); // Rm = Temp
            }
        }

        public static void Vtbl(CodeGenContext context, uint rd, uint rn, uint rm, bool op, uint len)
        {
            // On AArch64, TBL/TBX works with 128-bit vectors, while on AArch32 it works with 64-bit vectors.
            // We must combine the 64-bit vectors into a larger 128-bit one in some cases.

            // TODO: Peephole optimization to combine adjacent TBL instructions?

            Debug.Assert(len <= 3);

            bool isTbl = !op;

            len = Math.Min(len, 31 - rn);

            bool rangeMismatch = !isTbl && (len & 1) == 0;

            using ScopedRegister indicesReg = InstEmitNeonCommon.MoveScalarToSide(context, rm, false, rangeMismatch);

            if (rangeMismatch)
            {
                // Force any index >= 8 * regs to be the maximum value, since on AArch64 we are working with a full vector,
                // and the out of range value is 16 * regs, not 8 * regs.

                Debug.Assert(indicesReg.IsAllocated);

                using ScopedRegister tempRegister2 = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                if (len == 0)
                {
                    (uint immb, uint immh) = InstEmitNeonCommon.GetImmbImmhForShift(3, 0, isShl: false);

                    context.Arm64Assembler.UshrV(tempRegister2.Operand, indicesReg.Operand, immb, immh, 0);
                    context.Arm64Assembler.CmeqZeroV(tempRegister2.Operand, tempRegister2.Operand, 0, 0);
                    context.Arm64Assembler.Orn(indicesReg.Operand, indicesReg.Operand, tempRegister2.Operand, 0);
                }
                else
                {
                    (uint a, uint b, uint c, uint d, uint e, uint f, uint g, uint h) = Split(8u * (len + 1));

                    context.Arm64Assembler.Movi(tempRegister2.Operand, h, g, f, e, d, 0xe, c, b, a, 0, 0);
                    context.Arm64Assembler.CmgeRegV(tempRegister2.Operand, indicesReg.Operand, tempRegister2.Operand, 0, 0);
                    context.Arm64Assembler.OrrReg(indicesReg.Operand, indicesReg.Operand, tempRegister2.Operand, 0);
                }
            }

            ScopedRegister tableReg1 = default;
            ScopedRegister tableReg2 = default;

            switch (len)
            {
                case 0:
                    tableReg1 = MoveHalfToSideZeroUpper(context, rn);
                    break;
                case 1:
                    tableReg1 = MoveDoublewords(context, rn, rn + 1);
                    break;
                case 2:
                    tableReg1 = MoveDoublewords(context, rn, rn + 1, isOdd: true);
                    tableReg2 = MoveHalfToSideZeroUpper(context, rn + 2);
                    break;
                case 3:
                    tableReg1 = MoveDoublewords(context, rn, rn + 1);
                    tableReg2 = MoveDoublewords(context, rn + 2, rn + 3);
                    break;
            }

            // TBL works with consecutive registers, it is assumed that two consecutive calls to the register allocator
            // will return consecutive registers.

            Debug.Assert(len < 2 || tableReg1.Operand.GetRegister().Index + 1 == tableReg2.Operand.GetRegister().Index);

            using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

            if (isTbl)
            {
                context.Arm64Assembler.Tbl(tempRegister.Operand, tableReg1.Operand, len >> 1, indicesReg.Operand, 0);
            }
            else
            {
                InstEmitNeonCommon.MoveScalarToSide(context, tempRegister.Operand, rd, false);

                context.Arm64Assembler.Tbx(tempRegister.Operand, tableReg1.Operand, len >> 1, indicesReg.Operand, 0);
            }

            InstEmitNeonCommon.InsertResult(context, tempRegister.Operand, rd, false);

            tableReg1.Dispose();

            if (len > 1)
            {
                tableReg2.Dispose();
            }
        }

        public static void Vtrn(CodeGenContext context, uint rd, uint rm, uint size, uint q)
        {
            EmitVectorBinaryInterleavedTrn(context, rd, rm, size, q, context.Arm64Assembler.Trn1, context.Arm64Assembler.Trn2);
        }

        public static void Vuzp(CodeGenContext context, uint rd, uint rm, uint size, uint q)
        {
            EmitVectorBinaryInterleaved(context, rd, rm, size, q, context.Arm64Assembler.Uzp1, context.Arm64Assembler.Uzp2);
        }

        public static void Vzip(CodeGenContext context, uint rd, uint rm, uint size, uint q)
        {
            EmitVectorBinaryInterleaved(context, rd, rm, size, q, context.Arm64Assembler.Zip1, context.Arm64Assembler.Zip2);
        }

        public static (uint, uint, uint, uint, uint, uint, uint, uint) Split(uint imm8)
        {
            uint a = (imm8 >> 7) & 1;
            uint b = (imm8 >> 6) & 1;
            uint c = (imm8 >> 5) & 1;
            uint d = (imm8 >> 4) & 1;
            uint e = (imm8 >> 3) & 1;
            uint f = (imm8 >> 2) & 1;
            uint g = (imm8 >> 1) & 1;
            uint h = imm8 & 1;

            return (a, b, c, d, e, f, g, h);
        }

        private static ScopedRegister MoveHalfToSideZeroUpper(CodeGenContext context, uint srcReg)
        {
            uint elt = srcReg & 1u;

            Operand source = context.RegisterAllocator.RemapSimdRegister((int)(srcReg >> 1));
            ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempFpRegisterScoped(false);

            uint imm5 = InstEmitNeonCommon.GetImm5ForElementIndex(elt, false);

            context.Arm64Assembler.DupEltScalarFromElement(tempRegister.Operand, source, imm5);

            return tempRegister;
        }

        private static ScopedRegister MoveDoublewords(CodeGenContext context, uint lowerReg, uint upperReg, bool isOdd = false)
        {
            if ((lowerReg & 1) == 0 && upperReg == lowerReg + 1 && !isOdd)
            {
                return new ScopedRegister(context.RegisterAllocator, context.RegisterAllocator.RemapSimdRegister((int)(lowerReg >> 1)), false);
            }

            Operand lowerSrc = context.RegisterAllocator.RemapSimdRegister((int)(lowerReg >> 1));
            Operand upperSrc = context.RegisterAllocator.RemapSimdRegister((int)(upperReg >> 1));
            ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempFpRegisterScoped(false);

            uint imm5 = InstEmitNeonCommon.GetImm5ForElementIndex(lowerReg & 1u, false);

            context.Arm64Assembler.DupEltScalarFromElement(tempRegister.Operand, lowerSrc, imm5);

            imm5 = InstEmitNeonCommon.GetImm5ForElementIndex(1, false);

            context.Arm64Assembler.InsElt(tempRegister.Operand, upperSrc, (upperReg & 1u) << 3, imm5);

            return tempRegister;
        }

        private static void EmitVectorBinaryInterleavedTrn(
            CodeGenContext context,
            uint rd,
            uint rm,
            uint size,
            uint q,
            Action<Operand, Operand, Operand, uint, uint> action1,
            Action<Operand, Operand, Operand, uint, uint> action2)
        {
            if (rd == rm)
            {
                // The behaviour when the registers are the same is "unpredictable" according to the manual.

                if (q == 0)
                {
                    using ScopedRegister rdReg = InstEmitNeonCommon.MoveScalarToSide(context, rd, false);
                    using ScopedRegister rmReg = InstEmitNeonCommon.MoveScalarToSide(context, rm, false);

                    using ScopedRegister tempRegister1 = context.RegisterAllocator.AllocateTempSimdRegisterScoped();
                    using ScopedRegister tempRegister2 = InstEmitNeonCommon.PickSimdRegister(context.RegisterAllocator, rdReg, rmReg);

                    action1(tempRegister1.Operand, rdReg.Operand, rmReg.Operand, size, q);
                    action2(tempRegister2.Operand, rdReg.Operand, tempRegister1.Operand, size, q);

                    InstEmitNeonCommon.InsertResult(context, tempRegister2.Operand, rd, false);
                }
                else
                {
                    Operand rdOperand = context.RegisterAllocator.RemapSimdRegister((int)(rd >> 1));
                    Operand rmOperand = context.RegisterAllocator.RemapSimdRegister((int)(rm >> 1));

                    using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                    action1(tempRegister.Operand, rdOperand, rmOperand, size, q);
                    action2(rmOperand, rdOperand, tempRegister.Operand, size, q);
                }
            }
            else
            {
                EmitVectorBinaryInterleaved(context, rd, rm, size, q, action1, action2);
            }
        }

        private static void EmitVectorBinaryInterleaved(
            CodeGenContext context,
            uint rd,
            uint rm,
            uint size,
            uint q,
            Action<Operand, Operand, Operand, uint, uint> action1,
            Action<Operand, Operand, Operand, uint, uint> action2)
        {
            if (q == 0)
            {
                using ScopedRegister rdReg = InstEmitNeonCommon.MoveScalarToSide(context, rd, false);
                using ScopedRegister rmReg = InstEmitNeonCommon.MoveScalarToSide(context, rm, false);

                using ScopedRegister tempRegister1 = context.RegisterAllocator.AllocateTempSimdRegisterScoped();
                using ScopedRegister tempRegister2 = InstEmitNeonCommon.PickSimdRegister(context.RegisterAllocator, rdReg, rmReg);

                action1(tempRegister1.Operand, rdReg.Operand, rmReg.Operand, size, q);
                action2(tempRegister2.Operand, rdReg.Operand, rmReg.Operand, size, q);

                if (rd != rm)
                {
                    InstEmitNeonCommon.InsertResult(context, tempRegister1.Operand, rd, false);
                }

                InstEmitNeonCommon.InsertResult(context, tempRegister2.Operand, rm, false);
            }
            else
            {
                Operand rdOperand = context.RegisterAllocator.RemapSimdRegister((int)(rd >> 1));
                Operand rmOperand = context.RegisterAllocator.RemapSimdRegister((int)(rm >> 1));

                using ScopedRegister tempRegister = context.RegisterAllocator.AllocateTempSimdRegisterScoped();

                action1(tempRegister.Operand, rdOperand, rmOperand, size, q);
                action2(rmOperand, rdOperand, rmOperand, size, q);

                if (rd != rm)
                {
                    context.Arm64Assembler.OrrReg(rdOperand, tempRegister.Operand, tempRegister.Operand, 1);
                }
            }
        }
    }
}