-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFastcodeCompareTextUnit.pas
More file actions
530 lines (481 loc) · 13.7 KB
/
FastcodeCompareTextUnit.pas
File metadata and controls
530 lines (481 loc) · 13.7 KB
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
unit FastcodeCompareTextUnit;
(* ***** 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-2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Charalabos Michael <chmichael@creationpower.com>
* John O'Harrow <john@elmcrest.demon.co.uk>
* Aleksandr Sharahov
*
* BV Version: 1.53
* ***** END LICENSE BLOCK ***** *)
interface
{$I Fastcode.inc}
type
FastcodeCompareTextFunction = function(const S1, S2: string): Integer;
{Functions shared between Targets}
function CompareText_Sha_IA32_3(const S1, S2: string): Integer;
function CompareText_JOH_IA32_5(const S1, S2: string): Integer;
function CompareText_JOH_IA32_6(const S1, S2: string): Integer;
function CompareText_Sha_IA32_4(const S1, S2: string): integer;
function CompareText_Sha_Pas_5(const S1, S2: string): integer;
{Functions not shared between Targets}
{Functions}
const
FastcodeCompareTextP4R: FastcodeCompareTextFunction = CompareText_JOH_IA32_6;
FastcodeCompareTextP4N: FastcodeCompareTextFunction = CompareText_JOH_IA32_6;
FastcodeCompareTextPMY: FastcodeCompareTextFunction = CompareText_Sha_IA32_3;
FastcodeCompareTextPMD: FastcodeCompareTextFunction = CompareText_Sha_IA32_3;
FastcodeCompareTextAMD64: FastcodeCompareTextFunction = CompareText_JOH_IA32_5;
FastcodeCompareTextAMD64_SSE3: FastcodeCompareTextFunction = CompareText_JOH_IA32_5;
FastCodeCompareTextIA32SizePenalty: FastCodeCompareTextFunction = CompareText_Sha_IA32_4;
FastcodeCompareTextIA32: FastcodeCompareTextFunction = CompareText_JOH_IA32_5;
FastcodeCompareTextMMX: FastcodeCompareTextFunction = CompareText_JOH_IA32_5;
FastCodeCompareTextSSESizePenalty: FastCodeCompareTextFunction = CompareText_Sha_IA32_4;
FastcodeCompareTextSSE: FastcodeCompareTextFunction = CompareText_JOH_IA32_5;
FastcodeCompareTextSSE2: FastcodeCompareTextFunction = CompareText_JOH_IA32_5;
FastCodeCompareTextPascalSizePenalty: FastCodeCompareTextFunction = CompareText_Sha_Pas_5;
FastCodeCompareTextPascal: FastCodeCompareTextFunction = CompareText_Sha_Pas_5;
procedure CompareTextStub;
implementation
uses
SysUtils;
function CompareText_JOH_IA32_6(const S1, S2: string): Integer;
asm
cmp eax, edx
je @@Same {S1 = S2}
test eax, edx
jnz @@Compare
test eax, eax
jz @FirstNil {S1 = NIL}
test edx, edx
jnz @@Compare {S1 <> NIL and S2 <> NIL}
mov eax, [eax-4] {S2 = NIL, Result = Length(S1)}
ret
@@Same:
xor eax, eax
ret
@FirstNil:
sub eax, [edx-4] {S1 = NIL, Result = -Length(S2)}
ret
@@Compare:
push ebx
push ebp
push edi
push esi
mov ebx, [eax-4] {Length(S1)}
sub ebx, [edx-4] {Default Result if All Compared Characters Match}
push ebx {Save Default Result}
sbb ebp, ebp
and ebp, ebx
sub ebp, [eax-4] {-Min(Length(S1),Length(S2))}
sub eax, ebp {End of S1}
sub edx, ebp {End of S2}
@@MainLoop: {Compare 4 Characters per Loop}
mov ebx, [eax+ebp]
mov ecx, [edx+ebp]
cmp ebx, ecx
je @@Next
mov esi, ebx {Convert 4 Chars in EBX into Uppercase}
or ebx, $80808080
mov edi, ebx
sub ebx, $7B7B7B7B
xor edi, esi
or ebx, $80808080
sub ebx, $66666666
and ebx, edi
shr ebx, 2
xor ebx, esi
mov esi, ecx {Convert 4 Chars in ECX into Uppercase}
or ecx, $80808080
mov edi, ecx
sub ecx, $7B7B7B7B
xor edi, esi
or ecx, $80808080
sub ecx, $66666666
and ecx, edi
shr ecx, 2
xor ecx, esi
cmp ebx, ecx
jne @@CheckDiff
@@Next:
add ebp, 4
jl @@MainLoop {Loop until all required Characters Compared}
pop eax {Default Result}
jmp @@Done
@@CheckDiff:
pop eax {Default Result}
@@DiffLoop:
cmp bl, cl
jne @@SetResult
add ebp, 1
jz @@Done {Difference after Compare Length}
shr ecx, 8
shr ebx, 8
jmp @@DiffLoop
@@SetResult:
movzx eax, bl {Set Result from Character Difference}
and ecx, $ff
sub eax, ecx
@@Done:
pop esi
pop edi
pop ebp
pop ebx
end;
function CompareText_JOH_IA32_5(const S1, S2: string): Integer;
asm
cmp eax, edx
je @@Same {S1 = S2}
test eax, edx
jnz @@Compare
test eax, eax
jz @FirstNil {S1 = NIL}
test edx, edx
jnz @@Compare {S1 <> NIL and S2 <> NIL}
mov eax, [eax-4] {S2 = NIL, Result = Length(S1)}
ret
@@Same:
xor eax, eax
ret
@FirstNil:
sub eax, [edx-4] {S1 = NIL, Result = -Length(S2)}
ret
@@Compare:
push ebx
push ebp
push edi
push esi
mov ebx, [eax-4] {Length(S1)}
sub ebx, [edx-4] {Default Result if All Compared Characters Match}
push ebx {Save Default Result}
sbb ebp, ebp
and ebp, ebx
add ebp, [edx-4] {Compare Length = Min(Length(S1),Length(S2))}
add eax, ebp {End of S1}
add edx, ebp {End of S2}
neg ebp {Negate Compare Length}
@@MainLoop: {Compare 4 Characters per Loop}
mov ebx, [eax+ebp]
mov ecx, [edx+ebp]
cmp ebx, ecx
je @@Next
mov esi, ebx {Convert 4 Chars in EBX into Uppercase}
or ebx, $80808080
mov edi, ebx
sub ebx, $7B7B7B7B
xor edi, esi
or ebx, $80808080
sub ebx, $66666666
and ebx, edi
shr ebx, 2
xor ebx, esi
mov esi, ecx {Convert 4 Chars in ECX into Uppercase}
or ecx, $80808080
mov edi, ecx
sub ecx, $7B7B7B7B
xor edi, esi
or ecx, $80808080
sub ecx, $66666666
and ecx, edi
shr ecx, 2
xor ecx, esi
cmp ebx, ecx
jne @@CheckDiff
@@Next:
add ebp, 4
jl @@MainLoop {Loop until all required Characters Compared}
pop eax {Default Result}
jmp @@Done
@@CheckDiff:
pop eax {Default Result}
@@DiffLoop:
cmp cl, bl
jne @@SetResult
add ebp, 1
jz @@Done {Difference after Compare Length}
shr ecx, 8
shr ebx, 8
jmp @@DiffLoop
@@SetResult:
movzx eax, bl {Set Result from Character Difference}
and ecx, $ff
sub eax, ecx
@@Done:
pop esi
pop edi
pop ebp
pop ebx
end;
function CompareText_Sha_IA32_3(const S1, S2: string): Integer;
asm
test eax, eax
jz @nil1
test edx, edx
jnz @ptrok
@nil2: mov eax, [eax-4]
ret
@nil1: test edx, edx
jz @nil0
sub eax, [edx-4]
@nil0: ret
@ptrok: push edi
push ebx
xor edi, edi
mov ebx, [eax-4]
mov ecx, ebx
sub ebx, [edx-4]
adc edi, -1
push ebx
and ebx, edi
mov edi, eax
sub ebx, ecx
jge @len
@lenok: sub edi, ebx
sub edx, ebx
@loop: mov eax, [ebx+edi]
mov ecx, [ebx+edx]
cmp eax, ecx
jne @byte0
@same: add ebx, 4
jl @loop
@len: pop eax
pop ebx
pop edi
ret
@loop2: mov eax, [ebx+edi]
mov ecx, [ebx+edx]
cmp eax, ecx
je @same
@byte0: cmp al, cl
je @byte1
and eax, $FF
and ecx, $FF
sub eax, 'a'
sub ecx, 'a'
cmp al, 'z'-'a'
ja @up0a
sub eax, 'a'-'A'
@up0a: cmp cl, 'z'-'a'
ja @up0c
sub ecx, 'a'-'A'
@up0c: sub eax, ecx
jnz @done
mov eax, [ebx+edi]
mov ecx, [ebx+edx]
@byte1: cmp ah, ch
je @byte2
and eax, $FF00
and ecx, $FF00
sub eax, 'a'*256
sub ecx, 'a'*256
cmp ah, 'z'-'a'
ja @up1a
sub eax, ('a'-'A')*256
@up1a: cmp ch, 'z'-'a'
ja @up1c
sub ecx, ('a'-'A')*256
@up1c: sub eax, ecx
jnz @done
mov eax, [ebx+edi]
mov ecx, [ebx+edx]
@byte2: add ebx, 2
jnl @len2
shr eax, 16
shr ecx, 16
cmp al, cl
je @byte3
and eax, $FF
and ecx, $FF
sub eax, 'a'
sub ecx, 'a'
cmp al, 'z'-'a'
ja @up2a
sub eax, 'a'-'A'
@up2a: cmp cl, 'z'-'a'
ja @up2c
sub ecx, 'a'-'A'
@up2c: sub eax, ecx
jnz @done
movzx eax, word ptr [ebx+edi]
movzx ecx, word ptr [ebx+edx]
@byte3: cmp ah, ch
je @byte4
and eax, $FF00
and ecx, $FF00
sub eax, 'a'*256
sub ecx, 'a'*256
cmp ah, 'z'-'a'
ja @up3a
sub eax, ('a'-'A')*256
@up3a: cmp ch, 'z'-'a'
ja @up3c
sub ecx, ('a'-'A')*256
@up3c: sub eax, ecx
jnz @done
@byte4: add ebx, 2
jl @loop2
@len2: pop eax
pop ebx
pop edi
ret
@done: pop ecx
pop ebx
pop edi
end;
function CompareText_Sha_IA32_4(const S1, S2: string): integer;
asm
test eax, eax
jz @nil1
test edx, edx
jnz @ptrok
@nil2: mov eax, [eax-4]
ret
@nil1: test edx, edx
jz @nil0
sub eax, [edx-4]
@nil0: ret
@ptrok: push edi
push ebx
xor edi, edi
mov ebx, [eax-4]
mov ecx, ebx
sub ebx, [edx-4]
adc edi, -1
push ebx
and ebx, edi
mov edi, eax
sub ebx, ecx //ebx := -min(Length(s1),Length(s2))
jge @len
@lenok: sub edi, ebx
sub edx, ebx
@loop: mov eax, [ebx+edi]
mov ecx, [ebx+edx]
xor eax, ecx
jne @differ
@same: add ebx, 4
jl @loop
@len: pop eax
pop ebx
pop edi
ret
@loop2: mov eax, [ebx+edi]
mov ecx, [ebx+edx]
xor eax, ecx
je @same
@differ: test eax, $DFDFDFDF //$00 or $20
jnz @find
add eax, eax //$00 or $40
add eax, eax //$00 or $80
test eax, ecx
jnz @find
and ecx, $5F5F5F5F //$41..$5A
add ecx, $3F3F3F3F //$80..$99
and ecx, $7F7F7F7F //$00..$19
add ecx, $66666666 //$66..$7F
test ecx, eax
jnz @find
add ebx, 4
jl @loop2
@len2: pop eax
pop ebx
pop edi
ret
@loop3: add ebx, 1
jge @len2
@find: movzx eax, [ebx+edi]
movzx ecx, [ebx+edx]
sub eax, 'a'
sub ecx, 'a'
cmp al, 'z'-'a'
ja @upa
sub eax, 'a'-'A'
@upa: cmp cl, 'z'-'a'
ja @upc
sub ecx, 'a'-'A'
@upc: sub eax, ecx
jz @loop3
@found: pop ecx
pop ebx
pop edi
end;
function CompareText_Sha_Pas_5(const S1, S2: string): integer;
var
c1, c2, d, q, save: integer;
p: pIntegerArray;
label
past, find;
begin;
d:=integer(@pchar(pointer(s1))[-4]);
c1:=0;
c2:=0;
p:=@pchar(pointer(s2))[-4];
if d<>-4 then c1:=pinteger(d)^; //c1 = length of s1
if p<>pointer(-4) then c2:=pinteger(p)^; //c2 = length of s2
d:=(d-integer(p)) shr 2; //d = distance(s1-s2) div 4
q:=c1;
c1:=c1-c2;
if c1>0 then q:=c2; //q = min length
save:=c1; //save result for equal data
if q<=0 then begin;
Result:=c1;
exit;
end;
q:=q+integer(p);
repeat;
c1:=p[d+1]; //dword from s1
c2:=p[1]; //dword from s2
inc(integer(p),4);
c1:=c1 xor c2;
if c1<>0 then begin; //test the difference
//all bits of each byte must be 0, except bit5 (weight $20)
if (c1 and integer($DFDFDFDF))<>0 then goto find;
//bit5 can be 1 for letters only
c1:=c1 + c1; //$00 or $40
c1:=c1 + c1; //$00 or $80
if (c1 and c2)<>0 then goto find; //if not letter
c2:=c2 and $5F5F5F5F; //$41..$5A
c2:=c2 + $3F3F3F3F; //$80..$99
c2:=c2 and $7F7F7F7F; //$00..$19
c2:=c2 + $66666666; //$66..$7F
if (c1 and c2)<>0 then goto find; //if not letter
end;
until cardinal(p)>=cardinal(q);
past:
Result:=save;
exit;
repeat; //find mismatched characters
if cardinal(p)>=cardinal(q+4) then goto past;
find:
c1:=byte(p[d]);
c2:=byte(p[0]);
inc(integer(p));
c1:=c1-ord('a');
c2:=c2-ord('a');
if cardinal(c1)<=ord('z')-ord('a') then c1:=c1-(ord('a')-ord('A'));
if cardinal(c2)<=ord('z')-ord('a') then c2:=c2-(ord('a')-ord('A'));
until c1<>c2;
Result:=c1-c2;
end;
procedure CompareTextStub;
asm
call SysUtils.CompareText;
end;
end.