-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
1966 lines (1750 loc) · 74.8 KB
/
main.py
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
import math
import glob
import os
import json
import markdown
import re
import csv
# dirty way to reduce code
cur_simd = "lsx"
cur_vlen = 128
# read latency & throughput
cpus = ["3A6000", "3C5000"]
measure = {"3A6000": {}, "3C5000": {}}
for cpu in cpus:
with open(f"code/measure-{cpu}.csv", newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
latency = []
for part in row["latency"].split("/"):
if part == "":
lat = "N/A"
else:
lat = float(part)
if abs((lat - round(lat)) / lat) < 0.02:
lat = round(lat)
latency.append(lat)
latency = sorted(list(set(latency)))
throughput_ipc = float(row["throughput(ipc)"])
if abs(throughput_ipc - round(throughput_ipc)) < 0.03:
throughput_ipc = round(throughput_ipc)
# handle small ipc better by 1/cpi
if throughput_ipc < 1.0:
throughput_cpi = float(row["throughput(cpi)"])
if abs(throughput_cpi - round(throughput_cpi)) < 0.03:
throughput_cpi = round(throughput_cpi)
throughput_ipc = f"{throughput_ipc}(1/{throughput_cpi})"
measure[cpu][row["name"]] = {
"latency": ", ".join(map(str, latency)),
"throughput(ipc)": throughput_ipc,
}
# read examples
examples = {}
for line in open('code/examples.md', 'r'):
if '(' in line:
name = line.split('(')[0].strip().split(' ')[-1]
if name not in examples:
examples[name] = []
line = line.strip()
expr, res = line.split(":")
expr = expr.replace(" COMMA ", ", ")
examples[name].append(f"{expr}\n={res}")
# depends on implementation of env.macro()
def my_macro(env):
def wrap(fn):
def vfn(*args):
global cur_simd, cur_vlen
cur_simd = "lsx"
cur_vlen = 128
return fn(*args)
env.macros[f"{fn.__name__}"] = vfn
def xvfn(*args):
global cur_simd, cur_vlen
cur_simd = "lasx"
cur_vlen = 256
return fn(*args)
env.macros[f"x{fn.__name__}"] = xvfn
return fn
return wrap
def define_env(env):
widths = {
"b": 8,
"bu": 8,
"h": 16,
"hu": 16,
"w": 32,
"wu": 32,
"d": 64,
"du": 64,
"q": 128,
}
signednesses = {
"b": "signed",
"bu": "unsigned",
"h": "signed",
"hu": "unsigned",
"w": "signed",
"wu": "unsigned",
"d": "signed",
"du": "unsigned",
"q": "signed",
"qu": "unsigned",
}
precisions = {"s": "single", "d": "double"}
fp_types = {"s": "__m128", "d": "__m128d"}
def instruction(intrinsic, instr, desc):
global cur_simd
# try to be smart
file_name = None
intrinsic_name = ""
for part in intrinsic.split(" "):
if part.startswith("__lsx_"):
file_name = part[6:]
intrinsic_name = part
elif part.startswith("__lasx_"):
file_name = part[7:]
intrinsic_name = part
if cur_simd == "lasx" and file_name[0] != "x":
file_name = "x" + file_name
instr = "x" + instr
intrinsic = intrinsic.replace("m128", "m256").replace("_lsx_", "_lasx_x")
# replace vr to xr in instr
instr = re.sub("\\bvr\\b", "xr", instr)
intrinsic_name = intrinsic_name.replace("__lsx_", "__lasx_x")
if not os.path.exists(f"code/{file_name}.h"):
file_name = instr.split(" ")[0].replace(".", "_")
code = open(f"code/{file_name}.h").read().strip()
code = code.strip()
if os.path.exists(f"code/{file_name}.cpp"):
tested = "Tested on real machine."
else:
tested = ""
if os.path.exists(f"docs/diagram/{file_name}.svg"):
diagram = f'![](../diagram/{file_name}.svg)'
else:
diagram = ""
global measure
global cpus
instr_name = instr.split(" ")[0].replace(".", "_")
show_cpus = []
latencies = []
throughputs = []
for cpu in cpus:
if instr_name in measure[cpu]:
show_cpus.append(cpu)
latencies.append(measure[cpu][instr_name]["latency"])
throughputs.append(measure[cpu][instr_name]["throughput(ipc)"])
if len(show_cpus) > 0:
latency_throughput = f"""
### Latency and Throughput
| CPU | Latency | Throughput (IPC) |
|-----|---------|------------------|
"""
for i in range(len(show_cpus)):
latency_throughput += (
f"| {show_cpus[i]} | {latencies[i]} | {throughputs[i]} |\n"
)
else:
latency_throughput = ""
if intrinsic_name in examples:
inner = "\n".join(examples[intrinsic_name])
examples_text = f"""
### Examples
```c++
{inner}
```
"""
else:
examples_text = ""
return f"""
## {intrinsic}
### Synopsis
```c++
{intrinsic}
#include <{cur_simd}intrin.h>
Instruction: {instr}
CPU Flags: {cur_simd.upper()}
```
### Description
{desc}
{diagram}
{examples_text}
### Operation
```c++
{code}
```
{tested}
{latency_throughput}
"""
@my_macro(env)
def vabsd(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vabsd_{name} (__m128i a, __m128i b)",
instr=f"vabsd.{name} vr, vr, vr",
desc=f"Compute absolute difference of {signedness} {width}-bit elements in `a` and `b`, save the result in `dst`.",
)
@my_macro(env)
def vadd(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vadd_{name} (__m128i a, __m128i b)",
instr=f"vadd.{name} vr, vr, vr",
desc=f"Add {width}-bit elements in `a` and `b`, save the result in `dst`.",
)
@my_macro(env)
def vadda(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vadda_{name} (__m128i a, __m128i b)",
instr=f"vadda.{name} vr, vr, vr",
desc=f"Add absolute of {width}-bit elements in `a` and `b`, save the result in `dst`.",
)
@my_macro(env)
def vaddi(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vaddi_{name} (__m128i a, imm0_31 imm)",
instr=f"vaddi.{name} vr, vr, imm",
desc=f"Add {width}-bit elements in `a` and `imm`, save the result in `dst`.",
)
@my_macro(env)
def vsubi(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vsubi_{name} (__m128i a, imm0_31 imm)",
instr=f"vsubi.{name} vr, vr, imm",
desc=f"Subtract {width}-bit elements in `a` by `imm`, save the result in `dst`.",
)
@my_macro(env)
def vadd_mul_sub_w_ev_od(op, desc, even_odd, wide, narrow, narrow2=None):
wide_width = widths[wide]
if narrow2 is None:
narrow2 = narrow
inst_suffix = ""
intrinsic_suffix = ""
else:
inst_suffix = f".{narrow2}"
intrinsic_suffix = f"_{narrow2}"
narrow_width = widths[narrow]
signedness = signednesses[narrow]
signedness2 = signednesses[narrow2]
if even_odd == "even":
suffix = "ev"
else:
suffix = "od"
return instruction(
intrinsic=f"__m128i __lsx_v{op}w{suffix}_{wide}_{narrow}{intrinsic_suffix} (__m128i a, __m128i b)",
instr=f"v{op}w{suffix}.{wide}.{narrow}{inst_suffix} vr, vr, vr",
desc=f"{desc} {even_odd}-positioned {signedness} {narrow_width}-bit elements in `a` and {signedness2} elements in `b`, save the {wide_width}-bit result in `dst`.",
)
@my_macro(env)
def vaddwev(wide, narrow, narrow2=None):
return vadd_mul_sub_w_ev_od("add", "Add", "even", wide, narrow, narrow2)
@my_macro(env)
def vmulwev(wide, narrow, narrow2=None):
return vadd_mul_sub_w_ev_od("mul", "Multiply", "even", wide, narrow, narrow2)
@my_macro(env)
def vsubwev(wide, narrow):
return vadd_mul_sub_w_ev_od("sub", "Subtract", "even", wide, narrow)
@my_macro(env)
def vaddwod(wide, narrow, narrow2=None):
return vadd_mul_sub_w_ev_od("add", "Add", "odd", wide, narrow, narrow2)
@my_macro(env)
def vmulwod(wide, narrow, narrow2=None):
return vadd_mul_sub_w_ev_od("mul", "Multiply", "odd", wide, narrow, narrow2)
@my_macro(env)
def vsubwod(wide, narrow):
return vadd_mul_sub_w_ev_od("sub", "Subtract", "odd", wide, narrow)
@my_macro(env)
def vavg(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vavg_{name} (__m128i a, __m128i b)",
instr=f"vavg.{name} vr, vr, vr",
desc=f"Compute the average (rounded towards negative infinity) of {signedness} {width}-bit elements in `a` and `b`, save the result in `dst`.",
)
@my_macro(env)
def vavgr(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vavgr_{name} (__m128i a, __m128i b)",
instr=f"vavgr.{name} vr, vr, vr",
desc=f"Compute the average (rounded towards positive infinity) of {signedness} {width}-bit elements in `a` and `b`, save the result in `dst`.",
)
@my_macro(env)
def vbitclr(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vbitclr_{name} (__m128i a, __m128i b)",
instr=f"vbitclr.{name} vr, vr, vr",
desc=f"Clear the bit specified by elements in `b` from {width}-bit elements in `a`, save the result in `dst`.",
)
@my_macro(env)
def vbitclri(name):
width = widths[name]
imm_upper = width - 1
return instruction(
intrinsic=f"__m128i __lsx_vbitclri_{name} (__m128i a, imm0_{imm_upper} imm)",
instr=f"vbitclri.{name} vr, vr, imm",
desc=f"Clear the bit specified by `imm` from {width}-bit elements in `a`, save the result in `dst`.",
)
@my_macro(env)
def vbitset(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vbitset_{name} (__m128i a, __m128i b)",
instr=f"vbitset.{name} vr, vr, vr",
desc=f"Set the bit specified by elements in `b` from {width}-bit elements in `a`, save the result in `dst`.",
)
@my_macro(env)
def vbitseti(name):
width = widths[name]
imm_upper = width - 1
return instruction(
intrinsic=f"__m128i __lsx_vbitseti_{name} (__m128i a, imm0_{imm_upper} imm)",
instr=f"vbitseti.{name} vr, vr, imm",
desc=f"Set the bit specified by `imm` from {width}-bit elements in `a`, save the result in `dst`.",
)
@my_macro(env)
def vbitrev(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vbitrev_{name} (__m128i a, __m128i b)",
instr=f"vbitrev.{name} vr, vr, vr",
desc=f"Toggle the bit specified by elements in `b` from {width}-bit elements in `a`, save the result in `dst`.",
)
@my_macro(env)
def vbitrevi(name):
width = widths[name]
imm_upper = width - 1
return instruction(
intrinsic=f"__m128i __lsx_vbitrevi_{name} (__m128i a, imm0_{imm_upper} imm)",
instr=f"vbitrevi.{name} vr, vr, imm",
desc=f"Toggle the bit specified by `imm` from {width}-bit elements in `a`, save the result in `dst`.",
)
@my_macro(env)
def vclo(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vclo_{name} (__m128i a)",
instr=f"vclo.{name} vr, vr",
desc=f"Count leading ones of {width}-bit elements in `a`.",
)
@my_macro(env)
def vclz(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vclz_{name} (__m128i a)",
instr=f"vclz.{name} vr, vr",
desc=f"Count leading zeros of {width}-bit elements in `a`.",
)
@my_macro(env)
def vdiv(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vdiv_{name} (__m128i a, __m128i b)",
instr=f"vdiv.{name} vr, vr, vr",
desc=f"Divide {signedness} {width}-bit elements in `a` by elements in `b`.",
)
@my_macro(env)
def vmod(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vmod_{name} (__m128i a, __m128i b)",
instr=f"vmod.{name} vr, vr, vr",
desc=f"Modulo residual {signedness} {width}-bit elements in `a` by elements in `b`.",
)
@my_macro(env)
def vexth(name, name2):
width = widths[name[0]]
width2 = widths[name2[0]]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vexth_{name}_{name2} (__m128i a)",
instr=f"vexth.{name}.{name2} vr, vr",
desc=f"Extend {signedness} {width2}-bit elements in the higher half of `a` to {width}-bit.",
)
@my_macro(env)
def vextl(name, name2):
width = widths[name[0]]
width2 = widths[name2[0]]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vextl_{name}_{name2} (__m128i a)",
instr=f"vextl.{name}.{name2} vr, vr",
desc=f"Extend {signedness} {width2}-bit elements in the lower half of `a` to {width}-bit.",
)
@my_macro(env)
def vextrins(name):
width = widths[name[0]]
return instruction(
intrinsic=f"__m128i __lsx_vextrins_{name} (__m128i a, __m128i b, imm0_255 imm)",
instr=f"vextrins.{name} vr, vr, imm",
desc=f"Extract one {width}-bit element in `b` and insert it to `a` according to `imm`.",
)
@my_macro(env)
def vfcmp(cond):
if cond[0] == "c":
trap = "Do not trap for QNaN."
else:
trap = "Trap for QNaN."
desc = {
"af": "AF(Always False)",
"un": "UN(Unordered)",
"eq": "EQ(Equal)",
"ueq": "UEQ(Unordered or Equal)",
"lt": "LT(Less than)",
"ult": "ULT(Unordered or Less than)",
"le": "LE(Less than or Equal)",
"ule": "ULE(Unordered, Less than or Equal)",
"ne": "NE(Not Equal)",
"or": "OR(Ordered)",
"une": "UNE(Unordered or Not Equal)",
}[cond[1:]]
return (
instruction(
intrinsic=f"__m128i __lsx_vfcmp_{cond}_s (__m128 a, __m128 b)",
instr=f"vfcmp.{cond}.s vr, vr, vr",
desc=f"Compare single precision elements in `a` and `b`, save the comparison result (all ones if {desc}, all zeros otherwise) into `dst`. {trap}",
)
+ "\n"
+ instruction(
intrinsic=f"__m128i __lsx_vfcmp_{cond}_d (__m128d a, __m128d b)",
instr=f"vfcmp.{cond}.d vr, vr, vr",
desc=f"Compare double precision elements in `a` and `b`, save the comparison result (all ones if {desc}, all zeros otherwise) into `dst`. {trap}",
)
)
@my_macro(env)
def vfmul(name):
precision = precisions[name]
fp_type = fp_types[name]
return instruction(
intrinsic=f"{fp_type} __lsx_vfmul_{name} ({fp_type} a, {fp_type} b)",
instr=f"vfmul.{name} vr, vr, vr",
desc=f"Multiply {precision} precision floating point elements in `a` and elements in `b`.",
)
@my_macro(env)
def vfdiv(name):
precision = precisions[name]
fp_type = fp_types[name]
return instruction(
intrinsic=f"{fp_type} __lsx_vfdiv_{name} ({fp_type} a, {fp_type} b)",
instr=f"vfdiv.{name} vr, vr, vr",
desc=f"Divide {precision} precision floating point elements in `a` by elements in `b`.",
)
@my_macro(env)
def vfadd(name):
precision = precisions[name]
fp_type = fp_types[name]
return instruction(
intrinsic=f"{fp_type} __lsx_vfadd_{name} ({fp_type} a, {fp_type} b)",
instr=f"vfadd.{name} vr, vr, vr",
desc=f"Add {precision} precision floating point elements in `a` to elements in `b`.",
)
@my_macro(env)
def vfsub(name):
precision = precisions[name]
fp_type = fp_types[name]
return instruction(
intrinsic=f"{fp_type} __lsx_vfsub_{name} ({fp_type} a, {fp_type} b)",
instr=f"vfsub.{name} vr, vr, vr",
desc=f"Subtract {precision} precision floating point elements in `a` by elements in `b`.",
)
@my_macro(env)
def vfmax(name):
precision = precisions[name]
fp_type = fp_types[name]
return instruction(
intrinsic=f"{fp_type} __lsx_vfmax_{name} ({fp_type} a, {fp_type} b)",
instr=f"vfmax.{name} vr, vr, vr",
desc=f"Compute maximum of {precision} precision floating point elements in `a` and `b`.",
)
@my_macro(env)
def vfmaxa(name):
precision = precisions[name]
fp_type = fp_types[name]
return instruction(
intrinsic=f"{fp_type} __lsx_vfmaxa_{name} ({fp_type} a, {fp_type} b)",
instr=f"vfmaxa.{name} vr, vr, vr",
desc=f"Compute maximum of {precision} precision floating point elements in `a` and `b` by magnitude.",
)
@my_macro(env)
def vfmin(name):
precision = precisions[name]
fp_type = fp_types[name]
return instruction(
intrinsic=f"{fp_type} __lsx_vfmin_{name} ({fp_type} a, {fp_type} b)",
instr=f"vfmax.{name} vr, vr, vr",
desc=f"Compute minimum of {precision} precision floating point elements in `a` and `b`.",
)
@my_macro(env)
def vfmina(name):
precision = precisions[name]
fp_type = fp_types[name]
return instruction(
intrinsic=f"{fp_type} __lsx_vfmina_{name} ({fp_type} a, {fp_type} b)",
instr=f"vfmina.{name} vr, vr, vr",
desc=f"Compute minimum of {precision} precision floating point elements in `a` and `b` by magnitude.",
)
@my_macro(env)
def vhaddw(name, name2):
width = widths[name[0]]
width2 = widths[name2[0]]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vhaddw_{name}_{name2} (__m128i a, __m128i b)",
instr=f"vhaddw.{name}.{name2} vr, vr, vr",
desc=f"Add odd-positioned {signedness} {width2}-bit elements in `a` to even-positioned {signedness} {width2}-bit elements in `b` to get {width}-bit result.",
)
@my_macro(env)
def vhsubw(name, name2):
width = widths[name[0]]
width2 = widths[name2[0]]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vhsubw_{name}_{name2} (__m128i a, __m128i b)",
instr=f"vhsubw.{name}.{name2} vr, vr, vr",
desc=f"Subtract odd-positioned {signedness} {width2}-bit elements in `a` by even-positioned {signedness} {width2}-bit elements in `b` to get {width}-bit result.",
)
@my_macro(env)
def vilvh(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vilvh_{name} (__m128i a, __m128i b)",
instr=f"vilvh.{name} vr, vr, vr",
desc=f"Interleave {width}-bit elements in higher half of `a` and `b`.",
)
@my_macro(env)
def vilvl(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vilvl_{name} (__m128i a, __m128i b)",
instr=f"vilvl.{name} vr, vr, vr",
desc=f"Interleave {width}-bit elements in lower half of `a` and `b`.",
)
@my_macro(env)
def vinsgr2vr(name):
global cur_vlen
width = widths[name]
imm_upper = cur_vlen // width - 1
if name == "d":
long = "long "
else:
long = ""
return instruction(
intrinsic=f"__m128i __lsx_vinsgr2vr_{name} (__m128i a, {long}int b, imm0_{imm_upper} imm)",
instr=f"vinsgr2vr.{name} vr, r, imm",
desc=f"Insert {width}-bit element into lane indexed `imm`.",
)
@env.macro
def xvinsve0(name):
width = widths[name]
return instruction(
intrinsic=f"__m256i __lasx_xvinsve0_{name} (__m256i a, __m256i b, imm0_{256 // width - 1} imm)",
instr=f"xvinsve0.{name} xr, xr, imm",
desc=f"Insert the first {width}-bit lane of `b` into lane indexed `imm` of `a`.",
)
@env.macro
def vext2xv(name, name2):
global cur_simd
cur_simd = "lsx" # avoid replacing vext2xv to xvext2xv
width = widths[name]
width2 = widths[name2]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m256i __lasx_vext2xv_{name}_{name2} (__m256i a)",
instr=f"vext2xv.{name}.{name2} xr, xr",
desc=f"Extend {signedness} {width2}-bit lane of `a` to {signedness} {width}-bit elements.",
)
@my_macro(env)
def vminmax(min_max, name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_v{min_max}_{name} (__m128i a, __m128i b)",
instr=f"v{min_max}.{name} vr, vr, vr",
desc=f"Compute elementwise {min_max}imum for {signedness} {width}-bit elements in `a` and `b`.",
)
@my_macro(env)
def vmin(name):
return vminmax("min", name)
@my_macro(env)
def vmax(name):
return vminmax("max", name)
@my_macro(env)
def vminmaxi(min_max, name):
width = widths[name]
signedness = signednesses[name]
if signedness == "unsigned":
imm_range = "0_31"
else:
imm_range = "_n16_15"
return instruction(
intrinsic=f"__m128i __lsx_v{min_max}i_{name} (__m128i a, imm{imm_range} imm)",
instr=f"v{min_max}i.{name} vr, vr, imm",
desc=f"Compute elementwise {min_max}imum for {signedness} {width}-bit elements in `a` and `imm`.",
)
@my_macro(env)
def vmini(name):
return vminmaxi("min", name)
@my_macro(env)
def vmaxi(name):
return vminmaxi("max", name)
@my_macro(env)
def vshuf_b():
name = "b"
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vshuf_b (__m128i a, __m128i b, __m128i c)",
instr=f"vshuf.{name} vr, vr, vr, vr",
desc=f"""
Shuffle bytes from `a` and `b` with indices from `c`.
Caveat: the indices are placed in `c`, while in other `vshuf` intrinsics, they are placed in `a`.
""",
)
@my_macro(env)
def vshuf_hwd(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vshuf_{name} (__m128i a, __m128i b, __m128i c)",
instr=f"vshuf.{name} vr, vr, vr",
desc=f"Shuffle {width}-bit elements in `b` and `c` with indices from `a`, save the result to `dst`.",
)
@my_macro(env)
def vldrepl(name):
width = widths[name]
imm_range = 2048 // (width // 8)
shift = int(math.log2(width // 8))
return instruction(
intrinsic=f"__m128i __lsx_vldrepl_{name} (void * addr, imm_n{imm_range}_{imm_range-1} offset)",
instr=f"vldrepl.{name} vr, r, imm",
desc=f"Read {width}-bit data from memory address `addr + (offset << {shift})`, replicate the data to all vector lanes and save into `dst`.",
)
@my_macro(env)
def vsub(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vsub_{name} (__m128i a, __m128i b)",
instr=f"vsub.{name} vr, vr, vr",
desc=f"Subtract {width}-bit elements in `a` and `b`, save the result in `dst`.",
)
@my_macro(env)
def vlogical(op):
return instruction(
intrinsic=f"__m128i __lsx_v{op}_v (__m128i a, __m128i b)",
instr=f"v{op}.v vr, vr, vr",
desc=f"Compute bitwise {op.upper()} between elements in `a` and `b`.",
)
@my_macro(env)
def vlogicali(op):
return instruction(
intrinsic=f"__m128i __lsx_v{op}i_b (__m128i a, imm0_255 imm)",
instr=f"v{op}i.b vr, vr, imm",
desc=f"Compute bitwise {op.upper()} between elements in `a` and `imm`.",
)
@my_macro(env)
def vmadd(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vmadd_{name} (__m128i a, __m128i b, __m128i c)",
instr=f"vmadd.{name} vr, vr, vr",
desc=f"Multiply {width}-bit elements in `b` and `c`, add to elements in `a`, save the result in `dst`.",
)
@my_macro(env)
def vmaddw_ev_od(name, even_odd, wide, narrow, narrow2=None):
wide_width = widths[wide]
if narrow2 is None:
narrow2 = narrow
inst_suffix = ""
intrinsic_suffix = ""
else:
inst_suffix = f".{narrow2}"
intrinsic_suffix = f"_{narrow2}"
narrow_width = widths[narrow]
signedness = signednesses[narrow]
signedness2 = signednesses[narrow2]
return instruction(
intrinsic=f"__m128i __lsx_vmaddw{name}_{wide}_{narrow}{intrinsic_suffix} (__m128i a, __m128i b, __m128i c)",
instr=f"vmaddw{name}.{wide}.{narrow}{inst_suffix} vr, vr, vr",
desc=f"Multiply {even_odd}-positioned {signedness} {narrow_width}-bit elements in `b` and {signedness2} elements in `c`, add to {wide_width}-bit elements in `a`.",
)
@my_macro(env)
def vmaddwev(wide, narrow, narrow2=None):
return vmaddw_ev_od("ev", "even", wide, narrow, narrow2)
@my_macro(env)
def vmaddwod(wide, narrow, narrow2=None):
return vmaddw_ev_od("od", "odd", wide, narrow, narrow2)
@my_macro(env)
def vmul(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vmul_{name} (__m128i a, __m128i b)",
instr=f"vmul.{name} vr, vr, vr",
desc=f"Multiply {width}-bit elements in `a` and `b`, save the result in `dst`.",
)
@my_macro(env)
def vmuh(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vmuh_{name} (__m128i a, __m128i b)",
instr=f"vmuh.{name} vr, vr, vr",
desc=f"Multiply {signedness} {width}-bit elements in `a` and `b`, save the high {width}-bit result in `dst`.",
)
@my_macro(env)
def vmsub(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vmsub_{name} (__m128i a, __m128i b, __m128i c)",
instr=f"vmsub.{name} vr, vr, vr",
desc=f"Multiply {width}-bit elements in `b` and `c`, negate and add elements in `a`, save the result in `dst`.",
)
@my_macro(env)
def vpcnt(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vpcnt_{name} (__m128i a)",
instr=f"vpcnt.{name} vr, vr",
desc=f"Count the number of ones (population, popcount) in {width}-bit elements in `a`.",
)
@my_macro(env)
def vstelm(name):
global cur_vlen
width = widths[name]
imm_upper = cur_vlen // width - 1
return instruction(
intrinsic=f"void __lsx_vstelm_{name} (__m128i data, void * addr, imm_n128_127 offset, imm0_{imm_upper} lane)",
instr=f"vstelm.{name} vr, r, imm, imm",
desc=f"Store the {width}-bit element in `data` specified by `lane` to memory address `addr + offset`.",
)
@my_macro(env)
def vseq(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vseq_{name} (__m128i a, __m128i b)",
instr=f"vseq.{name} vr, vr, vr",
desc=f"Compare the {width}-bit elements in `a` and `b`, store all-ones to `dst` if equal, zero otherwise.",
)
@my_macro(env)
def vseqi(name):
width = widths[name]
return instruction(
intrinsic=f"__m128i __lsx_vseqi_{name} (__m128i a, imm_n16_15 imm)",
instr=f"vseqi.{name} vr, vr, imm",
desc=f"Compare the {width}-bit elements in `a` and `imm`, store all-ones to `dst` if equal, zero otherwise.",
)
@my_macro(env)
def vslt(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vslt_{name} (__m128i a, __m128i b)",
instr=f"vslt.{name} vr, vr, vr",
desc=f"Compare the {signedness} {width}-bit elements in `a` and `b`, store all-ones to `dst` if corresponding element in `a` is less than `b`, zero otherwise.",
)
@my_macro(env)
def vslti(name):
width = widths[name]
signedness = signednesses[name]
if signedness == "signed":
imm_range = "imm_n16_15"
else:
imm_range = "imm0_31"
return instruction(
intrinsic=f"__m128i __lsx_vslti_{name} (__m128i a, {imm_range} imm)",
instr=f"vslti.{name} vr, vr, imm",
desc=f"Compare the {signedness} {width}-bit elements in `a` and `imm`, store all-ones to `dst` if corresponding element in `a` is less than `b`, zero otherwise.",
)
@my_macro(env)
def vsle(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vsle_{name} (__m128i a, __m128i b)",
instr=f"vsle.{name} vr, vr, vr",
desc=f"Compare the {signedness} {width}-bit elements in `a` and `b`, store all-ones to `dst` if corresponding element in `a` is less than or equal `b`, zero otherwise.",
)
@my_macro(env)
def vslei(name):
width = widths[name]
signedness = signednesses[name]
if signedness == "signed":
imm_range = "imm_n16_15"
else:
imm_range = "imm0_31"
return instruction(
intrinsic=f"__m128i __lsx_vslei_{name} (__m128i a, {imm_range} imm)",
instr=f"vslei.{name} vr, vr, imm",
desc=f"Compare the {signedness} {width}-bit elements in `a` and `b`, store all-ones to `dst` if corresponding element in `a` is less than or equal `b`, zero otherwise.",
)
@my_macro(env)
def vsadd(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vsadd_{name} (__m128i a, __m128i b)",
instr=f"vsadd.{name} vr, vr, vr",
desc=f"Saturing add the {signedness} {width}-bit elements in `a` and `b`, store the result to `dst`.",
)
@my_macro(env)
def vssub(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vssub_{name} (__m128i a, __m128i b)",
instr=f"vssub.{name} vr, vr, vr",
desc=f"Saturing subtract the {signedness} {width}-bit elements in `a` and `b`, store the result to `dst`.",
)
@my_macro(env)
def vsll(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vsll_{name} (__m128i a, __m128i b)",
instr=f"vsll.{name} vr, vr, vr",
desc=f"Logical left shift the unsigned {width}-bit elements in `a` by elements in `b`, store the result to `dst`.",
)
@my_macro(env)
def vslli(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vslli_{name} (__m128i a, imm0_{width-1} imm)",
instr=f"vslli.{name} vr, vr, imm",
desc=f"Logical left shift the unsigned {width}-bit elements in `a` by `imm`, store the result to `dst`.",
)
@my_macro(env)
def vsrl(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vsrl_{name} (__m128i a, __m128i b)",
instr=f"vsrl.{name} vr, vr, vr",
desc=f"Logical right shift the unsigned {width}-bit elements in `a` by elements in `b`, store the result to `dst`.",
)
@my_macro(env)
def vsrli(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vsrli_{name} (__m128i a, imm0_{width-1} imm)",
instr=f"vsrli.{name} vr, vr, imm",
desc=f"Logical right shift the unsigned {width}-bit elements in `a` by `imm`, store the result to `dst`.",
)
@my_macro(env)
def vsra(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vsra_{name} (__m128i a, __m128i b)",
instr=f"vsra.{name} vr, vr, vr",
desc=f"Arithmetic right shift the signed {width}-bit elements in `a` by elements in `b`, store the result to `dst`.",
)
@my_macro(env)
def vsrai(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vsrai_{name} (__m128i a, imm0_{width-1} imm)",
instr=f"vsrai.{name} vr, vr, imm",
desc=f"Arithmetic right shift the signed {width}-bit elements in `a` by `imm`, store the result to `dst`.",
)
@my_macro(env)
def vrotr(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vrotr_{name} (__m128i a, __m128i b)",
instr=f"vrotr.{name} vr, vr, vr",
desc=f"Rotate right the unsigned {width}-bit elements in `a` by elements in `b`, store the result to `dst`.",
)
@my_macro(env)
def vrotri(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vrotri_{name} (__m128i a, imm0_{width-1} imm)",
instr=f"vrotri.{name} vr, vr, imm",
desc=f"Rotate right the unsigned {width}-bit elements in `a` by `imm`, store the result to `dst`.",
)
@my_macro(env)
def vsrlr(name):
width = widths[name]
signedness = signednesses[name]
return instruction(
intrinsic=f"__m128i __lsx_vsrlr_{name} (__m128i a, __m128i b)",