-
Notifications
You must be signed in to change notification settings - Fork 35
/
ASMMatrixRotationsx64.pas
821 lines (630 loc) · 20.9 KB
/
ASMMatrixRotationsx64.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
// ###################################################################
// #### 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.
// ###################################################################
// Vector/Matrix rotation routines mainly used for the SVD
unit ASMMatrixRotationsx64;
interface
{$I 'mrMath_CPU.inc'}
{$IFDEF x64}
uses MatrixConst;
procedure ASMApplyPlaneRotSeqRVB(width, height : NativeInt; A : PDouble; const LineWidthA : NativeInt; {$ifdef UNIX}unixC{$ELSE}C{$endif}, {$ifdef UNIX}unixS{$ELSE}S{$endif} : PConstDoubleArr); {$IFDEF FPC}assembler;{$ENDIF}
procedure ASMApplyPlaneRotSeqRVF(width, height : NativeInt; A : PDouble; const LineWidthA : NativeInt; {$ifdef UNIX}unixC{$ELSE}C{$endif}, {$ifdef UNIX}unixS{$ELSE}S{$endif} : PConstDoubleArr); {$IFDEF FPC}assembler;{$ENDIF}
procedure ASMApplyPlaneRotSeqLVF(width, height : NativeInt; A : PDouble; const LineWidthA : NativeInt; {$ifdef UNIX}unixC{$ELSE}C{$endif}, {$ifdef UNIX}unixS{$ELSE}S{$endif} : PConstDoubleArr); {$IFDEF FPC}assembler;{$ENDIF}
procedure ASMApplyPlaneRotSeqLVB(width, height : NativeInt; A : PDouble; const LineWidthA : NativeInt; {$ifdef UNIX}unixC{$ELSE}C{$endif}, {$ifdef UNIX}unixS{$ELSE}S{$endif} : PConstDoubleArr); {$IFDEF FPC}assembler;{$ENDIF}
procedure ASMMatrixRotate(N : NativeInt; X : PDouble; const LineWidthDX : NativeInt; Y : PDouble; LineWidthDY : NativeInt; const c, s : double);
{$ENDIF}
implementation
{$IFDEF x64}
const cLocOne : double = 1;
cLocMinusOne : double = -1;
cLocMulM1Bits : Array[0..1] of Int64 = ($8000000000000000, $0);
// rcx = width, rdx = height, r8 : A, r9 = LineWidthA
procedure ASMApplyPlaneRotSeqLVB(width, height : NativeInt; A : PDouble; const LineWidthA : NativeInt; {$ifdef UNIX}unixC{$ELSE}C{$endif}, {$ifdef UNIX}unixS{$ELSE}S{$endif} : PConstDoubleArr); {$IFDEF FPC}assembler;{$ENDIF}
var iRBX, iRDI : NativeInt;
dXMM4, dXMM5, dXMM6, dXMM7 : Array[0..1] of double;
{$IFDEF UNIX}
C, S : PConstDoubleArr;
{$ENDIF}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov C, r8;
mov S, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// ##########################################
// #### Prolog - stack and base variable init
//if (height < 2) or (width < 1) then
//exit;
cmp rdx, 2;
jl @@endproc;
cmp rcx, 1;
jl @@endproc;
mov iRBX, rbx;
mov iRDI, rdi;
movupd dXMM4, xmm4;
movupd dXMM5, xmm5;
movupd dXMM6, xmm6;
movupd dXMM7, xmm7;
// iter := -(width and $FFFFFFFE)*sizeof(double);
mov r10, rcx;
and r10, $FFFFFFFFFFFFFFFE;
imul r10, -8;
//y2 := height - 1;
dec rdx;
// rcx = width, rdx = height, r8 : A, r9 = LineWidthA
mov rax, c; // point to y (aka the end)
mov rbx, s;
mov rdi, rdx;
dec rdi;
shl rdi, 3; // y2*sizeof(double)
add rax, rdi;
add rbx, rdi;
mov rdi, r9; // A[y + 1][x]
imul rdi, rdx;
add r8, rdi;
sub r8, r10;
mov r11, r8; // A[y][x]
sub r11, r9;
movsd xmm7, [rip + cLocOne];
xorpd xmm6, xmm6; // haddpd zero extend
@@foryloop:
movddup xmm0, [rax]; // c[y]
movddup xmm1, [rbx]; // s[y]
// ###########################################
// #### if (ctemp <> 1) or (stemp <> 0) then
comisd xmm0, xmm7; // = 1
jne @@beginXLoop;
comisd xmm1, xmm6; // = 0
jne @@beginXLoop;
jmp @@nextLine; // c=1 and stemp=0 next line -> the statement
// ###########################################
// #### for x := 0 to width - 1 do
@@beginXLoop:
// init
mov rdi, r10;
test rdi, rdi;
jz @@LastElem;
@@forxloop:
//temp := pcAy1^[x];
// pcAy1^[x] := cTemp*temp - stemp*pcAy^[x];
// pcAy1^[x + 1] := cTemp*temp1 - stemp*pcAy1[x + 1];
// evaluate 2 values
movupd xmm2, [r11 + rdi];
movupd xmm3, [r8 + rdi];
// temp store...
movapd xmm4, xmm2
movapd xmm5, xmm3;
mulpd xmm3, xmm0; // ctemp*pcay1^[x] and ctemp*a[x+1]
mulpd xmm2, xmm1; // stemp*pcAy^[x] and stemp*a[x+1]
subpd xmm3, xmm2;
// pcAy^[x] := stemp*temp + ctemp*pcAy^[x];
// pcAy^[x + 1] := stemp*temp1 + ctemp*pcAy^[x + 1]
mulpd xmm4, xmm0;
mulpd xmm5, xmm1;
addpd xmm5, xmm4;
// write back...
movupd [r11 + rdi], xmm5;
movupd [r8 + rdi], xmm3;
add rdi, 16;
jnz @@forxloop;
@@LastElem:
// ###########################################
// #### Last element handling
mov rdi, rcx; // width
and rdi, 1;
jz @@nextLine;
// same as above but with single elements
movsd xmm2, [r11];
movsd xmm3, [r8];
movsd xmm4, xmm2;
movsd xmm5, xmm3;
mulsd xmm3, xmm0;
mulsd xmm2, xmm1;
subsd xmm3, xmm2;
mulsd xmm4, xmm0;
mulsd xmm5, xmm1;
addsd xmm5, xmm4;
movsd [r11], xmm5;
movsd [r8], xmm3;
// ###########################################
// #### next y
@@nextLine:
sub rbx, 8; // sizeof(double)
sub rax, 8;
sub r11, r9; // LineWidthA
sub r8, r9;
dec rdx;
jnz @@foryloop;
// ###############################################
// #### epilog - restore stack
mov rbx, iRBX;
mov rdi, iRDI;
movupd xmm4, dXMM4;
movupd xmm5, dXMM5;
movupd xmm6, dXMM6;
movupd xmm7, dXMM7;
@@endproc:
end;
procedure ASMApplyPlaneRotSeqLVF(width, height : NativeInt; A : PDouble; const LineWidthA : NativeInt; {$ifdef UNIX}unixC{$ELSE}C{$endif}, {$ifdef UNIX}unixS{$ELSE}S{$endif} : PConstDoubleArr); {$IFDEF FPC}assembler;{$ENDIF}
var iRBX, iRDI : NativeInt;
dXMM4, dXMM5, dXMM6, dXMM7 : Array[0..1] of double;
{$IFDEF UNIX}
C, S : PConstDoubleArr;
{$ENDIF}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov C, r8;
mov S, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// ##########################################
// #### Prolog - stack and base variable init
//if (height < 2) or (width < 1) then
//exit;
cmp rdx, 2;
jl @@endproc;
cmp rcx, 1;
jl @@endproc;
mov iRBX, rbx;
mov iRDI, rdi;
movupd dXMM4, xmm4;
movupd dXMM5, xmm5;
movupd dXMM6, xmm6;
movupd dXMM7, xmm7;
// iter := -(width and $FFFFFFFE)*sizeof(double);
mov r10, rcx;
and r10, $FFFFFFFFFFFFFFFE;
imul r10, -8;
//y2 := height - 1;
dec rdx;
mov rax, c; // point to y (aka the end)
mov rbx, s;
sub r8, r10;
mov r11, r8; // A[y][x]
add r11, r9;
movsd xmm7, [rip + cLocOne];
xorpd xmm6, xmm6; // haddpd zero extend
@@foryloop:
movddup xmm0, [rax]; // c[y]
movddup xmm1, [rbx]; // s[y]
// ###########################################
// #### if (ctemp <> 1) or (stemp <> 0) then
comisd xmm0, xmm7; // = 1
jne @@beginXLoop;
comisd xmm1, xmm6; // = 0
jne @@beginXLoop;
jmp @@nextLine; // c=1 and stemp=0 next line -> the statement
// ###########################################
// #### for x := 0 to width - 1 do
@@beginXLoop:
// init
mov rdi, r10;
test rdi, rdi;
jz @@LastElem;
@@forxloop:
//temp := pcAy1^[x];
// pcAy1^[x] := cTemp*temp - stemp*pcAy^[x];
// pcAy1^[x + 1] := cTemp*temp1 - stemp*pcAy1[x + 1];
// evaluate 2 values
movupd xmm2, [r8 + rdi];
movupd xmm3, [r11 + rdi];
// temp store...
movapd xmm4, xmm2
movapd xmm5, xmm3;
mulpd xmm3, xmm0; // ctemp*pcay1^[x] and ctemp*a[x+1]
mulpd xmm2, xmm1; // stemp*pcAy^[x] and stemp*a[x+1]
subpd xmm3, xmm2;
// pcAy^[x] := stemp*temp + ctemp*pcAy^[x];
// pcAy^[x + 1] := stemp*temp1 + ctemp*pcAy^[x + 1]
mulpd xmm4, xmm0;
mulpd xmm5, xmm1;
addpd xmm5, xmm4;
// write back...
movupd [r8 + rdi], xmm5;
movupd [r11 + rdi], xmm3;
add rdi, 16;
jnz @@forxloop;
@@LastElem:
// ###########################################
// #### Last element handling
mov rdi, rcx; // width
and rdi, 1;
jz @@nextLine;
// same as above but with single elements
movsd xmm2, [r8];
movsd xmm3, [r11];
movsd xmm4, xmm2;
movsd xmm5, xmm3;
mulsd xmm3, xmm0;
mulsd xmm2, xmm1;
subsd xmm3, xmm2;
mulsd xmm4, xmm0;
mulsd xmm5, xmm1;
addsd xmm5, xmm4;
movsd [r8], xmm5;
movsd [r11], xmm3;
// ###########################################
// #### next y
@@nextLine:
add rbx, 8; // sizeof(double)
add rax, 8;
add r11, r9; // LineWidthA
add r8, r9;
dec rdx;
jnz @@foryloop;
// ###############################################
// #### epilog - restore stack
mov rbx, iRBX;
mov rdi, iRDI;
movupd xmm4, dXMM4;
movupd xmm5, dXMM5;
movupd xmm6, dXMM6;
movupd xmm7, dXMM7;
@@endproc:
end;
// rcx = width, rdx = height, r8 : A, r9 = LineWidthA
procedure ASMApplyPlaneRotSeqRVB(width, height : NativeInt; A : PDouble; const LineWidthA : NativeInt; {$ifdef UNIX}unixC{$ELSE}C{$endif}, {$ifdef UNIX}unixS{$ELSE}S{$endif} : PConstDoubleArr); {$IFDEF FPC}assembler;{$ENDIF}
var iRBX, iRDI : NativeInt;
dXMM4, dXMM5, dXMM7 : Array[0..1] of double;
{$IFDEF UNIX}
C, S : PConstDoubleArr;
{$ENDIF}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov C, r8;
mov S, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// ##########################################
// #### Prolog - stack and base variable init
//if (height < 2) or (width < 1) then
//exit;
cmp rcx, 2;
jl @@endproc;
cmp rdx, 1;
jl @@endproc;
mov iRBX, rbx;
mov iRDI, rdi;
movupd dXMM4, xmm4;
movupd dXMM5, xmm5;
movupd dXMM7, xmm7;
dec rcx;
imul rcx, 8; //iter := (width - 1)*sizeof(double)
mov rax, c;
mov rbx, s;
movupd xmm7, [rip + cLocMulM1Bits];
@@foryloop:
mov rdi, rcx;
movhpd xmm2, [r8 + rdi];
// for x := width - 2 downto 0
@@forxloop:
movsd xmm4, [rax + rdi - 8]; // store c
movsd xmm3, [rbx + rdi - 8]; // store s
movlpd xmm2, [r8 + rdi - 8]; // a[x], a[x+1]
// handle x, x+1
// ####################################
// #### x, x+ 1
movlhps xmm3, xmm4;
movlhps xmm4, xmm3;
xorpd xmm3, xmm7; // -s, c
mulpd xmm3, xmm2; // a[x+1)*c[x] - s[x]*a[x]
haddpd xmm3, xmm3;
mulpd xmm4, xmm2; // a[x+1]*s[x] + a[x]*c[x]
haddpd xmm4, xmm4;
// write back first two values
movlhps xmm2, xmm4;
movsd [r8 + rdi], xmm3;
// next one
sub rdi, 8;
jnz @@forxloop;
movsd [r8 + rdi], xmm4;
add r8, r9;
dec rdx;
jnz @@foryloop;
// epilog
mov rbx, iRBX;
mov rdi, iRDI;
movupd xmm4, dXMM4;
movupd xmm5, dXMM5;
movupd xmm7, dXMM7;
@@endproc:
end;
procedure ASMApplyPlaneRotSeqRVF(width, height : NativeInt; A : PDouble; const LineWidthA : NativeInt; {$ifdef UNIX}unixC{$ELSE}C{$endif}, {$ifdef UNIX}unixS{$ELSE}S{$endif} : PConstDoubleArr); {$IFDEF FPC}assembler;{$ENDIF}
var iRBX, iRDI : NativeInt;
dXMM4, dXMM5, dXMM7 : Array[0..1] of double;
{$IFDEF UNIX}
C, S : PConstDoubleArr;
{$ENDIF}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov C, r8;
mov S, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// ##########################################
// #### Prolog - stack and base variable init
//if (height < 2) or (width < 1) then
//exit;
cmp rcx, 2;
jl @@endproc;
cmp rdx, 1;
jl @@endproc;
mov iRBX, rbx;
mov iRDI, rdi;
movupd dXMM4, xmm4;
movupd dXMM5, xmm5;
movupd dXMM7, xmm7;
dec rcx;
imul rcx, -8; //iter := (width - 1)*sizeof(double)
mov rax, c;
mov rbx, s;
sub rax, rcx; // iter
sub rbx, rcx;
sub r8, rcx;
movupd xmm7, [rip + cLocMulM1Bits];
@@foryloop:
mov rdi, rcx;
movsd xmm2, [r8 + rdi];
@@forxloop:
movsd xmm4, [rax + rdi]; // store c
movsd xmm3, [rbx + rdi]; // store s
movsd xmm0, [r8 + rdi + 8]; // a[x], a[x+1]
movlhps xmm2, xmm0;
// handle x, x+1
// ####################################
// #### x, x+ 1
movlhps xmm3, xmm4;
movlhps xmm4, xmm3;
xorpd xmm3, xmm7; // -s, c
mulpd xmm3, xmm2; // a[x+1)*c[x] - s[x]*a[x]
haddpd xmm3, xmm3;
mulpd xmm4, xmm2; // a[x+1]*s[x] + a[x]*c[x]
haddpd xmm4, xmm4;
// write back first two values
movsd xmm2, xmm3;
movsd [r8 + rdi], xmm4;
// next one
add rdi, 8;
jnz @@forxloop;
movsd [r8 + rdi], xmm2;
add r8, r9;
dec rdx;
jnz @@foryloop;
// epilog
mov rbx, iRBX;
mov rdi, iRDI;
movupd xmm4, dXMM4;
movupd xmm5, dXMM5;
movupd xmm7, dXMM7;
@@endproc:
end;
// its assumed that Linewidthdx and linewidthdy = sizeof(double)
// rcx = N, RDX = X, R8 = Y,
procedure ASMMatrixRotateAligned(N : NativeInt; X : PDouble;
Y : PDouble; c, {$ifdef UNIX}unixS{$ELSE}s{$endif} : PDouble); {$IFDEF FPC}assembler;{$ENDIF}
var dXMM4, dXMM5, dXMM6 : Array[0..1] of double;
{$ifdef UNIX}
s : PDouble;
{$endif}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov s, r8;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// ###########################################
// #### Stack push
movupd dXMM4, xmm4;
movupd dXMM5, xmm5;
movupd dXMM6, xmm6;
// init
mov rax, s;
movsd xmm2, [rax];
mulsd xmm2, [rip + cLocMinusOne];
movddup xmm0, xmm2;
movddup xmm1, [r9];
mov rax, s;
movddup xmm2, [rax];
xor r10, r10;
mov rax, rcx;
shr rax, 1;
test rax, rax;
jz @@exitLoop;
@@forNloop:
// do a full load -> intermediate store in xmm5, and xmm6
movupd xmm5, [rdx + r10]; // x, x+1
movupd xmm6, [r8 + r10]; // y, y+1
movapd xmm3, xmm5;
movapd xmm4, xmm6;
mulpd xmm3, xmm0; // x, x+1 * -s
mulpd xmm5, xmm1; // x, x+1 * c
mulpd xmm6, xmm2; // y, y+1 * s
mulpd xmm4, xmm1; // y, y+1 * c
addpd xmm5, xmm6; // c*x + s*y , c*(x+1) + s*(y+1)
addpd xmm3, xmm4; // -s*x + c*y, -s(x+1) + c*(y+1)
// write back
movupd [rdx + r10], xmm5;
movupd [r8 + r10], xmm3;
add r10, 16;
dec rax;
jnz @@forNloop;
@@exitLoop:
// test for an odd N
mov rax, rcx;
and rax, 1;
jz @@endProc;
// handle last element
movsd xmm5, [rdx + r10];
movsd xmm6, [r8 + r10];
//dtemp := c*pX^[i] + s*pY^[i];
//pY^[i] := - s*pX^[i] + c*pY^[i];
//px^[i] := dtemp;
movsd xmm3, xmm5;
movsd xmm4, xmm6;
mulsd xmm3, xmm0; // x * -s
mulsd xmm5, xmm1; // x * c
mulsd xmm6, xmm2; // y * s
mulsd xmm4, xmm1; // y * c
addsd xmm5, xmm6; // c*x + s*y
addsd xmm3, xmm4; // -s*x + c*y
// write back
movsd [rdx + r10], xmm5;
movsd [r8 + r10], xmm3;
@@endProc:
// ###########################################
// #### epilog
movupd xmm4, dXMM4;
movupd xmm5, dXMM5;
movupd xmm6, dXMM6;
end;
// rcx = N, RDX = X, R8 = LineWidthDX, R9 = Y;
procedure ASMMatrixRotateUnaligned(N : NativeInt; X : PDouble; const LineWidthDX : NativeInt;
Y : PDouble; {$ifdef UNIX}unixLineWidthDY{$ELSE}LineWidthDY{$endif} : NativeInt; {$ifdef UNIX}unixc{$ELSE}c{$endif}, s : Pdouble); {$IFDEF FPC}assembler;{$ENDIF}
var dXMM4, dXMM5, dXMM6 : Array[0..1] of double;
{$ifdef UNIX}
LineWidthDY : NativeInt;
C : PDouble;
{$endif}
asm
{$IFDEF UNIX}
// Linux uses a diffrent ABI -> copy over the registers so they meet with winABI
// (note that the 5th and 6th parameter are are on the stack)
// The parameters are passed in the following order:
// RDI, RSI, RDX, RCX -> mov to RCX, RDX, R8, R9
mov LineWidthDY, r8;
mov C, r9;
mov r8, rdx;
mov r9, rcx;
mov rcx, rdi;
mov rdx, rsi;
{$ENDIF}
// ###########################################
// #### Stack push
movupd dXMM4, xmm4;
movupd dXMM5, xmm5;
movupd dXMM6, xmm6;
mov rax, s;
movsd xmm2, [rax];
mulsd xmm2, [rip + cLocMinusOne];
movddup xmm0, xmm2;
mov rax, C;
movddup xmm1, [rax];
mov rax, s;
movddup xmm2, [rax];
mov r10, LineWidthDY;
// ###########################################
// #### loop
mov rax, rcx;
shr rax, 1;
test rax, rax;
jz @@exitLoop;
@@forNloop:
// do a full load -> intermediate store in xmm5, and xmm6
movlpd xmm5, [rdx];
movhpd xmm5, [rdx + r8];
movlpd xmm6, [r9];
movhpd xmm6, [r9 + r10];
movapd xmm3, xmm5;
movapd xmm4, xmm6;
mulpd xmm3, xmm0; // x, x+1 * -s
mulpd xmm5, xmm1; // x, x+1 * c
mulpd xmm6, xmm2; // y, y+1 * s
mulpd xmm4, xmm1; // y, y+1 * c
addpd xmm5, xmm6; // c*x + s*y , c*(x+1) + s*(y+1)
addpd xmm3, xmm4; // -s*x + c*y, -s(x+1) + c*(y+1)
// write back
movlpd [rdx], xmm5;
movhpd [rdx + r8], xmm5;
movlpd [r9], xmm3;
movhpd [r9 + r10], xmm3;
add rdx, r8;
add rdx, r8;
add r9, r10;
add r9, r10;
dec rax;
jnz @@forNloop;
@@exitLoop:
// test for an odd N
mov rax, rcx;
and rax, 1;
jz @@endProc;
// handle last element
movsd xmm5, [rdx];
movsd xmm6, [r9];
//dtemp := c*pX^[i] + s*pY^[i];
//pY^[i] := - s*pX^[i] + c*pY^[i];
//px^[i] := dtemp;
movsd xmm3, xmm5;
movsd xmm4, xmm6;
mulsd xmm3, xmm0; // x * -s
mulsd xmm5, xmm1; // x * c
mulsd xmm6, xmm2; // y * s
mulsd xmm4, xmm1; // y * c
addsd xmm5, xmm6; // c*x + s*y
addsd xmm3, xmm4; // -s*x + c*y
// write back
movsd [rdx], xmm5;
movsd [r9], xmm3;
@@endProc:
// ###########################################
// #### Epilog
movupd xmm4, dXMM4;
movupd xmm5, dXMM5;
movupd xmm6, dXMM6;
end;
procedure ASMMatrixRotate(N : NativeInt; X : PDouble; const LineWidthDX : NativeInt; Y : PDouble; LineWidthDY : NativeInt; const c, s : double);
begin
if N <= 0 then
exit;
if (LineWidthDX = sizeof(double)) and (LineWidthDY = sizeof(double))
then
ASMMatrixRotateAligned(N, X, Y, @c, @s)
else
ASMMatrixRotateUnAligned(N, X, LineWidthDX, Y, LineWidthDY, @c, @s)
end;
{$ENDIF}
end.