-
Notifications
You must be signed in to change notification settings - Fork 5
/
FastMove.pas
1568 lines (1505 loc) · 42.6 KB
/
FastMove.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
unit FastMove;
(*
Copyright (c) 2006, John O'Harrow ([email protected])
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
-------------------------------------------------------------------------------
Version: 3.03 : 06-MAR-2006
How to use:
Include this unit in any uses clause - Thats It!
What is Does:
This unit replaces all calls to system.move with calls to a faster move
procedure (Up to 3 times faster). The code will automatically detect and
use MMX, SSE, SSE2 or SSE3 where available.
Version Change
------- ------
2.00 Updated all Moves to use Move_JOH_XXX_6 (better code alignment).
2.10 VirtualProtect modified to use PAGE_EXECUTE_READWRITE to prevent
error under WinXP SP2 with AMD64 running DEP.
2.20 Added Check for "Already Patched" to prevent error when using.
Packages in both DLL's and EXE.
2.30 PrefetchLimit initialization moved outside of ReplaceMove.
3.00 Updated all Moves to use latest Move_JOH_XXX_10 procedures and added
SSE2 and SSE3 optimized moves.
Extra VirtualProtect removed. PatchSystemMove Directive added.
Local Move Procedure added for direct calling of fastest Move.
3.01 {$D-} Added to prevent stepping into the Move BASM Code
Badly Names PrefetchLimit renamed CacheLimit
3.02 Incorrect DB equivalents of lddqu instruction corrected
3.03 Compiler Directives for D2006 Added
Contact Email address corrected
*)
{$D-} {Prevent Steppping into Move Code}
{$DEFINE PatchSystemMove} {Enable Patching of System.Move}
{$IFDEF VER170}
{$DEFINE SSE2Basm}
{$WARN UNSAFE_CODE OFF}
{$ENDIF}
{$IFDEF CONDITIONALEXPRESSIONS}
{$IF CompilerVersion >= 18.0}
{$DEFINE SSE2Basm}
{$WARN UNSAFE_CODE OFF}
{$IFEND}
{$ENDIF}
interface
var
Move : procedure(const Source; var Dest; Count : Integer); {Fastest Move}
{Procedures interfaced only for testing and validation purposes}
procedure MoveJOH_PAS_10 (const Source; var Dest; Count : Integer);
procedure MoveJOH_IA32_10(const Source; var Dest; Count : Integer);
procedure MoveJOH_MMX_10 (const Source; var Dest; Count : Integer);
procedure MoveJOH_SSE_10 (const Source; var Dest; Count : Integer);
procedure MoveJOH_SSE2_10(const Source; var Dest; Count : Integer);
procedure MoveJOH_SSE3_10(const Source; var Dest; Count : Integer);
implementation
uses
Windows, SysUtils, FastcodeCPUID;
var
CacheLimit : Integer; {Used within SSE Moves}
{-------------------------------------------------------------------------}
{Move without using any BASM Code}
procedure MoveJOH_PAS_10(const Source; var Dest; Count : Integer);
var
S, D : Cardinal;
Temp, C, I : Integer;
L : PInteger;
begin
S := Cardinal(@Source);
D := Cardinal(@Dest);
if S = D then
Exit;
if Count <= 4 then
case Count of
1 : PByte(@Dest)^ := PByte(S)^;
2 : PWord(@Dest)^ := PWord(S)^;
3 : if D > S then
begin
PByte(Integer(@Dest)+2)^ := PByte(S+2)^;
PWord(@Dest)^ := PWord(S)^;
end
else
begin
PWord(@Dest)^ := PWord(S)^;
PByte(Integer(@Dest)+2)^ := PByte(S+2)^;
end;
4 : PInteger(@Dest)^ := PInteger(S)^
else Exit; {Count <= 0}
end
else
if D > S then
begin
Temp := PInteger(S)^;
I := Integer(@Dest);
C := Count - 4;
L := PInteger(Integer(@Dest) + C);
Inc(S, C);
repeat
L^ := PInteger(S)^;
if Count <= 8 then
Break;
Dec(Count, 4);
Dec(S, 4);
Dec(L);
until False;
PInteger(I)^ := Temp;
end
else
begin
C := Count - 4;
Temp := PInteger(S + Cardinal(C))^;
I := Integer(@Dest) + C;
L := @Dest;
repeat
L^ := PInteger(S)^;
if Count <= 8 then
Break;
Dec(Count, 4);
Inc(S, 4);
Inc(L);
until False;
PInteger(I)^ := Temp;
end;
end; {MoveJOH_PAS}
const
TINYSIZE = 36;
{-------------------------------------------------------------------------}
{Perform Forward Move of 0..36 Bytes}
{On Entry, ECX = Count, EAX = Source+Count, EDX = Dest+Count. Destroys ECX}
procedure SmallForwardMove_10;
asm
jmp dword ptr [@@FwdJumpTable+ecx*4]
nop {Align Jump Table}
@@FwdJumpTable:
dd @@Done {Removes need to test for zero size move}
dd @@Fwd01, @@Fwd02, @@Fwd03, @@Fwd04, @@Fwd05, @@Fwd06, @@Fwd07, @@Fwd08
dd @@Fwd09, @@Fwd10, @@Fwd11, @@Fwd12, @@Fwd13, @@Fwd14, @@Fwd15, @@Fwd16
dd @@Fwd17, @@Fwd18, @@Fwd19, @@Fwd20, @@Fwd21, @@Fwd22, @@Fwd23, @@Fwd24
dd @@Fwd25, @@Fwd26, @@Fwd27, @@Fwd28, @@Fwd29, @@Fwd30, @@Fwd31, @@Fwd32
dd @@Fwd33, @@Fwd34, @@Fwd35, @@Fwd36
@@Fwd36:
mov ecx, [eax-36]
mov [edx-36], ecx
@@Fwd32:
mov ecx, [eax-32]
mov [edx-32], ecx
@@Fwd28:
mov ecx, [eax-28]
mov [edx-28], ecx
@@Fwd24:
mov ecx, [eax-24]
mov [edx-24], ecx
@@Fwd20:
mov ecx, [eax-20]
mov [edx-20], ecx
@@Fwd16:
mov ecx, [eax-16]
mov [edx-16], ecx
@@Fwd12:
mov ecx, [eax-12]
mov [edx-12], ecx
@@Fwd08:
mov ecx, [eax-8]
mov [edx-8], ecx
@@Fwd04:
mov ecx, [eax-4]
mov [edx-4], ecx
ret
nop
@@Fwd35:
mov ecx, [eax-35]
mov [edx-35], ecx
@@Fwd31:
mov ecx, [eax-31]
mov [edx-31], ecx
@@Fwd27:
mov ecx, [eax-27]
mov [edx-27], ecx
@@Fwd23:
mov ecx, [eax-23]
mov [edx-23], ecx
@@Fwd19:
mov ecx, [eax-19]
mov [edx-19], ecx
@@Fwd15:
mov ecx, [eax-15]
mov [edx-15], ecx
@@Fwd11:
mov ecx, [eax-11]
mov [edx-11], ecx
@@Fwd07:
mov ecx, [eax-7]
mov [edx-7], ecx
mov ecx, [eax-4]
mov [edx-4], ecx
ret
nop
@@Fwd03:
movzx ecx, word ptr [eax-3]
mov [edx-3], cx
movzx ecx, byte ptr [eax-1]
mov [edx-1], cl
ret
@@Fwd34:
mov ecx, [eax-34]
mov [edx-34], ecx
@@Fwd30:
mov ecx, [eax-30]
mov [edx-30], ecx
@@Fwd26:
mov ecx, [eax-26]
mov [edx-26], ecx
@@Fwd22:
mov ecx, [eax-22]
mov [edx-22], ecx
@@Fwd18:
mov ecx, [eax-18]
mov [edx-18], ecx
@@Fwd14:
mov ecx, [eax-14]
mov [edx-14], ecx
@@Fwd10:
mov ecx, [eax-10]
mov [edx-10], ecx
@@Fwd06:
mov ecx, [eax-6]
mov [edx-6], ecx
@@Fwd02:
movzx ecx, word ptr [eax-2]
mov [edx-2], cx
ret
nop
nop
nop
@@Fwd33:
mov ecx, [eax-33]
mov [edx-33], ecx
@@Fwd29:
mov ecx, [eax-29]
mov [edx-29], ecx
@@Fwd25:
mov ecx, [eax-25]
mov [edx-25], ecx
@@Fwd21:
mov ecx, [eax-21]
mov [edx-21], ecx
@@Fwd17:
mov ecx, [eax-17]
mov [edx-17], ecx
@@Fwd13:
mov ecx, [eax-13]
mov [edx-13], ecx
@@Fwd09:
mov ecx, [eax-9]
mov [edx-9], ecx
@@Fwd05:
mov ecx, [eax-5]
mov [edx-5], ecx
@@Fwd01:
movzx ecx, byte ptr [eax-1]
mov [edx-1], cl
ret
@@Done:
end; {SmallForwardMove}
{-------------------------------------------------------------------------}
{Perform Backward Move of 0..36 Bytes}
{On Entry, ECX = Count, EAX = Source, EDX = Dest. Destroys ECX}
procedure SmallBackwardMove_10;
asm
jmp dword ptr [@@BwdJumpTable+ecx*4]
nop {Align Jump Table}
@@BwdJumpTable:
dd @@Done {Removes need to test for zero size move}
dd @@Bwd01, @@Bwd02, @@Bwd03, @@Bwd04, @@Bwd05, @@Bwd06, @@Bwd07, @@Bwd08
dd @@Bwd09, @@Bwd10, @@Bwd11, @@Bwd12, @@Bwd13, @@Bwd14, @@Bwd15, @@Bwd16
dd @@Bwd17, @@Bwd18, @@Bwd19, @@Bwd20, @@Bwd21, @@Bwd22, @@Bwd23, @@Bwd24
dd @@Bwd25, @@Bwd26, @@Bwd27, @@Bwd28, @@Bwd29, @@Bwd30, @@Bwd31, @@Bwd32
dd @@Bwd33, @@Bwd34, @@Bwd35, @@Bwd36
@@Bwd36:
mov ecx, [eax+32]
mov [edx+32], ecx
@@Bwd32:
mov ecx, [eax+28]
mov [edx+28], ecx
@@Bwd28:
mov ecx, [eax+24]
mov [edx+24], ecx
@@Bwd24:
mov ecx, [eax+20]
mov [edx+20], ecx
@@Bwd20:
mov ecx, [eax+16]
mov [edx+16], ecx
@@Bwd16:
mov ecx, [eax+12]
mov [edx+12], ecx
@@Bwd12:
mov ecx, [eax+8]
mov [edx+8], ecx
@@Bwd08:
mov ecx, [eax+4]
mov [edx+4], ecx
@@Bwd04:
mov ecx, [eax]
mov [edx], ecx
ret
nop
nop
nop
@@Bwd35:
mov ecx, [eax+31]
mov [edx+31], ecx
@@Bwd31:
mov ecx, [eax+27]
mov [edx+27], ecx
@@Bwd27:
mov ecx, [eax+23]
mov [edx+23], ecx
@@Bwd23:
mov ecx, [eax+19]
mov [edx+19], ecx
@@Bwd19:
mov ecx, [eax+15]
mov [edx+15], ecx
@@Bwd15:
mov ecx, [eax+11]
mov [edx+11], ecx
@@Bwd11:
mov ecx, [eax+7]
mov [edx+7], ecx
@@Bwd07:
mov ecx, [eax+3]
mov [edx+3], ecx
mov ecx, [eax]
mov [edx], ecx
ret
nop
nop
nop
@@Bwd03:
movzx ecx, word ptr [eax+1]
mov [edx+1], cx
movzx ecx, byte ptr [eax]
mov [edx], cl
ret
nop
nop
@@Bwd34:
mov ecx, [eax+30]
mov [edx+30], ecx
@@Bwd30:
mov ecx, [eax+26]
mov [edx+26], ecx
@@Bwd26:
mov ecx, [eax+22]
mov [edx+22], ecx
@@Bwd22:
mov ecx, [eax+18]
mov [edx+18], ecx
@@Bwd18:
mov ecx, [eax+14]
mov [edx+14], ecx
@@Bwd14:
mov ecx, [eax+10]
mov [edx+10], ecx
@@Bwd10:
mov ecx, [eax+6]
mov [edx+6], ecx
@@Bwd06:
mov ecx, [eax+2]
mov [edx+2], ecx
@@Bwd02:
movzx ecx, word ptr [eax]
mov [edx], cx
ret
nop
@@Bwd33:
mov ecx, [eax+29]
mov [edx+29], ecx
@@Bwd29:
mov ecx, [eax+25]
mov [edx+25], ecx
@@Bwd25:
mov ecx, [eax+21]
mov [edx+21], ecx
@@Bwd21:
mov ecx, [eax+17]
mov [edx+17], ecx
@@Bwd17:
mov ecx, [eax+13]
mov [edx+13], ecx
@@Bwd13:
mov ecx, [eax+9]
mov [edx+9], ecx
@@Bwd09:
mov ecx, [eax+5]
mov [edx+5], ecx
@@Bwd05:
mov ecx, [eax+1]
mov [edx+1], ecx
@@Bwd01:
movzx ecx, byte ptr[eax]
mov [edx], cl
ret
@@Done:
end; {SmallBackwardMove}
{-------------------------------------------------------------------------}
{Move ECX Bytes from EAX to EDX, where EAX > EDX and ECX > 36 (TINYSIZE)}
procedure Forwards_IA32_10;
asm
fild qword ptr [eax] {First 8}
lea eax, [eax+ecx-8]
lea ecx, [edx+ecx-8]
push edx
push ecx
fild qword ptr [eax] {Last 8}
neg ecx {QWORD Align Writes}
and edx, -8
lea ecx, [ecx+edx+8]
pop edx
@@Loop:
fild qword ptr [eax+ecx]
fistp qword ptr [edx+ecx]
add ecx, 8
jl @@Loop
pop eax
fistp qword ptr [edx] {Last 8}
fistp qword ptr [eax] {First 8}
end; {Forwards_IA32}
{-------------------------------------------------------------------------}
{Move ECX Bytes from EAX to EDX, where EAX < EDX and ECX > 36 (TINYSIZE)}
procedure Backwards_IA32_10;
asm
sub ecx, 8
fild qword ptr [eax+ecx] {Last 8}
fild qword ptr [eax] {First 8}
add ecx, edx {QWORD Align Writes}
push ecx
and ecx, -8
sub ecx, edx
@@Loop:
fild qword ptr [eax+ecx]
fistp qword ptr [edx+ecx]
sub ecx, 8
jg @@Loop
pop eax
fistp qword ptr [edx] {First 8}
fistp qword ptr [eax] {Last 8}
end; {Backwards_IA32}
{-------------------------------------------------------------------------}
{Move ECX Bytes from EAX to EDX, where EAX > EDX and ECX > 36 (TINYSIZE)}
procedure Forwards_MMX_10;
const
SMALLSIZE = 64;
LARGESIZE = 2048;
asm
cmp ecx, SMALLSIZE {Size at which using MMX becomes worthwhile}
jl Forwards_IA32_10
cmp ecx, LARGESIZE
jge @@FwdLargeMove
push ebx
mov ebx, edx
movq mm0, [eax] {First 8 Bytes}
add eax, ecx {QWORD Align Writes}
add ecx, edx
and edx, -8
add edx, 40
sub ecx, edx
add edx, ecx
neg ecx
nop {Align Loop}
@@FwdLoopMMX:
movq mm1, [eax+ecx-32]
movq mm2, [eax+ecx-24]
movq mm3, [eax+ecx-16]
movq mm4, [eax+ecx- 8]
movq [edx+ecx-32], mm1
movq [edx+ecx-24], mm2
movq [edx+ecx-16], mm3
movq [edx+ecx- 8], mm4
add ecx, 32
jle @@FwdLoopMMX
movq [ebx], mm0 {First 8 Bytes}
emms
pop ebx
neg ecx
add ecx, 32
jmp SmallForwardMove_10
nop {Align Loop}
nop
@@FwdLargeMove:
push ebx
mov ebx, ecx
test edx, 15
jz @@FwdAligned
lea ecx, [edx+15] {16 byte Align Destination}
and ecx, -16
sub ecx, edx
add eax, ecx
add edx, ecx
sub ebx, ecx
call SmallForwardMove_10
@@FwdAligned:
mov ecx, ebx
and ecx, -16
sub ebx, ecx {EBX = Remainder}
push esi
push edi
mov esi, eax {ESI = Source}
mov edi, edx {EDI = Dest}
mov eax, ecx {EAX = Count}
and eax, -64 {EAX = No of Bytes to Blocks Moves}
and ecx, $3F {ECX = Remaining Bytes to Move (0..63)}
add esi, eax
add edi, eax
neg eax
@@MMXcopyloop:
movq mm0, [esi+eax ]
movq mm1, [esi+eax+ 8]
movq mm2, [esi+eax+16]
movq mm3, [esi+eax+24]
movq mm4, [esi+eax+32]
movq mm5, [esi+eax+40]
movq mm6, [esi+eax+48]
movq mm7, [esi+eax+56]
movq [edi+eax ], mm0
movq [edi+eax+ 8], mm1
movq [edi+eax+16], mm2
movq [edi+eax+24], mm3
movq [edi+eax+32], mm4
movq [edi+eax+40], mm5
movq [edi+eax+48], mm6
movq [edi+eax+56], mm7
add eax, 64
jnz @@MMXcopyloop
emms {Empty MMX State}
add ecx, ebx
shr ecx, 2
rep movsd
mov ecx, ebx
and ecx, 3
rep movsb
pop edi
pop esi
pop ebx
end; {Forwards_MMX}
{-------------------------------------------------------------------------}
{Move ECX Bytes from EAX to EDX, where EAX < EDX and ECX > 36 (TINYSIZE)}
procedure Backwards_MMX_10;
const
SMALLSIZE = 64;
asm
cmp ecx, SMALLSIZE {Size at which using MMX becomes worthwhile}
jl Backwards_IA32_10
push ebx
movq mm0, [eax+ecx-8] {Get Last QWORD}
lea ebx, [edx+ecx] {QWORD Align Writes}
and ebx, 7
sub ecx, ebx
add ebx, ecx
sub ecx, 32
@@BwdLoopMMX:
movq mm1, [eax+ecx ]
movq mm2, [eax+ecx+ 8]
movq mm3, [eax+ecx+16]
movq mm4, [eax+ecx+24]
movq [edx+ecx+24], mm4
movq [edx+ecx+16], mm3
movq [edx+ecx+ 8], mm2
movq [edx+ecx ], mm1
sub ecx, 32
jge @@BwdLoopMMX
movq [edx+ebx-8], mm0 {Last QWORD}
emms
add ecx, 32
pop ebx
jmp SmallBackwardMove_10
end; {Backwards_MMX}
{-------------------------------------------------------------------------}
procedure LargeAlignedSSEMove;
asm
@@Loop:
movaps xmm0, [eax+ecx]
movaps xmm1, [eax+ecx+16]
movaps xmm2, [eax+ecx+32]
movaps xmm3, [eax+ecx+48]
movaps [edx+ecx], xmm0
movaps [edx+ecx+16], xmm1
movaps [edx+ecx+32], xmm2
movaps [edx+ecx+48], xmm3
movaps xmm4, [eax+ecx+64]
movaps xmm5, [eax+ecx+80]
movaps xmm6, [eax+ecx+96]
movaps xmm7, [eax+ecx+112]
movaps [edx+ecx+64], xmm4
movaps [edx+ecx+80], xmm5
movaps [edx+ecx+96], xmm6
movaps [edx+ecx+112], xmm7
add ecx, 128
js @@Loop
end; {LargeAlignedSSEMove}
{-------------------------------------------------------------------------}
procedure LargeUnalignedSSEMove;
asm
@@Loop:
movups xmm0, [eax+ecx]
movups xmm1, [eax+ecx+16]
movups xmm2, [eax+ecx+32]
movups xmm3, [eax+ecx+48]
movaps [edx+ecx], xmm0
movaps [edx+ecx+16], xmm1
movaps [edx+ecx+32], xmm2
movaps [edx+ecx+48], xmm3
movups xmm4, [eax+ecx+64]
movups xmm5, [eax+ecx+80]
movups xmm6, [eax+ecx+96]
movups xmm7, [eax+ecx+112]
movaps [edx+ecx+64], xmm4
movaps [edx+ecx+80], xmm5
movaps [edx+ecx+96], xmm6
movaps [edx+ecx+112], xmm7
add ecx, 128
js @@Loop
end; {LargeUnalignedSSEMove}
{-------------------------------------------------------------------------}
procedure HugeAlignedSSEMove;
const
Prefetch = 512;
asm
@@Loop:
prefetchnta [eax+ecx+Prefetch]
prefetchnta [eax+ecx+Prefetch+64]
movaps xmm0, [eax+ecx]
movaps xmm1, [eax+ecx+16]
movaps xmm2, [eax+ecx+32]
movaps xmm3, [eax+ecx+48]
movntps [edx+ecx], xmm0
movntps [edx+ecx+16], xmm1
movntps [edx+ecx+32], xmm2
movntps [edx+ecx+48], xmm3
movaps xmm4, [eax+ecx+64]
movaps xmm5, [eax+ecx+80]
movaps xmm6, [eax+ecx+96]
movaps xmm7, [eax+ecx+112]
movntps [edx+ecx+64], xmm4
movntps [edx+ecx+80], xmm5
movntps [edx+ecx+96], xmm6
movntps [edx+ecx+112], xmm7
add ecx, 128
js @@Loop
sfence
end; {HugeAlignedSSEMove}
{-------------------------------------------------------------------------}
procedure HugeUnalignedSSEMove;
const
Prefetch = 512;
asm
@@Loop:
prefetchnta [eax+ecx+Prefetch]
prefetchnta [eax+ecx+Prefetch+64]
movups xmm0, [eax+ecx]
movups xmm1, [eax+ecx+16]
movups xmm2, [eax+ecx+32]
movups xmm3, [eax+ecx+48]
movntps [edx+ecx], xmm0
movntps [edx+ecx+16], xmm1
movntps [edx+ecx+32], xmm2
movntps [edx+ecx+48], xmm3
movups xmm4, [eax+ecx+64]
movups xmm5, [eax+ecx+80]
movups xmm6, [eax+ecx+96]
movups xmm7, [eax+ecx+112]
movntps [edx+ecx+64], xmm4
movntps [edx+ecx+80], xmm5
movntps [edx+ecx+96], xmm6
movntps [edx+ecx+112], xmm7
add ecx, 128
js @@Loop
sfence
end; {HugeUnalignedSSEMove}
{-------------------------------------------------------------------------}
{Dest MUST be 16-Byes Aligned, Count MUST be multiple of 16 }
procedure LargeSSEMove(const Source; var Dest; Count: Integer);
asm
push ebx
mov ebx, ecx
and ecx, -128 {No of Bytes to Block Move (Multiple of 128)}
add eax, ecx {End of Source Blocks}
add edx, ecx {End of Dest Blocks}
neg ecx
cmp ecx, CacheLimit {Count > Limit - Use Prefetch}
jl @@Huge
test eax, 15 {Check if Both Source/Dest are Aligned}
jnz @@LargeUnaligned
call LargeAlignedSSEMove {Both Source and Dest 16-Byte Aligned}
jmp @@Remainder
@@LargeUnaligned: {Source Not 16-Byte Aligned}
call LargeUnalignedSSEMove
jmp @@Remainder
@@Huge:
test eax, 15 {Check if Both Source/Dest Aligned}
jnz @@HugeUnaligned
call HugeAlignedSSEMove {Both Source and Dest 16-Byte Aligned}
jmp @@Remainder
@@HugeUnaligned: {Source Not 16-Byte Aligned}
call HugeUnalignedSSEMove
@@Remainder:
and ebx, $7F {Remainder (0..112 - Multiple of 16)}
jz @@Done
add eax, ebx
add edx, ebx
neg ebx
@@RemainderLoop:
movups xmm0, [eax+ebx]
movaps [edx+ebx], xmm0
add ebx, 16
jnz @@RemainderLoop
@@Done:
pop ebx
end; {LargeSSEMove}
{-------------------------------------------------------------------------}
{Move ECX Bytes from EAX to EDX, where EAX > EDX and ECX > 36 (TINYSIZE)}
procedure Forwards_SSE_10;
const
SMALLSIZE = 64;
LARGESIZE = 2048;
asm
cmp ecx, SMALLSIZE
jle Forwards_IA32_10
push ebx
cmp ecx, LARGESIZE
jge @@FwdLargeMove
movups xmm0, [eax] {First 16 Bytes}
mov ebx, edx
add eax, ecx {Align Writes}
add ecx, edx
and edx, -16
add edx, 48
sub ecx, edx
add edx, ecx
neg ecx
nop {Align Loop}
@@FwdLoopSSE:
movups xmm1, [eax+ecx-32]
movups xmm2, [eax+ecx-16]
movaps [edx+ecx-32], xmm1
movaps [edx+ecx-16], xmm2
add ecx, 32
jle @@FwdLoopSSE
movups [ebx], xmm0 {First 16 Bytes}
neg ecx
add ecx, 32
pop ebx
jmp SmallForwardMove_10
@@FwdLargeMove:
mov ebx, ecx
test edx, 15
jz @@FwdLargeAligned
lea ecx, [edx+15] {16 byte Align Destination}
and ecx, -16
sub ecx, edx
add eax, ecx
add edx, ecx
sub ebx, ecx
call SmallForwardMove_10
mov ecx, ebx
@@FwdLargeAligned:
and ecx, -16
sub ebx, ecx {EBX = Remainder}
push edx
push eax
push ecx
call LargeSSEMove
pop ecx
pop eax
pop edx
add ecx, ebx
add eax, ecx
add edx, ecx
mov ecx, ebx
pop ebx
jmp SmallForwardMove_10
end; {Forwards_SSE}
{-------------------------------------------------------------------------}
{Move ECX Bytes from EAX to EDX, where EAX < EDX and ECX > 36 (TINYSIZE)}
procedure Backwards_SSE_10;
const
SMALLSIZE = 64;
asm
cmp ecx, SMALLSIZE
jle Backwards_IA32_10
push ebx
movups xmm0, [eax+ecx-16] {Last 16 Bytes}
lea ebx, [edx+ecx] {Align Writes}
and ebx, 15
sub ecx, ebx
add ebx, ecx
sub ecx, 32
@@BwdLoop:
movups xmm1, [eax+ecx]
movups xmm2, [eax+ecx+16]
movaps [edx+ecx], xmm1
movaps [edx+ecx+16], xmm2
sub ecx, 32
jge @@BwdLoop
movups [edx+ebx-16], xmm0 {Last 16 Bytes}
add ecx, 32
pop ebx
jmp SmallBackwardMove_10
end; {Backwards_SSE}
{-------------------------------------------------------------------------}
procedure LargeAlignedSSE2Move; {Also used in SSE3 Move}
asm
@@Loop:
movdqa xmm0, [eax+ecx]
movdqa xmm1, [eax+ecx+16]
movdqa xmm2, [eax+ecx+32]
movdqa xmm3, [eax+ecx+48]
movdqa [edx+ecx], xmm0
movdqa [edx+ecx+16], xmm1
movdqa [edx+ecx+32], xmm2
movdqa [edx+ecx+48], xmm3
movdqa xmm4, [eax+ecx+64]
movdqa xmm5, [eax+ecx+80]
movdqa xmm6, [eax+ecx+96]
movdqa xmm7, [eax+ecx+112]
movdqa [edx+ecx+64], xmm4
movdqa [edx+ecx+80], xmm5
movdqa [edx+ecx+96], xmm6
movdqa [edx+ecx+112], xmm7
add ecx, 128
js @@Loop
end; {LargeAlignedSSE2Move}
{-------------------------------------------------------------------------}
procedure LargeUnalignedSSE2Move;
asm
@@Loop:
movdqu xmm0, [eax+ecx]
movdqu xmm1, [eax+ecx+16]
movdqu xmm2, [eax+ecx+32]
movdqu xmm3, [eax+ecx+48]
movdqa [edx+ecx], xmm0
movdqa [edx+ecx+16], xmm1
movdqa [edx+ecx+32], xmm2
movdqa [edx+ecx+48], xmm3
movdqu xmm4, [eax+ecx+64]
movdqu xmm5, [eax+ecx+80]
movdqu xmm6, [eax+ecx+96]
movdqu xmm7, [eax+ecx+112]
movdqa [edx+ecx+64], xmm4
movdqa [edx+ecx+80], xmm5
movdqa [edx+ecx+96], xmm6
movdqa [edx+ecx+112], xmm7
add ecx, 128
js @@Loop
end; {LargeUnalignedSSE2Move}
{-------------------------------------------------------------------------}
procedure HugeAlignedSSE2Move; {Also used in SSE3 Move}
const
Prefetch = 512;
asm
@@Loop:
prefetchnta [eax+ecx+Prefetch]
prefetchnta [eax+ecx+Prefetch+64]
movdqa xmm0, [eax+ecx]
movdqa xmm1, [eax+ecx+16]
movdqa xmm2, [eax+ecx+32]
movdqa xmm3, [eax+ecx+48]
movntdq [edx+ecx], xmm0
movntdq [edx+ecx+16], xmm1
movntdq [edx+ecx+32], xmm2
movntdq [edx+ecx+48], xmm3
movdqa xmm4, [eax+ecx+64]
movdqa xmm5, [eax+ecx+80]
movdqa xmm6, [eax+ecx+96]
movdqa xmm7, [eax+ecx+112]
movntdq [edx+ecx+64], xmm4
movntdq [edx+ecx+80], xmm5
movntdq [edx+ecx+96], xmm6
movntdq [edx+ecx+112], xmm7
add ecx, 128
js @@Loop
sfence
end; {HugeAlignedSSE2Move}
{-------------------------------------------------------------------------}
procedure HugeUnalignedSSE2Move;
const
Prefetch = 512;
asm
@@Loop:
prefetchnta [eax+ecx+Prefetch]
prefetchnta [eax+ecx+Prefetch+64]
movdqu xmm0, [eax+ecx]
movdqu xmm1, [eax+ecx+16]
movdqu xmm2, [eax+ecx+32]
movdqu xmm3, [eax+ecx+48]
movntdq [edx+ecx], xmm0
movntdq [edx+ecx+16], xmm1
movntdq [edx+ecx+32], xmm2
movntdq [edx+ecx+48], xmm3
movdqu xmm4, [eax+ecx+64]
movdqu xmm5, [eax+ecx+80]
movdqu xmm6, [eax+ecx+96]
movdqu xmm7, [eax+ecx+112]
movntdq [edx+ecx+64], xmm4
movntdq [edx+ecx+80], xmm5
movntdq [edx+ecx+96], xmm6
movntdq [edx+ecx+112], xmm7
add ecx, 128
js @@Loop
sfence
end; {HugeUnalignedSSE2Move}
{-------------------------------------------------------------------------}
{Dest MUST be 16-Byes Aligned, Count MUST be multiple of 16 }
procedure LargeSSE2Move(const Source; var Dest; Count: Integer);
asm
push ebx
mov ebx, ecx
and ecx, -128 {No of Bytes to Block Move (Multiple of 128)}
add eax, ecx {End of Source Blocks}
add edx, ecx {End of Dest Blocks}
neg ecx
cmp ecx, CacheLimit {Count > Limit - Use Prefetch}
jl @@Huge
test eax, 15 {Check if Both Source/Dest are Aligned}
jnz @@LargeUnaligned
call LargeAlignedSSE2Move {Both Source and Dest 16-Byte Aligned}
jmp @@Remainder
@@LargeUnaligned: {Source Not 16-Byte Aligned}
call LargeUnalignedSSE2Move
jmp @@Remainder
@@Huge:
test eax, 15 {Check if Both Source/Dest Aligned}
jnz @@HugeUnaligned
call HugeAlignedSSE2Move {Both Source and Dest 16-Byte Aligned}
jmp @@Remainder
@@HugeUnaligned: {Source Not 16-Byte Aligned}
call HugeUnalignedSSE2Move
@@Remainder:
and ebx, $7F {Remainder (0..112 - Multiple of 16)}
jz @@Done
add eax, ebx
add edx, ebx
neg ebx
@@RemainderLoop:
movdqu xmm0, [eax+ebx]
movdqa [edx+ebx], xmm0
add ebx, 16