-
Notifications
You must be signed in to change notification settings - Fork 35
/
FMAMatrixMultOperations.pas
944 lines (711 loc) · 33.5 KB
/
FMAMatrixMultOperations.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
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
// ###################################################################
// #### 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 FMAMatrixMultOperations;
interface
{$I 'mrMath_CPU.inc'}
{$IFNDEF x64}
// full matrix operations
procedure FMAMatrixMultAligned(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1, height1, width2, height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
procedure FMAMatrixMultUnAligned(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1, height1, width2, height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
// some special types of multiplications used e.g. in QR Decomposition
// dest = mt1'*mt2; where mt2 is a lower triangular matrix and the operation is transposition
// the function assumes a unit diagonal (does not touch the real middle elements)
// width and height values are assumed to be the "original" (non transposed) ones
procedure FMAMtxMultTria2T1(dest : PDouble; LineWidthDest : NativeInt; mt1 : PDouble; LineWidth1 : NativeInt; mt2 : PDouble; LineWidth2 : NativeInt;
width1, height1, width2, height2 : NativeInt); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
// mt1 = mt1*mt2'; where mt2 is an upper triangular matrix
procedure FMAMtxMultTria2T1StoreT1(mt1 : PDouble; LineWidth1 : NativeInt; mt2 : PDouble; LineWidth2 : NativeInt;
width1, height1, width2, height2 : NativeInt); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
// W = C1*V1*T -> V1 is an upper triangular matrix with assumed unit diagonal entries. Operation on V1 transposition
procedure FMAMtxMultTria2TUpperUnit(dest : PDouble; LineWidthDest : NativeInt; mt1 : PDouble; LineWidth1 : NativeInt; mt2 : PDouble; LineWidth2 : NativeInt;
width1, height1, width2, height2 : NativeInt); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
// calculates mt1 = mt1*mt2', mt2 = lower triangular matrix. diagonal elements are assumed to be 1!
procedure FMAMtxMultLowTria2T2Store1(mt1 : PDouble; LineWidth1 : NativeInt; mt2 : PDouble; LineWidth2 : NativeInt;
width1, height1, width2, height2 : NativeInt); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
{$ENDIF}
implementation
{$IFNDEF x64}
procedure FMAMatrixMultAligned(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1, height1, width2, height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt);
var bytesWidth2, destOffset : NativeInt;
iter : NativeInt;
// eax = dest; edx = destLineWidth; ecx = mtx
asm
// prolog - simulate stack
push ebx;
push edi;
push esi;
mov edi, width1;
imul edi, -8;
mov iter, edi;
sub ecx, edi;
//destOffset := destLineWidth - Width2*sizeof(double);
mov ebx, Width2;
shl ebx, 3;
sub edx, ebx;
mov destOffset, edx;
//bytesWidth2 := width2*sizeof(double);
mov bytesWidth2, ebx;
// for y := 0 to height1 - 1 do
@@foryloop:
mov esi, width2;
sub esi, 2;
jl @LastXColumn;
@@forxloop:
// for x := 0 to width2 div 2 - 1
// esi: mt1 - width1*sizeof(double)
// mt2: mt2
//mov ecx, mt1;
mov ebx, mt2;
mov edx, iter;
mov edi, LineWidth2;
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF}
{$IFDEF AVXSUP}vxorpd ymm1, ymm1, ymm1; {$ELSE}db $C5,$F5,$57,$C9;{$ENDIF}
cmp edx, -32;
jg @@Innerloop2Begin;
// for z := 0 to width1 - 1do
// FMA part:
@@InnerLoop1:
// 4x4 block
{$IFDEF AVXSUP}vmovapd xmm2, [ebx]; {$ELSE}db $C5,$F9,$28,$13;{$ENDIF}
add ebx, edi;
{$IFDEF AVXSUP}vmovapd xmm4, xmm2; {$ELSE}db $C5,$F9,$29,$D4;{$ENDIF}
{$IFDEF AVXSUP}vmovapd xmm3, [ebx]; {$ELSE}db $C5,$F9,$28,$1B;{$ENDIF}
add ebx, edi;
// shuffle so we can multiply
// swap such that we can immediately multiply
{$IFDEF AVXSUP}vmovlhps xmm2, xmm2, xmm3; {$ELSE}db $C5,$E8,$16,$D3;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$12,$DC;{$ENDIF}
// next 4 elements
{$IFDEF AVXSUP}vmovapd xmm4, [ebx]; {$ELSE}db $C5,$F9,$28,$23;{$ENDIF}
add ebx, edi;
{$IFDEF AVXSUP}vmovapd xmm6, xmm4; {$ELSE}db $C5,$F9,$29,$E6;{$ENDIF}
{$IFDEF AVXSUP}vmovapd xmm5, [ebx]; {$ELSE}db $C5,$F9,$28,$2B;{$ENDIF}
add ebx, edi;
{$IFDEF AVXSUP}vmovapd ymm7, [ecx + edx] {$ELSE}db $C5,$FD,$28,$3C,$11;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm4, xmm4, xmm5; {$ELSE}db $C5,$D8,$16,$E5;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm5, xmm5, xmm6; {$ELSE}db $C5,$D0,$12,$EE;{$ENDIF}
{$IFDEF AVXSUP}vinsertf128 ymm2, ymm2, xmm4, 1; {$ELSE}db $C4,$E3,$6D,$18,$D4,$01;{$ENDIF}
{$IFDEF AVXSUP}vinsertf128 ymm3, ymm3, xmm5, 1; {$ELSE}db $C4,$E3,$65,$18,$DD,$01;{$ENDIF}
// now multiply and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm2, ymm7; {$ELSE}db $C4,$E2,$ED,$B8,$C7;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm1, ymm3, ymm7; {$ELSE}db $C4,$E2,$E5,$B8,$CF;{$ENDIF}
add edx, 32;
jl @@InnerLoop1;
{$IFDEF AVXSUP}vextractf128 xmm2, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C2,$01;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm3, ymm1, 1; {$ELSE}db $C4,$E3,$7D,$19,$CB,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm2; {$ELSE}db $C5,$F9,$7C,$C2;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm1, xmm1, xmm3; {$ELSE}db $C5,$F1,$7C,$CB;{$ENDIF}
test edx, edx;
jz @@InnerLoopEnd2;
@@Innerloop2Begin:
// rest in single elements
@@InnerLoop2:
{$IFDEF AVXSUP}vmovapd xmm2, [ebx]; {$ELSE}db $C5,$F9,$28,$13;{$ENDIF}
add ebx, edi;
{$IFDEF AVXSUP}vmovddup xmm3, [ecx + edx]; {$ELSE}db $C5,$FB,$12,$1C,$11;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm2, xmm2, xmm3; {$ELSE}db $C5,$E9,$59,$D3;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm2; {$ELSE}db $C5,$D8,$12,$E2;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm2; {$ELSE}db $C5,$FB,$58,$C2;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm1, xmm1, xmm4; {$ELSE}db $C5,$F3,$58,$CC;{$ENDIF}
add edx, 8;
jnz @@InnerLoop2;
@@InnerLoopEnd2:
// finall horizontal addition
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm1; {$ELSE}db $C5,$F9,$7C,$C1;{$ENDIF}
{$IFDEF AVXSUP}vmovapd [eax], xmm0; {$ELSE}db $C5,$F9,$29,$00;{$ENDIF}
// increment the pointers
// inc(mt2), inc(dest);
//add dword ptr [mt2], 8;
add mt2, 16;
add eax, 16;
// end for x := 0 to width2 div 2 - 1
sub esi, 2;
jge @@forxloop;
@LastXColumn:
cmp esi, -1;
jne @NextLine;
// last column of mt2
mov edx, iter;
mov ebx, mt2;
{$IFDEF AVXSUP}vxorpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$57,$C0;{$ENDIF}
@InnerLoop2:
{$IFDEF AVXSUP}vmovsd xmm1, [ecx + edx]; {$ELSE}db $C5,$FB,$10,$0C,$11;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm2, [ebx]; {$ELSE}db $C5,$FB,$10,$13;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm1, xmm1, xmm2; {$ELSE}db $C5,$F3,$59,$CA;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm1; {$ELSE}db $C5,$FB,$58,$C1;{$ENDIF}
add ebx, edi;
add edx, 8;
jnz @InnerLoop2;
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF}
add eax, 8;
add mt2, 8;
@NextLine:
// dec(mt2, Width2);
// inc(PByte(mt1), LineWidth1);
// inc(PByte(dest), destOffset);
//mov ebx, bytesWidth2;
//sub dword ptr [mt2], ebx;
mov edx, bytesWidth2;
sub mt2, edx;
add eax, destOffset;
add ecx, LineWidth1;
// end for y := 0 to height1 - 1
dec height1;
jnz @@foryloop;
// epilog
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
pop esi;
pop edi;
pop ebx;
end;
procedure FMAMatrixMultUnAligned(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1, height1, width2, height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt);
var bytesWidth2, destOffset : NativeInt;
iter : NativeInt;
// eax = dest; edx = destLineWidth; ecx = mtx
asm
// prolog - simulate stack
push ebx;
push edi;
push esi;
mov edi, width1;
imul edi, -8;
mov iter, edi;
sub ecx, edi;
//destOffset := destLineWidth - Width2*sizeof(double);
mov ebx, Width2;
shl ebx, 3;
sub edx, ebx;
mov destOffset, edx;
//bytesWidth2 := width2*sizeof(double);
mov bytesWidth2, ebx;
// for y := 0 to height1 - 1 do
@@foryloop:
mov esi, width2;
sub esi, 2;
jl @LastXColumn;
@@forxloop:
// for x := 0 to width2 div 2 - 1
// esi: mt1 - width1*sizeof(double)
// mt2: mt2
//mov ecx, mt1;
mov ebx, mt2;
mov edx, iter;
mov edi, LineWidth2;
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF}
{$IFDEF AVXSUP}vxorpd ymm1, ymm1, ymm1; {$ELSE}db $C5,$F5,$57,$C9;{$ENDIF}
cmp edx, -32;
jg @@Innerloop2Begin;
// for z := 0 to width1 - 1do
// AVX part:
@@InnerLoop1:
// 4x4 block
{$IFDEF AVXSUP}vmovupd xmm2, [ebx]; {$ELSE}db $C5,$F9,$10,$13;{$ENDIF}
add ebx, edi;
{$IFDEF AVXSUP}vmovupd xmm4, xmm2; {$ELSE}db $C5,$F9,$11,$D4;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm3, [ebx]; {$ELSE}db $C5,$F9,$10,$1B;{$ENDIF}
add ebx, edi;
// shuffle so we can multiply
// swap such that we can immediately multiply
{$IFDEF AVXSUP}vmovlhps xmm2, xmm2, xmm3; {$ELSE}db $C5,$E8,$16,$D3;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$12,$DC;{$ENDIF}
// next 4 elements
{$IFDEF AVXSUP}vmovupd xmm4, [ebx]; {$ELSE}db $C5,$F9,$10,$23;{$ENDIF}
add ebx, edi;
{$IFDEF AVXSUP}vmovupd xmm6, xmm4; {$ELSE}db $C5,$F9,$11,$E6;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm5, [ebx]; {$ELSE}db $C5,$F9,$10,$2B;{$ENDIF}
add ebx, edi;
{$IFDEF AVXSUP}vmovupd ymm7, [ecx + edx] {$ELSE}db $C5,$FD,$10,$3C,$11;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm4, xmm4, xmm5; {$ELSE}db $C5,$D8,$16,$E5;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm5, xmm5, xmm6; {$ELSE}db $C5,$D0,$12,$EE;{$ENDIF}
{$IFDEF AVXSUP}vinsertf128 ymm2, ymm2, xmm4, 1; {$ELSE}db $C4,$E3,$6D,$18,$D4,$01;{$ENDIF}
{$IFDEF AVXSUP}vinsertf128 ymm3, ymm3, xmm5, 1; {$ELSE}db $C4,$E3,$65,$18,$DD,$01;{$ENDIF}
// now multiply and add
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm2, ymm7; {$ELSE}db $C4,$E2,$ED,$B8,$C7;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm1, ymm3, ymm7; {$ELSE}db $C4,$E2,$E5,$B8,$CF;{$ENDIF}
add edx, 32;
jl @@InnerLoop1;
{$IFDEF AVXSUP}vextractf128 xmm2, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C2,$01;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm3, ymm1, 1; {$ELSE}db $C4,$E3,$7D,$19,$CB,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm2; {$ELSE}db $C5,$F9,$7C,$C2;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm1, xmm1, xmm3; {$ELSE}db $C5,$F1,$7C,$CB;{$ENDIF}
test edx, edx;
jz @@InnerLoopEnd2;
@@Innerloop2Begin:
// rest in single elements
@@InnerLoop2:
{$IFDEF AVXSUP}vmovupd xmm2, [ebx]; {$ELSE}db $C5,$F9,$10,$13;{$ENDIF}
add ebx, edi;
{$IFDEF AVXSUP}vmovddup xmm3, [ecx + edx]; {$ELSE}db $C5,$FB,$12,$1C,$11;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm2, xmm2, xmm3; {$ELSE}db $C5,$E9,$59,$D3;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm2; {$ELSE}db $C5,$D8,$12,$E2;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm2; {$ELSE}db $C5,$FB,$58,$C2;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm1, xmm1, xmm4; {$ELSE}db $C5,$F3,$58,$CC;{$ENDIF}
add edx, 8;
jnz @@InnerLoop2;
@@InnerLoopEnd2:
// finall horizontal addition
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm1; {$ELSE}db $C5,$F9,$7C,$C1;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [eax], xmm0; {$ELSE}db $C5,$F9,$11,$00;{$ENDIF}
// increment the pointers
// inc(mt2), inc(dest);
//add dword ptr [mt2], 8;
add mt2, 16;
add eax, 16;
// end for x := 0 to width2 div 2 - 1
sub esi, 2;
jge @@forxloop;
@LastXColumn:
cmp esi, -1;
jne @NextLine;
// last column of mt2
mov edx, iter;
mov ebx, mt2;
{$IFDEF AVXSUP}vxorpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$57,$C0;{$ENDIF}
@InnerLoop2:
{$IFDEF AVXSUP}vmovsd xmm1, [ecx + edx]; {$ELSE}db $C5,$FB,$10,$0C,$11;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm2, [ebx]; {$ELSE}db $C5,$FB,$10,$13;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm1, xmm1, xmm2; {$ELSE}db $C5,$F3,$59,$CA;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm1; {$ELSE}db $C5,$FB,$58,$C1;{$ENDIF}
add ebx, edi;
add edx, 8;
jnz @InnerLoop2;
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF}
add eax, 8;
add mt2, 8;
@NextLine:
// dec(mt2, Width2);
// inc(PByte(mt1), LineWidth1);
// inc(PByte(dest), destOffset);
//mov ebx, bytesWidth2;
//sub dword ptr [mt2], ebx;
mov edx, bytesWidth2;
sub mt2, edx;
add eax, destOffset;
add ecx, LineWidth1;
// end for y := 0 to height1 - 1
dec height1;
jnz @@foryloop;
// epilog
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
pop esi;
pop edi;
pop ebx;
end;
// ###########################################
// #### Special multiplication routines (for now only used in QR Decomposition)
// ###########################################
procedure FMAMtxMultTria2T1StoreT1(mt1 : PDouble; LineWidth1 : NativeInt; mt2 : PDouble; LineWidth2 : NativeInt;
width1, height1, width2, height2 : NativeInt);
var iter : NativeInt;
testExitLoopVal : NativeInt;
asm
// prolog: stack
push ebx;
push edi;
push esi;
//iter := -width1*sizeof(double);
mov esi, width1;
imul esi, -8;
mov iter, esi;
// testExitLoopVal := height2*sizeof(double) + iter;
mov edi, height2;
shl edi, 3; //*8
add edi, esi;
mov testExitLoopVal, edi;
// eax := mt1
sub eax, esi; // mt1 - iter
// for y loop
@@foryloop:
mov esi, iter;
push ecx;
sub ecx, esi;
@@forxloop:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF} // temp := 0
mov edi, esi; // loop counter x;
// test if height2 > width1 and loop counter > width1
test edi, edi;
jge @@foriloopend;
// in case x is odd -> handle the first element separately
test edi, $E;
jz @@foriloopFMA;
// single element handling
{$IFDEF AVXSUP}vmovsd xmm1, [eax + edi]; {$ELSE}db $C5,$FB,$10,$0C,$38;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm2, [ecx + edi]; {$ELSE}db $C5,$FB,$10,$14,$39;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm1, xmm1, xmm2; {$ELSE}db $C5,$F3,$59,$CA;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm1; {$ELSE}db $C5,$FB,$58,$C1;{$ENDIF}
add edi, 8;
@@foriloopFMA:
// 4 elements at a time
add edi, 32;
jg @@foriloopFMAend;
{$IFDEF AVXSUP}vmovupd ymm1, [eax + edi - 32]; {$ELSE}db $C5,$FD,$10,$4C,$38,$E0;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm2, [ecx + edi - 32]; {$ELSE}db $C5,$FD,$10,$54,$39,$E0;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, ymm2; {$ELSE}db $C4,$E2,$F5,$B8,$C2;{$ENDIF}
jmp @@foriloopFMA;
@@foriloopFMAend:
{$IFDEF AVXSUP}vextractf128 xmm2, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C2,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm2; {$ELSE}db $C5,$F9,$7C,$C2;{$ENDIF}
sub edi, 32;
jz @@foriloopend;
// for i := x to width1 - 1
@@foriloop:
// two elements at a time:
{$IFDEF AVXSUP}vmovupd xmm1, [eax + edi]; {$ELSE}db $C5,$F9,$10,$0C,$38;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm2, [ecx + edi]; {$ELSE}db $C5,$F9,$10,$14,$39;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd xmm0, xmm1, xmm2; {$ELSE}db $C4,$E2,$F1,$B8,$C2;{$ENDIF}
add edi, 16;
jnz @@foriloop;
@@foriloopend:
// final result
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + esi], xmm0; {$ELSE}db $C5,$FB,$11,$04,$30;{$ENDIF}
add ecx, LineWidth2;
add esi, 8;
cmp esi, testExitLoopVal;
jne @@forxloop;
// restore mt2
pop ecx;
add eax, edx;
dec height1;
jnz @@foryloop;
// epilog
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
pop esi;
pop edi;
pop ebx;
end;
procedure FMAMtxMultTria2TUpperUnit(dest : PDouble; LineWidthDest : NativeInt; mt1 : PDouble; LineWidth1 : NativeInt; mt2 : PDouble; LineWidth2 : NativeInt;
width1, height1, width2, height2 : NativeInt);
var iter : NativeInt;
testExitLoopVal : NativeInt;
asm
// prolog: stack
push ebx;
push edi;
push esi;
//iter := -width1*sizeof(double);
mov ebx, width1;
imul ebx, -8;
mov iter, ebx;
sub mt2, ebx;
// ecx := mt1
sub eax, ebx;
sub ecx, ebx; // mt1 - iter
// testExitLoopVal := height2*sizeof(double) + iter;
mov edi, height2;
shl edi, 3; //*8
add edi, ebx;
mov testExitLoopVal, edi;
// for y loop
@@foryloop:
mov ebx, mt2;
//sub ebx, iter;
mov esi, iter;
@@forxloop:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF} // temp := 0
mov edi, esi; // loop counter x;
// test if height2 > width1 and loop counter > width1
test edi, edi;
jge @@foriloopend;
// in case x is odd -> handle the first element separately
//and esi, $E;
test edi, $E;
jz @@foriloopinit;
// single element handling -> mt1 first element is assumed unit!
{$IFDEF AVXSUP}vmovsd xmm0, [ecx + edi]; {$ELSE}db $C5,$FB,$10,$04,$39;{$ENDIF}
add edi, 8;
jmp @@AfterLoopInit;
@@foriloopinit:
test edi, edi;
jz @@foriloopend;
// two elements init at a time:
{$IFDEF AVXSUP}vmovsd xmm0, [ecx + edi]; {$ELSE}db $C5,$FB,$10,$04,$39;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm1, [ecx + edi + 8]; {$ELSE}db $C5,$FB,$10,$4C,$39,$08;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm2, [ebx + edi + 8]; {$ELSE}db $C5,$FB,$10,$54,$3B,$08;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm1, xmm1, xmm2; {$ELSE}db $C5,$F3,$59,$CA;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm1; {$ELSE}db $C5,$FB,$58,$C1;{$ENDIF}
add edi, 16;
@@AfterLoopInit:
// in case the last single x element was handled we do not need further looping
test edi, edi;
jz @@finalizeloop;
// for i := x to width1 - 1
@@foriloop:
add edi, 32;
jg @@foriloopend;
// 4 elements at a time:
{$IFDEF AVXSUP}vmovupd ymm1, [ecx + edi - 32]; {$ELSE}db $C5,$FD,$10,$4C,$39,$E0;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm2, [ebx + edi - 32]; {$ELSE}db $C5,$FD,$10,$54,$3B,$E0;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, ymm2; {$ELSE}db $C4,$E2,$F5,$B8,$C2;{$ENDIF}
jmp @@foriloop;
@@foriloopend:
{$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}
sub edi, 32;
// test if we missed 2 elements
jz @@finalizeloop;
// need to process two more elements:
{$IFDEF AVXSUP}vmovupd xmm1, [ecx + edi]; {$ELSE}db $C5,$F9,$10,$0C,$39;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm2, [ebx + edi]; {$ELSE}db $C5,$F9,$10,$14,$3B;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd xmm0, xmm1, xmm2; {$ELSE}db $C4,$E2,$F1,$B8,$C2;{$ENDIF}
@@finalizeloop:
// final result
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + esi], xmm0; {$ELSE}db $C5,$FB,$11,$04,$30;{$ENDIF}
add ebx, LineWidth2;
add esi, 8;
cmp esi, testExitLoopVal;
jne @@forxloop;
add ecx, LineWidth1;
add eax, edx;
dec height1;
jnz @@foryloop;
// epilog
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
pop esi;
pop edi;
pop ebx;
end;
// note the result is stored in mt2 again!
// dest = mt1'*mt2; where mt2 is a lower triangular matrix and the operation is transposition
// the function assumes a unit diagonal (does not touch the real middle elements)
// width and height values are assumed to be the "original" (non transposed) ones
procedure FMAMtxMultTria2T1(dest : PDouble; LineWidthDest : NativeInt; mt1 : PDouble; LineWidth1 : NativeInt;
mt2 : PDouble; LineWidth2 : NativeInt;
width1, height1, width2, height2 : NativeInt);
var pMt2 : PDouble;
width2D2 : NativeInt;
aLineWidthDest : NativeInt;
asm
// Prolog
push ebx;
push esi;
push edi;
mov aLineWidthDest, edx;
// width2D2 := width2 div 2;
mov ebx, width2;
shr ebx, 1;
mov width2D2, ebx;
// for x := 0 to width1 - 1 do
@@forxloop:
// pMT2 := mt2;
// pDest := dest;
push eax; // eax is pDest
mov ebx, mt2;
mov pMT2, ebx;
// for y := 0 to width2D2 - 1 do
mov edx, width2D2;
test edx, edx;
jz @@foryloopend;
xor edx, edx;
@@foryloop:
// valCounter1 := PConstDoubleArr(mt1);
// inc(PByte(valCounter1), 2*y*LineWidth1);
mov esi, mt1;
mov ebx, edx;
add ebx, ebx;
imul ebx, LineWidth1;
add esi, ebx;
// valCounter2 := PConstDoubleArr(pMT2);
// inc(PByte(valCounter2), (2*y + 1)*LineWidth2);
mov edi, pMt2;
mov ebx, edx;
add ebx, ebx;
imul ebx, LineWidth2;
add ebx, LineWidth2;
add edi, ebx;
// tmp[0] := valCounter1^[0];
// inc(PByte(valCounter1), LineWidth1);
{$IFDEF AVXSUP}vmovsd xmm0, [esi]; {$ELSE}db $C5,$FB,$10,$06;{$ENDIF}
add esi, LineWidth1;
// if height2 - 2*y - 1 > 0 then
mov ebx, edx;
add ebx, ebx;
inc ebx;
cmp ebx, height2;
jnl @@PreInnerLoop;
// tmp[0] := tmp[0] + valCounter1^[0]*valCounter2^[0];
// tmp[1] := valCounter1^[0];
{$IFDEF AVXSUP}vmovsd xmm1, [esi]; {$ELSE}db $C5,$FB,$10,$0E;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm0, xmm0, xmm1; {$ELSE}db $C5,$F8,$16,$C1;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm1, xmm1, [edi]; {$ELSE}db $C5,$F3,$59,$0F;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm1; {$ELSE}db $C5,$FB,$58,$C1;{$ENDIF}
//inc(PByte(valCounter1), LineWidth1);
//inc(PByte(valCounter2), LineWidth2);
add esi, LineWidth1;
add edi, LineWidth2;
@@PreInnerLoop:
// rest is a double column!
// prepare loop
mov ebx, height2;
sub ebx, edx;
sub ebx, edx;
sub ebx, 2;
test ebx, ebx;
jle @@InnerLoopEnd;
@@InnerLoop:
// tmp[0] := tmp[0] + valCounter1^[0]*valCounter2^[0];
// tmp[1] := tmp[1] + valCounter1^[0]*valCounter2^[1];
{$IFDEF AVXSUP}vmovddup xmm1, [esi]; {$ELSE}db $C5,$FB,$12,$0E;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm2, [edi]; {$ELSE}db $C5,$F9,$10,$17;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd xmm0, xmm1, xmm2; {$ELSE}db $C4,$E2,$F1,$B8,$C2;{$ENDIF}
//inc(PByte(valCounter1), LineWidth1);
//inc(PByte(valCounter2), LineWidth2);
add esi, LineWidth1;
add edi, LineWidth2;
dec ebx;
jnz @@InnerLoop;
@@InnerLoopEnd:
// write back result
// pDest^ := tmp[0];
// PDouble(NativeUint(pDest) + sizeof(double))^ := tmp[1];
{$IFDEF AVXSUP}vmovupd [eax], xmm0; {$ELSE}db $C5,$F9,$11,$00;{$ENDIF}
// inc(pDest, 2);
// inc(pMT2, 2);
add pMT2, 16;
add eax, 16;
// end foryloop
inc edx;
cmp edx, width2D2;
jne @@foryloop;
@@foryloopend:
//if (width2 and $01) = 1 then
mov edx, width2;
and edx, 1;
jz @@ifend1;
// special handling of last column (just copy the value)
// valCounter1 := PConstDoubleArr(mt1);
mov edx, ecx;
//inc(PByte(valCounter1), LineWidth1*(height1 - 1));
mov ebx, height1;
dec ebx;
imul ebx, LineWidth1;
// pDest^ := valCounter1^[0];
{$IFDEF AVXSUP}vmovsd xmm0, [edx + ebx]; {$ELSE}db $C5,$FB,$10,$04,$1A;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF}
@@ifend1:
//inc(mt1);
//inc(PByte(dest), LineWidthDest);
pop eax;
add ecx, 8;
add eax, aLineWidthDest;
// end for loop
dec Width1;
jnz @@forxloop;
// epilog
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
pop edi;
pop esi;
pop ebx;
end;
// calculates mt1 = mt1*mt2', mt2 = lower triangular matrix. diagonal elements are assumed to be 1!
procedure FMAMtxMultLowTria2T2Store1(mt1 : PDouble; LineWidth1 : NativeInt; mt2 : PDouble; LineWidth2 : NativeInt;
width1, height1, width2, height2 : NativeInt);
var aLineWidth1 : NativeInt;
aMt1 : PDouble;
// eax = mt1, edx = LineWidth1, ecx = mt2
asm
push ebx;
push edi;
push esi;
// init
mov aMt1, eax;
mov aLineWidth1, edx;
// iter := -(width2 - 1)*sizeof(double);
mov edx, width2;
dec edx;
imul edx, -8;
// start from bottom
// ebx: mt2
// inc(PByte(mt2),(height2 - 1)*LineWidth2);
mov esi, height2;
dec esi;
imul esi, LineWidth2;
add ecx, esi;
sub ecx, edx;
// for x := 0 to width2 - 2
dec width2;
jz @@endproc;
@@forxloop:
mov eax, aMt1;
sub eax, edx;
// for y := 0 to height1 - 1
mov esi, height1;
@@foryloop:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF}
// for idx := 0 to width2 - x - 2
mov edi, edx;
test edi, edi;
jz @@foridxloopend;
// unrolled loop 4x2
add edi, 64;
jg @@foridxloopSSE;
@@foridxlongloop:
{$IFDEF AVXSUP}vmovupd ymm1, [eax + edi - 64]; {$ELSE}db $C5,$FD,$10,$4C,$38,$C0;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm2, [ecx + edi - 64]; {$ELSE}db $C5,$FD,$10,$54,$39,$C0;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, ymm2; {$ELSE}db $C4,$E2,$F5,$B8,$C2;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm1, [eax + edi - 32]; {$ELSE}db $C5,$FD,$10,$4C,$38,$E0;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm2, [ecx + edi - 32]; {$ELSE}db $C5,$FD,$10,$54,$39,$E0;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm1, ymm2; {$ELSE}db $C4,$E2,$F5,$B8,$C2;{$ENDIF}
add edi, 64;
jl @@foridxlongloop;
{$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}
// sse part
@@foridxloopSSE:
sub edi, 48;
jg @@foridxloopstart;
@@foridxSSEloop:
{$IFDEF AVXSUP}vmovupd xmm1, [eax + edi - 16]; {$ELSE}db $C5,$F9,$10,$4C,$38,$F0;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm2, [ecx + edi - 16]; {$ELSE}db $C5,$F9,$10,$54,$39,$F0;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd xmm0, xmm1, xmm2; {$ELSE}db $C4,$E2,$F1,$B8,$C2;{$ENDIF}
add edi, 16;
jl @@foridxSSEloop;
@@foridxloopStart:
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF}
sub edi, 16;
jz @@foridxloopend;
@@foridxloop:
{$IFDEF AVXSUP}vmovsd xmm1, [eax + edi]; {$ELSE}db $C5,$FB,$10,$0C,$38;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm2, [ecx + edi]; {$ELSE}db $C5,$FB,$10,$14,$39;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm1, xmm1, xmm2; {$ELSE}db $C5,$F3,$59,$CA;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm1; {$ELSE}db $C5,$FB,$58,$C1;{$ENDIF}
add edi, 8;
jnz @@foridxloop;
@@foridxloopend:
// last element is unit:
{$IFDEF AVXSUP}vmovsd xmm1, [eax]; {$ELSE}db $C5,$FB,$10,$08;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm1; {$ELSE}db $C5,$FB,$58,$C1;{$ENDIF}
// write back
// PConstDoubleArr(pMt1)^[width2 - x - 1] := tmp + valCounter1^[width2 - x - 1];
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF}
add eax, aLineWidth1;
dec esi;
jnz @@foryloop;
// dec(PByte(mt2), LineWidth2);
sub ecx, LineWidth2;
sub ecx, 8;
// adjust iterator to the next x value for the idxloop
add edx, 8;
dec width2;
jnz @@forxloop;
@@endproc:
// epilog: stack fixing
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
pop esi;
pop edi;
pop ebx;
end;
{$ENDIF}
end.