-
Notifications
You must be signed in to change notification settings - Fork 35
/
AVXMatrixOperations.pas
1247 lines (1106 loc) · 54.9 KB
/
AVXMatrixOperations.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) 2017, 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 AVXMatrixOperations;
// #################################################
// #### distributes the function to the assembler versions
// #################################################
interface
uses MatrixConst;
procedure AVXMatrixCopy(Dest : PDouble; destLineWidth : NativeInt; src : PDouble; srcLineWidth : NativeInt; Width, Height : NativeInt);
procedure AVXRowSwap(A, B : PDouble; width : NativeInt);
procedure AVXMatrixInit( dest : PDouble; destLineWidth : NativeInt; Width, Height : NativeInt; const value : double );
// note: The ASM routines always carry out 2x2 matrix multiplications thus there must be an additional zero line/column in the
// input matrices if the width/height is uneven. The routine also performs better if the matrices are aligned to 16 byte boundaries!
procedure AVXMatrixMult(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); overload;
procedure AVXMatrixMult(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; mem : PDouble); overload;
procedure AVXMatrixMultTransposed(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); overload;
procedure AVXMatrixMultDirect(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); overload;
procedure AVXMtxVecMult(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double);
procedure AVXMtxVecMultT(dest : PDouble; destLineWidth : NativeInt; mt1, v : PDouble; LineWidthMT, LineWidthV : NativeInt; width, height : NativeInt; alpha, beta : double);
procedure AVXRank1Update(A : PDouble; const LineWidthA : NativeInt; width, height : NativeInt;
X, Y : PDouble; incX, incY : NativeInt; alpha : double);
procedure AVXSymRank2UpdateUpper( C : PDouble; LineWidthC : NativeInt; A : PDouble; LineWidthA : NativeInt;
B : PDouble; LineWidthB : NativeInt; N : NativeInt; k : NativeInt );
function AVXMatrixVecDotMult( x : PDouble; incX : NativeInt; y : PDouble; incY : NativeInt; N : NativeInt ) : double;
// note: the matrix add routine tries to add two values at once and does not carry out any range checks thus the line widhts must
// be multiple of 16.
procedure AVXMatrixAdd(dest : PDouble; destLineWidth : NativeInt; mt1, mt2 : PDouble; width : NativeInt; height : NativeInt; LineWidth1, LineWidth2 : NativeInt);
procedure AVXMatrixAddVec(A : PDouble; LineWidthA : NativeInt; B : PDouble; incX : NativeInt; width, Height : NativeInt; rowWise : Boolean);
procedure AVXMatrixSub(dest : PDouble; destLineWidth : NativeInt; mt1, mt2 : PDouble; width : NativeInt; height : NativeInt; LineWidth1, LineWidth2 : NativeInt);
procedure AVXMatrixSubVec(A : PDouble; LineWidthA : NativeInt; B : PDouble; incX : NativeInt; width, Height : NativeInt; rowWise : Boolean);
procedure AVXMatrixElemMult(dest : PDouble; destLineWidth : NativeInt; mt1, mt2 : PDouble; width : NativeInt; height : NativeInt; LineWidth1, LineWidth2 : NativeInt);
procedure AVXMatrixElemDiv(dest : PDouble; destLineWidth : NativeInt; mt1, mt2 : PDouble; width : NativeInt; height : NativeInt; LineWidth1, LineWidth2 : NativeInt);
procedure AVXMatrixAddAndScale(Dest : PDouble; LineWidth, Width, Height : NativeInt; const dOffset, Scale : double);
procedure AVXMatrixElemAdd(Dest : PDouble; LineWidth, Width, Height : NativeInt; const dOffset : double);
procedure AVXMatrixScaleAndAdd(Dest : PDouble; LineWidth, Width, Height : NativeInt; const dOffset, Scale : double);
procedure AVXMatrixSQRT(Dest : PDouble; LineWidth : NativeInt; Width, Height : NativeInt);
procedure AVXMatrixAbs(Dest : PDouble; LineWidth : NativeInt; Width, Height : NativeInt);
function AVXMatrixMax(mt : PDouble; width, height : NativeInt; const LineWidth : NativeInt) : double; overload;
function AVXMatrixMin(mt : PDouble; width, height : NativeInt; const LineWidth : NativeInt) : double; overload;
procedure AVXMatrixMin(dest : PDouble; const LineWidth : NativeInt; width, height : NativeInt; minVal : double); overload;
procedure AVXMatrixMax(dest : PDouble; const LineWidth : NativeInt; width, height : NativeInt; maxVal : double); overload;
function AVXMatrixSumSum(src : PDouble; srcLineWidth : NativeInt; Width, Height : NativeInt): double;
procedure AVXMatrixTranspose(dest : PDouble; const destLineWidth : NativeInt; mt : PDouble; const LineWidth : NativeInt; width : NativeInt; height : NativeInt);
function AVXMatrixElementwiseNorm2(dest : PDouble; LineWidth : NativeInt; Width, height : NativeInt; doSqrt : boolean) : double;
procedure AVXMatrixNormalize(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean);
procedure AVXMatrixMean(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean);
procedure AVXMatrixVar(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean; unbiased : boolean);
procedure AVXMatrixMeanVar(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean; unbiased : boolean);
procedure AVXMatrixSum(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean); overload;
procedure AVXMatrixCumulativeSum(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean);
procedure AVXMatrixDifferentiate(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean);
// strassen algorithm for matrix multiplication
procedure AVXStrassenMatrixMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt);
implementation
{$I 'mrMath_CPU.inc'}
{$IFDEF FPC} {$S-} {$ENDIF}
uses Math,
{$IFDEF x64}
AVXMatrixMultTransposedOperationsx64, AVXMoveOperationsx64, AVXMatrixAbsOperationsx64,
AVXMatrixScaleOperationsx64, AVXMatrixTransposeOperationsx64, AVXMatrixVectorMultOperationsx64,
AVXMatrixAddSubOperationsx64, AVXMatrixMultOperationsx64, AVXMatrixCumSumDiffOperationsx64,
AVXMatrixElementwiseMultOperationsx64, AVXMatrixMinMaxOperationsx64, AVXMatrixMeanOperationsx64,
AVXMatrixNormOperationsx64, AVXMatrixSqrtOperationsx64, AVXMatrixSumOperationsx64, MatrixASMStubSwitch,
{$ELSE}
AVXMatrixMultOperations, AVXMatrixVectorMultOperations, AVXMatrixAbsOperations,
AVXMatrixMultTransposedOperations, AVXMatrixAddSubOperations, AVXMatrixMeanOperations,
AVXMatrixElementwiseMultOperations, AVXMatrixScaleOperations, AVXMatrixSQRTOperations,
AVXMoveOperations, AVXMatrixMinMaxOperations, AVXMatrixTransposeOperations,
AVXMatrixNormOperations, AVXMatrixSumOperations,
AVXMatrixCumSumDiffOperations, MatrixASMStubSwitch,
{$ENDIF}
ASMMatrixOperations, SimpleMatrixOperations;
procedure AVXMatrixAddAndScale(Dest : PDouble; LineWidth, Width, Height : NativeInt; const dOffset, Scale : double);
begin
if (width = 0) or (height = 0) then
exit;
if (width = 1) and (LineWidth = sizeof(double)) then
begin
width := height;
height := 1;
LineWidth := width*sizeof(double);
LineWidth := LineWidth + 32 - LineWidth and $1F;
end;
if (NativeUint(dest) and $0000001F = 0) and (LineWidth and $0000001F = 0) then
begin
if (width and 1) = 0
then
AVXMatrixAddScaleAlignedEvenW(dest, LineWidth, width, height, dOffset, Scale)
else
AVXMatrixAddScaleAlignedOddW(dest, LineWidth, width, height, dOffset, Scale);
end
else
begin
if (width and 1) = 0
then
AVXMatrixAddScaleUnAlignedEvenW(dest, LineWidth, width, height, dOffset, Scale)
else
AVXMatrixAddScaleUnAlignedOddW(dest, LineWidth, width, height, dOffset, Scale);
end;
end;
procedure AVXMatrixElemAdd(Dest : PDouble; LineWidth, Width, Height : NativeInt; const dOffset : double);
begin
if (width = 0) or (height = 0) then
exit;
if (width = 1) and (LineWidth = sizeof(double)) then
begin
width := height;
height := 1;
LineWidth := width*sizeof(double);
LineWidth := LineWidth + 32 - LineWidth and $1F;
end;
if (NativeUint(dest) and $0000001F = 0) and (LineWidth and $0000001F = 0) then
begin
if (width and 1) = 0
then
AVXMatrixAddAlignedEvenW(dest, LineWidth, width, height, dOffset)
else
AVXMatrixAddAlignedOddW(dest, LineWidth, width, height, dOffset);
end
else
begin
if (width and 1) = 0
then
AVXMatrixAddUnAlignedEvenW(dest, LineWidth, width, height, dOffset)
else
AVXMatrixAddUnAlignedOddW(dest, LineWidth, width, height, dOffset);
end;
end;
procedure AVXMatrixScaleAndAdd(Dest : PDouble; LineWidth, Width, Height : NativeInt; const dOffset, Scale : double);
begin
if (width = 0) or (height = 0) then
exit;
if (width = 1) and (LineWidth = sizeof(double)) then
begin
width := height;
height := 1;
LineWidth := width*sizeof(double);
LineWidth := LineWidth + $20 - LineWidth and $1F;
end;
if (NativeUint(dest) and $0000001F = 0) and (LineWidth and $0000001F = 0) then
begin
if (width and 1) = 0
then
AVXMatrixScaleAddAlignedEvenW(dest, LineWidth, width, height, dOffset, Scale)
else
AVXMatrixScaleAddAlignedOddW(dest, LineWidth, width, height, dOffset, Scale);
end
else
begin
if (width and 1) = 0
then
AVXMatrixScaleAddUnAlignedEvenW(dest, LineWidth, width, height, dOffset, Scale)
else
AVXMatrixScaleAddUnAlignedOddW(dest, LineWidth, width, height, dOffset, Scale);
end;
end;
procedure AVXMatrixSQRT(Dest : PDouble; LineWidth : NativeInt; Width, Height : NativeInt);
begin
if (width = 0) or (height = 0) then
exit;
if (width = 1) and (LineWidth = sizeof(double)) then
begin
width := height;
height := 1;
LineWidth := width*sizeof(double);
LineWidth := LineWidth + 32 - LineWidth and $1F;
end;
if (NativeUint(dest) and $0000001F = 0) and (LineWidth and $0000001F = 0)
then
AVXMatrixSQRTAligned(dest, LineWidth, width, height)
else
AVXMatrixSQRTUnAligned(dest, LineWidth, width, height);
end;
procedure AVXMatrixAbs(Dest : PDouble; LineWidth : NativeInt; Width, Height : NativeInt);
begin
if (width = 0) or (height = 0) then
exit;
if (width = 1) and (LineWidth = sizeof(double)) then
begin
width := height;
height := 1;
LineWidth := width*sizeof(double);
LineWidth := LineWidth + 32 - LineWidth and $1F;
end;
if (NativeUint(dest) and $0000001F = 0) and (LineWidth and $0000001F = 0)
then
AVXMatrixAbsAligned(dest, LineWidth, width, height)
else
AVXMatrixAbsUnAligned(dest, LineWidth, width, height);
end;
procedure AVXMatrixElemMult(dest : PDouble; destLineWidth : NativeInt; mt1, mt2 : PDouble; width : NativeInt; height : NativeInt; LineWidth1, LineWidth2 : NativeInt);
begin
if (width = 0) or (height = 0) then
exit;
if (width = 1) and (LineWidth1 = sizeof(double)) and (LineWidth2 = sizeof(double)) and (destLineWidth = sizeof(double)) then
begin
width := Height;
height := 1;
LineWidth1 := width*sizeof(double);
LineWidth1 := LineWidth1 + 32 - LineWidth1 and $1F;
LineWidth2 := LineWidth1;
destLineWidth := LineWidth1;
end;
if (NativeUint(mt1) and $00000001F = 0) and (NativeUint(mt2) and $00000001F = 0) and (NativeUint(dest) and $00000001F = 0) and
(destLineWidth and $00000001F = 0) and (LineWidth1 and $00000001F = 0) and (LineWidth2 and $00000001F = 0)
then
AVXMatrixElemMultAligned(dest, destLineWidth, mt1, mt2, width, height, LineWidth1, LineWidth2)
else
AVXMatrixElemMultUnAligned(dest, destLineWidth, mt1, mt2, width, height, LineWidth1, LineWidth2);
end;
procedure AVXMatrixElemDiv(dest : PDouble; destLineWidth : NativeInt; mt1, mt2 : PDouble; width : NativeInt; height : NativeInt; LineWidth1, LineWidth2 : NativeInt);
begin
if (width = 0) or (height = 0) then
exit;
if (width = 1) and (LineWidth1 = sizeof(double)) and (LineWidth2 = sizeof(double)) and (destLineWidth = sizeof(double)) then
begin
width := Height;
height := 1;
LineWidth1 := width*sizeof(double);
LineWidth1 := LineWidth1 + 32 - LineWidth1 and $1F;
LineWidth2 := LineWidth1;
destLineWidth := LineWidth1;
end;
if (NativeUint(mt1) and $0000001F = 0) and (NativeUint(mt2) and $0000001F = 0) and (NativeUint(dest) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (LineWidth1 and $0000001F = 0) and (LineWidth2 and $0000001F = 0)
then
AVXMatrixElemDivAligned(dest, destLineWidth, mt1, mt2, width, height, LineWidth1, LineWidth2)
else
AVXMatrixElemDivUnAligned(dest, destLineWidth, mt1, mt2, width, height, LineWidth1, LineWidth2);
end;
function AVXMatrixElementwiseNorm2(dest : PDouble; LineWidth : NativeInt; Width, height : NativeInt; doSqrt : boolean) : double;
begin
Result := 0;
if (width = 0) or (height = 0) then
exit;
// check if they are vector operations:
if (width = 1) and (LineWidth = sizeof(double)) then
begin
width := Height;
height := 1;
LineWidth := width*sizeof(double) + (width and $03)*sizeof(double);
end;
if (NativeUint(dest) and $0000001F = 0) and (LineWidth and $0000001F = 0)
then
Result := AVXMatrixElementwiseNorm2Aligned(dest, LineWidth, width, height)
else
Result := AVXMatrixElementwiseNorm2UnAligned(dest, LineWidth, width, height);
if doSqrt then
Result := Sqrt(Result);
end;
procedure AVXMatrixAdd(dest : PDouble; destLineWidth : NativeInt; mt1, mt2 : PDouble; width : NativeInt; height : NativeInt; LineWidth1, LineWidth2 : NativeInt);
begin
if (width = 0) or (height = 0) then
exit;
if (NativeUint(mt1) and $0000001F = 0) and (NativeUint(mt2) and $0000001F = 0) and (NativeUint(dest) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (LineWidth1 and $0000001F = 0) and (LineWidth2 and $0000001F = 0)
then
AVXMatrixAddAligned(dest, destLineWidth, mt1, mt2, width, height, LineWidth1, LineWidth2)
else
AVXMatrixAddUnAligned(dest, destLineWidth, mt1, mt2, width, height, LineWidth1, LineWidth2);
end;
procedure AVXMatrixSub(dest : PDouble; destLineWidth : NativeInt; mt1, mt2 : PDouble; width : NativeInt; height : NativeInt; LineWidth1, LineWidth2 : NativeInt);
begin
if (width = 0) or (height = 0) then
exit;
if (NativeUint(mt1) and $0000001F = 0) and (NativeUint(mt2) and $0000001F = 0) and (NativeUint(dest) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (LineWidth1 and $0000001F = 0) and (LineWidth2 and $0000001F = 0)
then
AVXMatrixSubAligned(dest, destLineWidth, mt1, mt2, width, height, LineWidth1, LineWidth2)
else
AVXMatrixSubUnAligned(dest, destLineWidth, mt1, mt2, width, height, LineWidth1, LineWidth2);
end;
procedure AVXMatrixSubVec(A : PDouble; LineWidthA : NativeInt; B : PDouble; incX : NativeInt; width, Height : NativeInt; rowWise : Boolean);
begin
if (width = 0) or (height = 0) then
exit;
if (NativeUint(A) and $0000001F = 0) and (NativeUint(B) and $0000001F = 0) and
(LineWidthA and $0000001F = 0)
then
begin
if rowWise then
begin
if incx = sizeof(double)
then
AVXMatrixSubVecAlignedVecRow(A, LineWidthA, B, incX, Width, Height)
else
AVXMatrixSubVecAlignedRow(A, LineWidthA, B, incX, Width, Height)
end
else
AVXMatrixSubVecAlignedCol(A, LineWidthA, B, incX, Width, Height);
end
else
begin
if rowWise then
begin
if incx = sizeof(double)
then
AVXMatrixSubVecUnAlignedVecRow(A, LineWidthA, B, incX, Width, Height)
else
AVXMatrixSubVecUnAlignedRow(A, LineWidthA, B, incX, Width, Height)
end
else
AVXMatrixSubVecUnAlignedCol(A, LineWidthA, B, incX, Width, Height)
end;
end;
procedure AVXMatrixAddVec(A : PDouble; LineWidthA : NativeInt; B : PDouble; incX : NativeInt; width, Height : NativeInt; rowWise : Boolean);
begin
if (width = 0) or (height = 0) then
exit;
if (NativeUint(A) and $0000001F = 0) and (NativeUint(B) and $0000001F = 0) and
(LineWidthA and $0000001F = 0)
then
begin
if rowWise then
begin
if incx = sizeof(double)
then
AVXMatrixAddVecAlignedVecRow(A, LineWidthA, B, incX, Width, Height)
else
AVXMatrixAddVecAlignedRow(A, LineWidthA, B, incX, Width, Height)
end
else
AVXMatrixAddVecAlignedCol(A, LineWidthA, B, incX, Width, Height);
end
else
begin
if rowWise then
begin
if incx = sizeof(double)
then
AVXMatrixAddVecUnAlignedVecRow(A, LineWidthA, B, incX, Width, Height)
else
AVXMatrixAddVecUnAlignedRow(A, LineWidthA, B, incX, Width, Height)
end
else
AVXMatrixAddVecUnAlignedCol(A, LineWidthA, B, incX, Width, Height)
end;
end;
function AVXMatrixMax(mt : PDouble; width, height : NativeInt; const LineWidth : NativeInt) : double;
begin
Result := -MaxDouble;
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= LineWidth), 'Dimension error');
if (NativeUint(mt) and $0000001F = 0) and (LineWidth and $0000001F = 0)
then
Result := AVXMatrixMaxAligned(mt, width, height, LineWidth)
else
Result := AVXMatrixMaxUnAligned(mt, width, height, LineWidth);
end;
function AVXMatrixMin(mt : PDouble; width, height : NativeInt; const LineWidth : NativeInt) : double;
begin
Result := MaxDouble;
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= LineWidth), 'Dimension error');
if (NativeUint(mt) and $0000001F = 0) and (LineWidth and $0000001F = 0)
then
Result := AVXMatrixMinAligned(mt, width, height, LineWidth)
else
Result := AVXMatrixMinUnAligned(mt, width, height, LineWidth);
end;
procedure AVXMatrixMin(dest : PDouble; const LineWidth : NativeInt; width, height : NativeInt; minVal : double);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= LineWidth), 'Dimension error');
if (NativeUint(dest) and $0000001F = 0) and (LineWidth and $0000001F = 0)
then
AVXMatrixMinValAligned(dest, lineWidth, width, height, minVal)
else
AVXMatrixMinValUnAligned(dest, LineWidth, width, height, minVal);
end;
procedure AVXMatrixMax(dest : PDouble; const LineWidth : NativeInt; width, height : NativeInt; maxVal : double);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= LineWidth), 'Dimension error');
if (NativeUint(dest) and $0000001F = 0) and (LineWidth and $0000001F = 0)
then
AVXMatrixMaxValAligned(dest, lineWidth, width, height, maxVal)
else
AVXMatrixMaxValUnAligned(dest, LineWidth, width, height, maxVal);
end;
function AVXMatrixSumSum(src : PDouble; srcLineWidth : NativeInt; Width, Height : NativeInt): double;
begin
Result := 0;
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= srcLineWidth), 'Dimension error');
if (NativeUint(src) and $0000001F = 0) and (srcLineWidth and $0000001F = 0)
then
Result := AVXMatrixSumAligned(src, srcLineWidth, width, height)
else
Result := AVXMatrixSumUnAligned(src, srcLineWidth, width, height);
end;
procedure AVXMatrixTranspose(dest : PDouble; const destLineWidth : NativeInt; mt : PDouble; const LineWidth : NativeInt; width : NativeInt; height : NativeInt);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= LineWidth), 'Dimension error');
if (width > 1) and (height > 1) then
begin
if (NativeUint(Dest) and $0000001F = 0) and (NativeUint(mt) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (LineWidth and $0000001F = 0)
then
AVXMatrixTransposeAligned(dest, destLineWidth, mt, LineWidth, width, height)
else
AVXMatrixTransposeUnAligned(dest, destLineWidth, mt, LineWidth, width, height);
end
else
GenericMtxTranspose(dest, destLineWidth, mt, LineWidth, width, height);
end;
procedure AVXMatrixSum(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= srcLineWidth), 'Dimension error');
assert((rowWise and (destLineWidth >= sizeof(double))) or (not rowWise and (destLineWidth >= width*sizeof(double))), 'Dimension error');
// check if they are vector operations:
if (width = 1) and (srcLineWidth = sizeof(double)) and not RowWise then
begin
width := Height;
height := 1;
srcLineWidth := width*sizeof(double) + (width and 1)*sizeof(double);
destLineWidth := srcLineWidth;
RowWise := True;
end;
if (NativeUint(Dest) and $0000001F = 0) and (NativeUint(src) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (srcLineWidth and $0000001F = 0)
then
begin
if RowWise
then
AVXMatrixSumRowAligned(dest, destLineWidth, Src, srcLineWidth, width, height)
else
AVXMatrixSumColumnAligned(dest, destLineWidth, Src, srcLineWidth, width, height);
end
else
begin
if RowWise
then
AVXMatrixSumRowUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height)
else
AVXMatrixSumColumnUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height);
end;
end;
procedure AVXMatrixCumulativeSum(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= srcLineWidth), 'Dimension error');
assert((rowWise and (destLineWidth >= sizeof(double))) or (not rowWise and (destLineWidth >= width*sizeof(double))), 'Dimension error');
// check if we have vector operations:
if (width = 1) and (srcLineWidth = sizeof(double)) and not RowWise then
begin
width := Height;
height := 1;
srcLineWidth := width*sizeof(double) + (width and 1)*sizeof(double);
destLineWidth := srcLineWidth;
RowWise := True;
end;
if RowWise
then
AVXMatrixCumulativeSumRow(dest, destLineWidth, Src, srcLineWidth, width, height)
else
begin
if (NativeUint(Dest) and $0000001F = 0) and (NativeUint(src) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (srcLineWidth and $0000001F = 0)
then
AVXMatrixCumulativeSumColumnAligned(dest, destLineWidth, Src, srcLineWidth, width, height)
else
AVXMatrixCumulativeSumColumnUnaligned(dest, destLineWidth, Src, srcLineWidth, width, height);
end;
end;
procedure AVXMatrixDifferentiate(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= srcLineWidth), 'Dimension error');
assert((rowWise and (destLineWidth >= sizeof(double))) or (not rowWise and (destLineWidth >= width*sizeof(double))), 'Dimension error');
// check if we have vector operations:
if (width = 1) and (srcLineWidth = sizeof(double)) and not RowWise then
begin
width := Height;
height := 1;
srcLineWidth := width*sizeof(double) + (width and 1)*sizeof(double);
destLineWidth := srcLineWidth;
RowWise := True;
end;
if RowWise
then
AVXMatrixDifferentiateRow(dest, destLineWidth, Src, srcLineWidth, width, height)
else
begin
if (NativeUint(Dest) and $0000001F = 0) and (NativeUint(src) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (srcLineWidth and $0000001F = 0)
then
AVXMatrixDifferentiateColumnAligned(dest, destLineWidth, Src, srcLineWidth, width, height)
else
AVXMatrixDifferentiateColumnUnaligned(dest, destLineWidth, Src, srcLineWidth, width, height);
end;
end;
procedure AVXMatrixMean(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= srcLineWidth), 'Dimension error');
assert((rowWise and (destLineWidth >= sizeof(double))) or (not rowWise and (destLineWidth >= width*sizeof(double))), 'Dimension error');
// check if they are vector operations:
if (width = 1) and (srcLineWidth = sizeof(double)) and not RowWise then
begin
width := Height;
height := 1;
srcLineWidth := width*sizeof(double) + (width and 1)*sizeof(double);
destLineWidth := srcLineWidth;
RowWise := True;
end;
if (NativeUint(Dest) and $0000001F = 0) and (NativeUint(src) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (srcLineWidth and $0000001F = 0)
then
begin
if RowWise
then
AVXMatrixMeanRowAligned(dest, destLineWidth, Src, srcLineWidth, width, height)
else
AVXMatrixMeanColumnAligned(dest, destLineWidth, Src, srcLineWidth, width, height);
end
else
begin
if RowWise
then
AVXMatrixMeanRowUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height)
else
AVXMatrixMeanColumnUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height);
end;
end;
procedure AVXMatrixVar(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean; unbiased : boolean);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= srcLineWidth), 'Dimension error');
assert((rowWise and (destLineWidth >= sizeof(double))) or (not rowWise and (destLineWidth >= width*sizeof(double))), 'Dimension error');
// check if they are vector operations:
if (width = 1) and (srcLineWidth = sizeof(double)) and not RowWise then
begin
width := Height;
height := 1;
srcLineWidth := width*sizeof(double) + (width and 1)*sizeof(double);
destLineWidth := srcLineWidth;
RowWise := True;
end;
if (NativeUint(Dest) and $0000001F = 0) and (NativeUint(src) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (srcLineWidth and $0000001F = 0)
then
begin
if RowWise
then
AVXMatrixVarRowAligned(dest, destLineWidth, Src, srcLineWidth, width, height, unbiased)
else
AVXMatrixVarColumnAligned(dest, destLineWidth, Src, srcLineWidth, width, height, unbiased);
end
else
begin
if RowWise
then
AVXMatrixVarRowUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height, unbiased)
else
AVXMatrixVarColumnUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height, unbiased);
end;
end;
procedure AVXMatrixMeanVar(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean; unbiased : boolean);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= srcLineWidth), 'Dimension error');
assert((rowWise and (destLineWidth >= 2*sizeof(double))) or (not rowWise and (destLineWidth >= width*sizeof(double))), 'Dimension error');
// check if they are vector operations:
if (width = 1) and (srcLineWidth = sizeof(double)) and not RowWise then
begin
width := Height;
height := 1;
srcLineWidth := width*sizeof(double) + (width and 1)*sizeof(double);
RowWise := True;
end;
if (NativeUint(Dest) and $0000001F = 0) and (NativeUint(src) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (srcLineWidth and $0000001F = 0)
then
begin
if RowWise
then
AVXMatrixMeanVarRowAligned(dest, destLineWidth, Src, srcLineWidth, width, height, unbiased)
else
AVXMatrixMeanVarColumnAligned(dest, destLineWidth, Src, srcLineWidth, width, height, unbiased);
end
else
begin
if RowWise
then
AVXMatrixMeanVarRowUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height, unbiased)
else
AVXMatrixMeanVarColumnUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height, unbiased);
end;
end;
procedure AVXMatrixNormalize(dest : PDouble; destLineWidth : NativeInt; Src : PDouble; srcLineWidth : NativeInt; width, height : NativeInt; RowWise : boolean);
begin
if (width = 0) or (height = 0) then
exit;
assert((width*sizeof(double) <= srcLineWidth) and (width*sizeof(double) <= destLineWidth), 'Dimension error');
// check if they are vector operations:
if (width = 1) and (srcLineWidth = sizeof(double)) and not RowWise then
begin
width := Height;
height := 1;
srcLineWidth := width*sizeof(double) + (width and 1)*sizeof(double);
destLineWidth := srcLineWidth;
RowWise := True;
end;
if (NativeUint(Dest) and $0000001F = 0) and (NativeUint(src) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (srcLineWidth and $0000001F = 0)
then
begin
if RowWise
then
AVXMatrixNormalizeRowAligned(dest, destLineWidth, Src, srcLineWidth, width, height)
else
AVXMatrixNormalizeColumnAligned(dest, destLineWidth, Src, srcLineWidth, width, height);
end
else
begin
if RowWise
then
AVXMatrixNormalizeRowUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height)
else
AVXMatrixNormalizeColumnUnAligned(dest, destLineWidth, Src, srcLineWidth, width, height);
end;
end;
procedure AVXMatrixCopy(Dest : PDouble; destLineWidth : NativeInt; src : PDouble; srcLineWidth : NativeInt; Width, Height : NativeInt);
begin
if (width = 0) or (height = 0) then
exit;
// check if we have a block operation
if (width*sizeof(double) = srcLineWidth) and (width*sizeof(double) = destLineWidth) then
begin
width := Height*width;
height := 1;
srcLineWidth := (width + (width and $03))*sizeof(double);
destLineWidth := srcLineWidth;
end;
(* todo: check if it's really faster... according to intel software optimiziation manual it's around factor 1.02 against AVX/SSE movs at memblocks > 2kB
if (width > 64) // and (width < maxcopy block
then
ASMCopyRepMov( dest, destLineWidth, src, srcLineWidth, width, height )
else *) if (NativeUint(Dest) and $0000001F = 0) and (NativeUint(src) and $0000001F = 0) and
(destLineWidth and $0000001F = 0) and (srcLineWidth and $0000001F = 0)
then
AVXMatrixCopyAligned(dest, destLineWidth, src, srcLineWidth, width, height)
else
AVXMatrixCopyUnAligned(dest, destLineWidth, src, srcLineWidth, width, height);
end;
procedure AVXMatrixInit( dest : PDouble; destLineWidth : NativeInt; Width, Height : NativeInt; const value : double );
begin
if (width = 0) or (height = 0) then
exit;
// check if they are vector operations:
if destLineWidth = width*sizeof(double) then
begin
width := Height*Width;
height := 1;
destLineWidth := (width + 4 - width and $03)*sizeof(double);
end;
if (NativeUint(Dest) and $0000001F = 0) and (destLineWidth and $0000001F = 0)
then
AVXMatrixInitAligned(dest, destLineWidth, width, height, value)
else
AVXMatrixInitUnAligned(dest, destLineWidth, width, height, value);
end;
procedure AVXRowSwap(A, B : PDouble; width : NativeInt);
begin
if (width = 0) then
exit;
if (NativeUint(A) and $0000001F = 0) and (NativeUint(B) and $0000001F = 0)
then
AVXRowSwapAligned(A, B, Width)
else
AVXRowSwapUnAligned(A, B, Width);
end;
procedure AVXMatrixMultTransposed(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); overload;
begin
if (width1 = 0) or (width2 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((width1 = width2), 'Dimension error');
assert((destLineWidth - height2*sizeof(double) >= 0) and (LineWidth1 >= width1*sizeof(double)) and (LineWidth2 >= width2*sizeof(double)), 'Line widths do not match');
if (width1 = 2)
then
ASMMatrixMultTransposed(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2)
else if (width1 < 2) or (width2 < 2) then
begin
// matrix/vector multiplication
GenericMtxMultTransp(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2);
end
else
begin
// ########################################################################
// #### In this case mt2 is already transposed -> direct multiplication
// check for alignment:
if ((NativeUint(dest) and $0000001F) = 0) and ((NativeUint(mt1) and $0000001F) = 0) and ((NativeUint(mt2) and $0000001F) = 0) and
((destLineWidth and $0000001F) = 0) and ((LineWidth1 and $0000001F) = 0) and ((LineWidth2 and $0000001F) = 0) then
begin
if width1 and $0000000F = 0 then
begin
if height2 and $01 = 1
then
AVXMatrixMultAlignedEvenW1OddH2TransposedMod16(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2)
else
AVXMatrixMultAlignedEvenW1EvenH2TransposedMod16(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2);
end
else
AVXMatrixMultAlignedTransposed(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2);
end
else
AVXMatrixMultUnAlignedTransposed(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2);
end;
end;
procedure AVXMatrixMult(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; mem : PDouble); overload;
var mtx : PDouble;
mtxLineWidth : NativeInt;
help : NativeInt;
aMem : Pointer;
begin
if (width1 = 0) or (width2 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((width1 = height2), 'Dimension error');
assert((destLineWidth - Width2*sizeof(double) >= 0) and (LineWidth1 >= width1*sizeof(double)) and (LineWidth2 >= width2*sizeof(double)), 'Line widths do not match');
if (width1 < 2) and (width2 < 2) then
begin
GenericMtxMult(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2);
end
else if (width1 < 2) or (width2 < 2) then
begin
// matrix/vector multiplication
if width2 = 1
then
AVXMtxVecMult(dest, destLineWidth, mt1, mt2, LineWidth1, LineWidth2, width1, height1, 1, 0)
else
GenericMtxMult(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2);
end
else
begin
// ########################################################################
// #### For all "bigger" matrices transpose first then multiply. It's always faster
aMem := nil;
mtxLineWidth := height2;
if height2 and $03 <> 0 then
mtxLineWidth := mtxLineWidth + 4 - (height2 and $03);
mtxLineWidth := mtxLineWidth*sizeof(double);
if Assigned(mem)
then
mtx := mem
else
mtx := MtxMallocAlign( width2*mtxLineWidth, aMem );
assert(assigned(mtx), 'Error could not reserve transpose memory');
AVXMatrixTranspose(mtx, mtxLineWidth, mt2, LineWidth2, width2, height2);
//GenericMtxTranspose(mtx, mtxLineWidth, mt2, LineWidth2, width2, height2);
help := width2;
width2 := height2;
height2 := help;
AVXMatrixMultTransposed(dest, destLineWidth, mt1, mtx, width1, height1, width2, height2, LineWidth1, mtxLineWidth);
if Assigned(aMem) then
FreeMem(aMem);
end;
end;
procedure AVXMatrixMult(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt); overload;
begin
AVXMatrixMult(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2, nil);
end;
procedure InternalAVXStrassenMult(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt;
width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; mem : PDouble);
var a11, a12, a21, a22 : PDouble;
b11, b12, b21, b22 : PDouble;
s1, s2, s3, s4 : PDouble;
t1, t2, t3, t4 : PDouble;
P1, P2, P3, P4, P5, P6, P7 : PDouble;
U1, U2, U3, U4, U5, U6, U7 : PDouble;
c11, c12, c21, c22 : PDouble;
k, m, n : NativeInt;
lineK : NativeInt;
lineN : NativeInt;
x, y : PDouble;
multLineW : NativeInt;
begin
if (width1 <= cStrassenMinSize) or (height1 <= cStrassenMinSize) or (width2 <= cStrassenMinSize) then
begin
multLineW := Max(cStrassenMinSize, height2)*sizeof(double);
AVXMatrixTranspose(mem, multLineW, mt2, LineWidth2, width2, height2);
AVXMatrixMultTransposed(dest, destLineWidth, mt1, mem, width1, height1, height2, width2, LineWidth1, multLineW);
end
else
begin
k := width1 div 2;
m := height1 div 2;
n := width2 div 2;
lineK := k*sizeof(double);
lineN := n*sizeof(double);
a11 := mt1;
a12 := a11;
inc(a12, k);
a21 := mt1;
inc(PByte(a21), m*LineWidth1);
a22 := a21;
inc(a22, k);
b11 := mt2;
b12 := b11;
inc(b12, n);
b21 := mt2;
inc(PByte(b21), k*LineWidth2);
b22 := b21;
inc(b22, n);
c11 := dest;
c12 := c11;
inc(c12, n);
c21 := dest;
inc(PByte(c21), m*destLineWidth);
c22 := c21;
inc(c22, n);
x := mem;
y := x;
inc(Y, m*Max(k, n));
S3 := X;
T3 := Y;
P7 := C21;
S1 := X;
T1 := Y;
P5 := C22;
S2 := X;
T2 := Y;
P6 := C12;
S4 := X;
P3 := C11;
P1 := X;
U2 := C12;
U3 := C21;
U4 := C12;
U7 := C22;
U5 := C12;
T4 := Y;
P4 := C11;
U6 := C21;
P2 := C11;
U1 := C11;
mem := Y;
inc(mem, k*n);
// memory efficient mult:
// s3 = A11 - A21
AVXMatrixSub(s3, lineK, a11, a21, k, m, LineWidth1, LineWidth1);
// t3 = B22 - B12
AVXMatrixSub(t3, lineN, B22, B12, n, k, LineWidth2, LineWidth2);
// p7 = s3*t3
InternalAVXStrassenMult(p7, destLineWidth, s3, t3, k, m, n, k, lineK, lineN, mem);
// s1 = a21 + a22
AVXMatrixAdd(s1, lineK, a21, a22, k, m, LineWidth1, LineWidth1);
// t1 = b12 - b11
AVXMatrixSub(t1, lineN, B12, B11, n, k, LineWidth2, LineWidth2);
// p5 = s1*t1
InternalAVXStrassenMult(p5, destLineWidth, s1, t1, k, m, n, k, lineK, lineN, mem);
// s2 = S1 - A11
AVXMatrixSub(s2, lineK, S1, A11, k, m, lineK, LineWidth1);
// t2 = b22 - t1
AVXMatrixSub(t2, lineN, B22, t1, n, k, LineWidth2, lineN);
// p6 = s2*t2
InternalAVXStrassenMult(p6, destLineWidth, s2, t2, k, m, n, k, lineK, lineN, mem);
// s4 = A12 - S2