-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFastcodePosExUnit.pas
More file actions
504 lines (472 loc) · 14.2 KB
/
FastcodePosExUnit.pas
File metadata and controls
504 lines (472 loc) · 14.2 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
unit FastcodePosExUnit;
(* ***** 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 <chmichael@creationpower.com>
* John O'Harrow <john@elmcrest.demon.co.uk>
* Aleksandr Sharahov
*
* BV Version: 1.64
* ***** END LICENSE BLOCK ***** *)
interface
{$I Fastcode.inc}
type
FastcodePosExFunction = function(const SubStr, S: string; Offset: Integer = 1): Integer;
{Functions shared between Targets}
function PosEx_JOH_IA32_8(const SubStr, S: string; Offset: Integer = 1): Integer;
function PosEx_JOH_IA32_7(const SubStr, S: string; Offset: Integer = 1): Integer;
function PosEx_Sha_IA32_3(const SubStr, Str: string; Offset: integer=1): integer;
function PosEx_Sha_Pas_2(const SubStr, S: string; Offset: Integer = 1): Integer;
{Functions not shared between Targets}
{Functions}
const
FastcodePosExP4R: FastcodePosExFunction = PosEx_JOH_IA32_8;
FastcodePosExP4N: FastcodePosExFunction = PosEx_JOH_IA32_8;
FastcodePosExPMY: FastcodePosExFunction = PosEx_Sha_IA32_3;
FastcodePosExPMD: FastcodePosExFunction = PosEx_Sha_IA32_3;
FastcodePosExAMD64: FastcodePosExFunction = PosEx_JOH_IA32_8;
FastcodePosExAMD64_SSE3: FastcodePosExFunction = PosEx_JOH_IA32_8;
FastCodePosExIA32SizePenalty: FastCodePosExFunction = PosEx_JOH_IA32_7;
FastcodePosExIA32: FastcodePosExFunction = PosEx_JOH_IA32_8;
FastcodePosExMMX: FastcodePosExFunction = PosEx_JOH_IA32_8;
FastCodePosExSSESizePenalty: FastCodePosExFunction = PosEx_JOH_IA32_7;
FastcodePosExSSE: FastcodePosExFunction = PosEx_JOH_IA32_8;
FastcodePosExSSE2: FastcodePosExFunction = PosEx_JOH_IA32_8;
FastCodePosExPascalSizePenalty: FastCodePosExFunction = PosEx_Sha_Pas_2;
FastCodePosExPascal: FastCodePosExFunction = PosEx_Sha_Pas_2;
procedure PosExStub;
implementation
uses
StrUtils;
function PosEx_JOH_IA32_8(const SubStr, S: string; Offset: Integer = 1): Integer;
asm {299 Bytes}
sub esp, 20
mov [esp], ebx
cmp eax, 1
sbb ebx, ebx {-1 if SubStr = '' else 0}
sub edx, 1 {-1 if S = ''}
sbb ebx, 0 {Negative if S = '' or SubStr = '' else 0}
sub ecx, 1 {Offset - 1}
or ebx, ecx {Negative if S = '' or SubStr = '' or Offset < 1}
jl @@InvalidInput
mov [esp+4], edi
mov [esp+8], esi
mov [esp+12], ebp
mov [esp+16], edx
mov edi, [eax-4] {Length(SubStr)}
mov esi, [edx-3] {Length(S)}
add ecx, edi
cmp ecx, esi
jg @@NotFound {Offset to High for a Match}
test edi, edi
jz @@NotFound {Length(SubStr = 0)}
lea ebp, [eax+edi] {Last Character Position in SubStr + 1}
add esi, edx {Last Character Position in S}
movzx eax, [ebp-1] {Last Character of SubStr}
add edx, ecx {Search Start Position in S for Last Character}
mov ah, al
neg edi {-Length(SubStr)}
mov ecx, eax
shl eax, 16
or ecx, eax {All 4 Bytes = Last Character of SubStr}
@@MainLoop:
add edx, 4
cmp edx, esi
ja @@Remainder {1 to 4 Positions Remaining}
mov eax, [edx-4] {Check Next 4 Bytes of S}
xor eax, ecx {Zero Byte at each Matching Position}
lea ebx, [eax-$01010101]
not eax
and eax, ebx
and eax, $80808080 {Set Byte to $80 at each Match Position else $00}
jz @@MainLoop {Loop Until any Match on Last Character Found}
bsf eax, eax {Find First Match Bit}
shr eax, 3 {Byte Offset of First Match (0..3)}
lea edx, [eax+edx-3] {Address of First Match on Last Character + 1}
@@Compare:
cmp edi, -4
jle @@Large {Lenght(SubStr) >= 4}
cmp edi, -1
je @@SetResult {Exit with Match if Lenght(SubStr) = 1}
mov ax, [ebp+edi] {Last Char Matches - Compare First 2 Chars}
cmp ax, [edx+edi]
jne @@MainLoop {No Match on First 2 Characters}
@@SetResult: {Full Match}
lea eax, [edx+edi] {Calculate and Return Result}
mov ebx, [esp]
mov edi, [esp+4]
mov esi, [esp+8]
mov ebp, [esp+12]
sub eax, [esp+16]
add esp, 20
ret
@@NotFound:
mov edi, [esp+4]
mov esi, [esp+8]
mov ebp, [esp+12]
@@InvalidInput:
mov ebx, [esp]
add esp, 20
xor eax, eax {Return 0}
ret
@@Remainder: {Check Last 1 to 4 Characters}
mov eax, [esi-3] {Last 4 Characters of S - May include Length
Bytes}
xor eax, ecx {Zero Byte at each Matching Position}
lea ebx, [eax-$01010101]
not eax
and eax, ebx
and eax, $80808080 {Set Byte to $80 at each Match Position else $00}
jz @@NotFound {No Match Possible}
lea eax, [edx-4] {Check Valid Match Positions}
cmp cl, [eax]
lea edx, [eax+1]
je @@Compare
cmp edx, esi
ja @@NotFound
lea edx, [eax+2]
cmp cl, [eax+1]
je @@Compare
cmp edx, esi
ja @@NotFound
lea edx, [eax+3]
cmp cl, [eax+2]
je @@Compare
cmp edx, esi
ja @@NotFound
lea edx, [eax+4]
jmp @@Compare
@@Large:
mov eax, [ebp-4] {Compare Last 4 Characters of S and SubStr}
cmp eax, [edx-4]
jne @@MainLoop {No Match on Last 4 Characters}
mov ebx, edi
@@CompareLoop: {Compare Remaining Characters}
add ebx, 4 {Compare 4 Characters per Loop}
jge @@SetResult {All Characters Matched}
mov eax, [ebp+ebx-4]
cmp eax, [edx+ebx-4]
je @@CompareLoop {Match on Next 4 Characters}
jmp @@MainLoop {No Match}
end; {PosEx}
function PosEx_JOH_IA32_7(const SubStr, S: string; Offset: Integer = 1): Integer;
asm {180 Bytes}
push ebx
cmp eax, 1
sbb ebx, ebx {-1 if SubStr = '' else 0}
sub edx, 1 {-1 if S = ''}
sbb ebx, 0 {Negative if S = '' or SubStr = '' else 0}
dec ecx {Offset - 1}
or ebx, ecx {Negative if S = '' or SubStr = '' or Offset < 1}
jl @@InvalidInput
push edi
push esi
push ebp
push edx
mov edi, [eax-4] {Length(SubStr)}
mov esi, [edx-3] {Length(S)}
add ecx, edi
cmp ecx, esi
jg @@NotFound {Offset to High for a Match}
test edi, edi
jz @@NotFound {Length(SubStr = 0)}
lea ebp, [eax+edi] {Last Character Position in SubStr + 1}
add esi, edx {Last Character Position in S}
movzx eax, [ebp-1] {Last Character of SubStr}
add edx, ecx {Search Start Position in S for Last Character}
mov ah, al
neg edi {-Length(SubStr)}
mov ecx, eax
shl eax, 16
or ecx, eax {All 4 Bytes = Last Character of SubStr}
@@MainLoop:
add edx, 4
cmp edx, esi
ja @@Remainder {1 to 4 Positions Remaining}
mov eax, [edx-4] {Check Next 4 Bytes of S}
xor eax, ecx {Zero Byte at each Matching Position}
lea ebx, [eax-$01010101]
not eax
and eax, ebx
and eax, $80808080 {Set Byte to $80 at each Match Position else $00}
jz @@MainLoop {Loop Until any Match on Last Character Found}
bsf eax, eax {Find First Match Bit}
shr eax, 3 {Byte Offset of First Match (0..3)}
lea edx, [eax+edx-4] {Address of First Match on Last Character}
@@Compare:
inc edx
cmp edi, -4
jle @@Large {Lenght(SubStr) >= 4}
cmp edi, -1
je @@SetResult {Exit with Match if Lenght(SubStr) = 1}
mov ax, [ebp+edi] {Last Char Matches - Compare First 2 Chars}
cmp ax, [edx+edi]
jne @@MainLoop {No Match on First 2 Characters}
@@SetResult: {Full Match}
lea eax, [edx+edi] {Calculate and Return Result}
pop edx
pop ebp
pop esi
pop edi
pop ebx
sub eax, edx {Subtract Start Position}
ret
@@NotFound:
pop edx {Dump Start Position}
pop ebp
pop esi
pop edi
@@InvalidInput:
pop ebx
xor eax, eax {No Match Found - Return 0}
ret
@@Remainder: {Check Last 1 to 4 Characters}
sub edx, 4
@@RemainderLoop:
cmp cl, [edx]
je @@Compare
cmp edx, esi
jae @@NotFound
inc edx
jmp @@RemainderLoop
@@Large:
mov eax, [ebp-4] {Compare Last 4 Characters}
cmp eax, [edx-4]
jne @@MainLoop {No Match on Last 4 Characters}
mov ebx, edi
@@CompareLoop: {Compare Remaining Characters}
add ebx, 4 {Compare 4 Characters per Loop}
jge @@SetResult {All Characters Matched}
mov eax, [ebp+ebx-4]
cmp eax, [edx+ebx-4]
je @@CompareLoop {Match on Next 4 Characters}
jmp @@MainLoop {No Match}
end; {PosEx}
function PosEx_Sha_IA32_3(const SubStr, Str: string; Offset: integer=1): integer;
asm
test eax, eax
jz @Nil
test edx, edx
jz @Nil
dec ecx
jl @Nil
push esi
push ebx
mov esi, [edx-4] //Length(Str)
mov ebx, [eax-4] //Length(Substr)
sub esi, ecx //effective length of Str
add edx, ecx //addr of the first char at starting position
cmp esi, ebx
jl @Past //jump if EffectiveLength(Str)<Length(Substr)
test ebx, ebx
jle @Past //jump if Length(Substr)<=0
add esp, -12
add ebx, -1 //Length(Substr)-1
add esi, edx //addr of the terminator
add edx, ebx //addr of the last char at starting position
mov [esp+8], esi //save addr of the terminator
add eax, ebx //addr of the last char of Substr
sub ecx, edx //-@Str[Length(Substr)]
neg ebx //-(Length(Substr)-1)
mov [esp+4], ecx //save -@Str[Length(Substr)]
mov [esp], ebx //save -(Length(Substr)-1)
movzx ecx, byte ptr [eax] //the last char of Substr
@SmallLoop:
cmp cl, [edx]
jz @Test0
@AfterTest0:
cmp cl, [edx+1]
jz @TestT
@AfterTest1:
add edx, 8
cmp edx, [esp+8]
jae @EndSmall
@MainLoop:
cmp cl, [edx-6]
jz @Test6
cmp cl, [edx-5]
jz @Test5
cmp cl, [edx-4]
jz @Test4
cmp cl, [edx-3]
jz @Test3
cmp cl, [edx-2]
jz @Test2
cmp cl, [edx-1]
jz @Test1
cmp cl, [edx]
jz @Test0
cmp cl, [edx+1]
jz @TestT
add edx, 8
cmp edx, [esp+8]
jb @MainLoop
@EndSmall:
add edx, -6
cmp edx, [esp+8]
jb @SmallLoop
@Exit:
add esp, 12
@Past:
pop ebx
pop esi
@Nil:
xor eax, eax
ret
@Test6:
add edx, -2
@Test4:
add edx, -2
@Test2:
add edx, -2
@Test0:
mov esi, [esp]
test esi, esi
jz @Found0
@Loop0:
movzx ebx, word ptr [esi+eax]
cmp bx, word ptr [esi+edx]
jnz @AfterTest0
cmp esi, -2
jge @Found0
movzx ebx, word ptr [esi+eax+2]
cmp bx, word ptr [esi+edx+2]
jnz @AfterTest0
add esi, 4
jl @Loop0
@Found0:
mov eax, [esp+4]
add edx, 1
add esp, 12
add eax, edx
pop ebx
pop esi
ret
@Test5:
add edx, -2
@Test3:
add edx, -2
@Test1:
add edx, -2
@TestT:
mov esi, [esp]
test esi, esi
jz @Found1
@Loop1:
movzx ebx, word ptr [esi+eax]
cmp bx, word ptr [esi+edx+1]
jnz @AfterTest1
cmp esi, -2
jge @Found1
movzx ebx, word ptr [esi+eax+2]
cmp bx, word ptr [esi+edx+3]
jnz @AfterTest1
add esi, 4
jl @Loop1
@Found1:
mov eax, [esp+4]
add edx, 2
cmp edx, [esp+8]
ja @Exit
add esp, 12
add eax, edx
pop ebx
pop esi
end;
function PosEx_Sha_Pas_2(const SubStr, S: string; Offset: Integer = 1): Integer;
var
len, lenSub: integer;
ch: char;
p, pSub, pStart, pStop: pchar;
label
Loop0, Loop4,
TestT, Test0, Test1, Test2, Test3, Test4,
AfterTestT, AfterTest0,
Ret, Exit;
begin;
pSub:=pointer(SubStr);
p:=pointer(S);
if (p=nil) or (pSub=nil) or (Offset<1) then begin;
Result:=0;
goto Exit;
end;
lenSub:=pinteger(pSub-4)^-1;
len:=pinteger(p-4)^;
if (len<lenSub+Offset) or (lenSub<0) then begin;
Result:=0;
goto Exit;
end;
pStop:=p+len;
p:=p+lenSub;
pSub:=pSub+lenSub;
pStart:=p;
p:=p+Offset+3;
ch:=pSub[0];
lenSub:=-lenSub;
if p<pStop then goto Loop4;
p:=p-4;
goto Loop0;
Loop4:
if ch=p[-4] then goto Test4;
if ch=p[-3] then goto Test3;
if ch=p[-2] then goto Test2;
if ch=p[-1] then goto Test1;
Loop0:
if ch=p[0] then goto Test0;
AfterTest0:
if ch=p[1] then goto TestT;
AfterTestT:
p:=p+6;
if p<pStop then goto Loop4;
p:=p-4;
if p<pStop then goto Loop0;
Result:=0;
goto Exit;
Test3: p:=p-2;
Test1: p:=p-2;
TestT: len:=lenSub;
if lenSub<>0 then repeat;
if (psub[len]<>p[len+1])
or (psub[len+1]<>p[len+2]) then goto AfterTestT;
len:=len+2;
until len>=0;
p:=p+2;
if p<=pStop then goto Ret;
Result:=0;
goto Exit;
Test4: p:=p-2;
Test2: p:=p-2;
Test0: len:=lenSub;
if lenSub<>0 then repeat;
if (psub[len]<>p[len])
or (psub[len+1]<>p[len+1]) then goto AfterTest0;
len:=len+2;
until len>=0;
inc(p);
Ret:
Result:=p-pStart;
Exit:
end;
procedure PosExStub;
asm
call StrUtils.PosEx;
end;
end.