-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrices.wast
1008 lines (870 loc) · 32.7 KB
/
matrices.wast
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
(module
(import "dummy" "memory" (memory 1))
;; Scalar transpose a square matrix (contiguous layout) of 32-bit elements in place
;;
;; @param $ptr source and destination -- byte index
;; @param $sz size of the matrix (number of elements in a row)
(func $tr
(export "transpose_32")
(param $ptr i32)
(param $sz i32)
(local $i i32)
(local $j i32)
(local $src_ptr i32)
(local $dst_ptr i32)
(set_local $j (i32.const 0))
(block $outer
(loop $outer_top
(br_if $outer (i32.eq (get_local $j) (i32.sub (get_local $sz) (i32.const 1))))
(set_local $i (i32.add (get_local $j) (i32.const 1)))
(block $inner
(loop $inner_top
(br_if $inner (i32.eq (get_local $i) (get_local $sz)))
(set_local $src_ptr
(i32.add (get_local $ptr)
(i32.mul (i32.const 4)
(i32.add (i32.mul (get_local $i) (get_local $sz)) (get_local $j))
)
)
)
(set_local $dst_ptr
(i32.add (get_local $ptr)
(i32.mul (i32.const 4)
(i32.add (i32.mul (get_local $j) (get_local $sz)) (get_local $i))
)
)
)
get_local $dst_ptr
get_local $src_ptr
i32.load offset=0 align=4
get_local $src_ptr
get_local $dst_ptr
i32.load offset=0 align=4
i32.store offset=0 align=4
i32.store offset=0 align=4
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $inner_top)
)
)
(set_local $j (i32.add (get_local $j) (i32.const 1)))
(br $outer_top)
)
)
)
;; Dot product of two f32 vectors
;;
;; @param $src1 memory location of the first vector
;; @param $src2 memory location of the second vector (transposed)
;; @param $sz vector length (# of elements)
;; @return dot product value
(func $df
(export "dot_f32")
(param $src1 i32)
(param $src2 i32)
(param $sz i32)
(result f32)
(local $off i32)
(local $len i32)
(local $sum f32)
(set_local $sum (f32.const 0))
(set_local $off (i32.const 0))
(set_local $len (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $off) (get_local $len)))
(set_local $sum
(f32.add (get_local $sum)
(f32.mul
(f32.load (i32.add (get_local $src1) (get_local $off)))
(f32.load (i32.add (get_local $src2) (get_local $off)))
)
)
)
(set_local $off (i32.add (get_local $off) (i32.const 4)))
(br $loop_top)
)
)
get_local $sum
)
;; Multiply f32 vector and a square f32 matrix
;;
;; @param $v vector memory location
;; @param $m memory location of transposed matrix
;; @param $dst memory location for the result
;; @param $sz size of both
(func $vmf
(export "vmmul_f32")
(param $v i32)
(param $m i32)
(param $dst i32)
(param $sz i32)
(local $i i32)
(local $mptr i32)
(local $dptr i32)
(local $rowlen i32)
(set_local $i (i32.const 0))
(set_local $mptr (get_local $m))
(set_local $dptr (get_local $dst))
(set_local $rowlen (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $sz)))
(f32.store (get_local $dptr) (call $df (get_local $v) (get_local $mptr) (get_local $sz)))
(set_local $mptr (i32.add (get_local $mptr) (get_local $rowlen)))
(set_local $dptr (i32.add (get_local $dptr) (i32.const 4)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Scalar multiplication of square f32 matrices (contiguous layout)
;;
;; @param $src1 location (byte index) of the first operand
;; @param $src2 location of the second operand -- transposed
;; @param $dst location where to write the result
;; @param $sz size of the matrix (number of elements in a row)
(func
(export "multiply_f32")
(param $src1 i32)
(param $src2 i32)
(param $dst i32)
(param $sz i32)
(local $i i32)
(local $off i32)
(local $rowlen i32)
(set_local $i (i32.const 0))
(set_local $off (i32.const 0))
(set_local $rowlen (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $sz)))
(call $vmf
(i32.add (get_local $src1) (get_local $off))
(get_local $src2)
(i32.add (get_local $dst) (get_local $off)) (get_local $sz)
)
(set_local $off (i32.add (get_local $off) (get_local $rowlen)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Dot product of two f32 vectors, SIMD version
;;
;; @param $src1 memory location of the first vector
;; @param $src2 memory location of the second vector (transposed)
;; @param $sz vector length (# of elements)
;; @return dot product value
(func $df_vec
(export "dot_f32_simd")
(param $src1 i32)
(param $src2 i32)
(param $sz i32)
(result f32)
(local $i i32)
(local $ptr1 i32)
(local $ptr2 i32)
(local $N i32)
(local $sum f32)
(local $v v128)
(set_local $v (v128.const i32 0 0 0 0))
(set_local $ptr1 (get_local $src1))
(set_local $ptr2 (get_local $src2))
(set_local $i (i32.const 0))
(set_local $N (i32.div_u (get_local $sz) (i32.const 4)))
(block $loop1
(loop $loop1_top
(br_if $loop1 (i32.eq (get_local $i) (get_local $N)))
(set_local $v
(f32x4.add
(get_local $v)
(f32x4.mul
(v128.load align=4 (get_local $ptr1))
(v128.load align=4 (get_local $ptr2))
)
)
)
(set_local $ptr1 (i32.add (get_local $ptr1) (i32.const 16)))
(set_local $ptr2 (i32.add (get_local $ptr2) (i32.const 16)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop1_top)
)
)
(set_local $sum
(f32.add (f32x4.extract_lane 0 (get_local $v))
(f32.add (f32x4.extract_lane 1 (get_local $v))
(f32.add (f32x4.extract_lane 2 (get_local $v))
(f32x4.extract_lane 3 (get_local $v))
)
)
)
)
(set_local $i (i32.const 0))
(set_local $N (i32.rem_u (get_local $sz) (i32.const 4)))
(block $loop2
(loop $loop2_top
(br_if $loop2 (i32.eq (get_local $i) (get_local $N)))
(set_local $sum
(f32.add (get_local $sum)
(f32.mul (f32.load (get_local $ptr1)) (f32.load (get_local $ptr2)))
)
)
(set_local $ptr1 (i32.add (get_local $ptr1) (i32.const 4)))
(set_local $ptr2 (i32.add (get_local $ptr2) (i32.const 4)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop2_top)
)
)
get_local $sum
)
;; Multiply f32 vector and a square matrix, SIMD version
;;
;; @param $v vector memory location
;; @param $m memory location of transposed matrix
;; @param $dst memory location for the result
;; @param $sz size of both
(func $vm_vec
(export "vmmul_f32_simd")
(param $v i32)
(param $m i32)
(param $dst i32)
(param $sz i32)
(local $i i32)
(local $mptr i32)
(local $dptr i32)
(local $rowlen i32)
(set_local $i (i32.const 0))
(set_local $mptr (get_local $m))
(set_local $dptr (get_local $dst))
(set_local $rowlen (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $sz)))
(f32.store (get_local $dptr) (call $df_vec (get_local $v) (get_local $mptr) (get_local $sz)))
(set_local $mptr (i32.add (get_local $mptr) (get_local $rowlen)))
(set_local $dptr (i32.add (get_local $dptr) (i32.const 4)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Vector multiplication of square f32 matrices (contiguous layout)
;;
;; @param $src1 location (byte index) of the first operand
;; @param $src2 location of the second operand -- transposed
;; @param $dst location where to write the result
;; @param $sz size of the matrix (number of elements in a row)
(func
(export "multiply_f32_simd")
(param $src1 i32)
(param $src2 i32)
(param $dst i32)
(param $sz i32)
(local $i i32)
(local $off i32)
(local $rowlen i32)
(set_local $i (i32.const 0))
(set_local $off (i32.const 0))
(set_local $rowlen (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $sz)))
(call $vm_vec
(i32.add (get_local $src1) (get_local $off))
(get_local $src2)
(i32.add (get_local $dst) (get_local $off)) (get_local $sz)
)
(set_local $off (i32.add (get_local $off) (get_local $rowlen)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Dot product of two i32 vectors
;;
;; @param $src1 memory location of the first vector
;; @param $src2 memory location of the second vector (transposed)
;; @param $sz vector length (# of elements)
;; @return dot product value
(func $di
(export "dot_i32")
(param $src1 i32)
(param $src2 i32)
(param $sz i32)
(result i32)
(local $off i32)
(local $len i32)
(local $sum i32)
(set_local $sum (i32.const 0))
(set_local $off (i32.const 0))
(set_local $len (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $off) (get_local $len)))
(set_local $sum
(i32.add (get_local $sum)
(i32.mul
(i32.load (i32.add (get_local $src1) (get_local $off)))
(i32.load (i32.add (get_local $src2) (get_local $off)))
)
)
)
(set_local $off (i32.add (get_local $off) (i32.const 4)))
(br $loop_top)
)
)
get_local $sum
)
;; Multiply i32 vector and a square i32 matrix
;;
;; @param $v vector memory location
;; @param $m memory location of transposed matrix
;; @param $dst memory location for the result
;; @param $sz size of both
(func $vmi
(export "vmmul_i32")
(param $v i32)
(param $m i32)
(param $dst i32)
(param $sz i32)
(local $i i32)
(local $mptr i32)
(local $dptr i32)
(local $rowlen i32)
(set_local $i (i32.const 0))
(set_local $mptr (get_local $m))
(set_local $dptr (get_local $dst))
(set_local $rowlen (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $sz)))
(i32.store (get_local $dptr) (call $di (get_local $v) (get_local $mptr) (get_local $sz)))
(set_local $mptr (i32.add (get_local $mptr) (get_local $rowlen)))
(set_local $dptr (i32.add (get_local $dptr) (i32.const 4)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Scalar multiplication of square i32 matrices (contiguous layout)
;;
;; @param $src1 location (byte index) of the first operand
;; @param $src2 location of the second operand -- transposed
;; @param $dst location where to write the result
;; @param $sz size of the matrix (number of elements in a row)
(func
(export "multiply_i32")
(param $src1 i32)
(param $src2 i32)
(param $dst i32)
(param $sz i32)
(local $i i32)
(local $off i32)
(local $rowlen i32)
(set_local $i (i32.const 0))
(set_local $off (i32.const 0))
(set_local $rowlen (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $sz)))
(call $vmi
(i32.add (get_local $src1) (get_local $off))
(get_local $src2)
(i32.add (get_local $dst) (get_local $off)) (get_local $sz)
)
(set_local $off (i32.add (get_local $off) (get_local $rowlen)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Dot product of two i32 vectors, SIMD version
;;
;; @param $src1 memory location of the first vector
;; @param $src2 memory location of the second vector (transposed)
;; @param $sz vector length (# of elements)
;; @return dot product value
(func $di_vec
(export "dot_i32_simd")
(param $src1 i32)
(param $src2 i32)
(param $sz i32)
(result i32)
(local $i i32)
(local $ptr1 i32)
(local $ptr2 i32)
(local $N i32)
(local $sum i32)
(local $v v128)
(set_local $v (v128.const i32 0 0 0 0))
(set_local $ptr1 (get_local $src1))
(set_local $ptr2 (get_local $src2))
(set_local $i (i32.const 0))
(set_local $N (i32.div_u (get_local $sz) (i32.const 4)))
(block $loop1
(loop $loop1_top
(br_if $loop1 (i32.eq (get_local $i) (get_local $N)))
(set_local $v
(i32x4.add
(get_local $v)
(f32x4.mul
(v128.load align=4 (get_local $ptr1))
(v128.load align=4 (get_local $ptr2))
)
)
)
(set_local $ptr1 (i32.add (get_local $ptr1) (i32.const 16)))
(set_local $ptr2 (i32.add (get_local $ptr2) (i32.const 16)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop1_top)
)
)
(set_local $sum
(i32.add (i32x4.extract_lane 0 (get_local $v))
(i32.add (i32x4.extract_lane 1 (get_local $v))
(i32.add (i32x4.extract_lane 2 (get_local $v))
(i32x4.extract_lane 3 (get_local $v))
)
)
)
)
(set_local $i (i32.const 0))
(set_local $N (i32.rem_u (get_local $sz) (i32.const 4)))
(block $loop2
(loop $loop2_top
(br_if $loop2 (i32.eq (get_local $i) (get_local $N)))
(set_local $sum
(i32.add (get_local $sum)
(i32.mul (i32.load (get_local $ptr1)) (i32.load (get_local $ptr2)))
)
)
(set_local $ptr1 (i32.add (get_local $ptr1) (i32.const 4)))
(set_local $ptr2 (i32.add (get_local $ptr2) (i32.const 4)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop2_top)
)
)
get_local $sum
)
;; Multiply i32 vector and a square matrix, SIMD version
;;
;; @param $v vector memory location
;; @param $m memory location of transposed matrix
;; @param $dst memory location for the result
;; @param $sz size of both
(func $vmi_vec
(export "vmmul_i32_simd")
(param $v i32)
(param $m i32)
(param $dst i32)
(param $sz i32)
(local $i i32)
(local $mptr i32)
(local $dptr i32)
(local $rowlen i32)
(set_local $i (i32.const 0))
(set_local $mptr (get_local $m))
(set_local $dptr (get_local $dst))
(set_local $rowlen (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $sz)))
(i32.store (get_local $dptr) (call $di_vec (get_local $v) (get_local $mptr) (get_local $sz)))
(set_local $mptr (i32.add (get_local $mptr) (get_local $rowlen)))
(set_local $dptr (i32.add (get_local $dptr) (i32.const 4)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Vector multiplication of square i32 matrices (contiguous layout)
;;
;; @param $src1 location (byte index) of the first operand
;; @param $src2 location of the second operand -- transposed
;; @param $dst location where to write the result
;; @param $sz size of the matrix (number of elements in a row)
(func
(export "multiply_i32_simd")
(param $src1 i32)
(param $src2 i32)
(param $dst i32)
(param $sz i32)
(local $i i32)
(local $off i32)
(local $rowlen i32)
(set_local $i (i32.const 0))
(set_local $off (i32.const 0))
(set_local $rowlen (i32.mul (get_local $sz) (i32.const 4)))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $sz)))
(call $vmi_vec
(i32.add (get_local $src1) (get_local $off))
(get_local $src2)
(i32.add (get_local $dst) (get_local $off)) (get_local $sz)
)
(set_local $off (i32.add (get_local $off) (get_local $rowlen)))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Test harness for scalar transpose for 32-bit elements
;;
;; @param $ptr source and destination -- byte index
;; @param $sz size of the matrix (number of elements in a row)
;; @param $cnt how many times to perform the transposition
(func
(export "test_transpose_32")
(param $ptr i32)
(param $sz i32)
(param $cnt i32)
(local $i i32)
(set_local $i (i32.const 0))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $cnt)))
(call $tr (get_local $ptr) (get_local $sz))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Swap 2x2 matrix
(func $swap
(param $a1 i32)
(param $a2 i32)
(param $b1 i32)
(param $b2 i32)
)
;; SIMD transpose a square matrix (contiguous layout) of 32-bit elements in place
;;
;; @param $ptr source and destination -- byte index
;; @param $sz size of the matrix (number of elements in a row)
(func $vtr
(export "simd_transpose_32")
(param $ptr i32)
(param $sz i32)
(local $i i32)
(local $j i32)
(local $src_ptr i32)
(local $dst_ptr i32)
(local $sz_norm i32)
(local $v v128)
(set_local $sz_norm (i32.mul (i32.const 2) (i32.div_u (get_local $sz) (i32.const 2))))
(set_local $j (i32.const 0))
(block $outer
(loop $outer_top
;;(br_if $outer (i32.eq (get_local $j) (i32.sub (get_local $sz_norm) (i32.const 1))))
;;(set_local $i (i32.add (get_local $j) (i32.const 1)))
(br_if $outer (i32.eq (get_local $j) (get_local $sz_norm)))
(set_local $i (get_local $j))
(block $inner
(loop $inner_top
;; FIXME sometimes $i == $sz_norm at the beginning, would that affect execution?
;; FIXME and if that is a bug in the runtime, how to demonstrate that?
(br_if $inner (i32.eq (get_local $i) (get_local $sz_norm)))
(set_local $src_ptr
(i32.add (get_local $ptr)
(i32.mul (i32.const 4)
(i32.add (i32.mul (get_local $i) (get_local $sz)) (get_local $j))
)
)
)
;; Tiles are handled differently based on whether they are on the diagonal or not
(if (i32.eq (get_local $i) (get_local $j))
(then
get_local $src_ptr
v128.load offset=0 align=4 ;; first line of the tile
get_local $src_ptr
get_local $sz
i32.const 4
i32.mul
i32.add
v128.load offset=0 align=4 ;; second line of the tile
v8x16.shuffle 0x03020100 0x13121110 0x07060504 0x17161514 ;; Transposed tile inline
;;v8x16.shuffle 0x03020100 0x07060504 0x0B0A0908 0x0F0E0D0C
set_local $v ;; TODO
;; Would it be better to scalar traspose diagonal tiles?
)
(else
(set_local $dst_ptr
(i32.add (get_local $ptr)
(i32.mul (i32.const 4)
(i32.add (i32.mul (get_local $j) (get_local $sz)) (get_local $i))
)
)
)
get_local $dst_ptr
get_local $src_ptr
i32.load offset=0 align=4
get_local $src_ptr
get_local $dst_ptr
i32.load offset=0 align=4
i32.store offset=0 align=4
i32.store offset=0 align=4
)
)
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $inner_top)
)
)
;; For odd sizes, we have one more element at the end
(block $onemore
(br_if $onemore (i32.eq (get_local $sz) (get_local $sz_norm)))
(set_local $src_ptr
(i32.add (get_local $ptr)
(i32.mul (i32.const 4)
(i32.add (i32.mul (get_local $i) (get_local $sz)) (get_local $j))
)
)
)
(set_local $dst_ptr
(i32.add (get_local $ptr)
(i32.mul (i32.const 4)
(i32.add (i32.mul (get_local $j) (get_local $sz)) (get_local $i))
)
)
)
get_local $dst_ptr
get_local $src_ptr
i32.load offset=0 align=4
get_local $src_ptr
get_local $dst_ptr
i32.load offset=0 align=4
i32.store offset=0 align=4
i32.store offset=0 align=4
)
(set_local $j (i32.add (get_local $j) (i32.const 1)))
(br $outer_top)
)
)
)
;; Vector transpose f32 2x2 matrix (contiguous layout) in place
;; Load a vector of four f32 numbers from location indicated by $ptr
(func $vt2x2
(export "simd_transpose_f32x2x2")
(param $ptr i32)
get_local $ptr
get_local $ptr
v128.load offset=0 align=4
v128.const i32 0 0 0 0
v8x16.shuffle 0x03020100 0x0B0A0908 0x07060504 0x0F0E0D0C
;;v8x16.shuffle 0x03020100 0x07060504 0x0B0A0908 0x0F0E0D0C
v128.store offset=0 align=4
)
(func
(export "test_vec_transpose_2x2")
(param $ptr i32)
(param $cnt i32)
(local $i i32)
(set_local $i (i32.const 0))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $cnt)))
(call $vt2x2 (get_local $ptr))
(set_local $i (i32.add (get_local $i) (i32.const 1)))
(br $loop_top)
)
)
)
;; Vector transpose 32-bit 4x4 matrix in-place
;;
;; Parameters $a1-$a4 are pointers to rows of the matrix
(func $vt4x4ip
(export "simd_transpose_32x4x4_inplace")
(param $a1 i32)
(param $a2 i32)
(param $a3 i32)
(param $a4 i32)
;; four 2x2 tiles that make up the result
(local $v11 v128)
(local $v12 v128)
(local $v21 v128)
(local $v22 v128)
;; Prepare tiles
;; indexed like this:
;; 0 1
;; 2 3
get_local $a1
v128.load offset=0 align=4
get_local $a2
v128.load offset=0 align=4
v8x16.shuffle 0x03020100 0x07060504 0x13121110 0x17161514
set_local $v11
get_local $a1
v128.load offset=0 align=4
get_local $a2
v128.load offset=0 align=4
v8x16.shuffle 0x0B0A0908 0x0F0E0D0C 0x1B1A1918 0x1F1E1D1C
set_local $v21
get_local $a3
v128.load offset=0 align=4
get_local $a4
v128.load offset=0 align=4
v8x16.shuffle 0x03020100 0x07060504 0x13121110 0x17161514
set_local $v12
get_local $a3
v128.load offset=0 align=4
get_local $a4
v128.load offset=0 align=4
v8x16.shuffle 0x0B0A0908 0x0F0E0D0C 0x1B1A1918 0x1F1E1D1C
set_local $v22
;; Prepare and store lines of the result
get_local $a1
get_local $v11
get_local $v12
v8x16.shuffle 0x03020100 0x0B0A0908 0x13121110 0x1B1A1918
v128.store offset=0 align=4
get_local $a2
get_local $v11
get_local $v12
v8x16.shuffle 0x07060504 0x0F0E0D0C 0x17161514 0x1F1E1D1C
v128.store offset=0 align=4
get_local $a3
get_local $v21
get_local $v22
v8x16.shuffle 0x03020100 0x0B0A0908 0x13121110 0x1B1A1918
v128.store offset=0 align=4
get_local $a4
get_local $v21
get_local $v22
v8x16.shuffle 0x07060504 0x0F0E0D0C 0x17161514 0x1F1E1D1C
v128.store offset=0 align=4
)
(func $vt4x4
(export "simd_transpose_f32x4x4")
(param i32 i32) ;; Transpose matrix expecting flat layout and swap the data between two pointers
(if (i32.eq (get_local 0) (get_local 1))
(then
(call $vt4x4ip
(get_local 0)
(i32.add (get_local 0) (i32.const 16))
(i32.add (get_local 0) (i32.const 32))
(i32.add (get_local 0) (i32.const 48))
)
)
(else
(call $vt4x4ip
(get_local 0)
(i32.add (get_local 0) (i32.const 16))
(i32.add (get_local 0) (i32.const 32))
(i32.add (get_local 0) (i32.const 48))
)
(call $vt4x4ip
(get_local 1)
(i32.add (get_local 1) (i32.const 16))
(i32.add (get_local 1) (i32.const 32))
(i32.add (get_local 1) (i32.const 48))
)
get_local 1
get_local 0
v128.load offset=0 align=4
get_local 1
get_local 0
v128.load offset=16 align=4
get_local 1
get_local 0
v128.load offset=32 align=4
get_local 1
get_local 0
v128.load offset=48 align=4
get_local 0
get_local 1
v128.load offset=0 align=4
get_local 0
get_local 1
v128.load offset=16 align=4
get_local 0
get_local 1
v128.load offset=32 align=4
get_local 0
get_local 1
v128.load offset=48 align=4
v128.store offset=0 align=4
v128.store offset=16 align=4
v128.store offset=32 align=4
v128.store offset=48 align=4
v128.store offset=0 align=4
v128.store offset=16 align=4
v128.store offset=32 align=4
v128.store offset=48 align=4
)
)
)
(func $vt4x4old
(export "simd_transpose_f32x4x4old")
(param i32 i32) ;; Transpose matrix expecting flat layout and swap the data between two pointers
;; Treat 4x4 as four 2x2 tiles -- transpose each one and then swap the
;; two that don't occupy the diagonal
get_local 1
get_local 0
v128.load offset=0 align=4
get_local 0
v128.load offset=16 align=4
get_local 1
get_local 0
v128.load offset=0 align=4
get_local 0
v128.load offset=16 align=4
v8x16.shuffle 0x03020100 0x13121110 0x0B0A0908 0x1B1A1918
;;v8x16.shuffle 0x03020100 0x07060504 0x0B0A0908 0x0F0E0D0C
v128.store offset=0 align=4
v8x16.shuffle 0x07060504 0x17161514 0x0F0E0D0C 0x1F1E1D1C
;;v8x16.shuffle 0x03020100 0x07060504 0x0B0A0908 0x0F0E0D0C
v128.store offset=16 align=4
get_local 1
get_local 0
v128.load offset=32 align=4
get_local 0
v128.load offset=48 align=4
get_local 1
get_local 0
v128.load offset=32 align=4
get_local 0
v128.load offset=48 align=4
v8x16.shuffle 0x03020100 0x13121110 0x0B0A0908 0x1B1A1918
;;v8x16.shuffle 0x03020100 0x07060504 0x0B0A0908 0x0F0E0D0C
v128.store offset=32 align=4
v8x16.shuffle 0x07060504 0x17161514 0x0F0E0D0C 0x1F1E1D1C
;;v8x16.shuffle 0x03020100 0x07060504 0x0B0A0908 0x0F0E0D0C
v128.store offset=48 align=4
;; Swap the tiles
get_local 1
get_local 0
i64.load offset=8 align=4
get_local 1
get_local 0
i64.load offset=32 align=4
i64.store offset=8 align=4
i64.store offset=32 align=4
get_local 1
get_local 0
i64.load offset=24 align=4
get_local 1
get_local 0
i64.load offset=48 align=4
i64.store offset=24 align=4
i64.store offset=48 align=4
)
(func
(export "test_vec_transpose_4x4")
(param $ptr i32)
(param $cnt i32)
(local $i i32)
(set_local $i (i32.const 0))
(block $loop
(loop $loop_top
(br_if $loop (i32.eq (get_local $i) (get_local $cnt)))
(call $vt4x4ip
(get_local $ptr)
(i32.add (i32.const 16) (get_local $ptr))
(i32.add (i32.const 32) (get_local $ptr))
(i32.add (i32.const 48) (get_local $ptr))