Repository navigation
Expand file tree
/
Copy pathlower_to_maxwell.h
More file actions
3510 lines (3499 loc) · 230 KB
/
Copy pathlower_to_maxwell.h
File metadata and controls
3510 lines (3499 loc) · 230 KB
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma GCC diagnostic ignored "-Wshadow"
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// Encoding reference: Mesa NAK sm70_encode.rs (MIT), Copyright 2022 Collabora, Ltd.
#pragma once
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bit>
#include <span>
#include <optional>
#include <string_view>
#include <vector>
#include "common/common_types.h"
namespace Shader::Ampere {
struct Instruction {
std::array<u32, 4> words;
u32 Bits(u32 offset, u32 count) const {
u64 bits = words[offset / 32];
if (offset / 32 + 1 < words.size()) {
bits |= u64{words[offset / 32 + 1]} << 32;
}
return static_cast<u32>((bits >> (offset % 32)) & ((u64{1} << count) - 1));
}
};
static_assert(sizeof(Instruction) == 16);
// (hookshot) set by the Vulkan back end when the GPU's cooperative matrices passed its self-test
// (vk_coopmat.cpp): HMMA.16816.F16 then lowers to the HMMA_nx2 pseudo instruction (one subgroup
// matrix multiply on the host's tensor cores) instead of the shuffle expansion.
inline thread_local bool native_matrix_multiply = false;
// (hookshot) Native programs whose GPRs reach the lowering's reserved registers (the uniform bank at R216-R245, R239's
// uniform predicates, the scratch R246-R254): OoT's big vertex and mesh programs use ~249 GPRs. Their high GPRs (from
// the reserved base up) live in IR registers 256 + n instead: each lowered word of a native instruction that names
// them is preceded by REGMAP_nx2 words (Maxwell 0xefe0, frontend/maxwell/translate/impl/register_map_nx2.cpp), and
// the translator reads those numbers in the next word as the extended registers. A high GPR in the scratch range
// (R246 up) or R239, which the lowering's own words name too, is renamed first: its field gets a marker, a number of
// the uniform bank the program never uses, and the prefix maps the marker to the register. Two runs a prefix: numbers
// at bits 0-7 and 20-27, targets at 8-15 and 28-35 (16-19 is the predicate, PT), lengths - 1 at 36-39, the run count
// at 40-41.
struct RegisterRun { u32 number, target, length; };
inline u64 RegisterMapPrefix(const RegisterRun* runs, u32 count) {
u64 w = 0xefe0000000000000ULL | (u64{7} << 16) | (u64{count} << 40);
constexpr u32 number_at[2]{0, 20}, target_at[2]{8, 28};
for (u32 i = 0; i < count; ++i) {
w |= u64{runs[i].number & 0xff} << number_at[i];
w |= u64{runs[i].target & 0xff} << target_at[i];
w |= u64{(runs[i].length - 1) & 3} << (36 + 2 * i);
}
return w;
}
// (hookshot) Set while a program that failed with the rules above is lowered again (LowerToMaxwell): more fields
// renamed and their exact widths. Only for those, so that every program that lowered before lowers to the same words.
inline thread_local bool extended_renaming = false;
// Fields that hold GPRs in the opcodes whose high GPRs may be renamed (ALU forms as operand() reads them, and the
// global loads and stores): 1 dest, 2 Ra, 4 Rb at 32, 8 at 64 (Rc, or Rb in forms 2, 3 and 7)
inline u32 RenameableFields(u32 opcode) {
const u32 family = opcode & 0x1ff, form = (opcode >> 9) & 7;
switch (opcode) {
case 0x381: case 0x981: return 1 | 2; // LDG Rd, [Ra] (0x381: Ra a register pair)
case 0x986: return 2 | 4; // STG [Ra], Rb
default: break;
}
if (extended_renaming) {
switch (opcode) {
case 0x805: return 1; // (hookshot) CS2R.64 Rd, SRZ: the pair it zeroes
case 0x308: return 1 | 4; // (hookshot) MUFU Rd, Rb
case 0x387: case 0x987: return 2 | 4; // (hookshot) STL [Ra..], Rb
case 0x983: return 1 | 2; // (hookshot) LDL Rd, [Ra..]
default: break;
}
// (hookshot) ISETP and FSETP: Ra, and Rb in the register form (their destinations are predicates)
if (family == 0x00c || family == 0x00b) return form >= 1 && form <= 6 ? 2 | (form == 1 ? 4 : 0) : 0;
}
const bool three = family == 0x010 || family == 0x011 || family == 0x012 || family == 0x016 || family == 0x019 ||
family == 0x023 || family == 0x024 || family == 0x025 || family == 0x027;
const bool two = family == 0x020 || family == 0x021 || family == 0x008 || family == 0x007 || family == 0x009 ||
family == 0x017;
// (hookshot: extended, form 6 too, a uniform register at 32 that is no GPR field: Rd, Ra and Rc are; OoT's mesh
// programs computed into R226 while UR10 lived there)
if ((!three && !two) || form < 1 || form > (extended_renaming ? 6U : 5U)) return 0;
u32 fields = 1 | 2;
if (form == 1) fields |= 4; // Rb a register
if (three || form == 2 || form == 3) fields |= 8; // Rc, or Rb at 64
return fields;
}
// (hookshot, extended) How many GPRs a field names, where the opcode tells: the 32-bit ALU forms one each, IMAD.WIDE's
// Rd and Rc pairs, CS2R.64's pair; 0 where it does not (a pair or quad by the register's alignment, as before). A
// 32-bit operand taken for a quad reached R239 or the scratch from R236 or R244 and failed whole mesh programs (OoT's
// foliage).
inline u32 FieldSpan(u32 opcode, u32 field) {
if (!extended_renaming) return 0;
const u32 family = opcode & 0x1ff, form = (opcode >> 9) & 7;
if (opcode == 0x805) return field == 0 ? 2 : 0;
if (opcode == 0x308) return 1;
if (opcode == 0x387 || opcode == 0x987 || opcode == 0x983) return field == 1 ? 1 : 0; // (the address register)
if (form < 1 || form > 7) return 0;
if (family == 0x025) return field == 0 || field == 3 ? 2 : 1;
switch (family) {
case 0x010: case 0x011: case 0x012: case 0x016: case 0x019: case 0x023: case 0x024: case 0x027:
case 0x020: case 0x021: case 0x007: case 0x008: case 0x009: case 0x017: case 0x00b: case 0x00c:
return 1;
default:
return 0;
}
}
struct LoweredProgram {
std::vector<u64> instructions;
std::vector<u32> source_instruction_offsets;
u32 source_size{};
u32 required_texture_buffer = ~0U;
// UR28 and up seen without a mirror: lowering again with this many registers below R216
// reserved for them (even, so pairs stay adjacent) maps them.
u32 high_uniform_registers{};
// (hookshot) the reserved numbers uniform_register gave, all instructions: what renamed registers' markers avoid
std::array<bool, 256> used_reserved{};
u32 register_map_words{}; // (hookshot: the REGMAP_nx2 prefixes emitted)
u32 error_offset{};
std::string_view error;
explicit operator bool() const { return error.empty(); }
};
// Lower only operations whose semantics are representable by the existing
// Maxwell frontend. These are new instructions, not reinterpretations of the
// native bytes. Scheduling/control words are added by the environment adapter.
inline LoweredProgram LowerToMaxwellWith(std::span<const Instruction> code, u32 high_uniforms,
const std::array<bool, 256>& avoid = {}, bool survey = false) {
LoweredProgram result;
// UR28 and up (when used) live just below R216: UR28 at R(216 - high_uniforms).
const u32 high_uniform_base = 216 - high_uniforms;
struct Relocation { u32 output_index; s64 target; };
std::vector<Relocation> relocations;
std::vector<u32> barrier_tokens;
std::vector<u32> call_targets;
std::vector<u32> call_links;
bool main_exited = false;
bool computed_branch = false; // a jump table: the program ends at its self-branch, not an EXIT
bool uses_uniform_bank = false;
for (const auto& instruction : code) {
const u32 op = instruction.Bits(0, 12);
uses_uniform_bank |= op == 0x3c4 || op == 0x2bd || op == 0x499 || op == 0x4a4 || op == 0xc07 || op == 0xc08 || op == 0x896 || op == 0xf60 || op == 0xc11 || op == 0xd08 || op == 0xc0c || op == 0x986 || op == 0x9a8 || op == 0xc12 || op == 0xc17 || op == 0xe24 || op == 0xc24 || op == 0xd00 || op == 0xd09 || op == 0x9c3 || op == 0xc45 || op == 0x2a4 || op == 0x891 || op == 0x890 || op == 0x290 || op == 0x28c || op == 0x88c || op == 0x287 || op == 0x887 || op == 0x958 || op == 0xab9 || op == 0xabb || op == 0x899 || op == 0xc20 || op == 0xe21 || op == 0xe23 || op == 0xc02 ||
op == 0x886 || op == 0x3c2 || op == 0xc82 || op == 0x882 || op == 0x292 || op == 0x892 || op == 0x291 || op == 0x981 || op == 0xc13 || op == 0xc10 || op == 0xc23 || op == 0x526 || op == 0x2be || op == 0x8a4 || op == 0xd05 || (op == 0x984 && instruction.Bits(91, 1)) || op == 0x89c || op == 0x883 || op == 0x2a5 || op == 0xd06 || op == 0xf63 || op == 0xf69 || op == 0xc0b || op == 0x8a5 || op == 0x299 || op == 0xc09 || (op == 0x81c && instruction.Bits(67, 1)) || op == 0xc0a ||
((op == 0x987 || op == 0x983) && instruction.Bits(91, 1));
}
const auto address_of = [](u32 index) { return 0x50U + index * 8 + ((index + 1) / 3) * 8; };
const auto fail = [&](u32 index, std::string_view reason) {
result.instructions.clear();
result.source_instruction_offsets.clear();
result.error_offset = index * sizeof(Instruction);
result.error = reason;
return result;
};
const auto finish = [&](u32 index) {
for (const auto& relocation : relocations) {
if (relocation.target > index) return fail(index, "native branch or call leaves captured program");
const u32 target = result.source_instruction_offsets[static_cast<size_t>(relocation.target)];
const s64 relative = s64{address_of(target)} - address_of(relocation.output_index) - 8;
if (relative < -0x800000 || relative > 0x7fffff) return fail(index, "lowered branch distance");
result.instructions[relocation.output_index] |= (static_cast<u64>(relative) & 0xffffff) << 20;
}
result.source_size = (index + 1) * sizeof(Instruction);
return result;
};
if (code.empty() || code.size() > 65536) return fail(0, "invalid native program size");
for (u32 index = 0; index < code.size(); ++index) {
auto insn = code[index];
result.source_instruction_offsets.push_back(static_cast<u32>(result.instructions.size()));
u32 opcode = insn.Bits(0, 12);
if (insn.Bits(12, 4) == 15) continue; // @!PT: never executes (compiler padding)
u32 f16_dest = 255, f16_second = 255, f16_components = 0;
std::optional<u32> active_link;
u32 active_entry = 0;
if (main_exited) {
for (size_t call = 0; call < call_targets.size(); ++call) {
if (call_targets[call] <= index && call_targets[call] >= active_entry) {
active_entry = call_targets[call];
active_link = call_links[call];
}
}
// RET, BRA, NOP and ISETP/FSETP (predicate outputs) have no destination GPR; bits 16..23
// are not a write to R0. IMAD.WIDE writes a register pair.
const bool no_destination = opcode == 0x950 || opcode == 0x947 || opcode == 0x918 ||
(opcode & 0x1ff) == 0x00c || (opcode & 0x1ff) == 0x00b;
const bool pair = opcode == 0x225 || opcode == 0x625 || opcode == 0x825;
if (active_link && !no_destination &&
(insn.Bits(16, 8) == *active_link || (pair && insn.Bits(16, 8) + 1 == *active_link)))
return fail(index, "native callee overwrites link register");
}
// Scratch values and emulated uniform state occupy reserved high GPRs. (hookshot) A native access that could
// alias them makes the instruction's high GPRs extended registers (RegisterMapPrefix): every field that may
// be one, each as the first of four (vectors), the over-counted kept apart from the lowering's own below
const u32 reserved_base = uses_uniform_bank ? high_uniform_base : 246U;
std::array<bool, 256> native_high{};
bool any_high = false, high_in_scratch = false, high_239 = false;
RegisterRun renamed[8];
u32 nrenamed = 0;
std::array<bool, 256> marker_taken{};
std::array<bool, 256> native_occupied{};
{
const u32 renameable = RenameableFields(opcode);
constexpr u32 bits[4]{16, 24, 32, 64};
// (a pair or quad starts at an even or a 4-aligned register: an odd one is one register, R4n+2 at most two)
const auto span_of = [](u32 reg) { return (reg & 1) ? 1U : (reg & 2) ? 2U : 4U; };
for (u32 f = 0; f < 4; ++f) { // what may be native high GPRs, and which are to be renamed
const u32 bit = bits[f], reg = insn.Bits(bit, 8);
// Survey all original operands before rewriting any of them. A marker chosen for
// an early field must not alias a later source (including GPRs below reserved_base).
// Keep the same conservative pair/quad spans used for high-register accesses.
for (u32 k = 0; k < span_of(reg) && reg + k < 255; ++k)
native_occupied[reg + k] = true;
// (ISBEWR's data too: not counted, a mesh program's output held in a high GPR was read from the plain register,
// never written, while what computes it wrote the extended one: vertices past the first 32 came out undefined.
// SUST and SURED's data as well, the same field. (amd-fix, Lichii #149) RED's too: a reduction's data in a high GPR
// read the plain register, never written. (Lichii #588) TXD's four gradients (0xb6c) and the global atomic's
// data (0x9a8) too: both are registers at bit 32)
const bool register_field = bit != 32 || insn.Bits(9, 3) == 1 || opcode == 0xb60 || opcode == 0xb66 ||
opcode == 0x927 || opcode == 0xb9b || opcode == 0x59f || opcode == 0x983 ||
opcode == 0x98e || opcode == 0xb6c || opcode == 0x9a8 || (renameable & (1U << f));
if (reg >= 255 || reg + 3 < reserved_base) continue; // (a quad below may reach up)
if (register_field && reg >= reserved_base) any_high = true;
}
for (u32 f = 0; f < 4 && any_high; ++f) {
const u32 bit = bits[f], reg = insn.Bits(bit, 8);
if (reg >= 255 || reg + 3 < reserved_base) continue;
const u32 exact = FieldSpan(opcode, f);
const u32 span = exact ? exact : span_of(reg);
if (exact && reg + span <= reserved_base) continue; // (wholly below the reserved registers)
bool reaches_scratch = reg + span > 246 || (reg <= 239 && reg + span > 239);
for (u32 k = 0; k < span && reg + k < 255; ++k) // (or a number a uniform register has somewhere)
reaches_scratch |= avoid[reg + k];
if ((renameable & (1U << f)) && reaches_scratch && reg >= reserved_base) {
// a marker block, aligned as the register, among the uniform bank's numbers nobody uses here
u32 marker = 255;
for (u32 m = 216; m + span <= 246 && marker == 255; m += span) {
bool ok = true;
for (u32 k = 0; k < span && ok; ++k)
ok = m + k != 239 && !avoid[m + k] && !marker_taken[m + k] && !native_occupied[m + k];
if (ok) marker = m;
}
if (marker != 255 && nrenamed < 8) {
bool same = false; // (the same register in two fields: one marker)
for (u32 r = 0; r < nrenamed; ++r)
if (renamed[r].target == reg) {
same = true;
insn.words[bit / 32] = (insn.words[bit / 32] & ~(0xffU << (bit % 32))) | (renamed[r].number << (bit % 32));
}
if (!same) {
for (u32 k = 0; k < span; ++k) marker_taken[marker + k] = true;
renamed[nrenamed++] = {marker, reg, span};
insn.words[bit / 32] = (insn.words[bit / 32] & ~(0xffU << (bit % 32))) | (marker << (bit % 32));
}
continue;
}
}
for (u32 k = 0; k < span && reg + k < 255; ++k) {
if (reg + k < reserved_base) continue;
native_high[reg + k] = true;
high_in_scratch |= reg + k >= 246;
high_239 |= reg + k == 239;
}
}
}
if (!any_high) native_high = {};
static const bool no_extended = std::getenv("RX_VK_NO_EXTREGS") != nullptr; // (as before them: rejected)
if (any_high && no_extended) return fail(index, "native program uses lowering scratch registers");
std::array<bool, 256> lowering_used{}; // (the reserved numbers uniform_register gave this instruction)
const size_t words_before = result.instructions.size();
const size_t relocations_before = relocations.size();
// TLD/TLD4 .F16 (bit 79): the components come packed in pairs, (0, 1) in Rd and (2, 3) in
// the second destination. Lowered as the F32 form into R248/R249 and R246/R247, then packed.
if ((opcode == 0xb66 || opcode == 0xb63) && insn.Bits(79, 2) == 1) {
f16_dest = insn.Bits(16, 8);
f16_second = insn.Bits(64, 8);
f16_components = static_cast<u32>(std::popcount(insn.Bits(72, 4)));
if (f16_dest >= 246 || (f16_second != 255 && f16_second >= 246) || insn.Bits(61, 3) == 5 ||
(f16_second == 255 && f16_components > 2))
return fail(index, "native half-precision texture result registers");
insn.words[2] &= ~(1U << 15);
insn.words[0] = (insn.words[0] & ~0x00ff0000U) | (f16_dest == 255 ? 0x00ff0000U : (248U << 16));
if (f16_second != 255) insn.words[2] = (insn.words[2] & ~0xffU) | 246U;
}
// UR0-UR21 live in R216-R237 and UR22-UR27 in R240-R245 (R239 holds the uniform
// predicates), so even/odd pairs stay adjacent; UR28 and up below R216. Sites that
// still check reg < 16 rely on UR0-UR15 being R216-R231.
const auto uniform_register_of = [&](u32 reg) -> u32 {
if (reg < 22) return 216 + reg;
if (reg < 28) return 218 + reg;
if (reg == 63) return 255;
if (reg < 63 && reg - 28 < high_uniforms) return high_uniform_base + (reg - 28);
if (reg < 63) result.high_uniform_registers = (std::max)(result.high_uniform_registers, (reg - 26) & ~1U);
return ~0U;
};
const auto uniform_register = [&](u32 reg) -> u32 {
const u32 r = uniform_register_of(reg);
if (r < 255) lowering_used[r] = true;
return r;
};
bool rewritten_uniform = false;
bool uniform_guard = false;
u32 uniform_output = 7; // ULOP3's UPn output: computed in P(6-n), then merged into R239
if (opcode == 0x2a4 || opcode == 0x8a4 || opcode == 0x891 || opcode == 0x890 || opcode == 0x290 ||
opcode == 0x292 || opcode == 0x892 || opcode == 0x291 || opcode == 0x896 || opcode == 0x8a5 ||
opcode == 0x4a4) { // (hookshot: 0x4a4, UIMAD URd, URa, imm, URc, as IMAD 0x424)
// (hookshot) a UPn guard on any of these that writes no predicate (no carry out: bits 81-83 and 84-86
// PT), as ULOP3's: swapped into P(6-n) around the op (guarded_uniform below)
// (ULEA, 0x291/0x891, has one carry out: its bits 84-86 are no predicate)
const bool lea = opcode == 0x291 || opcode == 0x891;
const bool no_predicate_out = insn.Bits(81, 3) == 7 && (lea || insn.Bits(84, 3) == 7);
// (hookshot) and a guarded ULEA with a carry out: the LEA below starts that UPn from R239 when guarded
if (insn.Bits(12, 4) != 7 && opcode != 0x292 && opcode != 0x892 && !no_predicate_out && !lea)
return fail(index, "native uniform arithmetic predication");
uniform_guard = insn.Bits(12, 4) != 7;
rewritten_uniform = true;
const u32 dst = uniform_register(insn.Bits(16, 8));
const u32 a = uniform_register(insn.Bits(24, 8));
const u32 c = uniform_register(insn.Bits(64, 8));
if (dst == ~0U || a == ~0U || c == ~0U) return fail(index, "native uniform arithmetic register");
insn.words[0] = (insn.words[0] & 0xffff) | (dst << 16) | (a << 24);
insn.words[2] = (insn.words[2] & ~0x080000ffU) | c;
if (opcode == 0x2a4 || opcode == 0x290 || opcode == 0x292 || opcode == 0x291) {
const u32 b = uniform_register(insn.Bits(32, 8));
if (b == ~0U) return fail(index, "native uniform arithmetic second register");
insn.words[1] = (insn.words[1] & ~0xffU) | b;
}
if ((opcode == 0x292 || opcode == 0x892) && insn.Bits(80, 4) != 0xe) {
if (insn.Bits(80, 1)) return fail(index, "native uniform logic predicate output");
uniform_output = insn.Bits(81, 3);
insn.words[2] = (insn.words[2] & ~(7U << 17)) | ((6 - uniform_output) << 17);
}
opcode = opcode == 0x2a4 ? 0x224 : opcode == 0x8a4 ? 0x824 : opcode == 0x4a4 ? 0x424 : opcode == 0x891 ? 0x811 : opcode == 0x890 ? 0x810
: opcode == 0x292 ? 0x212 : opcode == 0x892 ? 0x812 : opcode == 0x291 ? 0x211
: opcode == 0x896 ? 0x816 : opcode == 0x8a5 ? 0x825 : 0x210;
}
// (hookshot) and the uniform shifts, whose guard is swapped in the same way
const bool uniform_predication = opcode == 0xabb || opcode == 0xab9 || opcode == 0xc82 || opcode == 0x882 ||
opcode == 0x899 || opcode == 0x299 || opcode == 0x499;
const u32 native_predicate = insn.Bits(12, 4);
const bool guarded_uniform = (uniform_predication || uniform_guard) && native_predicate != 7;
// A uniform guard UPn lives in R239 bit 6-n and is swapped into P(6-n) for the op.
const u32 guard_slot = 6 - (native_predicate & 7);
u64 predicate = u64{guarded_uniform ? ((native_predicate & 8) | guard_slot) : native_predicate} << 16;
const u64 dest = insn.Bits(16, 8);
const auto base = [&](u64 opcode_bits) { return opcode_bits | predicate | dest; };
// NVN2 fullscreen vertex shaders read bank 24, which the game never binds (the console reads
// zeros from an unbound bank); Maxwell has 18, so read it from bank 17, also never bound.
const auto bank = [&]() -> u32 {
const u32 value = insn.Bits(54, 5);
return value == 24 ? 17U : value;
};
const auto cbuf = [&]() -> u64 {
return (u64{insn.Bits(38, 16) / 4} << 20) | (u64{bank()} << 34);
};
const auto valid_cbuf = [&] {
return !insn.Bits(91, 1) && bank() < 18 &&
(insn.Bits(38, 16) % 4) == 0;
};
std::string_view operand_error;
const auto emit = [&](u64 word) { result.instructions.push_back(word); };
// Bound Maxwell texture instructions share one descriptor constant buffer per program.
const auto bind_texture_buffer = [&](u32 binding) {
if (result.required_texture_buffer != ~0U && result.required_texture_buffer != binding)
return false;
result.required_texture_buffer = binding;
return true;
};
// P(guard slot) while a uniform guard is swapped in; P6 for lowerings that use it as
// scratch (R2UR, MATCH, HFMA2.RELU), which save and restore it with the defaults below.
const u32 guard_mask = guard_slot < 7 ? 1U << guard_slot : 0U;
const auto save_predicate = [&](u32 mask = 1U << 6) {
emit(0x38e8000000070000ULL | 253 | (u64{255} << 8) | (u64{mask} << 20));
};
const auto restore_predicate = [&](u32 mask = 1U << 6) {
emit(0x38f0000000070000ULL | (u64{253} << 8) | (u64{mask} << 20));
};
if (guarded_uniform) {
save_predicate(guard_mask);
emit(0x38f0000000070000ULL | (u64{239} << 8) | (u64{guard_mask} << 20));
}
const u32 output_mask = uniform_output == 7 ? 0U : 1U << (6 - uniform_output);
if (output_mask) {
// Keep the guest P(6-n) in R254 and start from the current UPn (a false guard keeps it).
emit(0x38e8000000070000ULL | 254 | (u64{255} << 8) | (u64{output_mask} << 20));
emit(0x38f0000000070000ULL | (u64{239} << 8) | (u64{output_mask} << 20));
}
// Pd = LUT(a, b, c) over predicate operands (index | negate << 3): each input becomes
// 0 or ~0 (SEL), and LOP3 sets Pd when the result is nonzero.
const auto predicate_lut = [&](u32 lut, u32 a, u32 b, u32 c, u32 target, bool uniform_c = false) {
const u32 inputs[3]{a, b, c};
for (u32 i = 0; i < 3; ++i) {
if (i == 2 && uniform_c) {
// UPn from R239 bit 6-n as 0 or ~0 (UPT is ~0); a negation flips the table.
if ((c & 7) == 7) {
emit(0x0100000000000000ULL | predicate | 252 | (u64{15} << 12) | (u64{~0U} << 20));
} else {
emit(0x3800000000000000ULL | predicate | 252 | (u64{239} << 8) |
(u64{(6 - (c & 7)) | (1U << 8)} << 20));
emit(0x5c10000000000000ULL | predicate | 252 | (u64{255} << 8) |
(u64{252} << 20) | (u64{1} << 48));
}
if (c & 8) lut = ((lut & 0x55) << 1) | ((lut & 0xaa) >> 1);
continue;
}
emit(0x38a0000000000000ULL | predicate | (250 + i) | (u64{255} << 8) |
(u64{0x7ffff} << 20) | (u64{1} << 56) | (u64{inputs[i] ^ 8U} << 39));
}
emit(0x5be0000000000000ULL | predicate | 255 | (u64{250} << 8) | (u64{251} << 20) |
(u64{lut} << 28) | (u64{3} << 36) | (u64{252} << 39) | (u64{target} << 48));
};
const auto mov_imm = [&](u32 reg, u32 value) {
emit(0x0100000000000000ULL | predicate | reg | (u64{15} << 12) | (u64{value} << 20));
};
const auto materialize = [&](u32 kind, u32 reg, u32 scratch, bool absolute, bool negate) -> u32 {
if (kind == 3) {
const u32 mapped = uniform_register(reg);
if (mapped == ~0U) { operand_error = "native uniform source register"; return 255; }
reg = mapped;
} else if (kind == 1) {
mov_imm(scratch, insn.words[1]);
reg = scratch;
} else if (kind == 2) {
if (!valid_cbuf()) { operand_error = "native constant buffer address"; return 255; }
emit(0x4c98000000000000ULL | predicate | scratch | (u64{15} << 39) | cbuf());
reg = scratch;
}
if (absolute || negate) {
if (reg != scratch) emit(0x5c98000000000000ULL | predicate | scratch | (u64{reg} << 20) | (u64{15} << 39));
if (absolute) emit(0x0400000000000000ULL | predicate | scratch | (u64{scratch} << 8) | (u64{0x7fffffff} << 20));
if (negate) emit(0x0400000000000000ULL | predicate | scratch | (u64{scratch} << 8) | (u64{0x80000000} << 20) | (u64{2} << 53));
reg = scratch;
}
return reg;
};
const auto operand = [&](u32 which, bool floating = true) -> u32 {
const u32 form = insn.Bits(9, 3);
u32 reg{}, kind{}, abs{}, neg{};
if (which == 0) {
reg = insn.Bits(24, 8); abs = insn.Bits(73, 1); neg = insn.Bits(72, 1);
} else {
if (form != 1 && form != 2 && form != 3 && form != 4 && form != 5 && form != 6 && form != 7) {
operand_error = "native ALU source form"; return 255;
}
const bool wide = which == 1 ? (form == 4 || form == 5 || form == 6) : (form == 2 || form == 3 || form == 7);
if (wide) {
kind = form == 6 || form == 7 ? 3 : (form == 4 || form == 2) ? 1 : 2;
if (kind == 3) reg = insn.Bits(32, 8);
if (kind == 2 || kind == 3) { abs = insn.Bits(62, 1); neg = insn.Bits(63, 1); }
} else {
const bool upper = which == 1 ? (form == 2 || form == 3 || form == 7) : true;
reg = insn.Bits(upper ? 64 : 32, 8);
abs = insn.Bits(upper ? 74 : 62, 1); neg = insn.Bits(upper ? 75 : 63, 1);
}
}
return materialize(kind, reg, 250 + which, floating && abs, floating && neg);
};
// (hookshot) Ampere SHF in general: (hi:lo) shifted by an immediate or a register, bits 73-74 the type (0 S64,
// 1 U64, 2 S32, 3 U32), 75 wrap, 76 right, 80 the high word of the result. Maxwell's SHF.R gives the low
// word of (hi:lo) >> s and SHF.L the high word of (hi:lo) << s (max shift at 37-38: U32 0, U64 2, S64 3;
// wrap at 50, as Eden's decoder reads them); the other two words are a plain SHR of hi or SHL of lo
// (.W at 39). False: a shift by an immediate past the type's width.
const auto funnel = [&](u32 dst, u32 lo, u32 hi, bool shift_in_register, u32 shift, u32 type, bool right,
bool hi_word, bool wrap) -> bool {
const bool wide = type < 2, sign = type == 0 || type == 2;
if (!shift_in_register && shift >= (wide ? 64U : 33U)) return false;
const u64 amount = u64{shift} << 20, w39 = u64{wrap} << 39;
if (right != hi_word) {
// (hookshot, from Lichii) an immediate's bits 20-38 are all the amount: the clamp at 37-38 made every shift 63
if (!shift_in_register && wide && shift >= 32) {
emit((right ? 0x3828000000000000ULL : 0x3848000000000000ULL) | predicate | dst |
(u64{right ? hi : lo} << 8) | (u64{shift - 32} << 20) | (u64{right && sign} << 48));
return true;
}
const u64 op = shift_in_register ? (right ? 0x5cf8000000000000ULL : 0x5bf8000000000000ULL)
: (right ? 0x38f8000000000000ULL : 0x36f8000000000000ULL);
const u64 max_shift = !shift_in_register || !wide ? 0U : sign ? 3U : 2U;
emit(op | predicate | dst | (u64{lo} << 8) | amount | (max_shift << 37) | (u64{hi} << 39) |
(u64{wrap} << 50));
return true;
}
if (!shift_in_register && shift >= 32) { // (the word shifted out entirely)
if (right && sign) emit(0x3828000000000000ULL | predicate | dst | (u64{hi} << 8) | (u64{31} << 20) |
(u64{1} << 48));
else emit(0x5c98000000000000ULL | predicate | dst | (u64{255} << 20) | (u64{15} << 39));
return true;
}
if (right) emit((shift_in_register ? 0x5c28000000000000ULL : 0x3828000000000000ULL) | predicate | dst |
(u64{hi} << 8) | amount | w39 | (u64{sign} << 48));
else emit((shift_in_register ? 0x5c48000000000000ULL : 0x3848000000000000ULL) | predicate | dst |
(u64{lo} << 8) | amount | w39);
return true;
};
struct HalfSource { u32 reg, swizzle; bool absolute, negate; };
const auto half_source = [&](u32 which) -> HalfSource {
if (which == 0) return {insn.Bits(24, 8), insn.Bits(74, 2),
insn.Bits(73, 1) != 0, insn.Bits(72, 1) != 0};
const u32 form = insn.Bits(9, 3);
const bool wide = which == 1 ? (form == 4 || form == 5) : (form == 2 || form == 3);
if (wide) {
const bool immediate = form == 2 || form == 4;
const u32 reg = materialize(immediate ? 1 : 2, 255, 250 + which, false, false);
return {reg, immediate ? 0U : insn.Bits(60, 2),
!immediate && insn.Bits(62, 1), !immediate && insn.Bits(63, 1)};
}
const bool upper = which == 2 || form == 2 || form == 3;
return {insn.Bits(upper ? 64 : 32, 8), insn.Bits(upper ? 81 : 60, 2),
insn.Bits(upper ? 83 : 62, 1) != 0, insn.Bits(upper ? 84 : 63, 1) != 0};
};
const auto extract_half = [&](HalfSource source, u32 lane, u32 target) {
const bool high = source.swizzle == 3 || (source.swizzle == 0 && lane == 1);
emit(0x5ca8000000000000ULL | predicate | target | (u64{2} << 8) |
(u64{source.swizzle == 1 ? 2U : 1U} << 10) | (u64{source.reg} << 20) |
(u64{high} << 41) | (u64{source.negate} << 45) | (u64{source.absolute} << 49));
};
const auto xmad = [&](u32 target, u32 a, u32 b, u32 c, bool a_high,
bool b_high, u32 mode, bool immediate) {
emit((immediate ? 0x3600000000000000ULL : 0x5b00000000000000ULL) |
predicate | target | (u64{a} << 8) |
(u64{immediate ? ((b >> (b_high ? 16 : 0)) & 0xffff) : b} << 20) |
(u64{!immediate && b_high} << 35) | (u64{c} << 39) |
(u64{mode} << 50) | (u64{a_high} << 53));
};
const auto multiply_low = [&](u32 a, u32 b, u32 c, bool immediate) {
xmad(250, a, b, c, false, false, 0, immediate);
xmad(251, a, b, 255, true, false, 0, immediate);
xmad(251, a, b, 251, false, true, 0, immediate);
emit(base(0x5c18000000000000ULL) | (u64{251} << 8) |
(u64{250} << 20) | (u64{16} << 39));
};
switch (opcode) {
case 0x304: { // F2F.F16.F32, scalar round-to-nearest conversion.
if (insn.words[2] != 0x00200800 || insn.Bits(24, 8) ||
(insn.words[1] & ~0xc00000ffU))
return fail(index, "native scalar float conversion mode");
emit(base(0x5ca8000000000000ULL) | (u64{1} << 8) | (u64{2} << 10) |
(u64{insn.Bits(32, 8)} << 20) | (u64{insn.Bits(62, 1)} << 49) |
(u64{insn.Bits(63, 1)} << 45));
break;
}
case 0x23e: case 0xa3e: { // F2FP.PACK_AB / MERGE_C, round-to-nearest
if (insn.Bits(62, 2) || insn.Bits(73, 5) || insn.Bits(79, 2) ||
insn.Bits(81, 10) || insn.Bits(91, 1)) {
return fail(index, "native float packing modifiers");
}
const bool merge = insn.Bits(78, 1);
if (insn.Bits(72, 1)) return fail(index, "native float packing selector");
const u32 a = merge ? operand(2, false) : operand(0, false);
const u32 b = operand(1, false);
// Canonical F2F writes the converted half in its low 16 bits.
if (merge) {
emit(0x5c98000000000000ULL | predicate | 250 | (u64{a} << 20) | (u64{15} << 39));
} else {
emit(0x5ca8000000000000ULL | predicate | 250 | (u64{1} << 8) |
(u64{2} << 10) | (u64{a} << 20));
}
emit(0x5ca8000000000000ULL | predicate | 251 | (u64{1} << 8) |
(u64{2} << 10) | (u64{b} << 20));
emit(base(0x5c18000000000000ULL) | (u64{250} << 8) | (u64{251} << 20) | (u64{16} << 39));
break;
}
case 0x232: case 0x832: case 0xa32: { // HMUL2
if (insn.Bits(78, 2) || insn.Bits(81, 10) || insn.Bits(91, 1))
return fail(index, "native half multiply mode");
const auto a = half_source(0), b = half_source(1);
const u32 precision = insn.Bits(76, 1) ? 2 : insn.Bits(80, 1);
emit(base(0x5d08000000000000ULL) | (u64{a.reg} << 8) | (u64{b.reg} << 20) |
(u64{b.swizzle} << 28) | (u64{b.absolute} << 30) | (u64{a.negate != b.negate} << 31) |
(u64{insn.Bits(77, 1)} << 32) | (u64{precision} << 39) |
(u64{a.absolute} << 44) | (u64{a.swizzle} << 47));
break;
}
case 0x230: case 0x430: case 0x630: { // HADD2
if (insn.Bits(76, 1) || insn.Bits(79, 1) || insn.Bits(81, 10) || insn.Bits(91, 1))
return fail(index, "native half add mode");
const auto a = half_source(0);
const auto b = half_source(opcode == 0x230 ? 1 : 2);
if (insn.Bits(78, 1)) {
if (a.reg != 255 || !a.negate || a.absolute || a.swizzle != 0 ||
b.swizzle < 2 || insn.Bits(77, 1) || insn.Bits(80, 1))
return fail(index, "native float-output half add mode");
extract_half(b, 0, static_cast<u32>(dest));
} else {
emit(base(0x5d10000000000000ULL) | (u64{a.reg} << 8) | (u64{b.reg} << 20) |
(u64{b.swizzle} << 28) | (u64{b.absolute} << 30) | (u64{b.negate} << 31) |
(u64{insn.Bits(77, 1)} << 32) | (u64{insn.Bits(80, 1)} << 39) |
(u64{a.negate} << 43) | (u64{a.absolute} << 44) | (u64{a.swizzle} << 47));
}
break;
}
case 0x231: case 0x431: case 0x631: case 0x831: case 0xa31: { // HFMA2
const bool relu = insn.Bits(79, 1) != 0;
if (insn.Bits(78, 1) || insn.Bits(85, 1) || insn.Bits(91, 1) ||
(relu ? (insn.Bits(87, 4) != 7 ||
insn.Bits(77, 1)) : insn.Bits(87, 4) != 0))
return fail(index, "native half fused multiply-add mode");
if (relu && native_predicate != 7) {
// Guard the whole expansion before its temporary P6 updates.
relocations.push_back({static_cast<u32>(result.instructions.size()), index + 1});
emit(0xe24000000000000fULL | (u64{native_predicate ^ 8U} << 16));
predicate = u64{7} << 16;
}
auto a = half_source(0);
auto b = half_source(1);
const auto c = half_source(2);
if (insn.Bits(86, 1)) { // H0_NH1: negate only the upper packed component.
if (b.swizzle != 0 || b.absolute || b.reg >= 250)
return fail(index, "native half mixed-sign source mode");
emit(0x0400000000000000ULL | predicate | 251 | (u64{b.reg} << 8) |
(u64{0x80000000} << 20) | (u64{2} << 53));
b.reg = 251;
}
if (b.absolute || c.absolute || (a.absolute && a.reg >= 248))
return fail(index, "native half fused multiply-add absolute source");
if (a.absolute) {
emit(0x0400000000000000ULL | predicate | 250 | (u64{a.reg} << 8) |
(u64{a.swizzle == 1 ? 0x7fffffffU : 0x7fff7fffU} << 20));
a.reg = 250;
a.absolute = false;
}
const u32 precision = insn.Bits(76, 1) ? 2 : insn.Bits(80, 1);
emit(0x5d00000000000000ULL | predicate | (relu ? 254U : dest) | (u64{a.reg} << 8) | (u64{b.reg} << 20) |
(u64{b.swizzle} << 28) | (u64{c.negate} << 30) | (u64{a.negate != b.negate} << 31) |
(u64{insn.Bits(77, 1)} << 32) | (u64{c.swizzle} << 35) |
(u64{precision} << 37) | (u64{c.reg} << 39) | (u64{a.swizzle} << 47));
if (relu) {
// Clamp the rounded binary16 lanes, preserving positive infinity
// and canonicalizing either sign of NaN without saturating to one.
save_predicate();
for (u32 lane = 0; lane < 2; ++lane) {
emit(0x3828000000070000ULL | 251 | (u64{254} << 8) | (u64{lane * 16} << 20));
emit(0x0400000000070000ULL | 251 | (u64{251} << 8) | (u64{0xffff} << 20));
emit(0x0400000000070000ULL | 252 | (u64{251} << 8) | (u64{0x7fff} << 20));
mov_imm(250, 0x7c00);
emit(0x5b60000000070000ULL | 7 | (u64{6} << 3) | (u64{250} << 8) |
(u64{252} << 20) | (u64{7} << 39) | (u64{1} << 49));
emit(0x3828000000070000ULL | 252 | (u64{251} << 8) | (u64{15} << 20));
mov_imm(250, 1);
emit(0x5c10000000070000ULL | 252 | (u64{252} << 8) | (u64{250} << 20) | (u64{1} << 48));
emit(0x5c40000000070000ULL | 251 | (u64{251} << 8) | (u64{252} << 20) | (u64{7} << 48));
mov_imm(250, 0x7fff);
emit(0x5ca0000000070000ULL | 251 | (u64{250} << 8) | (u64{251} << 20) | (u64{6} << 39));
if (lane == 0) {
emit(base(0x5c98000000000000ULL) | (u64{251} << 20) | (u64{15} << 39));
} else {
emit(0x3848000000070000ULL | 251 | (u64{251} << 8) | (u64{16} << 20));
emit(base(0x5c40000000000000ULL) | (dest << 8) | (u64{251} << 20) |
(u64{1} << 41) | (u64{7} << 48));
}
}
restore_predicate();
}
break;
}
case 0x240: case 0x840: { // HMNMX2, exact half promotion for min/max
if (insn.Bits(76, 11) || insn.Bits(91, 1))
return fail(index, "native half min/max mode");
const auto a = half_source(0);
const auto b = opcode == 0x840 ? HalfSource{255, 0, false, false} : half_source(1);
if (a.swizzle == 1 || b.swizzle == 1 || (b.reg >= 250 && b.reg != 255))
return fail(index, "native half min/max source mode");
for (u32 lane = 0; lane < 2; ++lane) {
extract_half(a, lane, 250);
if (opcode == 0x840) {
const u32 half = (insn.words[1] >> (lane * 16)) & 0xffff;
mov_imm(251, half);
extract_half(HalfSource{251, 2, false, false}, 0, 251);
} else {
extract_half(b, lane, 251);
}
const u32 target = lane == 0 ? 252 : 250;
emit(0x5c60000000000000ULL | predicate | target | (u64{250} << 8) | (u64{251} << 20) |
(u64{insn.Bits(87, 3)} << 39) | (u64{insn.Bits(90, 1)} << 42));
emit(0x5ca8000000000000ULL | predicate | target | (u64{1} << 8) |
(u64{2} << 10) | (u64{target} << 20));
}
emit(base(0x5c18000000000000ULL) | (u64{250} << 8) | (u64{252} << 20) | (u64{16} << 39));
break;
}
case 0xc24: case 0xe24: { // IMAD low word with a uniform multiplier or addend.
// 0xe24 keeps the uniform addend in bits 32-37; bit 63 negates it ("RZ, RZ, -UR4").
const bool negate_c = opcode == 0xe24 && insn.Bits(63, 1) != 0;
if ((insn.words[2] & ~0x080002ffU) != 0x078e0000 ||
(insn.words[1] & ~(0xffU | (negate_c ? 0x80000000U : 0U))))
return fail(index, "native uniform-addend multiply mode");
const u32 a = operand(0, false), b = operand(1, false);
u32 c = operand(2, false);
if (negate_c) {
emit(0x5c10000000000000ULL | predicate | 252 | (u64{255} << 8) | (u64{c} << 20) |
(u64{1} << 48));
c = 252;
}
multiply_low(a, b, c, false);
break;
}
case 0x224: { // IMAD register / zero-product move
if (insn.Bits(24, 8) == 255 && insn.words[1] == 255 && insn.Bits(75, 1) &&
(insn.words[2] & ~0xaffU) == 0x078e0000) {
emit(base(0x5c10000000000000ULL) | (u64{255} << 8) |
(u64{insn.Bits(64, 8)} << 20) | (u64{1} << 48));
break;
}
if (insn.Bits(24, 8) == 255 && insn.words[1] == 255 &&
(insn.words[2] & ~0xffU) == 0x078e0000) {
emit(base(0x5c98000000000000ULL) | (u64{15} << 39) | (u64{insn.Bits(64, 8)} << 20));
break;
}
if (insn.Bits(24, 8) == 255 && insn.words[1] == 255 && (insn.words[2] & ~0x07800affU) == 0x000e0400 &&
(!rewritten_uniform || insn.Bits(87, 3) == 7)) {
// IMAD.X RZ, RZ, c, Pn: the high word of a 64-bit add (bit 75 inverts c).
u32 c = insn.Bits(64, 8);
mov_imm(250, 1);
emit(0x5ca0000000000000ULL | predicate | 250 | (u64{250} << 8) | (u64{255} << 20) |
(u64{insn.Bits(87, 3)} << 39) | (u64{insn.Bits(90, 1)} << 42));
if (insn.Bits(75, 1)) {
emit(0x0400000000000000ULL | predicate | 251 | (u64{c} << 8) | (u64{0xffffffffU} << 20) | (u64{2} << 53));
c = 251;
}
emit(base(0x5c10000000000000ULL) | (u64{c} << 8) | (u64{250} << 20));
break;
}
if ((insn.words[2] & ~0x2ffU) != 0x078e0000 || (insn.words[1] & ~0xffU))
return fail(index, "native integer register multiply mode");
multiply_low(insn.Bits(24, 8), insn.Bits(32, 8), insn.Bits(64, 8), false);
break;
}
case 0x2a5: { // UIMAD.WIDE.U32 UR(d):UR(d+1) = URa * URb + URc:UR(c+1)
const u32 a = uniform_register(insn.Bits(24, 8)), b = uniform_register(insn.Bits(32, 8));
const u32 c = insn.Bits(64, 8);
const u32 c_low = uniform_register(c), c_high = c == 63 ? 255U : uniform_register(c + 1);
const u32 d_low = uniform_register(insn.Bits(16, 8)), d_high = uniform_register(insn.Bits(16, 8) + 1);
if (native_predicate != 7 || (insn.words[1] & ~0xffU) || (insn.words[2] & ~0xffU) != 0x0f8e0000 ||
(insn.Bits(16, 8) & 1) || (c != 63 && (c & 1)) || a == ~0U || b == ~0U || c_low == ~0U ||
c_high == ~0U || d_low == ~0U || d_high == ~0U || d_low == 255)
return fail(index, "native uniform wide multiply mode");
// High word in R253 (base-2^16 long multiplication, as IMAD.HI), low word in R250.
xmad(250, a, b, 255, false, false, 0, false);
xmad(251, a, b, 250, true, false, 2, false);
xmad(250, a, b, 251, false, true, 1, false);
xmad(252, a, b, 251, true, true, 2, false);
emit(0x3828000000070000ULL | 250 | (u64{250} << 8) | (u64{16} << 20));
emit(0x5c10000000070000ULL | 253 | (u64{250} << 8) | (u64{252} << 20));
xmad(250, a, b, 255, false, false, 0, false);
xmad(251, a, b, 255, true, false, 0, false);
xmad(251, a, b, 251, false, true, 0, false);
emit(0x5c18000000070000ULL | 250 | (u64{251} << 8) | (u64{250} << 20) | (u64{16} << 39));
emit(0x5c10000000070000ULL | 250 | (u64{250} << 8) | (u64{c_low} << 20) | (u64{1} << 47));
emit(0x5c10000000070000ULL | d_high | (u64{253} << 8) | (u64{c_high} << 20) | (u64{1} << 43));
emit(0x5c10000000070000ULL | d_low | (u64{250} << 8) | (u64{255} << 20));
break;
}
case 0x226: { // IDP.2A.LO.U16.U8: a.lo16 * b.byte0 + a.hi16 * b.byte1 + c
if ((insn.words[2] & ~0xffU) != 0x1000 || (insn.words[1] & ~0xffU))
return fail(index, "native packed dot-product mode");
const u32 a = insn.Bits(24, 8), b = insn.Bits(32, 8), c = insn.Bits(64, 8);
for (u32 component = 0; component < 2; ++component) {
emit(0x3828000000000000ULL | predicate | (250 + component) | (u64{b} << 8) | (u64{component * 8} << 20));
emit(0x0400000000000000ULL | predicate | (250 + component) | (u64{250 + component} << 8) | (u64{0xff} << 20));
}
xmad(252, a, 250, c, false, false, 0, false);
xmad(static_cast<u32>(dest), a, 251, 252, true, false, 0, false);
break;
}
case 0x225: case 0x227: case 0x625: case 0x825: case 0x827: case 0xa27: {
// IMAD.HI Rd / IMAD.WIDE Rd:Rd+1 = a * b + c:c+1 (.U32 or signed, bit 73), with an optional
// carry-out (bits 81-83). b is a register, an immediate (0x8xx) or a cbuf (0xa27); 0x625 takes
// b from bits 64-71 and adds a cbuf pair.
const bool wide = opcode == 0x225 || opcode == 0x625 || opcode == 0x825;
const bool immediate = opcode == 0x825 || opcode == 0x827, signed_product = insn.Bits(73, 1);
const u32 c = opcode == 0x625 ? 255U : insn.Bits(64, 8), carry_output = insn.Bits(81, 3);
if ((insn.words[2] & ~0x000e02ffU) != 0x07800000 ||
((opcode == 0x225 || opcode == 0x227) && (insn.words[1] & ~0xffU)) ||
((opcode == 0xa27 || opcode == 0x625) && (!valid_cbuf() || insn.Bits(62, 2))) ||
(opcode == 0x625 && insn.Bits(38, 16) > 0xfff8) || (c != 255 && (c & 1)) ||
(wide && (dest == 255 || (dest & 1))) || (signed_product && carry_output != 7))
return fail(index, "native high multiply mode");
const u32 a = insn.Bits(24, 8);
const u32 b = immediate ? insn.words[1] : opcode == 0xa27 ? 254U : insn.Bits(opcode == 0x625 ? 64 : 32, 8);
if (opcode == 0xa27) emit(0x4c98000000000000ULL | predicate | 254 | (u64{15} << 39) | cbuf());
if (signed_product) {
// The signed high word is the unsigned one minus (a < 0 ? b : 0) + (b < 0 ? a : 0),
// which R253 holds (R252 is free until the multiplication).
const u32 b_register = immediate ? 254U : b;
if (immediate) mov_imm(254, b);
emit(0x3828000000000000ULL | predicate | 253 | (u64{a} << 8) | (u64{31} << 20) | (u64{1} << 48));
emit(0x5c40000000000000ULL | predicate | 253 | (u64{253} << 8) | (u64{b_register} << 20) | (u64{7} << 48));
emit(0x3828000000000000ULL | predicate | 252 | (u64{b_register} << 8) | (u64{31} << 20) | (u64{1} << 48));
emit(0x5c40000000000000ULL | predicate | 252 | (u64{252} << 8) | (u64{a} << 20) | (u64{7} << 48));
emit(0x5c10000000000000ULL | predicate | 253 | (u64{253} << 8) | (u64{252} << 20));
}
// Base-2^16 long multiplication. Each intermediate sum fits U32.
xmad(250, a, b, 255, false, false, 0, immediate);
xmad(251, a, b, 250, true, false, 2, immediate);
xmad(250, a, b, 251, false, true, 1, immediate);
xmad(252, a, b, 251, true, true, 2, immediate);
emit(0x3828000000000000ULL | predicate | 250 | (u64{250} << 8) | (u64{16} << 20));
if (signed_product)
emit(0x5c10000000000000ULL | predicate | 250 | (u64{250} << 8) | (u64{253} << 20) | (u64{1} << 48));
if (!wide && c == 255 && carry_output == 7) {
emit(base(0x5c10000000000000ULL) | (u64{250} << 8) | (u64{252} << 20));
break;
}
// High word in R253 and low word in R250 (as UIMAD.WIDE.U32), then c:c+1 is added.
emit(0x5c10000000000000ULL | predicate | 253 | (u64{250} << 8) | (u64{252} << 20));
xmad(250, a, b, 255, false, false, 0, immediate);
xmad(251, a, b, 255, true, false, 0, immediate);
xmad(251, a, b, 251, false, true, 0, immediate);
emit(0x5c18000000000000ULL | predicate | 250 | (u64{251} << 8) | (u64{250} << 20) | (u64{16} << 39));
u32 c_low = c, c_high = c == 255 ? 255U : c + 1;
if (opcode == 0x625) {
emit(0x4c98000000000000ULL | predicate | 252 | (u64{15} << 39) | cbuf());
emit(0x4c98000000000000ULL | predicate | 254 | (u64{15} << 39) | (cbuf() + (u64{1} << 20)));
c_low = 252;
c_high = 254;
}
const u32 high = wide ? static_cast<u32>(dest) + 1 : static_cast<u32>(dest);
emit(0x5c10000000000000ULL | predicate | 250 | (u64{250} << 8) | (u64{c_low} << 20) | (u64{1} << 47));
if (carry_output == 7) {
emit(0x5c10000000000000ULL | predicate | high | (u64{253} << 8) | (u64{c_high} << 20) | (u64{1} << 43));
if (wide) emit(base(0x5c10000000000000ULL) | (u64{250} << 8) | (u64{255} << 20));
break;
}
// Maxwell IADD has no .X with .CC: R251 takes the low carry, R254 counts the high word's.
emit(0x5c10000000000000ULL | predicate | 251 | (u64{255} << 8) | (u64{255} << 20) | (u64{1} << 43));
emit(0x5c10000000000000ULL | predicate | 253 | (u64{253} << 8) | (u64{c_high} << 20) | (u64{1} << 47));
emit(0x5c10000000000000ULL | predicate | 254 | (u64{255} << 8) | (u64{255} << 20) | (u64{1} << 43));
emit(0x5c10000000000000ULL | predicate | 253 | (u64{253} << 8) | (u64{251} << 20) | (u64{1} << 47));
emit(0x5c10000000000000ULL | predicate | 254 | (u64{254} << 8) | (u64{255} << 20) | (u64{1} << 43));
emit(0x5c98000000000000ULL | predicate | high | (u64{253} << 20) | (u64{15} << 39));
if (wide) emit(base(0x5c98000000000000ULL) | (u64{250} << 20) | (u64{15} << 39));
emit(0x5b60000000000000ULL | predicate | 7 | (u64{carry_output} << 3) | (u64{254} << 8) |
(u64{255} << 20) | (u64{7} << 39) | (u64{5} << 49));
break;
}
case 0xa24: { // IMAD, constant-buffer multiplier
if ((insn.words[2] & ~0x2ffU) != 0x078e0000 || !valid_cbuf() || insn.Bits(62, 2))
return fail(index, "native integer constant-buffer multiply mode");
emit(0x4c98000000000000ULL | predicate | 252 | (u64{15} << 39) | cbuf());
multiply_low(insn.Bits(24, 8), 252, insn.Bits(64, 8), false);
break;
}
case 0x424: case 0x624: { // IMAD.MOV.U32: zero product plus immediate/cbuf
if (opcode == 0x624 && insn.Bits(24, 8) == 255 &&
(insn.words[2] & ~0x07800000U) == 0x000e06ffU) {
const u32 source = operand(2, false);
mov_imm(250, 1);
emit(0x5ca0000000000000ULL | predicate | 250 | (u64{250} << 8) | (u64{255} << 20) |
(u64{insn.Bits(87, 3)} << 39) | (u64{insn.Bits(90, 1)} << 42));
emit(base(0x5c10000000000000ULL) | (u64{source} << 8) | (u64{250} << 20));
break;
}
if (insn.Bits(24, 8) != 255 || insn.Bits(64, 8) != 255 || insn.words[2] != 0x078e00ff) {
if ((insn.words[2] & ~0x2ffU) != 0x078e0000 || (opcode == 0x624 && insn.Bits(62, 2)))
return fail(index, "native multiply wide addend mode");
const u32 a = operand(0, false), b = operand(1, false), c = operand(2, false);
multiply_low(a, b, c, false);
break;
}
if (insn.Bits(24, 8) != 255 || insn.Bits(64, 8) != 255 ||
insn.words[2] != 0x078e00ff)
return fail(index, "native integer move mode");
if (opcode == 0x624) {
if (!valid_cbuf() || insn.Bits(62, 2))
return fail(index, "native integer move constant buffer");
emit(base(0x4c98000000000000ULL) | (u64{15} << 39) | cbuf());
} else {
emit(base(0x0100000000000000ULL) | (u64{15} << 12) |
(u64{insn.words[1]} << 20));
}
break;
}
case 0x824: { // Captured low-word IMAD forms; bit 75 negates the addend.
const bool negate_c = insn.Bits(75, 1) != 0;
if (insn.words[1] == 1 && (insn.words[2] & ~(0x07800800U | 0xffU)) == 0x000e0600U) {
// IMAD.X Rd, Ra, 1, Rc, Pn: Ra + Rc plus the carry predicate in bits 87-90 (~Rc with bit 75).
u32 c = insn.Bits(64, 8);
mov_imm(252, 1);
emit(0x5ca0000000000000ULL | predicate | 252 | (u64{252} << 8) | (u64{255} << 20) |
(u64{insn.Bits(87, 3)} << 39) | (u64{insn.Bits(90, 1)} << 42));
if (negate_c) {
emit(0x0400000000000000ULL | predicate | 251 | (u64{c} << 8) | (u64{0xffffffffU} << 20) | (u64{2} << 53));
c = 251;
}
emit(0x5c10000000000000ULL | predicate | 250 | (u64{insn.Bits(24, 8)} << 8) |
(u64{c} << 20));
emit(base(0x5c10000000000000ULL) | (u64{250} << 8) | (u64{252} << 20));
break;
}
if (insn.words[1] == 1 && (insn.words[2] & 0xfffff700U) == 0x078e0200) {
emit(base(0x5c10000000000000ULL) | (u64{insn.Bits(24, 8)} << 8) |
(u64{insn.Bits(64, 8)} << 20) | (u64{negate_c} << 48));
break;
}
const u32 multiplier = insn.words[1];
if (insn.Bits(64, 8) == 255 && !negate_c && multiplier > 1 && !(multiplier & (multiplier - 1)) &&
(insn.words[2] & ~0x2ffU) == 0x078e0000) {
// IMAD.SHL by a power of two: one shift (also the form Eden's jump-table tracking
// expects between the clamp and the table load).
u32 shift = 0;
while ((1U << shift) != multiplier) ++shift;
emit(base(0x3848000000000000ULL) | (u64{insn.Bits(24, 8)} << 8) | (u64{shift} << 20));
break;
}
if (insn.words[1] != 48 || insn.words[2] != 0x078e02ff) {
if ((insn.words[2] & ~0xaffU) != 0x078e0000)
return fail(index, "native integer multiply mode");
u32 c = insn.Bits(64, 8);
if (negate_c && c != 255) {
emit(0x5c10000000000000ULL | predicate | 252 | (u64{255} << 8) | (u64{c} << 20) |
(u64{1} << 48));
c = 252;
}
multiply_low(insn.Bits(24, 8), insn.words[1], c, true);
break;
}
const u64 source = insn.Bits(24, 8);
emit(0x5c18000000000000ULL | predicate | 250 | (source << 8) | (source << 20) | (u64{1} << 39));
emit(base(0x3848000000000000ULL) | (u64{250} << 8) | (u64{4} << 20));
break;
}
case 0x806: { // VOTE: register ballot, ordinary predicate input and output.
if (insn.words[1] || insn.Bits(72, 2) > 2 || (insn.words[2] & ~0x078e0300U))
return fail(index, "native vote mode");
emit(base(0x50d8000000000000ULL) | (u64{insn.Bits(87, 4)} << 39) |
(u64{insn.Bits(81, 3)} << 45) | (u64{insn.Bits(72, 2)} << 48));
break;
}
case 0x886: { // VOTEU ballot and independent uniform predicate output.
const u32 output = insn.Bits(81, 3), target = insn.Bits(16, 8);
if ((target != 63 && uniform_register(target) == ~0U) || insn.words[1] || // (hookshot: any UR)
insn.Bits(72, 2) > 2 || native_predicate != 7)
return fail(index, "native uniform vote mode");
const u32 temporary = output == 7 ? 7 : 6 - output;
const u32 mask = 1U << temporary;
if (output != 7) emit(0x38e8000000070000ULL | 253 | (u64{255} << 8) | (u64{mask} << 20));
emit(0x50d8000000070000ULL | uniform_register(target) |
(u64{insn.Bits(87, 4)} << 39) | (u64{temporary} << 45) | (u64{insn.Bits(72, 2)} << 48));
if (output != 7) {
emit(0x38e8000000070000ULL | 239 | (u64{239} << 8) | (u64{mask} << 20));
emit(0x38f0000000070000ULL | (u64{253} << 8) | (u64{mask} << 20));
}
break;
}
case 0x3c2: { // R2UR: broadcast the first lane satisfying the execution predicate.
const u32 target = uniform_register(static_cast<u32>(dest));
if (target == ~0U || target == 255 || insn.words[1] || insn.words[2] != 0x000e0000)
return fail(index, "native register-to-uniform mode");
save_predicate();
emit(0x50d8000000070000ULL | 250 | (u64{native_predicate} << 39) |
(u64{6} << 45) | (u64{1} << 48));
emit(0x5c10000000070000ULL | 251 | (u64{250} << 8) | (u64{255} << 20) | (u64{1} << 49));
emit(0x5c40000000070000ULL | 250 | (u64{250} << 8) | (u64{251} << 20) | (u64{7} << 48));
emit(0x5c30000000070000ULL | 250 | (u64{250} << 20));
emit(0xef10000000070000ULL | 251 | (u64{insn.Bits(24, 8)} << 8) |
(u64{250} << 20) | (u64{1} << 29) | (u64{31} << 34) | (u64{7} << 48));
if (native_predicate == 7) {
// Every lane executing it is in the ballot, so no lane-less fallback can be taken.
// Leaving the old value selectable would make it a source of the result for the
// texture and global memory tracking (a handle traced to the uniform's last load).
emit(0x5c98000000070000ULL | target | (u64{15} << 39) | (u64{251} << 20));
} else {
emit(0x5ca0000000070000ULL | target | (u64{251} << 8) |
(u64{target} << 20) | (u64{6} << 39));
}
restore_predicate();
break;
}
case 0x81c: { // PLOP3.LUT; bit 67 makes the third source a uniform predicate.
const u32 target = insn.Bits(81, 3);
const u32 lut = insn.Bits(64, 3) | (insn.Bits(72, 5) << 3);
if (target == 7 || insn.Bits(84, 3) != 7 ||
insn.Bits(16, 8) || insn.words[1])
return fail(index, "native predicate LUT mode");
if (!insn.Bits(67, 1) && (lut == 0xfe || lut == 0xa8)) {
emit(0x5090000000000000ULL | predicate | (u64{target} << 3) | 7 |
(u64{insn.Bits(87, 4)} << 12) | (u64{1} << 24) |
(u64{insn.Bits(77, 4)} << 29) | (u64{insn.Bits(68, 4)} << 39) |
(u64{lut == 0xfe} << 45));
break;
}
if (!insn.Bits(67, 1) && (lut == 0x80 ||
(lut == 8 && insn.Bits(68, 4) == 7 && insn.Bits(77, 4) == 7))) {
emit(0x5090000000000000ULL | predicate | (u64{target} << 3) | 7 |
(u64{insn.Bits(87, 4) ^ (lut == 8 ? 8U : 0U)} << 12) | (u64{insn.Bits(77, 4)} << 29) |
(u64{insn.Bits(68, 4)} << 39));
break;
}
predicate_lut(lut, insn.Bits(87, 4), insn.Bits(77, 4), insn.Bits(68, 4), target,
insn.Bits(67, 1) != 0);
break;
}
case 0x89c: { // UPLOP3.LUT: load the uniform predicates into P0-P6, combine, merge back.
const u32 target = insn.Bits(81, 3);
const u32 lut = insn.Bits(64, 3) | (insn.Bits(72, 5) << 3);
if (insn.Bits(84, 3) != 7 || insn.Bits(67, 1) || insn.Bits(16, 8) || insn.words[1])
return fail(index, "native uniform predicate LUT mode");
if (target == 7) break;