-
Notifications
You must be signed in to change notification settings - Fork 35
/
FMAMatrixVectorMultOperations.pas
1135 lines (861 loc) · 53.7 KB
/
FMAMatrixVectorMultOperations.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
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
// ###################################################################
// #### 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 FMAMatrixVectorMultOperations;
// #######################################################
// #### special routines for matrix vector multiplications.
// #######################################################
interface
{$I 'mrMath_CPU.inc'}
{$IFNDEF x64}
procedure FMAMatrixVectMultT(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
// routines with special input layouts: LineWidthV needs to be sizeof(double)
procedure FMAMatrixVectMultAlignedVAligned(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
procedure FMAMatrixVectMultUnAlignedVAligned(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
// destlinewidth needs to be sizeof(double)!
// no speed gain agains amsmatrixVectMultT
procedure FMAMatrixVecMultTDestVec(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
// rank1 update: A = A + alpha*X*Y' where x and y are vectors. It's assumed that y is sequential
procedure FMARank1UpdateSeq(A : PDouble; const LineWidthA : NativeInt; width, height : NativeInt;
X, Y : PDouble; incX, incY : NativeInt; alpha : double); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
procedure FMARank1UpdateSeqAligned(A : PDouble; const LineWidthA : NativeInt; width, height : NativeInt;
X, Y : PDouble; incX, incY : NativeInt; alpha : double); {$IFDEF FPC} assembler; {$ELSE} register; {$ENDIF}
{$ENDIF}
implementation
{$IFNDEF x64}
// routines with special input layouts: LineWidthV needs to be sizeof(double)
procedure FMAMatrixVectMultAlignedVAligned(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double);
var aDestLineWidth : NativeInt;
asm
// prolog - simulate stack
push ebx;
push edi;
push esi;
mov aDestLineWidth, edx;
// for the final multiplication
lea ebx, alpha;
{$IFDEF AVXSUP}vmovsd xmm6, [ebx]; {$ELSE}db $C5,$FB,$10,$33;{$ENDIF}
lea ebx, beta;
{$IFDEF AVXSUP}vmovhpd xmm6, xmm6, [ebx]; {$ELSE}db $C5,$C9,$16,$33;{$ENDIF}
// prepare for loop
mov esi, LineWidthMT;
sub width, 4;
sub height, 4;
js @@foryloopend;
// we need at least a width of 4 for the fast unrolled loop
cmp width, 0;
jl @@foryloopend;
// init for x := 0 to width - 1:
@@foryloop:
// init values:
{$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}
{$IFDEF AVXSUP}vxorpd ymm2, ymm2, ymm2; {$ELSE}db $C5,$ED,$57,$D2;{$ENDIF}
{$IFDEF AVXSUP}vxorpd ymm3, ymm3, ymm3; {$ELSE}db $C5,$E5,$57,$DB;{$ENDIF} // res := 0;
push ecx; // ecx = first matrix element
mov ebx, v; // ebx = first vector element
mov edx, width; // edx = width
@@forxloop:
{$IFDEF AVXSUP}vmovapd ymm4, [ebx]; {$ELSE}db $C5,$FD,$28,$23;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm4, [ecx]; {$ELSE}db $C4,$E2,$DD,$B8,$01;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm1, ymm4, [ecx + esi]; {$ELSE}db $C4,$E2,$DD,$B8,$0C,$31;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm4, [ecx + 2*esi]; {$ELSE}db $C4,$E2,$DD,$B8,$14,$71;{$ENDIF}
add ecx, esi;
{$IFDEF AVXSUP}vfmadd231pd ymm3, ymm4, [ecx + 2*esi]; {$ELSE}db $C4,$E2,$DD,$B8,$1C,$71;{$ENDIF}
sub ecx, esi;
add ecx, 32;
add ebx, 32;
sub edx, 4;
jge @@forxloop;
{$IFDEF AVXSUP}vextractf128 xmm4, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C4,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm4; {$ELSE}db $C5,$F9,$7C,$C4;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm5, ymm1, 1; {$ELSE}db $C4,$E3,$7D,$19,$CD,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm1, xmm1, xmm5; {$ELSE}db $C5,$F1,$7C,$CD;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm4, ymm2, 1; {$ELSE}db $C4,$E3,$7D,$19,$D4,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm4; {$ELSE}db $C5,$E9,$7C,$D4;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm5, ymm3, 1; {$ELSE}db $C4,$E3,$7D,$19,$DD,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm3, xmm3, xmm5; {$ELSE}db $C5,$E1,$7C,$DD;{$ENDIF}
// special treatment for the last value(s):
add edx, 4;
jz @@resbuild;
@@shortloopx:
{$IFDEF AVXSUP}vmovsd xmm4, [ebx]; {$ELSE}db $C5,$FB,$10,$23;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm5, [ecx]; {$ELSE}db $C5,$FB,$10,$29;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm5; {$ELSE}db $C5,$FB,$58,$C5;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm5, [ecx + esi]; {$ELSE}db $C5,$FB,$10,$2C,$31;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm1, xmm1, xmm5; {$ELSE}db $C5,$F3,$58,$CD;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm5, [ecx + 2*esi]; {$ELSE}db $C5,$FB,$10,$2C,$71;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm2, xmm2, xmm5; {$ELSE}db $C5,$EB,$58,$D5;{$ENDIF}
add ecx, esi;
{$IFDEF AVXSUP}vmovsd xmm5, [ecx + 2*esi]; {$ELSE}db $C5,$FB,$10,$2C,$71;{$ENDIF}
sub ecx, esi;
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm3, xmm3, xmm5; {$ELSE}db $C5,$E3,$58,$DD;{$ENDIF}
add ecx, 8;
add ebx, 8;
sub edx, 1;
jnz @@shortloopx;
@@resbuild:
// result building
// write back result (final addition and compactation)
// calculate dest = beta*dest + alpha*xmm0
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF} // first element
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm5; {$ELSE}db $C5,$F9,$7C,$C5;{$ENDIF} // calculate xmm0_1 + xmm0_2 (store low) xmm5_1 + xmm5_2 (store high)
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // beta * dest + alpha*xmm0
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF} // final add
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF} // store back
add eax, adestLineWidth;
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm1, xmm1, xmm5; {$ELSE}db $C5,$F1,$7C,$CD;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm1, xmm1, xmm6; {$ELSE}db $C5,$F1,$59,$CE;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm1, xmm1, xmm1; {$ELSE}db $C5,$F1,$7C,$C9;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm1; {$ELSE}db $C5,$FB,$11,$08;{$ENDIF}
add eax, adestLineWidth;
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm5; {$ELSE}db $C5,$E9,$7C,$D5;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm2, xmm2, xmm6; {$ELSE}db $C5,$E9,$59,$D6;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm2; {$ELSE}db $C5,$E9,$7C,$D2;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm2; {$ELSE}db $C5,$FB,$11,$10;{$ENDIF}
add eax, adestLineWidth;
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm3, xmm3, xmm5; {$ELSE}db $C5,$E1,$7C,$DD;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm6; {$ELSE}db $C5,$E1,$59,$DE;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm3, xmm3, xmm3; {$ELSE}db $C5,$E1,$7C,$DB;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
add eax, adestLineWidth;
pop ecx;
lea ecx, [ecx + 4*esi];
// next rseult
sub height, 4;
jge @@foryloop;
// ###########################################
// #### Remaining rows (max 4 or more if width is <4):
@@foryloopend:
add height, 4;
jz @@vecend;
@@foryshortloop:
// init values:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF}
push ecx; // ecx = first matrix element
mov ebx, v; // ebx = first vector element
mov edx, width; // r10 = width - 4
cmp edx, 0;
jl @@shortloopend;
@@forxshortloop:
{$IFDEF AVXSUP}vmovapd ymm4, [ebx]; {$ELSE}db $C5,$FD,$28,$23;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm4, [ecx]; {$ELSE}db $C4,$E2,$DD,$B8,$01;{$ENDIF}
add ecx, 32;
add ebx, 32;
sub edx, 4;
jge @@forxshortloop;
{$IFDEF AVXSUP}vextractf128 xmm4, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C4,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm4; {$ELSE}db $C5,$F9,$7C,$C4;{$ENDIF}
@@shortloopend:
add edx, 4;
// test if there are elements left
jz @@resbuildshort;
@@forxshortestloop:
{$IFDEF AVXSUP}vmovsd xmm4, [ebx]; {$ELSE}db $C5,$FB,$10,$23;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm5, [ecx]; {$ELSE}db $C5,$FB,$10,$29;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm5; {$ELSE}db $C5,$FB,$58,$C5;{$ENDIF}
add ecx, 8;
add ebx, 8;
dec edx;
jnz @@forxshortestloop;
@@resbuildshort:
// build result
// calculate dest = beta*dest + alpha*xmm0
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm5; {$ELSE}db $C5,$F9,$7C,$C5;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF}
add eax, adestLineWidth;
pop ecx;
add ecx, esi;
dec height;
jnz @@foryshortloop;
@@vecend:
// epilog pop "stack"
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
pop esi;
pop edi;
pop ebx;
end;
procedure FMAMatrixVectMultUnAlignedVAligned(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double);
var aDestLineWidth : NativeInt;
asm
// prolog - simulate stack
push ebx;
push edi;
push esi;
mov aDestLineWidth, edx;
// for the final multiplication
lea ebx, alpha;
{$IFDEF AVXSUP}vmovsd xmm6, [ebx]; {$ELSE}db $C5,$FB,$10,$33;{$ENDIF}
lea ebx, beta;
{$IFDEF AVXSUP}vmovhpd xmm6, xmm6, [ebx]; {$ELSE}db $C5,$C9,$16,$33;{$ENDIF}
// prepare for loop
mov esi, LineWidthMT;
sub width, 4;
sub height, 4;
js @@foryloopend;
// we need at least a width of 4 for the fast unrolled loop
cmp width, 0;
jl @@foryloopend;
// init for x := 0 to width - 1:
@@foryloop:
// init values:
{$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}
{$IFDEF AVXSUP}vxorpd ymm2, ymm2, ymm2; {$ELSE}db $C5,$ED,$57,$D2;{$ENDIF}
{$IFDEF AVXSUP}vxorpd ymm3, ymm3, ymm3; {$ELSE}db $C5,$E5,$57,$DB;{$ENDIF} // res := 0;
push ecx; // ecx = first matrix element
mov ebx, v; // ebx = first vector element
mov edx, width; // edx = width
@@forxloop:
{$IFDEF AVXSUP}vmovupd ymm4, [ebx]; {$ELSE}db $C5,$FD,$10,$23;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm5, [ecx]; {$ELSE}db $C5,$FD,$10,$29;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm4, ymm5; {$ELSE}db $C4,$E2,$DD,$B8,$C5;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm5, [ecx + esi]; {$ELSE}db $C5,$FD,$10,$2C,$31;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm1, ymm4, ymm5; {$ELSE}db $C4,$E2,$DD,$B8,$CD;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm5, [ecx + 2*esi]; {$ELSE}db $C5,$FD,$10,$2C,$71;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm2, ymm4, ymm5; {$ELSE}db $C4,$E2,$DD,$B8,$D5;{$ENDIF}
add ecx, esi;
{$IFDEF AVXSUP}vmovupd ymm5, [ecx + 2*esi]; {$ELSE}db $C5,$FD,$10,$2C,$71;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm3, ymm4, ymm5; {$ELSE}db $C4,$E2,$DD,$B8,$DD;{$ENDIF}
sub ecx, esi;
add ecx, 32;
add ebx, 32;
sub edx, 4;
jge @@forxloop;
{$IFDEF AVXSUP}vextractf128 xmm4, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C4,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm4; {$ELSE}db $C5,$F9,$7C,$C4;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm5, ymm1, 1; {$ELSE}db $C4,$E3,$7D,$19,$CD,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm1, xmm1, xmm5; {$ELSE}db $C5,$F1,$7C,$CD;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm4, ymm2, 1; {$ELSE}db $C4,$E3,$7D,$19,$D4,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm4; {$ELSE}db $C5,$E9,$7C,$D4;{$ENDIF}
{$IFDEF AVXSUP}vextractf128 xmm5, ymm3, 1; {$ELSE}db $C4,$E3,$7D,$19,$DD,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm3, xmm3, xmm5; {$ELSE}db $C5,$E1,$7C,$DD;{$ENDIF}
// special treatment for the last value(s):
add edx, 4;
jz @@resbuild;
@@shortloopx:
{$IFDEF AVXSUP}vmovsd xmm4, [ebx]; {$ELSE}db $C5,$FB,$10,$23;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm5, [ecx]; {$ELSE}db $C5,$FB,$10,$29;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm5; {$ELSE}db $C5,$FB,$58,$C5;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm5, [ecx + esi]; {$ELSE}db $C5,$FB,$10,$2C,$31;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm1, xmm1, xmm5; {$ELSE}db $C5,$F3,$58,$CD;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm5, [ecx + 2*esi]; {$ELSE}db $C5,$FB,$10,$2C,$71;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm2, xmm2, xmm5; {$ELSE}db $C5,$EB,$58,$D5;{$ENDIF}
add ecx, esi;
{$IFDEF AVXSUP}vmovsd xmm5, [ecx + 2*esi]; {$ELSE}db $C5,$FB,$10,$2C,$71;{$ENDIF}
sub ecx, esi;
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm3, xmm3, xmm5; {$ELSE}db $C5,$E3,$58,$DD;{$ENDIF}
add ecx, 8;
add ebx, 8;
sub edx, 1;
jnz @@shortloopx;
@@resbuild:
// result building
// write back result (final addition and compactation)
// calculate dest = beta*dest + alpha*xmm0
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF} // first element
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm5; {$ELSE}db $C5,$F9,$7C,$C5;{$ENDIF} // calculate xmm0_1 + xmm0_2 (store low) xmm5_1 + xmm5_2 (store high)
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // beta * dest + alpha*xmm0
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF} // final add
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF} // store back
add eax, adestLineWidth;
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm1, xmm1, xmm5; {$ELSE}db $C5,$F1,$7C,$CD;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm1, xmm1, xmm6; {$ELSE}db $C5,$F1,$59,$CE;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm1, xmm1, xmm1; {$ELSE}db $C5,$F1,$7C,$C9;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm1; {$ELSE}db $C5,$FB,$11,$08;{$ENDIF}
add eax, adestLineWidth;
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm5; {$ELSE}db $C5,$E9,$7C,$D5;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm2, xmm2, xmm6; {$ELSE}db $C5,$E9,$59,$D6;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm2, xmm2, xmm2; {$ELSE}db $C5,$E9,$7C,$D2;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm2; {$ELSE}db $C5,$FB,$11,$10;{$ENDIF}
add eax, adestLineWidth;
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm3, xmm3, xmm5; {$ELSE}db $C5,$E1,$7C,$DD;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm6; {$ELSE}db $C5,$E1,$59,$DE;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm3, xmm3, xmm3; {$ELSE}db $C5,$E1,$7C,$DB;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
add eax, adestLineWidth;
pop ecx;
lea ecx, [ecx + 4*esi];
// next rseult
sub height, 4;
jge @@foryloop;
// ###########################################
// #### Remaining rows (max 4 or more if width is <4):
@@foryloopend:
add height, 4;
jz @@vecend;
@@foryshortloop:
// init values:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF}
push ecx; // ecx = first matrix element
mov ebx, v; // ebx = first vector element
mov edx, width; // r10 = width - 4
cmp edx, 0;
jl @@shortloopend;
@@forxshortloop:
{$IFDEF AVXSUP}vmovupd ymm4, [ebx]; {$ELSE}db $C5,$FD,$10,$23;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm5, [ecx]; {$ELSE}db $C5,$FD,$10,$29;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm4, ymm5; {$ELSE}db $C4,$E2,$DD,$B8,$C5;{$ENDIF}
add ecx, 32;
add ebx, 32;
sub edx, 4;
jge @@forxshortloop;
{$IFDEF AVXSUP}vextractf128 xmm4, ymm0, 1; {$ELSE}db $C4,$E3,$7D,$19,$C4,$01;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm4; {$ELSE}db $C5,$F9,$7C,$C4;{$ENDIF}
@@shortloopend:
add edx, 4;
// test if there are elements left
jz @@resbuildshort;
@@forxshortestloop:
{$IFDEF AVXSUP}vmovsd xmm4, [ebx]; {$ELSE}db $C5,$FB,$10,$23;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm5, [ecx]; {$ELSE}db $C5,$FB,$10,$29;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm5, xmm5, xmm4; {$ELSE}db $C5,$D3,$59,$EC;{$ENDIF}
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm5; {$ELSE}db $C5,$FB,$58,$C5;{$ENDIF}
add ecx, 8;
add ebx, 8;
dec edx;
jnz @@forxshortestloop;
@@resbuildshort:
// build result
// calculate dest = beta*dest + alpha*xmm0
{$IFDEF AVXSUP}vmovsd xmm5, [eax]; {$ELSE}db $C5,$FB,$10,$28;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm5; {$ELSE}db $C5,$F9,$7C,$C5;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF}
{$IFDEF AVXSUP}vhaddpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$7C,$C0;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF}
add eax, adestLineWidth;
pop ecx;
add ecx, esi;
dec height;
jnz @@foryshortloop;
@@vecend:
// epilog pop "stack"
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
pop esi;
pop edi;
pop ebx;
end;
// this function is not that well suited for use of simd instructions...
// so only this version exists
procedure FMAMatrixVectMultT(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double);
// var dymm8, dymm9, dymm10, dymm11 : TYMMArr;
var aDestLineWidth : NativeInt;
aMt1 : PDouble;
asm
// prolog - simulate stack
push ebx;
push edi;
push esi;
mov aDestLineWidth, edx;
mov aMt1, ecx;
// reserve memory on the stack for dymm8 to dymm11
sub esp, 128;
// for the final multiplication
lea edi, alpha;
{$IFDEF AVXSUP}vbroadcastsd ymm6, [edi]; {$ELSE}db $C4,$E2,$7D,$19,$37;{$ENDIF}
lea edi, beta;
{$IFDEF AVXSUP}vbroadcastsd ymm7, [edi]; {$ELSE}db $C4,$E2,$7D,$19,$3F;{$ENDIF}
// prepare for loop
mov esi, LineWidthMT;
mov edi, LineWidthV;
sub width, 16;
js @@forxloopend;
// init for x := 0 to width - 1:
@@forxloop:
// init values:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 00], ymm0; {$ELSE}db $C5,$FD,$11,$04,$24;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 32], ymm0; {$ELSE}db $C5,$FD,$11,$44,$24,$20;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 64], ymm0; {$ELSE}db $C5,$FD,$11,$44,$24,$40;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 96], ymm0; {$ELSE}db $C5,$FD,$11,$44,$24,$60;{$ENDIF}
mov ecx, aMt1; // ecx = first matrix element
mov ebx, V; // ebx = first vector element
mov edx, height;
@@foryloop:
{$IFDEF AVXSUP}vbroadcastsd ymm3, [ebx]; {$ELSE}db $C4,$E2,$7D,$19,$1B;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm0, [esp + 00]; {$ELSE}db $C5,$FD,$10,$04,$24;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm4, [ecx]; {$ELSE}db $C5,$FD,$10,$21;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm3, ymm4; {$ELSE}db $C4,$E2,$E5,$B8,$C4;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 00], ymm0; {$ELSE}db $C5,$FD,$11,$04,$24;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm1, [esp + 32]; {$ELSE}db $C5,$FD,$10,$4C,$24,$20;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm4, [ecx + 32]; {$ELSE}db $C5,$FD,$10,$61,$20;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm1, ymm3, ymm4; {$ELSE}db $C4,$E2,$E5,$B8,$CC;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 32], ymm1; {$ELSE}db $C5,$FD,$11,$4C,$24,$20;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm0, [esp + 64]; {$ELSE}db $C5,$FD,$10,$44,$24,$40;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm4, [ecx + 64]; {$ELSE}db $C5,$FD,$10,$61,$40;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm3, ymm4; {$ELSE}db $C4,$E2,$E5,$B8,$C4;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 64], ymm0; {$ELSE}db $C5,$FD,$11,$44,$24,$40;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm1, [esp + 96]; {$ELSE}db $C5,$FD,$10,$4C,$24,$60;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm4, [ecx + 96]; {$ELSE}db $C5,$FD,$10,$61,$60;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm1, ymm3, ymm4; {$ELSE}db $C4,$E2,$E5,$B8,$CC;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 96], ymm1; {$ELSE}db $C5,$FD,$11,$4C,$24,$60;{$ENDIF}
add ecx, esi;
add ebx, edi;
dec edx;
jnz @@foryloop;
// result building
// write back result (final addition and compactation)
// calculate dest = beta*dest + alpha*xmm0
// first two
mov edx, adestLineWidth;
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [eax + edx]; {$ELSE}db $C5,$FB,$10,$24,$10;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$16,$DC;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm0, [esp + 00]; {$ELSE}db $C5,$F9,$10,$04,$24;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E1,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd xmm3, xmm3, xmm0; {$ELSE}db $C5,$E1,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm3; {$ELSE}db $C5,$D8,$12,$E3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + edx], xmm4; {$ELSE}db $C5,$FB,$11,$24,$10;{$ENDIF}
add eax, edx;
add eax, edx;
// second two
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [eax + edx]; {$ELSE}db $C5,$FB,$10,$24,$10;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$16,$DC;{$ENDIF}
//movupd xmm0, res1;
{$IFDEF AVXSUP}vmovupd xmm0, [esp + 16]; {$ELSE}db $C5,$F9,$10,$44,$24,$10;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E1,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd xmm3, xmm3, xmm0; {$ELSE}db $C5,$E1,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm3; {$ELSE}db $C5,$D8,$12,$E3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + edx], xmm4; {$ELSE}db $C5,$FB,$11,$24,$10;{$ENDIF}
add eax, edx;
add eax, edx;
// third two
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [eax + edx]; {$ELSE}db $C5,$FB,$10,$24,$10;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$16,$DC;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm0, [esp + 32]; {$ELSE}db $C5,$F9,$10,$44,$24,$20;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E1,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd xmm3, xmm3, xmm0; {$ELSE}db $C5,$E1,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm3; {$ELSE}db $C5,$D8,$12,$E3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + edx], xmm4; {$ELSE}db $C5,$FB,$11,$24,$10;{$ENDIF}
add eax, edx;
add eax, edx;
// forth two
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [eax + edx]; {$ELSE}db $C5,$FB,$10,$24,$10;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$16,$DC;{$ENDIF}
//movupd xmm0, res3;
{$IFDEF AVXSUP}vmovupd xmm0, [esp + 48]; {$ELSE}db $C5,$F9,$10,$44,$24,$30;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E1,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd xmm3, xmm3, xmm0; {$ELSE}db $C5,$E1,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm3; {$ELSE}db $C5,$D8,$12,$E3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + edx], xmm4; {$ELSE}db $C5,$FB,$11,$24,$10;{$ENDIF}
add eax, edx;
add eax, edx;
// fith two
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [eax + edx]; {$ELSE}db $C5,$FB,$10,$24,$10;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$16,$DC;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm0, [esp + 64]; {$ELSE}db $C5,$F9,$10,$44,$24,$40;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E1,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd xmm3, xmm3, xmm0; {$ELSE}db $C5,$E1,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm3; {$ELSE}db $C5,$D8,$12,$E3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + edx], xmm4; {$ELSE}db $C5,$FB,$11,$24,$10;{$ENDIF}
add eax, edx;
add eax, edx;
// sixth two
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [eax + edx]; {$ELSE}db $C5,$FB,$10,$24,$10;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$16,$DC;{$ENDIF}
//movupd xmm0, res5;
{$IFDEF AVXSUP}vmovupd xmm0, [esp + 80]; {$ELSE}db $C5,$F9,$10,$44,$24,$50;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E1,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd xmm3, xmm3, xmm0; {$ELSE}db $C5,$E1,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm3; {$ELSE}db $C5,$D8,$12,$E3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + edx], xmm4; {$ELSE}db $C5,$FB,$11,$24,$10;{$ENDIF}
add eax, edx;
add eax, edx;
// seventh two
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [eax + edx]; {$ELSE}db $C5,$FB,$10,$24,$10;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$16,$DC;{$ENDIF}
{$IFDEF AVXSUP}vmovupd xmm0, [esp + 96]; {$ELSE}db $C5,$F9,$10,$44,$24,$60;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E1,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd xmm3, xmm3, xmm0; {$ELSE}db $C5,$E1,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm3; {$ELSE}db $C5,$D8,$12,$E3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + edx], xmm4; {$ELSE}db $C5,$FB,$11,$24,$10;{$ENDIF}
add eax, edx;
add eax, edx;
// eighth two
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd xmm4, [eax + edx]; {$ELSE}db $C5,$FB,$10,$24,$10;{$ENDIF}
{$IFDEF AVXSUP}vmovlhps xmm3, xmm3, xmm4; {$ELSE}db $C5,$E0,$16,$DC;{$ENDIF}
//movupd xmm0, res7;
{$IFDEF AVXSUP}vmovupd xmm0, [esp + 112]; {$ELSE}db $C5,$F9,$10,$44,$24,$70;{$ENDIF}
{$IFDEF AVXSUP}vmulpd xmm0, xmm0, xmm6; {$ELSE}db $C5,$F9,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E1,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd xmm3, xmm3, xmm0; {$ELSE}db $C5,$E1,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovhlps xmm4, xmm4, xmm3; {$ELSE}db $C5,$D8,$12,$E3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm3; {$ELSE}db $C5,$FB,$11,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax + edx], xmm4; {$ELSE}db $C5,$FB,$11,$24,$10;{$ENDIF}
add eax, edx;
add eax, edx;
// next results:
mov ecx, amt1;
add ecx, 8*16; // next mt1 element
mov amt1, ecx;
sub width, 16;
jns @@forxloop;
@@forxloopend:
// ###########################################
// elements that not fit into mod 16
add width, 16;
jz @@vecaddend;
@@forxshortloop:
{$IFDEF AVXSUP}vxorpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$57,$C0;{$ENDIF} // first two elements
mov ecx, amt1; // ecx = first matrix element
mov ebx, v; // ebx = first vector element
mov edx, height;
@@forshortyloop:
{$IFDEF AVXSUP}vmovsd xmm1, [ecx]; {$ELSE}db $C5,$FB,$10,$09;{$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 ecx, esi;
add ebx, edi;
dec edx;
jnz @@forshortyloop;
{$IFDEF AVXSUP}vmulsd xmm0, xmm0, xmm6; {$ELSE}db $C5,$FB,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E3,$59,$DF;{$ENDIF} //dest*beta
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm3; {$ELSE}db $C5,$FB,$58,$C3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF}
// next row
add eax, adestLineWidth;
mov ecx, amt1;
add ecx, 8;
mov amt1, ecx;
dec width;
jnz @@forxshortloop;
@@vecaddend:
// epilog pop "stack"
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
// free reserved stack space
add esp, 128;
pop esi;
pop edi;
pop ebx;
end;
procedure FMAMatrixVecMultTDestVec(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double);
var aDestLineWidth : NativeInt;
aMt1 : PDouble;
asm
// prolog - simulate stack
push ebx;
push edi;
push esi;
mov aDestLineWidth, edx;
mov aMt1, ecx;
// simulate local vars dymm8 - dymm11
sub esp, $10 + 128; // 4*sizeof ymm register
// for the final multiplication
lea esi, alpha;
{$IFDEF AVXSUP}vbroadcastsd ymm6, [esi]; {$ELSE}db $C4,$E2,$7D,$19,$36;{$ENDIF}
lea edi, beta;
{$IFDEF AVXSUP}vbroadcastsd ymm7, [edi]; {$ELSE}db $C4,$E2,$7D,$19,$3F;{$ENDIF}
// prepare for loop
mov esi, LineWidthMT;
mov edi, LineWidthV;
sub width, 16;
js @@forxloopend;
// init for x := 0 to width - 1:
@@forxloop:
// init values:
{$IFDEF AVXSUP}vxorpd ymm0, ymm0, ymm0; {$ELSE}db $C5,$FD,$57,$C0;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 00], ymm0; {$ELSE}db $C5,$FD,$11,$04,$24;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 32], ymm0; {$ELSE}db $C5,$FD,$11,$44,$24,$20;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 64], ymm0; {$ELSE}db $C5,$FD,$11,$44,$24,$40;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 96], ymm0; {$ELSE}db $C5,$FD,$11,$44,$24,$60;{$ENDIF}
mov ecx, aMt1; // ecx = first matrix element
mov ebx, v; // ebx = first vector element
mov edx, height;
@@foryloop:
{$IFDEF AVXSUP}vbroadcastsd ymm3, [ebx]; {$ELSE}db $C4,$E2,$7D,$19,$1B;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm0, [esp + 00]; {$ELSE}db $C5,$FD,$10,$04,$24;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm4, [ecx]; {$ELSE}db $C5,$FD,$10,$21;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm3, ymm4; {$ELSE}db $C4,$E2,$E5,$B8,$C4;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 00], ymm0; {$ELSE}db $C5,$FD,$11,$04,$24;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm1, [esp + 32]; {$ELSE}db $C5,$FD,$10,$4C,$24,$20;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm4, [ecx + 32]; {$ELSE}db $C5,$FD,$10,$61,$20;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm1, ymm3, ymm4; {$ELSE}db $C4,$E2,$E5,$B8,$CC;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 32], ymm1; {$ELSE}db $C5,$FD,$11,$4C,$24,$20;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm0, [esp + 64]; {$ELSE}db $C5,$FD,$10,$44,$24,$40;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm4, [ecx + 64]; {$ELSE}db $C5,$FD,$10,$61,$40;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm0, ymm3, ymm4; {$ELSE}db $C4,$E2,$E5,$B8,$C4;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 64], ymm0; {$ELSE}db $C5,$FD,$11,$44,$24,$40;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm1, [esp + 96]; {$ELSE}db $C5,$FD,$10,$4C,$24,$60;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm4, [ecx + 96]; {$ELSE}db $C5,$FD,$10,$61,$60;{$ENDIF}
{$IFDEF AVXSUP}vfmadd231pd ymm1, ymm3, ymm4; {$ELSE}db $C4,$E2,$E5,$B8,$CC;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [esp + 96], ymm1; {$ELSE}db $C5,$FD,$11,$4C,$24,$60;{$ENDIF}
add ecx, esi;
add ebx, edi;
dec edx;
jnz @@foryloop;
// result building
// write back result (final addition and compactation)
// calculate dest = beta*dest + alpha*xmm0
// first 4
{$IFDEF AVXSUP}vmovupd ymm3, [eax]; {$ELSE}db $C5,$FD,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm0, [esp + 00]; {$ELSE}db $C5,$FD,$10,$04,$24;{$ENDIF}
{$IFDEF AVXSUP}vmulpd ymm0, ymm0, ymm6; {$ELSE}db $C5,$FD,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd ymm3, ymm3, ymm7; {$ELSE}db $C5,$E5,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd ymm3, ymm3, ymm0; {$ELSE}db $C5,$E5,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [eax], ymm3; {$ELSE}db $C5,$FD,$11,$18;{$ENDIF}
add eax, 32;
// second 4
{$IFDEF AVXSUP}vmovupd ymm3, [eax]; {$ELSE}db $C5,$FD,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm0, [esp + 32]; {$ELSE}db $C5,$FD,$10,$44,$24,$20;{$ENDIF}
{$IFDEF AVXSUP}vmulpd ymm0, ymm0, ymm6; {$ELSE}db $C5,$FD,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd ymm3, ymm3, ymm7; {$ELSE}db $C5,$E5,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd ymm3, ymm3, ymm0; {$ELSE}db $C5,$E5,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [eax], ymm3; {$ELSE}db $C5,$FD,$11,$18;{$ENDIF}
add eax, 32;
// third 4
{$IFDEF AVXSUP}vmovupd ymm3, [eax]; {$ELSE}db $C5,$FD,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm0, [esp + 64]; {$ELSE}db $C5,$FD,$10,$44,$24,$40;{$ENDIF}
{$IFDEF AVXSUP}vmulpd ymm0, ymm0, ymm6; {$ELSE}db $C5,$FD,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd ymm3, ymm3, ymm7; {$ELSE}db $C5,$E5,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd ymm3, ymm3, ymm0; {$ELSE}db $C5,$E5,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [eax], ymm3; {$ELSE}db $C5,$FD,$11,$18;{$ENDIF}
add eax, 32;
// forth 4
{$IFDEF AVXSUP}vmovupd ymm3, [eax]; {$ELSE}db $C5,$FD,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm0, [esp + 96]; {$ELSE}db $C5,$FD,$10,$44,$24,$60;{$ENDIF}
{$IFDEF AVXSUP}vmulpd ymm0, ymm0, ymm6; {$ELSE}db $C5,$FD,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmulpd ymm3, ymm3, ymm7; {$ELSE}db $C5,$E5,$59,$DF;{$ENDIF} // dest*beta
{$IFDEF AVXSUP}vaddpd ymm3, ymm3, ymm0; {$ELSE}db $C5,$E5,$58,$D8;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [eax], ymm3; {$ELSE}db $C5,$FD,$11,$18;{$ENDIF}
add eax, 32;
// next results:
mov ecx, aMt1;
add ecx, 8*16; // next mt1 element
mov aMt1, ecx;
sub width, 16;
jns @@forxloop;
@@forxloopend:
// ###########################################
// elements that not fit into mod 16
add width, 16;
jz @@vecaddend;
@@forxshortloop:
{$IFDEF AVXSUP}vxorpd xmm0, xmm0, xmm0; {$ELSE}db $C5,$F9,$57,$C0;{$ENDIF} // first two elements
mov ecx, aMt1; // ecx = first matrix element
mov ebx, v; // ebx = first vector element
mov edx, height;
@@forshortyloop:
{$IFDEF AVXSUP}vmovsd xmm1, [ecx]; {$ELSE}db $C5,$FB,$10,$09;{$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 ecx, esi;
add ebx, edi;
dec edx;
jnz @@forshortyloop;
{$IFDEF AVXSUP}vmulsd xmm0, xmm0, xmm6; {$ELSE}db $C5,$FB,$59,$C6;{$ENDIF} // alpha*res
{$IFDEF AVXSUP}vmovsd xmm3, [eax]; {$ELSE}db $C5,$FB,$10,$18;{$ENDIF}
{$IFDEF AVXSUP}vmulsd xmm3, xmm3, xmm7; {$ELSE}db $C5,$E3,$59,$DF;{$ENDIF} //dest*beta
{$IFDEF AVXSUP}vaddsd xmm0, xmm0, xmm3; {$ELSE}db $C5,$FB,$58,$C3;{$ENDIF}
{$IFDEF AVXSUP}vmovsd [eax], xmm0; {$ELSE}db $C5,$FB,$11,$00;{$ENDIF}
// next column
add eax, 8;
mov ecx, amt1;
add ecx, 8;
mov amt1, ecx;
dec Width;
jnz @@forxshortloop;
@@vecaddend:
// epilog pop "stack"
{$IFDEF AVXSUP}vzeroupper; {$ELSE}db $C5,$F8,$77;{$ENDIF}
// undo local mem
add esp, $10 + 128;
pop esi;
pop edi;
pop ebx;
end;
procedure FMARank1UpdateSeq(A : PDouble; const LineWidthA : NativeInt; width, height : NativeInt;
X, Y : PDouble; incX, incY : NativeInt; alpha : double);
var aLineWidthA : NativeInt;
aWidth : NativeInt;
asm
// prolog - simulate stack
push ebx;
push edi;
push esi;
mov aLineWidthA, edx;
mov aWidth, ecx;
// performs A = A + alpha*X*Y' in row major form
mov edi, X;
// for the temp calculation
lea esi, alpha;
{$IFDEF AVXSUP}vbroadcastsd ymm3, [esi]; {$ELSE}db $C4,$E2,$7D,$19,$1E;{$ENDIF}
// prepare for loop
mov esi, incx;
// init for y := 0 to height - 1:
@@foryloop:
// init values:
{$IFDEF AVXSUP}vbroadcastsd ymm0, [edi]; {$ELSE}db $C4,$E2,$7D,$19,$07;{$ENDIF} // res := 0;
{$IFDEF AVXSUP}vmulpd ymm0, ymm0, ymm3; {$ELSE}db $C5,$FD,$59,$C3;{$ENDIF} // tmp := alpha*pX^
mov ecx, eax; // ecx = first destination element A
mov ebx, Y; // ebx = first y vector element
// for j := 0 to width - 1 do
mov edx, aWidth;
sub edx, 4;
jl @@last3Elem;
@@forxloop:
{$IFDEF AVXSUP}vmovupd ymm1, [ecx]; {$ELSE}db $C5,$FD,$10,$09;{$ENDIF}
{$IFDEF AVXSUP}vmovupd ymm2, [ebx]; {$ELSE}db $C5,$FD,$10,$13;{$ENDIF}
// pA^[j] := pA^[j] + tmp*pY1^[j];
{$IFDEF AVXSUP}vfmadd231pd ymm1, ymm2, ymm0; {$ELSE}db $C4,$E2,$ED,$B8,$C8;{$ENDIF}
{$IFDEF AVXSUP}vmovupd [ecx], ymm1; {$ELSE}db $C5,$FD,$11,$09;{$ENDIF}
add ecx, 32;
add ebx, 32;
sub edx, 4;
jge @@forxloop;
@@last3Elem:
add edx, 4;
jz @@nextline;