-
Notifications
You must be signed in to change notification settings - Fork 35
/
FMAMatrixMultTransposedOperationsx64.pas
807 lines (637 loc) · 31.3 KB
/
FMAMatrixMultTransposedOperationsx64.pas
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
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2018, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
unit FMAMatrixMultTransposedOperationsx64;
// ##############################################################
// #### Assembler optimized matrix multiplication assuming a transposed second
// #### matrix
// ##############################################################
interface
{$I 'mrMath_CPU.inc'}
{$IFDEF x64}
procedure FMAMatrixMultAlignedTransposed(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; {$ifdef UNIX}unixWidth1{$ELSE}width1{$endif} : NativeInt; {$ifdef UNIX}unixHeight1{$ELSE}height1{$endif} : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC}assembler;{$ENDIF}
procedure FMAMatrixMultAlignedEvenW1EvenH2TransposedMod16(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; {$ifdef UNIX}unixWidth1{$ELSE}width1{$endif} : NativeInt; {$ifdef UNIX}unixHeight1{$ELSE}height1{$endif} : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC}assembler;{$ENDIF}
procedure FMAMatrixMultAlignedEvenW1OddH2TransposedMod16(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; {$ifdef UNIX}unixWidth1{$ELSE}width1{$endif} : NativeInt; {$ifdef UNIX}unixHeight1{$ELSE}height1{$endif} : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC}assembler;{$ENDIF}
procedure FMAMatrixMultUnAlignedTransposed(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; {$ifdef UNIX}unixWidth1{$ELSE}width1{$endif} : NativeInt; {$ifdef UNIX}unixHeight1{$ELSE}height1{$endif} : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC}assembler;{$ENDIF}
{$ENDIF}
implementation
{$IFDEF x64}
procedure FMAMatrixMultAlignedEvenW1EvenH2TransposedMod16(dest : PDouble; const destLineWidth : NativeInt;
mt1, mt2 : PDouble; {$ifdef UNIX}unixWidth1{$ELSE}width1{$endif} : NativeInt; {$ifdef UNIX}unixHeight1{$ELSE}height1{$endif} : NativeInt;
width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC}assembler;{$ENDIF}
var iRBX, iRSI, iRDI, iR12, iR13, iR14, iR15 : int64;
{$IFDEF UNIX}
width1 : NativeInt;
height1 : NativeInt;
{$ENDIF}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov width1, r8;
mov height1, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// prolog - simulate stack
mov iRBX, rbx;
mov iRSI, rsi;
mov iRDI, rdi;
mov iR12, r12;
mov iR13, r13;
mov iR14, r14;
mov iR15, r15;
sub rsp, $10;
{$IFDEF AVXSUP}vmovupd [rsp + $00], xmm4; {$ELSE}db $C5,$F9,$11,$24,$24;{$ENDIF}
// iters1 := height2 div 2;
mov r15, height2;
shr r15, 1;
// iters2 := -width1*sizeof(double);
mov r14, width1;
imul r14, -8;
// LineWidth2_2 := 2*LineWidth2;
mov r13, LineWidth2;
mov r12, r13;
shl r12, 1;
// prepare matrix pointers - remove constant offset here instead each time in the loop:
sub r8, r14;
sub r9, r14;
mov r10, r9;
add r10, r13;
// for y := 0 to height1 - 1:
mov r11, Height1;
@@forylabel:
mov rdi, r9;
mov rsi, r10;
mov r13, rcx;
// for y2 := 0 to height2 - 1:
mov rbx, r15;
@@fory2label:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF} // dest^ := 0
{$IFDEF AVXSUP}vxorpd ymm2, ymm2, ymm2; {$ELSE}db $C5,$ED,$57,$D2;{$ENDIF} // dest + 1 := 0;
// for idx := 0 to width1 div 2 do
mov rax, r14;
// for x := 0 to width1 - 1
@@InnerLoop:
// prefetch [ecx + edx + 128];
// prefetch [edi + edx + 128];
// prefetch [eax + edx + 128];
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax]; {$ELSE}db $C4,$C1,$7D,$28,$0C,$00;{$ENDIF}
// multiply 2x2 and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax]; {$ELSE}db $C4,$E2,$F5,$B8,$04,$07;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, [rsi + rax]; {$ELSE}db $C4,$E2,$F5,$B8,$14,$06;{$ENDIF}
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax + 32]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$20;{$ENDIF}
// multiply 2x2 and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax + 32];{$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$20;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, [rsi + rax + 32];{$ELSE}db $C4,$E2,$F5,$B8,$54,$06,$20;{$ENDIF}
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax + 64]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$40;{$ENDIF}
// load 2x2 block
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax + 64];{$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$40;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, [rsi + rax + 64];{$ELSE}db $C4,$E2,$F5,$B8,$54,$06,$40;{$ENDIF}
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax + 96]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$60;{$ENDIF}
// multiply 2x2 and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax + 96];{$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$60;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, [rsi + rax + 96];{$ELSE}db $C4,$E2,$F5,$B8,$54,$06,$60;{$ENDIF}
// end for idx := 0 to width1 div 2
add rax, 128;
jnz @@InnerLoop;
{$IFDEF AVXSUP}vextractf128 xmm1, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C1,$01;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm3, ymm2, 1; {$ELSE}db $C4,$E3,$7D,$19,$D3,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm1; {$ELSE}db $C5,$F9,$7C,$C1;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm3; {$ELSE}db $C5,$E9,$7C,$D3;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm2; {$ELSE}db $C5,$F9,$7C,$C2;{$ENDIF}
// store back result
{$IFDEF AVXSUP}vmovapd [r13], xmm0; {$ELSE}db $C4,$C1,$79,$29,$45,$00;{$ENDIF}
// increment the pointers
// inc(mt2), inc(dest);
//add dword ptr [mt2], 8;
add r13, 16;
add rdi, r12;
add rsi, r12;
// end for x := 0 to width2 - 1
dec rbx;
jnz @@fory2label;
// dec(mt2, Width2);
// inc(PByte(mt1), LineWidth1);
// inc(PByte(dest), destOffset);
//mov ebx, bytesWidth2;
//sub dword ptr [mt2], ebx;
add r8, LineWidth1;
add rcx, rdx;
// end for y := 0 to height1 - 1
dec r11;
jnz @@forylabel;
// epilog - cleanup stack
mov rbx, iRBX;
mov rsi, iRSI;
mov rdi, iRDI;
mov r12, iR12;
mov r13, iR13;
mov r14, iR14;
mov r15, iR15;
{$IFDEF AVXSUP}vmovupd xmm4, [rsp + $00]; {$ELSE}db $C5,$F9,$10,$24,$24;{$ENDIF}
add rsp, $10;
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
end;
procedure FMAMatrixMultAlignedEvenW1OddH2TransposedMod16(dest : PDouble; const destLineWidth : NativeInt;
mt1, mt2 : PDouble; {$ifdef UNIX}unixWidth1{$ELSE}width1{$endif} : NativeInt; {$ifdef UNIX}unixHeight1{$ELSE}height1{$endif} : NativeInt;
width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC}assembler;{$ENDIF}
var iRBX, iRSI, iRDI, iR12, iR13, iR14, iR15 : int64;
{$IFDEF UNIX}
width1 : NativeInt;
height1 : NativeInt;
{$ENDIF}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov width1, r8;
mov height1, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// prolog - simulate stack
mov iRBX, rbx;
mov iRSI, rsi;
mov iRDI, rdi;
mov iR12, r12;
mov iR13, r13;
mov iR14, r14;
mov iR15, r15;
sub rsp, $10;
{$IFDEF AVXSUP}vmovupd [rsp + $00], xmm4; {$ELSE}db $C5,$F9,$11,$24,$24;{$ENDIF}
// iters1 := height2 div 2;
mov r15, height2;
shr r15, 1;
// iters2 := -width1*sizeof(double);
mov r14, width1;
imul r14, -8;
// LineWidth2_2 := 2*LineWidth2;
mov r13, LineWidth2;
mov r12, r13;
shl r12, 1;
// prepare matrix pointers - remove constant offset here instead each time in the loop:
sub r8, r14;
sub r9, r14;
mov r10, r9;
add r10, r13;
// for y := 0 to height1 - 1:
mov r11, Height1;
@@forylabel:
mov rdi, r9;
mov rsi, r10;
mov r13, rcx;
// for y2 := 0 to height2 div 2 - 1:
mov rbx, r15;
@@fory2label:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF} // dest^ := 0
{$IFDEF AVXSUP}vxorpd ymm2, ymm2, ymm2; {$ELSE}db $C5,$ED,$57,$D2;{$ENDIF} // dest + 1 := 0;
// for idx := 0 to width1 div 2 do
mov rax, r14;
// for x := 0 to width1 - 1
@@InnerLoop:
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax]; {$ELSE}db $C4,$C1,$7D,$28,$0C,$00;{$ENDIF}
// multiply 2x2 and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax]; {$ELSE}db $C4,$E2,$F5,$B8,$04,$07;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, [rsi + rax]; {$ELSE}db $C4,$E2,$F5,$B8,$14,$06;{$ENDIF}
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax + 32]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$20;{$ENDIF}
// multiply 2x2 and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax + 32];{$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$20;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, [rsi + rax + 32];{$ELSE}db $C4,$E2,$F5,$B8,$54,$06,$20;{$ENDIF}
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax + 64]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$40;{$ENDIF}
// load 2x2 block
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax + 64];{$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$40;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, [rsi + rax + 64];{$ELSE}db $C4,$E2,$F5,$B8,$54,$06,$40;{$ENDIF}
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax + 96]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$60;{$ENDIF}
// multiply 2x2 and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax + 96];{$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$60;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, [rsi + rax + 96];{$ELSE}db $C4,$E2,$F5,$B8,$54,$06,$60;{$ENDIF}
// end for idx := 0 to width1 div 2
// end for idx := 0 to width1 div 2
add rax, 128;
jnz @@InnerLoop;
{$IFDEF AVXSUP}vextractf128 xmm1, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C1,$01;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm3, ymm2, 1; {$ELSE}db $C4,$E3,$7D,$19,$D3,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm1; {$ELSE}db $C5,$F9,$7C,$C1;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm3; {$ELSE}db $C5,$E9,$7C,$D3;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm2; {$ELSE}db $C5,$F9,$7C,$C2;{$ENDIF}
// store back result
{$IFDEF AVXSUP}vmovapd [r13], xmm0; {$ELSE}db $C4,$C1,$79,$29,$45,$00;{$ENDIF}
// increment the pointers
// inc(mt2), inc(dest);
//add dword ptr [mt2], 8;
add r13, 16;
add rdi, r12;
add rsi, r12;
// end for y := 0 to height2 div 2 - 1
dec rbx;
jnz @@fory2label;
// last odd line:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF} // dest^ := 0
// for idx := 0 to width1 div 2 do
mov rax, r14;
// for x := 0 to width1 - 1
@@InnerLoop2:
// prefetch [ecx + edx + 128];
// prefetch [edi + edx + 128];
// prefetch [eax + edx + 128];
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax]; {$ELSE}db $C4,$C1,$7D,$28,$0C,$00;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax]; {$ELSE}db $C4,$E2,$F5,$B8,$04,$07;{$ENDIF}
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax + 32]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$20;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax + 32]; {$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$20;{$ENDIF}
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax + 64]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$40;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi +rax + 64]; {$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$40;{$ENDIF}
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax + 96]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$60;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax +96]; {$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$60;{$ENDIF}
// end for idx := 0 to width1 div 2
add rax, 128;
jnz @@InnerLoop2;
{$IFDEF AVXSUP}vextractf128 xmm1, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C1,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm1; {$ELSE}db $C5,$F9,$7C,$C1;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF}
// store back result
{$IFDEF AVXSUP}vmovsd [r13], xmm0; {$ELSE}db $C4,$C1,$7B,$11,$45,$00;{$ENDIF}
// dec(mt2, Width2);
// inc(PByte(mt1), LineWidth1);
// inc(PByte(dest), destOffset);
//mov ebx, bytesWidth2;
//sub dword ptr [mt2], ebx;
add r8, LineWidth1;
add rcx, rdx;
// end for y := 0 to height1 - 1
dec r11;
jnz @@forylabel;
// epilog - cleanup stack
mov rbx, iRBX;
mov rsi, iRSI;
mov rdi, iRDI;
mov r12, iR12;
mov r13, iR13;
mov r14, iR14;
mov r15, iR15;
{$IFDEF AVXSUP}vmovupd xmm4, [rsp + $00]; {$ELSE}db $C5,$F9,$10,$24,$24;{$ENDIF}
add rsp, $10;
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
end;
// note mt2 is transposed this time -> width1 and width2 must be the same!
procedure FMAMatrixMultUnAlignedTransposed(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble;
{$ifdef UNIX}unixWidth1{$ELSE}width1{$endif} : NativeInt; {$ifdef UNIX}unixHeight1{$ELSE}height1{$endif} : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC}assembler;{$ENDIF}
var iRBX, iRSI, iRDI, iR12, iR13, iR14, iR15 : int64;
{$IFDEF UNIX}
width1 : NativeInt;
height1 : NativeInt;
{$ENDIF}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov width1, r8;
mov height1, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// prolog - simulate stack
mov iRBX, rbx;
mov iRSI, rsi;
mov iRDI, rdi;
mov iR12, r12;
mov iR13, r13;
mov iR14, r14;
mov iR15, r15;
sub rsp, $10;
{$IFDEF AVXSUP}vmovupd [rsp + $00], xmm4; {$ELSE}db $C5,$F9,$11,$24,$24;{$ENDIF}
// height 2
mov r15, height2;
// iters2 := -width1*sizeof(double);
mov r14, width1;
shl r14, 3;
imul r14, -1;
// LineWidth2_2 := 2*LineWidth2;
mov r13, LineWidth2;
mov r12, r13;
shl r12, 1;
// prepare matrix pointers - remove constant offset here instead each time in the loop:
sub r8, r14;
sub r9, r14;
mov r10, r9;
add r10, r13;
// for y := 0 to height1 - 1:
mov r11, Height1;
@@forylabel:
mov rdi, r9;
mov rsi, r10;
mov r13, rcx;
// for x := 0 to width2 - 1:
mov rbx, r15;
sub rbx, 2;
jl @@fory2loopend;
@@fory2label:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF} // dest^ := 0
{$IFDEF AVXSUP}vxorpd ymm2, ymm2, ymm2; {$ELSE}db $C5,$ED,$57,$D2;{$ENDIF} // dest + 1 := 0;
// for idx := 0 to width1 div 4 do
mov rax, r14;
add rax, 32;
jg @InnerLoopEnd;
@@InnerLoop:
{$IFDEF AVXSUP}vmovupd ymm1, [r8 + rax - 32]; {$ELSE}db $C4,$C1,$7D,$10,$4C,$00,$E0;{$ENDIF}
// load 2x2 block
{$IFDEF AVXSUP}vmovupd ymm3, [rdi + rax - 32]; {$ELSE}db $C5,$FD,$10,$5C,$07,$E0;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm4, [rsi + rax - 32]; {$ELSE}db $C5,$FD,$10,$64,$06,$E0;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, ymm3; {$ELSE}db $C4,$E2,$F5,$B8,$C3;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, ymm4; {$ELSE}db $C4,$E2,$F5,$B8,$D4;{$ENDIF}
// end for idx := 0 to width1 div 2
add rax, 32;
jle @@InnerLoop;
{$IFDEF AVXSUP}vextractf128 xmm1, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C1,$01;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm3, ymm2, 1; {$ELSE}db $C4,$E3,$7D,$19,$D3,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm1; {$ELSE}db $C5,$F9,$7C,$C1;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm3; {$ELSE}db $C5,$E9,$7C,$D3;{$ENDIF}
@InnerLoopEnd:
sub rax, 32;
jz @@InnerLoopEnd2;
// do the final few elements
@@InnerLoop2:
{$IFDEF AVXSUP}vmovsd xmm1, [r8 + rax]; {$ELSE}db $C4,$C1,$7B,$10,$0C,$00;{$ENDIF}
// load 2x2 block
{$IFDEF AVXSUP}vmovsd xmm3, [rdi + rax]; {$ELSE}db $C5,$FB,$10,$1C,$07;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [rsi + rax]; {$ELSE}db $C5,$FB,$10,$24,$06;{$ENDIF}
// multiply 2x2 and add
{$IFDEF AVXSUP}vmulsd xmm3, xmm3, xmm1; {$ELSE}db $C5,$E3,$59,$D9;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm4, xmm4, xmm1; {$ELSE}db $C5,$DB,$59,$E1;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm3; {$ELSE}db $C5,$FB,$58,$C3;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm2, xmm2, xmm4; {$ELSE}db $C5,$EB,$58,$D4;{$ENDIF}
add rax, 8;
jnz @@InnerLoop2;
@@InnerLoopEnd2:
// final add and compact result
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm2; {$ELSE}db $C5,$F9,$7C,$C2;{$ENDIF}
// store back result
{$IFDEF AVXSUP}vmovupd [r13], xmm0; {$ELSE}db $C4,$C1,$79,$11,$45,$00;{$ENDIF}
// increment the pointers
// inc(mt2), inc(dest);
//add dword ptr [mt2], 8;
add r13, 16;
add rdi, r12;
add rsi, r12;
// end for x := 0 to width2 - 1
sub rbx, 2;
jge @@fory2label;
@@fory2loopend:
add rbx, 2;
// test for odd h2
jz @@NextLine;
// we have an odd height2 -> special treatment for the last line
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF} // dest^ := 0
// for idx := 0 to width1 div 4 do
mov rax, r14;
add rax, 32;
jg @LastLineInnerLoopEnd;
@@LastLineInnerLoop:
{$IFDEF AVXSUP}vmovupd ymm1, [r8 + rax - 32]; {$ELSE}db $C4,$C1,$7D,$10,$4C,$00,$E0;{$ENDIF}
// load block
{$IFDEF AVXSUP}vmovupd ymm3, [rdi + rax - 32]; {$ELSE}db $C5,$FD,$10,$5C,$07,$E0;{$ENDIF}
// multiply and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, ymm3; {$ELSE}db $C4,$E2,$F5,$B8,$C3;{$ENDIF}
// end for idx := 0 to width1 div 2
add rax, 32;
jle @@LastLineInnerLoop;
{$IFDEF AVXSUP}vextractf128 xmm1, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C1,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm1; {$ELSE}db $C5,$F9,$7C,$C1;{$ENDIF}
@LastLineInnerLoopEnd:
sub rax, 32;
jz @@LastLineInnerLoopEnd2;
// do the final few elements
@@LastLineInnerLoop2:
{$IFDEF AVXSUP}vmovsd xmm1, [r8 + rax]; {$ELSE}db $C4,$C1,$7B,$10,$0C,$00;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm3, [rdi + rax]; {$ELSE}db $C5,$FB,$10,$1C,$07;{$ENDIF}
// multiply and add
{$IFDEF AVXSUP}vmulsd xmm3, xmm3, xmm1; {$ELSE}db $C5,$E3,$59,$D9;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm3; {$ELSE}db $C5,$FB,$58,$C3;{$ENDIF}
// next element
add rax, 8;
jnz @@LastLineInnerLoop2;
@@LastLineInnerLoopEnd2:
// final add and compact result
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF}
// store back result
{$IFDEF AVXSUP}vmovsd [r13], xmm0; {$ELSE}db $C4,$C1,$7B,$11,$45,$00;{$ENDIF}
// ################################################
// #### next line of mt1
@@NextLine:
// dec(mt2, Width2);
// inc(PByte(mt1), LineWidth1);
// inc(PByte(dest), destOffset);
//mov ebx, bytesWidth2;
//sub dword ptr [mt2], ebx;
add r8, LineWidth1;
add rcx, rdx;
// end for y := 0 to height1 - 1
dec r11;
jnz @@forylabel;
// epilog - cleanup stack
mov rbx, iRBX;
mov rsi, iRSI;
mov rdi, iRDI;
mov r12, iR12;
mov r13, iR13;
mov r14, iR14;
mov r15, iR15;
{$IFDEF AVXSUP}vmovupd xmm4, [rsp + $00]; {$ELSE}db $C5,$F9,$10,$24,$24;{$ENDIF}
add rsp, $10;
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
end;
// note mt2 is transposed this time -> width1 and width2 must be the same!
procedure FMAMatrixMultAlignedTransposed(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble;
{$ifdef UNIX}unixWidth1{$ELSE}width1{$endif} : NativeInt; {$ifdef UNIX}unixHeight1{$ELSE}height1{$endif} : NativeInt;
width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC}assembler;{$ENDIF}
var iRBX, iRSI, iRDI, iR12, iR13, iR14, iR15 : int64;
{$IFDEF UNIX}
width1 : NativeInt;
height1 : NativeInt;
{$ENDIF}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov width1, r8;
mov height1, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// prolog - simulate stack
mov iRBX, rbx;
mov iRSI, rsi;
mov iRDI, rdi;
mov iR12, r12;
mov iR13, r13;
mov iR14, r14;
mov iR15, r15;
sub rsp, $10;
{$IFDEF AVXSUP}vmovupd [rsp + $00], xmm4; {$ELSE}db $C5,$F9,$11,$24,$24;{$ENDIF}
// height2 helper
mov r15, height2;
// iters2 := -width1*sizeof(double);
mov r14, width1;
shl r14, 3;
imul r14, -1;
// LineWidth2_2 := 2*LineWidth2;
mov r13, LineWidth2;
mov r12, r13;
shl r12, 1;
// prepare matrix pointers - remove constant offset here instead each time in the loop:
sub r8, r14;
sub r9, r14;
mov r10, r9;
add r10, r13;
// for y := 0 to height1 - 1:
mov r11, Height1;
@@forylabel:
mov rdi, r9;
mov rsi, r10;
mov r13, rcx;
// for x := 0 to width2 - 1:
mov rbx, r15;
sub rbx, 2;
jl @@fory2loopend;
@@fory2label:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF} // dest^ := 0
{$IFDEF AVXSUP}vxorpd ymm2, ymm2, ymm2; {$ELSE}db $C5,$ED,$57,$D2;{$ENDIF} // dest + 1 := 0;
// for idx := 0 to width1 div 4 do
mov rax, r14;
add rax, 32;
jg @InnerLoopEnd;
@@InnerLoop:
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax - 32]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$E0;{$ENDIF}
// multiply 2x2 and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax - 32];{$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$E0;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm1, [rsi + rax - 32];{$ELSE}db $C4,$E2,$F5,$B8,$54,$06,$E0;{$ENDIF}
// end for idx := 0 to width1 div 2
add rax, 32;
jle @@InnerLoop;
{$IFDEF AVXSUP}vextractf128 xmm1, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C1,$01;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm3, ymm2, 1; {$ELSE}db $C4,$E3,$7D,$19,$D3,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm1; {$ELSE}db $C5,$F9,$7C,$C1;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm3; {$ELSE}db $C5,$E9,$7C,$D3;{$ENDIF}
@InnerLoopEnd:
sub rax, 32;
jz @@InnerLoopEnd2;
// do the final few elements
@@InnerLoop2:
{$IFDEF AVXSUP}vmovsd xmm1, [r8 + rax]; {$ELSE}db $C4,$C1,$7B,$10,$0C,$00;{$ENDIF}
// load 2x2 block
{$IFDEF AVXSUP}vmovsd xmm3, [rdi + rax]; {$ELSE}db $C5,$FB,$10,$1C,$07;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [rsi + rax]; {$ELSE}db $C5,$FB,$10,$24,$06;{$ENDIF}
// multiply 2x2 and add
{$IFDEF AVXSUP}vmulsd xmm3, xmm3, xmm1; {$ELSE}db $C5,$E3,$59,$D9;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm4, xmm4, xmm1; {$ELSE}db $C5,$DB,$59,$E1;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm3; {$ELSE}db $C5,$FB,$58,$C3;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm2, xmm2, xmm4; {$ELSE}db $C5,$EB,$58,$D4;{$ENDIF}
add rax, 8;
jnz @@InnerLoop2;
@@InnerLoopEnd2:
// final add and compact result
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm2; {$ELSE}db $C5,$F9,$7C,$C2;{$ENDIF}
// store back result
{$IFDEF AVXSUP}vmovapd [r13], xmm0; {$ELSE}db $C4,$C1,$79,$29,$45,$00;{$ENDIF}
// increment the pointers
// inc(mt2), inc(dest);
//add dword ptr [mt2], 8;
add r13, 16;
add rdi, r12;
add rsi, r12;
// end for x := 0 to width2 - 1
sub rbx, 2;
jge @@fory2label;
@@fory2loopend:
add rbx, 2;
// test for odd h2
jz @@NextLine;
// we have an odd height2 -> special treatment for the last line
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF} // dest^ := 0
// for idx := 0 to width1 div 4 do
mov rax, r14;
add rax, 32;
jg @LastLineInnerLoopEnd;
@@LastLineInnerLoop:
{$IFDEF AVXSUP}vmovapd ymm1, [r8 + rax - 32]; {$ELSE}db $C4,$C1,$7D,$28,$4C,$00,$E0;{$ENDIF}
// multiply and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, [rdi + rax - 32]; {$ELSE}db $C4,$E2,$F5,$B8,$44,$07,$E0;{$ENDIF}
// end for idx := 0 to width1 div 2
add rax, 32;
jle @@LastLineInnerLoop;
{$IFDEF AVXSUP}vextractf128 xmm1, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C1,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm1; {$ELSE}db $C5,$F9,$7C,$C1;{$ENDIF}
@LastLineInnerLoopEnd:
sub rax, 32;
jz @@LastLineInnerLoopEnd2;
// do the final few elements
@@LastLineInnerLoop2:
{$IFDEF AVXSUP}vmovsd xmm1, [r8 + rax]; {$ELSE}db $C4,$C1,$7B,$10,$0C,$00;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm3, [rdi + rax]; {$ELSE}db $C5,$FB,$10,$1C,$07;{$ENDIF}
// multiply and add
{$IFDEF AVXSUP}vmulsd xmm3, xmm3, xmm1; {$ELSE}db $C5,$E3,$59,$D9;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm3; {$ELSE}db $C5,$FB,$58,$C3;{$ENDIF}
// next element
add rax, 8;
jnz @@LastLineInnerLoop2;
@@LastLineInnerLoopEnd2:
// final add and compact result
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF}
// store back result
{$IFDEF AVXSUP}vmovsd [r13], xmm0; {$ELSE}db $C4,$C1,$7B,$11,$45,$00;{$ENDIF}
// ################################################
// #### next line of mt1
@@NextLine:
// dec(mt2, Width2);
// inc(PByte(mt1), LineWidth1);
// inc(PByte(dest), destOffset);
//mov ebx, bytesWidth2;
//sub dword ptr [mt2], ebx;
add r8, LineWidth1;
add rcx, rdx;
// end for y := 0 to height1 - 1
dec r11;
jnz @@forylabel;
// epilog - cleanup stack
mov rbx, iRBX;
mov rsi, iRSI;
mov rdi, iRDI;
mov r12, iR12;
mov r13, iR13;
mov r14, iR14;
mov r15, iR15;
{$IFDEF AVXSUP}vmovupd xmm4, [rsp + $00]; {$ELSE}db $C5,$F9,$10,$24,$24;{$ENDIF}
add rsp, $10;
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
end;
{$ENDIF}
end.