forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRiscVEmitter.cpp
More file actions
4867 lines (4021 loc) · 184 KB
/
RiscVEmitter.cpp
File metadata and controls
4867 lines (4021 loc) · 184 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
// Copyright (c) 2022- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "ppsspp_config.h"
#include <algorithm>
#include <cstring>
#if PPSSPP_ARCH(RISCV64) && PPSSPP_PLATFORM(LINUX)
#include <sys/cachectl.h>
#endif
#include "Common/BitScan.h"
#include "Common/CPUDetect.h"
#include "Common/RiscVEmitter.h"
namespace RiscVGen {
static inline bool SupportsCompressed(char zcx = '\0') {
if (!cpu_info.RiscV_C)
return false;
switch (zcx) {
case 'b': return cpu_info.RiscV_Zcb;
case '\0': return true;
default: return false;
}
}
static inline uint8_t BitsSupported() {
return cpu_info.OS64bit ? 64 : 32;
}
static inline uint8_t FloatBitsSupported() {
if (cpu_info.RiscV_D)
return 64;
if (cpu_info.RiscV_F)
return 32;
return 0;
}
static inline bool SupportsMulDiv(bool allowZmmul = false) {
// TODO allowZmmul?
return cpu_info.RiscV_M;
}
static inline bool SupportsAtomic() {
return cpu_info.RiscV_A;
}
static inline bool SupportsZicsr() {
return cpu_info.RiscV_Zicsr;
}
static inline bool SupportsVector() {
return cpu_info.RiscV_V;
}
static inline bool SupportsVectorBitmanip(char zvxb) {
switch (zvxb) {
case 'b': return cpu_info.RiscV_Zvbb;
case 'k': return cpu_info.RiscV_Zvkb;
default: return false;
}
}
static inline bool SupportsBitmanip(char zbx) {
switch (zbx) {
case 'a': return cpu_info.RiscV_Zba;
case 'b': return cpu_info.RiscV_Zbb;
case 'c': return cpu_info.RiscV_Zbc;
case 's': return cpu_info.RiscV_Zbs;
default: return false;
}
}
static inline bool SupportsIntConditional() {
return cpu_info.RiscV_Zicond;
}
static inline bool SupportsFloatHalf(bool allowMin = false) {
return cpu_info.RiscV_Zfh || (cpu_info.RiscV_Zfhmin && allowMin);
}
static inline bool SupportsFloatExtra() {
return cpu_info.RiscV_Zfa;
}
enum class Opcode32 {
// Note: invalid, just used for FixupBranch.
ZERO = 0b0000000,
LOAD = 0b0000011,
LOAD_FP = 0b0000111,
MISC_MEM = 0b0001111,
OP_IMM = 0b0010011,
AUIPC = 0b0010111,
OP_IMM_32 = 0b0011011,
STORE = 0b0100011,
STORE_FP = 0b0100111,
AMO = 0b0101111,
OP = 0b0110011,
LUI = 0b0110111,
OP_32 = 0b0111011,
FMADD = 0b1000011,
FMSUB = 0b1000111,
FNMSUB = 0b1001011,
FNMADD = 0b1001111,
OP_FP = 0b1010011,
OP_V = 0b1010111,
BRANCH = 0b1100011,
JALR = 0b1100111,
JAL = 0b1101111,
System = 0b1110011,
};
enum class Opcode16 {
C0 = 0b00,
C1 = 0b01,
C2 = 0b10,
};
enum class Funct3 {
// Note: invalid, just used for FixupBranch.
ZERO = 0b000,
PRIV = 0b000,
FENCE = 0b000,
FENCE_I = 0b001,
BEQ = 0b000,
BNE = 0b001,
BLT = 0b100,
BGE = 0b101,
BLTU = 0b110,
BGEU = 0b111,
LS_B = 0b000,
LS_H = 0b001,
LS_W = 0b010,
LS_D = 0b011,
LS_BU = 0b100,
LS_HU = 0b101,
LS_WU = 0b110,
ADD = 0b000,
SLL = 0b001,
SLT = 0b010,
SLTU = 0b011,
XOR = 0b100,
SRL = 0b101,
OR = 0b110,
AND = 0b111,
MUL = 0b000,
MULH = 0b001,
MULHSU = 0b010,
MULHU = 0b011,
DIV = 0b100,
DIVU = 0b101,
REM = 0b110,
REMU = 0b111,
FSGNJ = 0b000,
FSGNJN = 0b001,
FSGNJX = 0b010,
FMIN = 0b000,
FMAX = 0b001,
FMINM = 0b010,
FMAXM = 0b011,
FMV = 0b000,
FCLASS = 0b001,
FLE = 0b000,
FLT = 0b001,
FEQ = 0b010,
CSRRW = 0b001,
CSRRS = 0b010,
CSRRC = 0b011,
CSRRWI = 0b101,
CSRRSI = 0b110,
CSRRCI = 0b111,
OPIVV = 0b000,
OPFVV = 0b001,
OPMVV = 0b010,
OPIVI = 0b011,
OPIVX = 0b100,
OPFVF = 0b101,
OPMVX = 0b110,
OPCFG = 0b111,
VLS_8 = 0b000,
VLS_16 = 0b101,
VLS_32 = 0b110,
VLS_64 = 0b111,
CLMUL = 0b001,
CLMULR = 0b010,
CLMULH = 0b011,
MIN = 0b100,
MINU = 0b101,
MAX = 0b110,
MAXU = 0b111,
SH1ADD = 0b010,
SH2ADD = 0b100,
SH3ADD = 0b110,
COUNT_SEXT_ROL = 0b001,
ZEXT = 0b100,
ROR = 0b101,
BSET = 0b001,
BEXT = 0b101,
CZERO_EQZ = 0b101,
CZERO_NEZ = 0b111,
C_ADDI4SPN = 0b000,
C_FLD = 0b001,
C_LW = 0b010,
C_FLW = 0b011,
C_LD = 0b011,
C_FSD = 0b101,
C_SW = 0b110,
C_FSW = 0b111,
C_SD = 0b111,
C_ADDI = 0b000,
C_JAL = 0b001,
C_ADDIW = 0b001,
C_LI = 0b010,
C_LUI = 0b011,
C_ARITH = 0b100,
C_J = 0b101,
C_BEQZ = 0b110,
C_BNEZ = 0b111,
C_SLLI = 0b000,
C_FLDSP = 0b001,
C_LWSP = 0b010,
C_FLWSP = 0b011,
C_LDSP = 0b011,
C_ADD = 0b100,
C_FSDSP = 0b101,
C_SWSP = 0b110,
C_FSWSP = 0b111,
C_SDSP = 0b111,
};
enum class Funct2 {
S = 0b00,
D = 0b01,
H = 0b10,
Q = 0b11,
C_SRLI = 0b00,
C_SRAI = 0b01,
C_ANDI = 0b10,
C_REGARITH = 0b11,
C_SUB = 0b00,
C_XOR = 0b01,
C_OR = 0b10,
C_AND = 0b11,
C_SUBW = 0b00,
C_ADDW = 0b01,
C_MUL = 0b10,
};
enum class Funct7 {
ZERO = 0b0000000,
SUB = 0b0100000,
SRA = 0b0100000,
MULDIV = 0b0000001,
ADDUW_ZEXT = 0b0000100,
MINMAX_CLMUL = 0b0000101,
CZERO = 0b0000111,
SH_ADD = 0b0010000,
BSET_ORC = 0b0010100,
NOT = 0b0100000,
BCLREXT = 0b0100100,
COUNT_SEXT_ROT = 0b0110000,
BINV_REV = 0b0110100,
};
enum class Funct5 {
AMOADD = 0b00000,
AMOSWAP = 0b00001,
LR = 0b00010,
SC = 0b00011,
AMOXOR = 0b00100,
AMOAND = 0b01100,
AMOOR = 0b01000,
AMOMIN = 0b10000,
AMOMAX = 0b10100,
AMOMINU = 0b11000,
AMOMAXU = 0b11100,
FADD = 0b00000,
FSUB = 0b00001,
FMUL = 0b00010,
FDIV = 0b00011,
FSGNJ = 0b00100,
FMINMAX = 0b00101,
FCVT_SZ = 0b01000,
FSQRT = 0b01011,
FCMP = 0b10100,
FCVT_TOX = 0b11000,
FCVT_FROMX = 0b11010,
FMV_TOX = 0b11100,
FMV_FROMX = 0b11110,
VZEXT_VF8 = 0b00010,
VSEXT_VF8 = 0b00011,
VZEXT_VF4 = 0b00100,
VSEXT_VF4 = 0b00101,
VZEXT_VF2 = 0b00110,
VSEXT_VF2 = 0b00111,
VFSQRT = 0b00000,
VFRSQRT7 = 0b00100,
VFREC7 = 0b00101,
VFCLASS = 0b10000,
VFCVT_XU_F = 0b00000,
VFCVT_X_F = 0b00001,
VFCVT_F_XU = 0b00010,
VFCVT_F_X = 0b00011,
VFCVT_RTZ_XU_F = 0b00110,
VFCVT_RTZ_X_F = 0b00111,
VFWCVT_XU_F = 0b01000,
VFWCVT_X_F = 0b01001,
VFWCVT_F_XU = 0b01010,
VFWCVT_F_X = 0b01011,
VFWCVT_F_F = 0b01100,
VFWCVT_RTZ_XU_F = 0b01110,
VFWCVT_RTZ_X_F = 0b01111,
VFNCVT_XU_F = 0b10000,
VFNCVT_X_F = 0b10001,
VFNCVT_F_XU = 0b10010,
VFNCVT_F_X = 0b10011,
VFNCVT_F_F = 0b10100,
VFNCVT_ROD_F_F = 0b10101,
VFNCVT_RTZ_XU_F = 0b10110,
VFNCVT_RTZ_X_F = 0b10111,
VMV_S = 0b00000,
VBREV8 = 0b01000,
VREV8 = 0b01001,
VBREV = 0b01010,
VCLZ = 0b01100,
VCTZ = 0b01101,
VCPOP_V = 0b01110,
VCPOP = 0b10000,
VFIRST = 0b10001,
VMSBF = 0b00001,
VMSOF = 0b00010,
VMSIF = 0b00011,
VIOTA = 0b10000,
VID = 0b10001,
CLZ = 0b00000,
CTZ = 0b00001,
CPOP = 0b00010,
SEXT_B = 0b00100,
SEXT_H = 0b00101,
ORC_B = 0b00111,
C_ZEXT_B = 0b11000,
C_SEXT_B = 0b11001,
C_ZEXT_H = 0b11010,
C_SEXT_H = 0b11011,
C_ZEXT_W = 0b11100,
C_NOT = 0b11101,
};
enum class Funct4 {
C_JR = 0b1000,
C_MV = 0b1000,
C_JALR = 0b1001,
C_ADD = 0b1001,
};
enum class Funct6 {
C_OP = 0b100011,
C_OP_32 = 0b100111,
C_LBU = 0b100000,
C_LH = 0b100001,
C_SB = 0b100010,
C_SH = 0b100011,
VADD = 0b000000,
VANDN = 0b000001,
VSUB = 0b000010,
VRSUB = 0b000011,
VMINU = 0b000100,
VMIN = 0b000101,
VMAXU = 0b000110,
VMAX = 0b000111,
VAND = 0b001001,
VOR = 0b001010,
VXOR = 0b001011,
VRGATHER = 0b001100,
VSLIDEUP = 0b001110,
VRGATHEREI16 = 0b001110,
VSLIDEDOWN = 0b001111,
VROR = 0b010100,
VROL = 0b010101,
VWSLL = 0b110101,
VREDSUM = 0b000000,
VREDAND = 0b000001,
VREDOR = 0b000010,
VREDXOR = 0b000011,
VAADDU = 0b001000,
VAADD = 0b001001,
VASUBU = 0b001010,
VASUB = 0b001011,
VFREDUSUM = 0b000001,
VFREDOSUM = 0b000011,
VFMIN = 0b000100,
VFMAX = 0b000110,
VFSGNJ = 0b001000,
VFSGNJN = 0b001001,
VFSGNJX = 0b001010,
VADC = 0b010000,
VMADC = 0b010001,
VSBC = 0b010010,
VMSBC = 0b010011,
VMV = 0b010111,
VMSEQ = 0b011000,
VMSNE = 0b011001,
VMSLTU = 0b011010,
VMSLT = 0b011011,
VMSLEU = 0b011100,
VMSLE = 0b011101,
VMSGTU = 0b011110,
VMSGT = 0b011111,
VMFEQ = 0b011000,
VMFLE = 0b011001,
VMFLT = 0b011011,
VMFNE = 0b011100,
VMFGT = 0b011101,
VMFGE = 0b011111,
VRWUNARY0 = 0b010000,
VFXUNARY0 = 0b010010,
VFXUNARY1 = 0b010011,
VMUNARY0 = 0b010100,
VCOMPRESS = 0b010111,
VMANDNOT = 0b011000,
VMAND = 0b011001,
VMOR = 0b011010,
VMXOR = 0b011011,
VMORNOT = 0b011100,
VMNAND = 0b011101,
VMNOR = 0b011110,
VMXNOR = 0b011111,
VSADDU = 0b100000,
VSADD = 0b100001,
VSSUBU = 0b100010,
VSSUB = 0b100011,
VSLL = 0b100101,
VSMUL_VMVR = 0b100111,
VSRL = 0b101000,
VSRA = 0b101001,
VSSRL = 0b101010,
VSSRA = 0b101011,
VNSRL = 0b101100,
VNSRA = 0b101101,
VNCLIPU = 0b101110,
VNCLIP = 0b101111,
VDIVU = 0b100000,
VDIV = 0b100001,
VREMU = 0b100010,
VREM = 0b100011,
VMULHU = 0b100100,
VMUL = 0b100101,
VMULHSU = 0b100110,
VMULH = 0b100111,
VMADD = 0b101001,
VNMSUB = 0b101011,
VMACC = 0b101101,
VNMSAC = 0b101111,
VFDIV = 0b100000,
VFRDIV = 0b100001,
VFMUL = 0b100100,
VFRSUB = 0b100111,
VFMADD = 0b101000,
VFNMADD = 0b101001,
VFMSUB = 0b101010,
VFNMSUB = 0b101011,
VFMACC = 0b101100,
VFNMACC = 0b101101,
VFMSAC = 0b101110,
VFNMSAC = 0b101111,
VWREDSUMU = 0b110000,
VWREDSUM = 0b110001,
VWADDU = 0b110000,
VWADD = 0b110001,
VWSUBU = 0b110010,
VWSUB = 0b110011,
VWADDU_W = 0b110100,
VWADD_W = 0b110101,
VWSUBU_W = 0b110110,
VWSUB_W = 0b110111,
VWMULU = 0b111000,
VWMULSU = 0b111010,
VWMUL = 0b111011,
VWMACCU = 0b111100,
VWMACC = 0b111101,
VWMACCUS = 0b111110,
VWMACCSU = 0b111111,
VFWADD = 0b110000,
VFWREDUSUM = 0b110001,
VFWSUB = 0b110010,
VFWREDOSUM = 0b110011,
VFWADD_W = 0b110100,
VFWSUB_W = 0b110110,
VFWMUL = 0b111000,
VFWMACC = 0b111100,
VFWNMACC = 0b111101,
VFWMSAC = 0b111110,
VFWNMSAC = 0b111111,
};
enum class Funct12 {
ECALL = 0b000000000000,
EBREAK = 0b000000000001,
};
enum class RiscCReg {
X8, X9, X10, X11, X12, X13, X14, X15,
};
enum class VLSUMop {
ELEMS = 0b00000,
REG = 0b01000,
MASK = 0b01011,
ELEMS_LOAD_FF = 0b10000,
};
enum class VMop {
UNIT = 0b00,
INDEXU = 0b01,
STRIDE = 0b10,
INDEXO = 0b11,
};
static inline RiscVReg DecodeReg(RiscVReg reg) { return (RiscVReg)(reg & 0x1F); }
static inline bool IsGPR(RiscVReg reg) { return (reg & ~0x1F) == 0; }
static inline bool IsFPR(RiscVReg reg) { return (reg & ~0x1F) == 0x20; }
static inline bool IsVPR(RiscVReg reg) { return (reg & ~0x1F) == 0x40; }
static inline bool CanCompress(RiscVReg reg) {
return (DecodeReg(reg) & 0x18) == 0x08;
}
static inline RiscCReg CompressReg(RiscVReg reg) {
_assert_msg_(CanCompress(reg), "Compressed reg must be between 8 and 15");
return (RiscCReg)(reg & 0x07);
}
static inline s32 SignReduce32(s32 v, int width) {
int shift = 32 - width;
return (v << shift) >> shift;
}
static inline s64 SignReduce64(s64 v, int width) {
int shift = 64 - width;
return (v << shift) >> shift;
}
// Compressed encodings have weird immediate bit order, trying to make it more readable.
static inline u8 ImmBit8(int imm, int bit) {
return (imm >> bit) & 1;
}
static inline u8 ImmBits8(int imm, int start, int sz) {
int mask = (1 << sz) - 1;
return (imm >> start) & mask;
}
static inline u16 ImmBit16(int imm, int bit) {
return (imm >> bit) & 1;
}
static inline u16 ImmBits16(int imm, int start, int sz) {
int mask = (1 << sz) - 1;
return (imm >> start) & mask;
}
static inline u32 ImmBit32(int imm, int bit) {
return (imm >> bit) & 1;
}
static inline u32 ImmBits32(int imm, int start, int sz) {
int mask = (1 << sz) - 1;
return (imm >> start) & mask;
}
static inline u32 EncodeR(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, Funct7 funct7) {
return (u32)opcode | ((u32)DecodeReg(rd) << 7) | ((u32)funct3 << 12) | ((u32)DecodeReg(rs1) << 15) | ((u32)DecodeReg(rs2) << 20) | ((u32)funct7 << 25);
}
static inline u32 EncodeGR(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, Funct7 funct7) {
_assert_msg_(IsGPR(rd), "R instruction rd must be GPR");
_assert_msg_(IsGPR(rs1), "R instruction rs1 must be GPR");
_assert_msg_(IsGPR(rs2), "R instruction rs2 must be GPR");
return EncodeR(opcode, rd, funct3, rs1, rs2, funct7);
}
static inline u32 EncodeGR(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, Funct5 funct5, Funct7 funct7) {
_assert_msg_(IsGPR(rd), "R instruction rd must be GPR");
_assert_msg_(IsGPR(rs1), "R instruction rs1 must be GPR");
return EncodeR(opcode, rd, funct3, rs1, (RiscVReg)funct5, funct7);
}
static inline u32 EncodeAtomicR(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, Atomic ordering, Funct5 funct5) {
u32 funct7 = ((u32)funct5 << 2) | (u32)ordering;
return EncodeGR(opcode, rd, funct3, rs1, rs2, (Funct7)funct7);
}
static inline u32 EncodeR4(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, Funct2 funct2, RiscVReg rs3) {
return (u32)opcode | ((u32)DecodeReg(rd) << 7) | ((u32)funct3 << 12) | ((u32)DecodeReg(rs1) << 15) | ((u32)DecodeReg(rs2) << 20) | ((u32)funct2 << 25) | ((u32)DecodeReg(rs3) << 27);
}
static inline u32 EncodeFR4(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, Funct2 funct2, RiscVReg rs3) {
_assert_msg_(IsFPR(rd), "R4 instruction rd must be FPR");
_assert_msg_(IsFPR(rs1), "R4 instruction rs1 must be FPR");
_assert_msg_(IsFPR(rs2), "R4 instruction rs2 must be FPR");
_assert_msg_(IsFPR(rs3), "R4 instruction rs3 must be FPR");
return EncodeR4(opcode, rd, funct3, rs1, rs2, funct2, rs3);
}
static inline u32 EncodeR(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, Funct2 funct2, Funct5 funct5) {
return EncodeR(opcode, rd, funct3, rs1, rs2, (Funct7)(((u32)funct5 << 2) | (u32)funct2));
}
static inline u32 EncodeFR(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, Funct2 funct2, Funct5 funct5) {
_assert_msg_(IsFPR(rd), "FR instruction rd must be FPR");
_assert_msg_(IsFPR(rs1), "FR instruction rs1 must be FPR");
_assert_msg_(IsFPR(rs2), "FR instruction rs2 must be FPR");
return EncodeR(opcode, rd, funct3, rs1, rs2, (Funct7)(((u32)funct5 << 2) | (u32)funct2));
}
static inline u32 EncodeI(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, s32 simm12) {
_assert_msg_(SignReduce32(simm12, 12) == simm12, "I immediate must be signed s11.0: %d", simm12);
return (u32)opcode | ((u32)DecodeReg(rd) << 7) | ((u32)funct3 << 12) | ((u32)DecodeReg(rs1) << 15) | ((u32)simm12 << 20);
}
static inline u32 EncodeGI(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, s32 simm12) {
_assert_msg_(IsGPR(rd), "I instruction rd must be GPR");
_assert_msg_(IsGPR(rs1), "I instruction rs1 must be GPR");
return EncodeI(opcode, rd, funct3, rs1, simm12);
}
static inline u32 EncodeGIShift(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, u32 shamt, Funct7 funct7) {
_assert_msg_(IsGPR(rd), "IShift instruction rd must be GPR");
_assert_msg_(IsGPR(rs1), "IShift instruction rs1 must be GPR");
_assert_msg_(shamt < BitsSupported(), "IShift instruction shift out of range %d", shamt);
// Low bits of funct7 must be 0 to allow for shift amounts.
return (u32)opcode | ((u32)DecodeReg(rd) << 7) | ((u32)funct3 << 12) | ((u32)DecodeReg(rs1) << 15) | ((u32)shamt << 20) | ((u32)funct7 << 25);
}
static inline u32 EncodeI(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, Funct12 funct12) {
return EncodeI(opcode, rd, funct3, rs1, SignReduce32((s32)funct12, 12));
}
static inline u32 EncodeGI(Opcode32 opcode, RiscVReg rd, Funct3 funct3, RiscVReg rs1, Funct12 funct12) {
_assert_msg_(IsGPR(rd), "I instruction rd must be GPR");
_assert_msg_(IsGPR(rs1), "I instruction rs1 must be GPR");
return EncodeI(opcode, rd, funct3, rs1, funct12);
}
static inline u32 EncodeS(Opcode32 opcode, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, s32 simm12) {
_assert_msg_(SignReduce32(simm12, 12) == simm12, "S immediate must be signed s11.0: %d", simm12);
u32 imm4_0 = ImmBits32(simm12, 0, 5);
u32 imm11_5 = ImmBits32(simm12, 5, 7);
return (u32)opcode | (imm4_0 << 7) | ((u32)funct3 << 12) | ((u32)DecodeReg(rs1) << 15) | ((u32)DecodeReg(rs2) << 20) | (imm11_5 << 25);
}
static inline u32 EncodeGS(Opcode32 opcode, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, s32 simm12) {
_assert_msg_(IsGPR(rs1), "S instruction rs1 must be GPR");
_assert_msg_(IsGPR(rs2), "S instruction rs2 must be GPR");
return EncodeS(opcode, funct3, rs1, rs2, simm12);
}
static inline u32 EncodeB(Opcode32 opcode, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, s32 simm13) {
_assert_msg_(SignReduce32(simm13, 13) == simm13, "B immediate must be signed s12.0: %d", simm13);
_assert_msg_((simm13 & 1) == 0, "B immediate must be even");
// This weird encoding scheme is to keep most bits the same as S, but keep sign at 31.
u32 imm4_1_11 = (ImmBits32(simm13, 1, 4) << 1) | ImmBit32(simm13, 11);
u32 imm12_10_5 = (ImmBit32(simm13, 12) << 6) | ImmBits32(simm13, 5, 6);
return (u32)opcode | ((u32)imm4_1_11 << 7) | ((u32)funct3 << 12) | ((u32)DecodeReg(rs1) << 15) | ((u32)DecodeReg(rs2) << 20) | ((u32)imm12_10_5 << 25);
}
static inline u32 EncodeGB(Opcode32 opcode, Funct3 funct3, RiscVReg rs1, RiscVReg rs2, s32 simm13) {
_assert_msg_(IsGPR(rs1), "B instruction rs1 must be GPR");
_assert_msg_(IsGPR(rs2), "B instruction rs2 must be GPR");
return EncodeB(opcode, funct3, rs1, rs2, simm13);
}
static inline u32 EncodeU(Opcode32 opcode, RiscVReg rd, s32 simm32) {
_assert_msg_((simm32 & 0x0FFF) == 0, "U immediate must not have lower 12 bits set");
return (u32)opcode | ((u32)DecodeReg(rd) << 7) | (u32)simm32;
}
static inline u32 EncodeGU(Opcode32 opcode, RiscVReg rd, s32 simm32) {
_assert_msg_(IsGPR(rd), "I instruction rd must be GPR");
return EncodeU(opcode, rd, simm32);
}
static inline u32 EncodeJ(Opcode32 opcode, RiscVReg rd, s32 simm21) {
_assert_msg_(SignReduce32(simm21, 21) == simm21, "J immediate must be signed s20.0: %d", simm21);
_assert_msg_((simm21 & 1) == 0, "J immediate must be even");
u32 imm11 = ImmBit32(simm21, 11);
u32 imm20 = ImmBit32(simm21, 20);
u32 imm10_1 = ImmBits32(simm21, 1, 10);
u32 imm19_12 = ImmBits32(simm21, 12, 8);
// This encoding scheme tries to keep the bits from B in the same places, plus sign.
u32 imm20_10_1_11_19_12 = (imm20 << 19) | (imm10_1 << 9) | (imm11 << 8) | imm19_12;
return (u32)opcode | ((u32)DecodeReg(rd) << 7) | (imm20_10_1_11_19_12 << 12);
}
static inline u32 EncodeGJ(Opcode32 opcode, RiscVReg rd, s32 simm21) {
_assert_msg_(IsGPR(rd), "J instruction rd must be GPR");
return EncodeJ(opcode, rd, simm21);
}
static inline u32 EncodeV(RiscVReg vd, Funct3 funct3, RiscVReg vs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(SupportsVector(), "V instruction not supported");
_assert_msg_(IsVPR(vs2), "V instruction vs2 must be VPR");
return EncodeR(Opcode32::OP_V, vd, funct3, vs1, vs2, (Funct7)(((s32)funct6 << 1) | (s32)vm));
}
static inline u32 EncodeVV(RiscVReg vd, Funct3 funct3, RiscVReg vs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(IsVPR(vd), "VV instruction vd must be VPR");
_assert_msg_(IsVPR(vs1), "VV instruction vs1 must be VPR");
return EncodeV(vd, funct3, vs1, vs2, vm, funct6);
}
static inline u32 EncodeIVV_M(RiscVReg vd, RiscVReg vs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
return EncodeVV(vd, Funct3::OPIVV, vs1, vs2, vm, funct6);
}
static inline u32 EncodeIVV(RiscVReg vd, RiscVReg vs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(vm != VUseMask::V0_T || vd != V0, "IVV instruction vd overlap with mask");
return EncodeIVV_M(vd, vs1, vs2, vm, funct6);
}
static inline u32 EncodeMVV_M(RiscVReg vd, RiscVReg vs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
return EncodeVV(vd, Funct3::OPMVV, vs1, vs2, vm, funct6);
}
static inline u32 EncodeMVV(RiscVReg vd, RiscVReg vs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(vm != VUseMask::V0_T || vd != V0, "MVV instruction vd overlap with mask");
return EncodeMVV_M(vd, vs1, vs2, vm, funct6);
}
static inline u32 EncodeFVV_M(RiscVReg vd, RiscVReg vs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(FloatBitsSupported() >= 32, "FVV instruction requires vector float support");
return EncodeVV(vd, Funct3::OPFVV, vs1, vs2, vm, funct6);
}
static inline u32 EncodeFVV(RiscVReg vd, RiscVReg vs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(vm != VUseMask::V0_T || vd != V0, "FVV instruction vd overlap with mask");
return EncodeFVV_M(vd, vs1, vs2, vm, funct6);
}
static inline u32 EncodeFVV(RiscVReg vd, Funct5 funct5, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(FloatBitsSupported() >= 32, "FVV instruction requires vector float support");
_assert_msg_(IsVPR(vd), "VV instruction vd must be VPR");
_assert_msg_(vm != VUseMask::V0_T || vd != V0, "FVV instruction vd overlap with mask");
return EncodeV(vd, Funct3::OPFVV, (RiscVReg)funct5, vs2, vm, funct6);
}
static inline u32 EncodeIVI_M(RiscVReg vd, s8 simm5, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(IsVPR(vd), "IVI instruction vd must be VPR");
_assert_msg_(SignReduce32(simm5, 5) == simm5, "VI immediate must be signed 5-bit: %d", simm5);
return EncodeV(vd, Funct3::OPIVI, (RiscVReg)(simm5 & 0x1F), vs2, vm, funct6);
}
static inline u32 EncodeIVI(RiscVReg vd, s8 simm5, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(vm != VUseMask::V0_T || vd != V0, "IVI instruction vd overlap with mask");
return EncodeIVI_M(vd, simm5, vs2, vm, funct6);
}
static inline u32 EncodeIVX_M(RiscVReg vd, RiscVReg rs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(IsVPR(vd), "IVX instruction vd must be VPR");
_assert_msg_(IsGPR(rs1), "IVX instruction rs1 must be GPR");
return EncodeV(vd, Funct3::OPIVX, rs1, vs2, vm, funct6);
}
static inline u32 EncodeIVX(RiscVReg vd, RiscVReg rs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(vm != VUseMask::V0_T || vd != V0, "IVX instruction vd overlap with mask");
return EncodeIVX_M(vd, rs1, vs2, vm, funct6);
}
static inline u32 EncodeMVX(RiscVReg vd, RiscVReg rs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(IsVPR(vd), "MVX instruction vd must be VPR");
_assert_msg_(IsGPR(rs1), "MVX instruction rs1 must be GPR");
return EncodeV(vd, Funct3::OPMVX, rs1, vs2, vm, funct6);
}
static inline u32 EncodeFVF_M(RiscVReg vd, RiscVReg rs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(FloatBitsSupported() >= 32, "FVF instruction requires vector float support");
_assert_msg_(IsVPR(vd), "FVF instruction vd must be VPR");
_assert_msg_(IsFPR(rs1), "FVF instruction rs1 must be FPR");
return EncodeV(vd, Funct3::OPFVF, rs1, vs2, vm, funct6);
}
static inline u32 EncodeFVF(RiscVReg vd, RiscVReg rs1, RiscVReg vs2, VUseMask vm, Funct6 funct6) {
_assert_msg_(vm != VUseMask::V0_T || vd != V0, "FVF instruction vd overlap with mask");
return EncodeFVF_M(vd, rs1, vs2, vm, funct6);
}
static inline u16 EncodeCR(Opcode16 op, RiscVReg rs2, RiscVReg rd, Funct4 funct4) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
return (u16)op | ((u16)rs2 << 2) | ((u16)rd << 7) | ((u16)funct4 << 12);
}
static inline u16 EncodeCI(Opcode16 op, u8 uimm6, RiscVReg rd, Funct3 funct3) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_(uimm6 <= 0x3F, "CI immediate overflow: %04x", uimm6);
u16 imm4_0 = ImmBits16(uimm6, 0, 5);
u16 imm5 = ImmBit16(uimm6, 5);
return (u16)op | (imm4_0 << 2) | ((u16)rd << 7) | (imm5 << 12) | ((u16)funct3 << 13);
}
static inline u16 EncodeCSS(Opcode16 op, RiscVReg rs2, u8 uimm6, Funct3 funct3) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_(uimm6 <= 0x3F, "CI immediate overflow: %04x", uimm6);
return (u16)op | ((u16)rs2 << 2) | ((u16)uimm6 << 7) | ((u16)funct3 << 13);
}
static inline u16 EncodeCIW(Opcode16 op, RiscCReg rd, u8 uimm8, Funct3 funct3) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
return (u16)op | ((u16)rd << 2) | ((u16)uimm8 << 5) | ((u16)funct3 << 13);
}
static inline u16 EncodeCL(Opcode16 op, RiscCReg rd, u8 uimm2, RiscCReg rs1, u8 uimm3, Funct3 funct3) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_(uimm2 <= 3, "CL immediate1 overflow: %04x", uimm2);
_assert_msg_(uimm3 <= 7, "CL immediate2 overflow: %04x", uimm3);
return (u16)op | ((u16)rd << 2) | ((u16)uimm2 << 5) | ((u16)rs1 << 7) | ((u16)uimm3 << 10) | ((u16)funct3 << 13);
}
static inline u16 EncodeCL8(Opcode16 op, RiscCReg rd, RiscCReg rs1, u8 uimm8, Funct3 funct3) {
_assert_msg_((uimm8 & 0xF8) == uimm8, "CL immediate must fit in 8 bits and be a multiple of 8: %d", (int)uimm8);
u8 imm7_6 = ImmBits8(uimm8, 6, 2);
u8 imm5_4_3 = ImmBits8(uimm8, 3, 3);
return EncodeCL(op, rd, imm7_6, rs1, imm5_4_3, funct3);
}
static inline u16 EncodeCL4(Opcode16 op, RiscCReg rd, RiscCReg rs1, u8 uimm7, Funct3 funct3) {
_assert_msg_((uimm7 & 0x7C) == uimm7, "CL immediate must fit in 7 bits and be a multiple of 4: %d", (int)uimm7);
u8 imm2_6 = (ImmBit8(uimm7, 2) << 1) | ImmBit8(uimm7, 6);
u8 imm5_4_3 = ImmBits8(uimm7, 3, 3);
return EncodeCL(op, rd, imm2_6, rs1, imm5_4_3, funct3);
}
static inline u16 EncodeCS(Opcode16 op, RiscCReg rs2, u8 uimm2, RiscCReg rs1, u8 uimm3, Funct3 funct3) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_(uimm2 <= 3, "CS immediate1 overflow: %04x", uimm2);
_assert_msg_(uimm3 <= 7, "CS immediate2 overflow: %04x", uimm3);
return (u16)op | ((u16)rs2 << 2) | ((u16)uimm2 << 5) | ((u16)rs1 << 7) | ((u16)uimm3 << 10) | ((u16)funct3 << 13);
}
static inline u16 EncodeCS8(Opcode16 op, RiscCReg rd, RiscCReg rs1, u8 uimm8, Funct3 funct3) {
_assert_msg_((uimm8 & 0xF8) == uimm8, "CS immediate must fit in 8 bits and be a multiple of 8: %d", (int)uimm8);
u8 imm7_6 = ImmBits8(uimm8, 6, 2);
u8 imm5_4_3 = ImmBits8(uimm8, 3, 3);
return EncodeCS(op, rd, imm7_6, rs1, imm5_4_3, funct3);
}
static inline u16 EncodeCS4(Opcode16 op, RiscCReg rd, RiscCReg rs1, u8 uimm7, Funct3 funct3) {
_assert_msg_((uimm7 & 0x7C) == uimm7, "CS immediate must fit in 7 bits and be a multiple of 4: %d", (int)uimm7);
u8 imm2_6 = (ImmBit8(uimm7, 2) << 1) | ImmBit8(uimm7, 6);
u8 imm5_4_3 = ImmBits8(uimm7, 3, 3);
return EncodeCS(op, rd, imm2_6, rs1, imm5_4_3, funct3);
}
static inline u16 EncodeCA(Opcode16 op, RiscCReg rs2, Funct2 funct2a, RiscCReg rd, Funct6 funct6) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
return (u16)op | ((u16)rs2 << 2) | ((u16)funct2a << 5) | ((u16)rd << 7) | ((u16)funct6 << 10);
}
static inline u16 EncodeCB(Opcode16 op, u8 uimm6, RiscCReg rd, Funct2 funct2, Funct3 funct3) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_(uimm6 <= 0x3F, "CI immediate overflow: %04x", uimm6);
u16 imm4_0 = ImmBits16(uimm6, 0, 5);
u16 imm5 = ImmBit16(uimm6, 5);
return (u16)op | (imm4_0 << 2) | ((u16)rd << 7) | ((u16)funct2 << 10) | (imm5 << 12) | ((u16)funct3 << 13);
}
static inline u16 EncodeCB(Opcode16 op, s32 simm9, RiscCReg rs1, Funct3 funct3) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_(SignReduce32(simm9, 9) == simm9, "CB immediate must be signed s8.0: %d", simm9);
_assert_msg_((simm9 & 1) == 0, "CB immediate must be even: %d", simm9);
u16 imm76_21_5 = (ImmBits16(simm9, 6, 2) << 3) | (ImmBits16(simm9, 1, 2) << 1) | ImmBit16(simm9, 5);
u16 imm8_43 = (ImmBit16(simm9, 8) << 2) | ImmBits16(simm9, 3, 2);
return (u16)op | (imm76_21_5 << 2) | ((u16)rs1 << 7) | (imm8_43 << 10) | ((u16)funct3 << 13);
}
static inline u16 EncodeCJ(Opcode16 op, s32 simm12, Funct3 funct3) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_(SignReduce32(simm12, 12) == simm12, "CJ immediate must be signed s11.0: %d", simm12);
_assert_msg_((simm12 & 1) == 0, "CJ immediate must be even: %d", simm12);
u16 imm7_3_2_1_5 = (ImmBit16(simm12, 7) << 4) | (ImmBits16(simm12, 1, 3) << 1) | ImmBit16(simm12, 5);
u16 imm9_8_10_6 = (ImmBits16(simm12, 8, 2) << 2) | (ImmBit16(simm12, 10) << 1) | ImmBit16(simm12, 6);
u16 imm11_4 = (ImmBit16(simm12, 11) << 1) | ImmBit16(simm12, 4);
u16 imm11_4_9_8_10_6_7_3_2_1_5 = (imm11_4 << 9) | (imm9_8_10_6 << 5) | imm7_3_2_1_5;
return (u16)op | (imm11_4_9_8_10_6_7_3_2_1_5 << 2) | ((u16)funct3 << 13);
}
static inline u16 EncodeCLB(Opcode16 op, RiscCReg rd, u8 uimm2, RiscCReg rs1, Funct6 funct6) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_((uimm2 & 3) == uimm2, "CLB immediate must be 2 bit: %d", uimm2);
return (u16)op | ((u16)rd << 2) | ((u16)uimm2 << 5) | ((u16)rs1 << 7) | ((u16)funct6 << 10);
}
static inline u16 EncodeCSB(Opcode16 op, RiscCReg rs2, u8 uimm2, RiscCReg rs1, Funct6 funct6) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_((uimm2 & 3) == uimm2, "CSB immediate must be 2 bit: %d", uimm2);
return (u16)op | ((u16)rs2 << 2) | ((u16)uimm2 << 5) | ((u16)rs1 << 7) | ((u16)funct6 << 10);
}
static inline u16 EncodeCLH(Opcode16 op, RiscCReg rd, u8 uimm1, bool funct1, RiscCReg rs1, Funct6 funct6) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_((uimm1 & 1) == uimm1, "CLH immediate must be 1 bit: %d", uimm1);
return (u16)op | ((u16)rd << 2) | ((u16)uimm1 << 5) | ((u16)funct1 << 6) | ((u16)rs1 << 7) | ((u16)funct6 << 10);
}
static inline u16 EncodeCSH(Opcode16 op, RiscCReg rs2, u8 uimm1, bool funct1, RiscCReg rs1, Funct6 funct6) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
_assert_msg_((uimm1 & 1) == uimm1, "CSH immediate must be 1 bit: %d", uimm1);
return (u16)op | ((u16)rs2 << 2) | ((u16)uimm1 << 5) | ((u16)funct1 << 6) | ((u16)rs1 << 7) | ((u16)funct6 << 10);
}
static inline u16 EncodeCU(Opcode16 op, Funct5 funct5, RiscCReg rd, Funct6 funct6) {
_assert_msg_(SupportsCompressed(), "Compressed instructions unsupported");
return (u16)op | ((u16)funct5 << 2) | ((u16)rd << 7) | ((u16)funct6 << 10);
}
static inline Funct3 BitsToFunct3(int bits, bool useFloat = false, bool allowHalfMin = false) {
int bitsSupported = useFloat ? FloatBitsSupported() : BitsSupported();
_assert_msg_(bitsSupported >= bits, "Cannot use funct3 width %d, only have %d", bits, bitsSupported);
switch (bits) {
case 16:
_assert_msg_(SupportsFloatHalf(allowHalfMin), "Cannot use width 16 without Zfh/Zfhmin");
return Funct3::LS_H;
case 32:
return Funct3::LS_W;
case 64:
return Funct3::LS_D;
default:
_assert_msg_(false, "Invalid funct3 width %d", bits);
return Funct3::LS_W;
}
}
static inline Funct2 BitsToFunct2(int bits, bool allowHalfMin = false) {
_assert_msg_(FloatBitsSupported() >= bits, "Cannot use funct2 width %d, only have %d", bits, FloatBitsSupported());
switch (bits) {
case 16:
_assert_msg_(SupportsFloatHalf(allowHalfMin), "Cannot use width 16 without Zfh/Zfhmin");
return Funct2::H;
case 32:
return Funct2::S;
case 64:
return Funct2::D;
case 128:
return Funct2::Q;
default:
_assert_msg_(false, "Invalid funct2 width %d", bits);