-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFastcodeCompareMemUnit.pas
634 lines (612 loc) · 14 KB
/
FastcodeCompareMemUnit.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
unit FastcodeCompareMemUnit;
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Fastcode
*
* The Initial Developer of the Original Code is Fastcode
*
* Portions created by the Initial Developer are Copyright (C) 2002-2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Charalabos Michael <[email protected]>
* John O'Harrow <[email protected]>
* Pierre le Riche
* Dennis Kjaer Christensen
* Aleksandr Sharahov
*
* BV Version: 2.01
* ***** END LICENSE BLOCK ***** *)
interface
{$I Fastcode.inc}
type
FastcodeCompareMemFunction = function(const p1, p2: pointer; const length: integer): boolean;
{Functions shared between Targets}
function CompareMem_JOH_IA32_1(const P1, P2: pointer; const Length: integer): boolean;
function CompareMem_PLR_IA32_1(const P1, P2: pointer; const Length: integer): boolean;
function CompareMem_PLR_IA32_2(const P1, P2: pointer; const Length: integer): boolean;
function CompareMem_Sha_Pas_4(const p1, p2: pointer; const length: integer): boolean;
{Functions not shared between Targets}
function CompareMem_DKC_SSE3_2(const P1, P2: Pointer; const Length: Integer): Boolean;
function CompareMem_DKC_SSE2_1(const P1, P2: Pointer; const Length: Integer): Boolean;
function CompareMem_DKC_SSE2_6(const P1, P2: Pointer; const Length: Integer): Boolean;
{Functions}
const
FastcodeCompareMemP4R: FastcodeCompareMemFunction = CompareMem_DKC_SSE3_2;
FastcodeCompareMemP4N: FastcodeCompareMemFunction = CompareMem_DKC_SSE2_1;
FastcodeCompareMemPMY: FastcodeCompareMemFunction = CompareMem_JOH_IA32_1;
FastcodeCompareMemPMD: FastcodeCompareMemFunction = CompareMem_JOH_IA32_1;
FastcodeCompareMemAMD64: FastcodeCompareMemFunction = CompareMem_PLR_IA32_1;
FastcodeCompareMemAMD64_SSE3: FastcodeCompareMemFunction = CompareMem_PLR_IA32_1;
FastCodeCompareMemIA32SizePenalty: FastCodeCompareMemFunction = CompareMem_PLR_IA32_2;
FastcodeCompareMemIA32: FastcodeCompareMemFunction = CompareMem_PLR_IA32_2;
FastcodeCompareMemMMX: FastcodeCompareMemFunction = CompareMem_PLR_IA32_2;
FastCodeCompareMemSSESizePenalty: FastCodeCompareMemFunction = CompareMem_PLR_IA32_2;
FastcodeCompareMemSSE: FastcodeCompareMemFunction = CompareMem_PLR_IA32_2;
FastcodeCompareMemSSE2: FastcodeCompareMemFunction = CompareMem_DKC_SSE2_6;
FastCodeCompareMemPascalSizePenalty: FastCodeCompareMemFunction = CompareMem_Sha_Pas_4;
FastCodeCompareMemPascal: FastCodeCompareMemFunction = CompareMem_Sha_Pas_4;
procedure CompareMemStub;
implementation
uses
SysUtils;
function CompareMem_DKC_SSE3_2(const P1, P2: Pointer; const Length: Integer): Boolean;
asm
push esi
push edi
test ecx,ecx
jle @ResultTrueExit1
cmp ecx,4
jnl @MediumCompares
mov esi,edx
lea edi,[ecx+eax]
@Loop1Start :
movzx edx,[eax]
cmp dl,[esi]
jnz @ResultFalseExit1
add eax,1
cmp edi,eax
jbe @Loop1End
add esi,1
movzx edx,[eax]
cmp dl,[esi]
jnz @ResultFalseExit1
add eax,1
add esi,1
cmp edi,eax
jnbe @Loop1Start
@Loop1End :
xor eax,eax
add eax,1
pop edi
pop esi
ret
@MediumCompares :
cmp ecx,256
jnl @BigCompares
sub ecx,4
xor esi,esi
@MediumCompareLoop :
mov edi,[eax+esi]
cmp edi,[edx+esi]
jnz @ResultFalseExit1
add esi,4
cmp esi,ecx
jb @MediumCompareLoop
mov edi,[eax+ecx]
cmp edi,[edx+ecx]
jnz @ResultFalseExit1
@ResultTrueExit1 :
xor eax,eax
add eax,1
pop edi
pop esi
ret
@BigCompares :
push ebx
mov edi,eax
mov esi,edx
lea eax,[ecx+eax]
mov edx,edi
and edx,15
mov eax,16
sub eax,edx
mov ebx,edi
mov edx,[edi]
cmp edx,[esi]
jnz @ResultFalseExit
mov edx,[edi+4]
cmp edx,[esi+4]
jnz @ResultFalseExit
mov edx,[edi+8]
cmp edx,[esi+8]
jnz @ResultFalseExit
mov edx,[edi+12]
cmp edx,[esi+12]
jnz @ResultFalseExit
add edi,eax
add esi,eax
add ecx,ebx
sub ecx,16
@Loop3Start :
//lddqu xmm0,[esi]
db $F2 db $0F db $F0 db $06
pcmpeqb xmm0,[edi]
pmovmskb ebx,xmm0
cmp ebx,$0000FFFF
jnz @ResultFalseExit
add edi,16
add esi,16
cmp edi,ecx
jle @Loop3Start
add ecx,16
cmp edi,ecx
jnl @ResultTrueExit
@Loop4Start :
movzx edx,[edi]
cmp dl,[esi]
jnz @ResultFalseExit
add edi,1
add esi,1
cmp ecx,edi
jnbe @Loop4Start
@ResultTrueExit :
xor eax,eax
add eax,1
pop ebx
pop edi
pop esi
ret
@ResultFalseExit :
xor eax,eax
pop ebx
pop edi
pop esi
ret
@ResultFalseExit1 :
xor eax,eax
pop edi
pop esi
ret
end;
function CompareMem_DKC_SSE2_1(const P1, P2: Pointer; const Length: Integer): Boolean;
asm
push ebx
push esi
push edi
test ecx,ecx
jle @ResultTrueExit
mov edi,eax
mov esi,edx
lea eax,[ecx+edi]
cmp ecx,32
jnl @IfEnd2
@Loop1Start :
movzx edx,[edi]
cmp dl,[esi]
jnz @ResultFalseExit
add edi,1
add esi,1
cmp eax,edi
jnbe @Loop1Start
xor eax,eax
add eax,1
pop edi
pop esi
pop ebx
ret
@IfEnd2 :
mov edx,edi
and edx,15
mov eax,16
sub eax,edx
mov ebx,edi
mov edx,[edi]
cmp edx,[esi]
jnz @ResultFalseExit
mov edx,[edi+4]
cmp edx,[esi+4]
jnz @ResultFalseExit
mov edx,[edi+8]
cmp edx,[esi+8]
jnz @ResultFalseExit
mov edx,[edi+12]
cmp edx,[esi+12]
jnz @ResultFalseExit
add edi,eax
add esi,eax
add ecx,ebx
sub ecx,16
@Loop3Start :
movdqu xmm0,[esi]
pcmpeqb xmm0,[edi]
pmovmskb ebx,xmm0
cmp bx,$FFFF
jnz @ResultFalseExit
add edi,16
add esi,16
cmp edi,ecx
jle @Loop3Start
add ecx,16
cmp edi,ecx
jnl @ResultTrueExit
@Loop4Start :
movzx edx,[edi]
cmp dl,[esi]
jnz @ResultFalseExit
add edi,1
add esi,1
cmp ecx,edi
jnbe @Loop4Start
@ResultTrueExit :
xor eax,eax
add eax,1
pop edi
pop esi
pop ebx
ret
@ResultFalseExit :
xor eax,eax
pop edi
pop esi
pop ebx
ret
end;
function CompareMem_DKC_SSE2_6(const P1, P2: Pointer; const Length: Integer): Boolean;
asm
push esi
push edi
test ecx,ecx
jle @ResultTrueExit1
cmp ecx,32
jnl @IfEnd2
mov esi,edx
lea edi,[ecx+eax]
@Loop1Start :
movzx edx,[eax]
cmp dl,[esi]
jnz @ResultFalseExit1
add eax,1
cmp edi,eax
jbe @Loop1End
add esi,1
movzx edx,[eax]
cmp dl,[esi]
jnz @ResultFalseExit1
add eax,1
add esi,1
cmp edi,eax
jnbe @Loop1Start
@Loop1End :
xor eax,eax
add eax,1
pop edi
pop esi
ret
@IfEnd2 :
push ebx
mov edi,eax
mov esi,edx
lea eax,[ecx+eax]
mov edx,edi
and edx,15
mov eax,16
sub eax,edx
mov ebx,edi
mov edx,[edi]
cmp edx,[esi]
jnz @ResultFalseExit
mov edx,[edi+4]
cmp edx,[esi+4]
jnz @ResultFalseExit
mov edx,[edi+8]
cmp edx,[esi+8]
jnz @ResultFalseExit
mov edx,[edi+12]
cmp edx,[esi+12]
jnz @ResultFalseExit
add edi,eax
add esi,eax
add ecx,ebx
sub ecx,16
@Loop3Start :
movdqu xmm0,[esi]
pcmpeqb xmm0,[edi]
pmovmskb ebx,xmm0
cmp ebx,$0000FFFF
jnz @ResultFalseExit
add edi,16
add esi,16
cmp edi,ecx
jle @Loop3Start
add ecx,16
cmp edi,ecx
jnl @ResultTrueExit
@Loop4Start :
movzx edx,[edi]
cmp dl,[esi]
jnz @ResultFalseExit
add edi,1
add esi,1
cmp ecx,edi
jnbe @Loop4Start
@ResultTrueExit :
xor eax,eax
add eax,1
pop ebx
pop edi
pop esi
ret
@ResultFalseExit :
xor eax,eax
pop ebx
pop edi
pop esi
ret
@ResultTrueExit1 :
xor eax,eax
add eax,1
pop edi
pop esi
ret
@ResultFalseExit1 :
xor eax,eax
pop edi
pop esi
ret
end;
// ** //
function CompareMem_JOH_IA32_1(const P1, P2: pointer; const Length: integer): boolean;
asm
push ebx
sub ecx, 8
jl @@Small
mov ebx, [eax] {Compare First 4 Bytes}
cmp ebx, [edx]
jne @@False
lea ebx, [eax+ecx] {Compare Last 8 Bytes}
add edx, ecx
mov eax, [ebx]
cmp eax, [edx]
jne @@False
mov eax, [ebx+4]
cmp eax, [edx+4]
jne @@False
sub ecx, 4
jle @@True {All Bytes already Compared}
neg ecx {-(Length-12)}
add ecx, ebx {DWORD Align Reads}
and ecx, -4
sub ecx, ebx
@@LargeLoop: {Compare 8 Bytes per Loop}
mov eax, [ebx+ecx]
cmp eax, [edx+ecx]
jne @@False
mov eax, [ebx+ecx+4]
cmp eax, [edx+ecx+4]
jne @@False
add ecx, 8
jl @@LargeLoop
@@True:
mov eax, 1
pop ebx
ret
@@Small:
add ecx, 8
jle @@True {Length <= 0}
@@SmallLoop:
mov bl, [eax]
cmp bl, [edx]
jne @@False
inc eax
inc edx
dec ecx
jnz @@SmallLoop
jmp @@True
@@False:
xor eax, eax
pop ebx
end;
// ** //
function CompareMem_PLR_IA32_1(const P1, P2: pointer; const Length: integer): boolean;
asm
{Subtract 8 from the count}
sub ecx, 8
jl @SmallCompare
{Save registers}
push ebx
push esi
{Compare the first 4 and last 8 bytes}
mov ebx, [eax]
xor ebx, [edx]
{Point to the last 8 bytes}
add eax, ecx
add edx, ecx
{Compare the last 8}
mov esi, [eax]
xor esi, [edx]
or ebx, esi
mov esi, [eax + 4]
xor esi, [edx + 4]
or ebx, esi
jnz @Mismatch
{Less than 12 to compare?}
sub ecx, 4
jle @Match
{DWord align reads from P1 (P2 reads are not aligned)}
neg ecx
add ecx, eax
and ecx, -4
sub ecx, eax
{Read chunks of 8 bytes at a time}
@CompareLoop:
mov ebx, [eax + ecx]
mov esi, [eax + ecx + 4]
xor ebx, [edx + ecx]
xor esi, [edx + ecx + 4]
or ebx, esi
jnz @Mismatch
add ecx, 8
jl @CompareLoop
@Match:
pop esi
pop ebx
@MatchSmall:
mov al, True
ret
@SmallCompare:
add ecx, 8
jle @MatchSmall
@SmallLoop:
mov ch, [eax]
xor ch, [edx]
jnz @MismatchSmall
add eax, 1
add edx, 1
sub cl, 1
jnz @SmallLoop
jmp @MatchSmall
@Mismatch:
pop esi
pop ebx
@MismatchSmall:
xor eax, eax
end;
function CompareMem_PLR_IA32_2(const P1, P2: pointer; const Length: integer): boolean;
asm
{Less than a qword to compare?}
sub ecx, 8
jl @VerySmallCompare
{save ebx}
push ebx
{Compare first dword}
mov ebx, [eax]
cmp ebx, [edx]
je @FirstFourMatches
@InitialMismatch:
xor eax, eax
pop ebx
ret
@FirstFourMatches:
{Point eax and edx to the last 8 bytes}
add eax, ecx
add edx, ecx
{Compare the second last dword}
mov ebx, [eax]
cmp ebx, [edx]
jne @InitialMismatch
{Compare the last dword}
mov ebx, [eax + 4]
cmp ebx, [edx + 4]
jne @InitialMismatch
{12 or less bytes to compare?}
sub ecx, 4
jle @InitialMatch
{Save esi}
push esi
{DWord align reads from P1 (P2 reads are not aligned)}
neg ecx
add ecx, eax
and ecx, -4
sub ecx, eax
{Compare chunks of 8 bytes at a time}
@CompareLoop:
mov ebx, [eax + ecx]
mov esi, [eax + ecx + 4]
xor ebx, [edx + ecx]
xor esi, [edx + ecx + 4]
or ebx, esi
jnz @LargeMismatch
add ecx, 8
jl @CompareLoop
pop esi
@InitialMatch:
pop ebx
@MatchSmall:
mov al, True
ret
@VerySmallCompare:
add ecx, 8
jle @MatchSmall
@SmallLoop:
mov ch, [eax]
xor ch, [edx]
jnz @MismatchSmall
add eax, 1
add edx, 1
sub cl, 1
jnz @SmallLoop
jmp @MatchSmall
@LargeMismatch:
pop esi
pop ebx
@MismatchSmall:
xor eax, eax
end;
function CompareMem_Sha_Pas_4(const p1, p2: pointer; const length: integer): boolean;
var
q1, q2: pIntegerArray;
c: cardinal;
label
Ret0;
begin;
c:=length;
c:=c+cardinal(p1)-8;
q1:=p1;
q2:=p2;
if c>=cardinal(q1) then begin;
if q1[0]<>q2[0] then goto Ret0;
inc(cardinal(q1),4);
inc(cardinal(q2),4);
dec(cardinal(q2),cardinal(q1));
cardinal(q1):=cardinal(q1) and -4;
inc(cardinal(q2),cardinal(q1));
//if c>=cardinal(q1) then repeat;
//replaced: compiler creates a copy of cardinal(q1) for statement above
if pchar(c)>=q1 then repeat;
if q1[0]<>q2[0] then goto Ret0;
if q1[1]<>q2[1] then goto Ret0;
inc(cardinal(q1),8);
inc(cardinal(q2),8);
if c<cardinal(q1) then break;
if q1[0]<>q2[0] then goto Ret0;
if q1[1]<>q2[1] then goto Ret0;
inc(cardinal(q1),8);
inc(cardinal(q2),8);
until c<cardinal(q1);
end;
c:=c-cardinal(q1)+8;
if integer(c)>=4 then begin;
if q1[0]<>q2[0] then goto Ret0;
inc(cardinal(q1),4);
inc(cardinal(q2),4);
c:=c-4;
end;
if integer(c)>=2 then begin;
if pword(q1)^<>pword(q2)^ then goto Ret0;
inc(cardinal(q1),2);
inc(cardinal(q2),2);
c:=c-2;
end;
if integer(c)>=1 then if pchar(q1)^<>pchar(q2)^ then goto Ret0;
Result:=true;
exit;
Ret0:
Result:=false;
end;
procedure CompareMemStub;
asm
call SysUtils.CompareMem;
end;
end.