aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Shader/Instructions/InstEmitIntegerArithmetic.cs
blob: 99922f7a1f684568ea8b18de8ff5a1aca8619037 (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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation;

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

namespace Ryujinx.Graphics.Shader.Instructions
{
    static partial class InstEmit
    {
        public static void IaddR(EmitterContext context)
        {
            InstIaddR op = context.GetOp<InstIaddR>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcB);

            EmitIadd(context, srcA, srcB, op.Dest, op.AvgMode, op.X, op.WriteCC);
        }

        public static void IaddI(EmitterContext context)
        {
            InstIaddI op = context.GetOp<InstIaddI>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));

            EmitIadd(context, srcA, srcB, op.Dest, op.AvgMode, op.X, op.WriteCC);
        }

        public static void IaddC(EmitterContext context)
        {
            InstIaddC op = context.GetOp<InstIaddC>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);

            EmitIadd(context, srcA, srcB, op.Dest, op.AvgMode, op.X, op.WriteCC);
        }

        public static void Iadd32i(EmitterContext context)
        {
            InstIadd32i op = context.GetOp<InstIadd32i>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, op.Imm32);

            EmitIadd(context, srcA, srcB, op.Dest, op.AvgMode, op.X, op.WriteCC);
        }

        public static void Iadd3R(EmitterContext context)
        {
            InstIadd3R op = context.GetOp<InstIadd3R>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcB);
            var srcC = GetSrcReg(context, op.SrcC);

            EmitIadd3(context, op.Lrs, srcA, srcB, srcC, op.Apart, op.Bpart, op.Cpart, op.Dest, op.NegA, op.NegB, op.NegC);
        }

        public static void Iadd3I(EmitterContext context)
        {
            InstIadd3I op = context.GetOp<InstIadd3I>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
            var srcC = GetSrcReg(context, op.SrcC);

            EmitIadd3(context, Lrs.None, srcA, srcB, srcC, HalfSelect.B32, HalfSelect.B32, HalfSelect.B32, op.Dest, op.NegA, op.NegB, op.NegC);
        }

        public static void Iadd3C(EmitterContext context)
        {
            InstIadd3C op = context.GetOp<InstIadd3C>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
            var srcC = GetSrcReg(context, op.SrcC);

            EmitIadd3(context, Lrs.None, srcA, srcB, srcC, HalfSelect.B32, HalfSelect.B32, HalfSelect.B32, op.Dest, op.NegA, op.NegB, op.NegC);
        }

        public static void ImadR(EmitterContext context)
        {
            InstImadR op = context.GetOp<InstImadR>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcB);
            var srcC = GetSrcReg(context, op.SrcC);

            EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
        }

        public static void ImadI(EmitterContext context)
        {
            InstImadI op = context.GetOp<InstImadI>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
            var srcC = GetSrcReg(context, op.SrcC);

            EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
        }

        public static void ImadC(EmitterContext context)
        {
            InstImadC op = context.GetOp<InstImadC>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
            var srcC = GetSrcReg(context, op.SrcC);

            EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
        }

        public static void ImadRc(EmitterContext context)
        {
            InstImadRc op = context.GetOp<InstImadRc>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcC);
            var srcC = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);

            EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
        }

        public static void Imad32i(EmitterContext context)
        {
            InstImad32i op = context.GetOp<InstImad32i>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, op.Imm32);
            var srcC = GetSrcReg(context, op.Dest);

            EmitImad(context, srcA, srcB, srcC, op.Dest, op.AvgMode, op.ASigned, op.BSigned, op.Hilo);
        }

        public static void ImulR(EmitterContext context)
        {
            InstImulR op = context.GetOp<InstImulR>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcB);

            EmitImad(context, srcA, srcB, Const(0), op.Dest, AvgMode.NoNeg, op.ASigned, op.BSigned, op.Hilo);
        }

        public static void ImulI(EmitterContext context)
        {
            InstImulI op = context.GetOp<InstImulI>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));

            EmitImad(context, srcA, srcB, Const(0), op.Dest, AvgMode.NoNeg, op.ASigned, op.BSigned, op.Hilo);
        }

        public static void ImulC(EmitterContext context)
        {
            InstImulC op = context.GetOp<InstImulC>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);

            EmitImad(context, srcA, srcB, Const(0), op.Dest, AvgMode.NoNeg, op.ASigned, op.BSigned, op.Hilo);
        }

        public static void Imul32i(EmitterContext context)
        {
            InstImul32i op = context.GetOp<InstImul32i>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, op.Imm32);

            EmitImad(context, srcA, srcB, Const(0), op.Dest, AvgMode.NoNeg, op.ASigned, op.BSigned, op.Hilo);
        }

        public static void IscaddR(EmitterContext context)
        {
            InstIscaddR op = context.GetOp<InstIscaddR>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcB);

            EmitIscadd(context, srcA, srcB, op.Dest, op.Imm5, op.AvgMode, op.WriteCC);
        }

        public static void IscaddI(EmitterContext context)
        {
            InstIscaddI op = context.GetOp<InstIscaddI>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));

            EmitIscadd(context, srcA, srcB, op.Dest, op.Imm5, op.AvgMode, op.WriteCC);
        }

        public static void IscaddC(EmitterContext context)
        {
            InstIscaddC op = context.GetOp<InstIscaddC>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);

            EmitIscadd(context, srcA, srcB, op.Dest, op.Imm5, op.AvgMode, op.WriteCC);
        }

        public static void Iscadd32i(EmitterContext context)
        {
            InstIscadd32i op = context.GetOp<InstIscadd32i>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, op.Imm32);

            EmitIscadd(context, srcA, srcB, op.Dest, op.Imm5, AvgMode.NoNeg, op.WriteCC);
        }

        public static void LeaR(EmitterContext context)
        {
            InstLeaR op = context.GetOp<InstLeaR>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcB);

            EmitLea(context, srcA, srcB, op.Dest, op.NegA, op.ImmU5);
        }

        public static void LeaI(EmitterContext context)
        {
            InstLeaI op = context.GetOp<InstLeaI>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));

            EmitLea(context, srcA, srcB, op.Dest, op.NegA, op.ImmU5);
        }

        public static void LeaC(EmitterContext context)
        {
            InstLeaC op = context.GetOp<InstLeaC>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);

            EmitLea(context, srcA, srcB, op.Dest, op.NegA, op.ImmU5);
        }

        public static void LeaHiR(EmitterContext context)
        {
            InstLeaHiR op = context.GetOp<InstLeaHiR>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcB);
            var srcC = GetSrcReg(context, op.SrcC);

            EmitLeaHi(context, srcA, srcB, srcC, op.Dest, op.NegA, op.ImmU5);
        }

        public static void LeaHiC(EmitterContext context)
        {
            InstLeaHiC op = context.GetOp<InstLeaHiC>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
            var srcC = GetSrcReg(context, op.SrcC);

            EmitLeaHi(context, srcA, srcB, srcC, op.Dest, op.NegA, op.ImmU5);
        }

        public static void XmadR(EmitterContext context)
        {
            InstXmadR op = context.GetOp<InstXmadR>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcB);
            var srcC = GetSrcReg(context, op.SrcC);

            EmitXmad(context, op.XmadCop, srcA, srcB, srcC, op.Dest, op.ASigned, op.BSigned, op.HiloA, op.HiloB, op.Psl, op.Mrg, op.X, op.WriteCC);
        }

        public static void XmadI(EmitterContext context)
        {
            InstXmadI op = context.GetOp<InstXmadI>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcImm(context, op.Imm16);
            var srcC = GetSrcReg(context, op.SrcC);

            EmitXmad(context, op.XmadCop, srcA, srcB, srcC, op.Dest, op.ASigned, op.BSigned, op.HiloA, false, op.Psl, op.Mrg, op.X, op.WriteCC);
        }

        public static void XmadC(EmitterContext context)
        {
            InstXmadC op = context.GetOp<InstXmadC>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
            var srcC = GetSrcReg(context, op.SrcC);

            EmitXmad(context, op.XmadCop, srcA, srcB, srcC, op.Dest, op.ASigned, op.BSigned, op.HiloA, op.HiloB, op.Psl, op.Mrg, op.X, op.WriteCC);
        }

        public static void XmadRc(EmitterContext context)
        {
            InstXmadRc op = context.GetOp<InstXmadRc>();

            var srcA = GetSrcReg(context, op.SrcA);
            var srcB = GetSrcReg(context, op.SrcC);
            var srcC = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);

            EmitXmad(context, op.XmadCop, srcA, srcB, srcC, op.Dest, op.ASigned, op.BSigned, op.HiloA, op.HiloB, false, false, op.X, op.WriteCC);
        }

        private static void EmitIadd(
            EmitterContext context,
            Operand srcA,
            Operand srcB,
            int rd,
            AvgMode avgMode,
            bool extended,
            bool writeCC)
        {
            srcA = context.INegate(srcA, avgMode == AvgMode.NegA);
            srcB = context.INegate(srcB, avgMode == AvgMode.NegB);

            Operand res = context.IAdd(srcA, srcB);

            if (extended)
            {
                res = context.IAdd(res, context.BitwiseAnd(GetCF(), Const(1)));
            }

            SetIaddFlags(context, res, srcA, srcB, writeCC, extended);

            // TODO: SAT.

            context.Copy(GetDest(rd), res);
        }

        private static void EmitIadd3(
            EmitterContext context,
            Lrs mode,
            Operand srcA,
            Operand srcB,
            Operand srcC,
            HalfSelect partA,
            HalfSelect partB,
            HalfSelect partC,
            int rd,
            bool negateA,
            bool negateB,
            bool negateC)
        {
            Operand Extend(Operand src, HalfSelect part)
            {
                if (part == HalfSelect.B32)
                {
                    return src;
                }

                if (part == HalfSelect.H0)
                {
                    return context.BitwiseAnd(src, Const(0xffff));
                }
                else if (part == HalfSelect.H1)
                {
                    return context.ShiftRightU32(src, Const(16));
                }
                else
                {
                    context.TranslatorContext.GpuAccessor.Log($"Iadd3 has invalid component selection {part}.");
                }

                return src;
            }

            srcA = context.INegate(Extend(srcA, partA), negateA);
            srcB = context.INegate(Extend(srcB, partB), negateB);
            srcC = context.INegate(Extend(srcC, partC), negateC);

            Operand res = context.IAdd(srcA, srcB);

            if (mode != Lrs.None)
            {
                if (mode == Lrs.LeftShift)
                {
                    res = context.ShiftLeft(res, Const(16));
                }
                else if (mode == Lrs.RightShift)
                {
                    res = context.ShiftRightU32(res, Const(16));
                }
                else
                {
                    // TODO: Warning.
                }
            }

            res = context.IAdd(res, srcC);

            context.Copy(GetDest(rd), res);

            // TODO: CC, X, corner cases.
        }

        private static void EmitImad(
            EmitterContext context,
            Operand srcA,
            Operand srcB,
            Operand srcC,
            int rd,
            AvgMode avgMode,
            bool signedA,
            bool signedB,
            bool high)
        {
            srcB = context.INegate(srcB, avgMode == AvgMode.NegA);
            srcC = context.INegate(srcC, avgMode == AvgMode.NegB);

            Operand res;

            if (high)
            {
                if (signedA && signedB)
                {
                    res = context.MultiplyHighS32(srcA, srcB);
                }
                else
                {
                    res = context.MultiplyHighU32(srcA, srcB);

                    if (signedA)
                    {
                        res = context.IAdd(res, context.IMultiply(srcB, context.ShiftRightS32(srcA, Const(31))));
                    }
                    else if (signedB)
                    {
                        res = context.IAdd(res, context.IMultiply(srcA, context.ShiftRightS32(srcB, Const(31))));
                    }
                }
            }
            else
            {
                res = context.IMultiply(srcA, srcB);
            }

            if (srcC.Type != OperandType.Constant || srcC.Value != 0)
            {
                res = context.IAdd(res, srcC);
            }

            // TODO: CC, X, SAT, and more?

            context.Copy(GetDest(rd), res);
        }

        private static void EmitIscadd(
            EmitterContext context,
            Operand srcA,
            Operand srcB,
            int rd,
            int shift,
            AvgMode avgMode,
            bool writeCC)
        {
            srcA = context.ShiftLeft(srcA, Const(shift));

            srcA = context.INegate(srcA, avgMode == AvgMode.NegA);
            srcB = context.INegate(srcB, avgMode == AvgMode.NegB);

            Operand res = context.IAdd(srcA, srcB);

            SetIaddFlags(context, res, srcA, srcB, writeCC, false);

            context.Copy(GetDest(rd), res);
        }

        public static void EmitLea(EmitterContext context, Operand srcA, Operand srcB, int rd, bool negateA, int shift)
        {
            srcA = context.ShiftLeft(srcA, Const(shift));
            srcA = context.INegate(srcA, negateA);

            Operand res = context.IAdd(srcA, srcB);

            context.Copy(GetDest(rd), res);

            // TODO: CC, X.
        }

        private static void EmitLeaHi(
            EmitterContext context,
            Operand srcA,
            Operand srcB,
            Operand srcC,
            int rd,
            bool negateA,
            int shift)
        {
            Operand aLow = context.ShiftLeft(srcA, Const(shift));
            Operand aHigh = shift == 0 ? Const(0) : context.ShiftRightU32(srcA, Const(32 - shift));
            aHigh = context.BitwiseOr(aHigh, context.ShiftLeft(srcC, Const(shift)));

            if (negateA)
            {
                // Perform 64-bit negation by doing bitwise not of the value,
                // then adding 1 and carrying over from low to high.
                aLow = context.BitwiseNot(aLow);
                aHigh = context.BitwiseNot(aHigh);

#pragma warning disable IDE0059 // Remove unnecessary value assignment
                aLow = AddWithCarry(context, aLow, Const(1), out Operand aLowCOut);
#pragma warning restore IDE0059
                aHigh = context.IAdd(aHigh, aLowCOut);
            }

            Operand res = context.IAdd(aHigh, srcB);

            context.Copy(GetDest(rd), res);

            // TODO: CC, X.
        }

        public static void EmitXmad(
            EmitterContext context,
            XmadCop2 mode,
            Operand srcA,
            Operand srcB,
            Operand srcC,
            int rd,
            bool signedA,
            bool signedB,
            bool highA,
            bool highB,
            bool productShiftLeft,
            bool merge,
            bool extended,
            bool writeCC)
        {
            XmadCop modeConv;
            switch (mode)
            {
                case XmadCop2.Cfull:
                    modeConv = XmadCop.Cfull;
                    break;
                case XmadCop2.Clo:
                    modeConv = XmadCop.Clo;
                    break;
                case XmadCop2.Chi:
                    modeConv = XmadCop.Chi;
                    break;
                case XmadCop2.Csfu:
                    modeConv = XmadCop.Csfu;
                    break;
                default:
                    context.TranslatorContext.GpuAccessor.Log($"Invalid XMAD mode \"{mode}\".");
                    return;
            }

            EmitXmad(context, modeConv, srcA, srcB, srcC, rd, signedA, signedB, highA, highB, productShiftLeft, merge, extended, writeCC);
        }

        public static void EmitXmad(
            EmitterContext context,
            XmadCop mode,
            Operand srcA,
            Operand srcB,
            Operand srcC,
            int rd,
            bool signedA,
            bool signedB,
            bool highA,
            bool highB,
            bool productShiftLeft,
            bool merge,
            bool extended,
            bool writeCC)
        {
            var srcBUnmodified = srcB;

            Operand Extend16To32(Operand src, bool high, bool signed)
            {
                if (signed && high)
                {
                    return context.ShiftRightS32(src, Const(16));
                }
                else if (signed)
                {
                    return context.BitfieldExtractS32(src, Const(0), Const(16));
                }
                else if (high)
                {
                    return context.ShiftRightU32(src, Const(16));
                }
                else
                {
                    return context.BitwiseAnd(src, Const(0xffff));
                }
            }

            srcA = Extend16To32(srcA, highA, signedA);
            srcB = Extend16To32(srcB, highB, signedB);

            Operand res = context.IMultiply(srcA, srcB);

            if (productShiftLeft)
            {
                res = context.ShiftLeft(res, Const(16));
            }

            switch (mode)
            {
                case XmadCop.Cfull:
                    break;

                case XmadCop.Clo:
                    srcC = Extend16To32(srcC, high: false, signed: false);
                    break;
                case XmadCop.Chi:
                    srcC = Extend16To32(srcC, high: true, signed: false);
                    break;

                case XmadCop.Cbcc:
                    srcC = context.IAdd(srcC, context.ShiftLeft(srcBUnmodified, Const(16)));
                    break;

                case XmadCop.Csfu:
                    Operand signAdjustA = context.ShiftLeft(context.ShiftRightU32(srcA, Const(31)), Const(16));
                    Operand signAdjustB = context.ShiftLeft(context.ShiftRightU32(srcB, Const(31)), Const(16));

                    srcC = context.ISubtract(srcC, context.IAdd(signAdjustA, signAdjustB));
                    break;

                default:
                    context.TranslatorContext.GpuAccessor.Log($"Invalid XMAD mode \"{mode}\".");
                    return;
            }

            Operand product = res;

            if (extended)
            {
                // Add with carry.
                res = context.IAdd(res, context.BitwiseAnd(GetCF(), Const(1)));
            }
            else
            {
                // Add (no carry in).
                res = context.IAdd(res, srcC);
            }

            SetIaddFlags(context, res, product, srcC, writeCC, extended);

            if (merge)
            {
                res = context.BitwiseAnd(res, Const(0xffff));
                res = context.BitwiseOr(res, context.ShiftLeft(srcBUnmodified, Const(16)));
            }

            context.Copy(GetDest(rd), res);
        }

        private static void SetIaddFlags(EmitterContext context, Operand res, Operand srcA, Operand srcB, bool setCC, bool extended)
        {
            if (!setCC)
            {
                return;
            }

            if (extended)
            {
                // C = (d == a && CIn) || d < a
                Operand tempC0 = context.ICompareEqual(res, srcA);
                Operand tempC1 = context.ICompareLessUnsigned(res, srcA);

                tempC0 = context.BitwiseAnd(tempC0, GetCF());

                context.Copy(GetCF(), context.BitwiseOr(tempC0, tempC1));
            }
            else
            {
                // C = d < a
                context.Copy(GetCF(), context.ICompareLessUnsigned(res, srcA));
            }

            // V = (d ^ a) & ~(a ^ b) < 0
            Operand tempV0 = context.BitwiseExclusiveOr(res, srcA);
            Operand tempV1 = context.BitwiseExclusiveOr(srcA, srcB);

            tempV1 = context.BitwiseNot(tempV1);

            Operand tempV = context.BitwiseAnd(tempV0, tempV1);

            context.Copy(GetVF(), context.ICompareLess(tempV, Const(0)));

            SetZnFlags(context, res, setCC: true, extended: extended);
        }
    }
}