-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFastcodePosUnit.pas
More file actions
392 lines (367 loc) · 11.7 KB
/
FastcodePosUnit.pas
File metadata and controls
392 lines (367 loc) · 11.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
unit FastcodePosUnit;
(* ***** 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>
*
* BV Version: 5.60
* ***** END LICENSE BLOCK ***** *)
interface
{$I Fastcode.inc}
type
FastcodePosFunction = function(const SubStr: AnsiString; const Str: AnsiString): Integer;
{Functions shared between Targets}
function Pos_JOH_IA32_6(const SubStr : AnsiString; const Str : AnsiString) : Integer;
function Pos_JOH_SSE2_3(const SubStr : AnsiString; const Str : AnsiString) : Integer;
function Pos_JOH_PAS_6(const SubStr : AnsiString; const Str : AnsiString) : Integer; {$IFDEF Inline} inline; {$ENDIF}
{Functions not shared between Targets}
{Functions}
const
FastcodePosP4R: FastcodePosFunction = Pos_JOH_IA32_6;
FastcodePosP4N: FastcodePosFunction = Pos_JOH_IA32_6;
FastcodePosPMY: FastcodePosFunction = Pos_JOH_SSE2_3;
FastcodePosPMD: FastcodePosFunction = Pos_JOH_SSE2_3;
FastcodePosAMD64: FastcodePosFunction = Pos_JOH_IA32_6;
FastcodePosAMD64_SSE3: FastcodePosFunction = Pos_JOH_IA32_6;
FastCodePosIA32SizePenalty: FastCodePosFunction = Pos_JOH_PAS_6;
FastcodePosIA32: FastcodePosFunction = Pos_JOH_IA32_6;
FastcodePosMMX: FastcodePosFunction = Pos_JOH_IA32_6;
FastCodePosSSESizePenalty: FastCodePosFunction = Pos_JOH_PAS_6;
FastcodePosSSE: FastcodePosFunction = Pos_JOH_IA32_6;
FastcodePosSSE2: FastcodePosFunction = Pos_JOH_IA32_6;
FastCodePosPascalSizePenalty: FastCodePosFunction = Pos_JOH_PAS_6;
FastCodePosPascal: FastCodePosFunction = Pos_JOH_PAS_6;
function PosStub(const substr: AnsiString; const s: AnsiString): Integer;
implementation
uses
StrUtils;
function Pos_JOH_IA32_6(const SubStr : AnsiString; const Str : AnsiString) : Integer;
asm {Slightly Cut-Down version of PosEx_JOH_6}
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}
jl @@InvalidInput
push edi
push esi
push ebp
push edx
mov edi, [eax-4] {Length(SubStr)}
mov esi, [edx-3] {Length(S)}
cmp edi, 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, edi {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}
movzx eax, word ptr [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}
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;
function Pos_JOH_SSE2_3(const SubStr : AnsiString; const Str : AnsiString) : Integer;
asm
test eax, eax
jz @NotFoundExit {Exit if SurStr = ''}
test edx, edx
jz @NotFound {Exit if Str = ''}
mov ecx, [edx-4] {Length(Str)}
cmp [eax-4], 1 {Length SubStr = 1?}
je @SingleChar {Yes - Exit via CharPos}
jl @NotFound {Exit if Length(SubStr) < 1}
sub ecx, [eax-4] {Subtract Length(SubStr)}
jl @NotFound {Exit if Length(SubStr) > Length(Str)}
push esi {Save Registers}
push edi
push ebx
push ebp
mov esi, eax {Start Address of SubStr}
lea edi, [ecx+1] {Initial Remainder Count}
mov eax, [eax] {AL = 1st Char of SubStr}
mov ebp, edx {Start Address of Str}
mov ebx, eax {Maintain 1st Search Char in BL}
@StrLoop:
mov eax, ebx {AL = 1st char of SubStr}
mov ecx, edi {Remaining Length}
push edx {Save Start Position}
call @CharPos {Search for 1st Character}
pop edx {Restore Start Position}
test eax, eax {Result = 0?}
jz @StrExit {Exit if 1st Character Not Found}
mov ecx, [esi-4] {Length SubStr}
add edx, eax {Update Start Position for Next Loop}
sub edi, eax {Update Remaining Length for Next Loop}
sub ecx, 1 {Remaining Characters to Compare}
@StrCheck:
mov ax, [edx+ecx-2] {Compare Next Char of SubStr and Str}
cmp ax, [esi+ecx-1]
jne @StrLoop {Different - Return to First Character Search}
sub ecx, 2
jg @StrCheck {Check each Remaining Character}
mov eax, edx {All Characters Matched - Calculate Result}
sub eax, ebp
@StrExit:
pop ebp {Restore Registers}
pop ebx
pop edi
pop esi
ret
@NotFound:
xor eax, eax {Return 0}
@NotFoundExit:
ret
@SingleChar:
mov al, [eax] {Search Character}
@CharPos:
PUSH EBX
MOV EBX, EAX
CMP ECX, 16
JL @@Small
@@NotSmall:
MOV AH, AL {Fill each Byte of XMM1 with AL}
MOVD XMM1, EAX
PSHUFLW XMM1, XMM1, 0
PSHUFD XMM1, XMM1, 0
@@First16:
MOVUPS XMM0, [EDX] {Unaligned}
PCMPEQB XMM0, XMM1 {Compare First 16 Characters}
PMOVMSKB EAX, XMM0
TEST EAX, EAX
JNZ @@FoundStart {Exit on any Match}
CMP ECX, 32
JL @@Medium {If Length(Str) < 32, Check Remainder}
@@Align:
SUB ECX, 16 {Align Block Reads}
PUSH ECX
MOV EAX, EDX
NEG EAX
AND EAX, 15
ADD EDX, ECX
NEG ECX
ADD ECX, EAX
@@Loop:
MOVAPS XMM0, [EDX+ECX] {Aligned}
PCMPEQB XMM0, XMM1 {Compare Next 16 Characters}
PMOVMSKB EAX, XMM0
TEST EAX, EAX
JNZ @@Found {Exit on any Match}
ADD ECX, 16
JLE @@Loop
@Remainder:
POP EAX {Check Remaining Characters}
ADD EDX, 16
ADD EAX, ECX {Count from Last Loop End Position}
JMP DWORD PTR [@@JumpTable2-ECX*4]
@@NullString:
XOR EAX, EAX {Result = 0}
RET
@@FoundStart:
BSF EAX, EAX {Get Set Bit}
POP EBX
ADD EAX, 1 {Set Result}
RET
@@Found:
POP EDX
BSF EAX, EAX {Get Set Bit}
ADD EDX, ECX
POP EBX
LEA EAX, [EAX+EDX+1] {Set Result}
RET
@@Medium:
ADD EDX, ECX {End of String}
MOV EAX, 16 {Count from 16}
JMP DWORD PTR [@@JumpTable1-64-ECX*4]
@@Small:
ADD EDX, ECX {End of String}
XOR EAX, EAX {Count from 0}
JMP DWORD PTR [@@JumpTable1-ECX*4]
nop; nop {Aligb Jump Tables}
@@JumpTable1:
DD @@NotFound, @@01, @@02, @@03, @@04, @@05, @@06, @@07
DD @@08, @@09, @@10, @@11, @@12, @@13, @@14, @@15, @@16
@@JumpTable2:
DD @@16, @@15, @@14, @@13, @@12, @@11, @@10, @@09, @@08
DD @@07, @@06, @@05, @@04, @@03, @@02, @@01, @@NotFound
@@16:
ADD EAX, 1
CMP BL, [EDX-16]
JE @@Done
@@15:
ADD EAX, 1
CMP BL, [EDX-15]
JE @@Done
@@14:
ADD EAX, 1
CMP BL, [EDX-14]
JE @@Done
@@13:
ADD EAX, 1
CMP BL, [EDX-13]
JE @@Done
@@12:
ADD EAX, 1
CMP BL, [EDX-12]
JE @@Done
@@11:
ADD EAX, 1
CMP BL, [EDX-11]
JE @@Done
@@10:
ADD EAX, 1
CMP BL, [EDX-10]
JE @@Done
@@09:
ADD EAX, 1
CMP BL, [EDX-9]
JE @@Done
@@08:
ADD EAX, 1
CMP BL, [EDX-8]
JE @@Done
@@07:
ADD EAX, 1
CMP BL, [EDX-7]
JE @@Done
@@06:
ADD EAX, 1
CMP BL, [EDX-6]
JE @@Done
@@05:
ADD EAX, 1
CMP BL, [EDX-5]
JE @@Done
@@04:
ADD EAX, 1
CMP BL, [EDX-4]
JE @@Done
@@03:
ADD EAX, 1
CMP BL, [EDX-3]
JE @@Done
@@02:
ADD EAX, 1
CMP BL, [EDX-2]
JE @@Done
@@01:
ADD EAX, 1
CMP BL, [EDX-1]
JE @@Done
@@NotFound:
XOR EAX, EAX
@@Done:
POP EBX
end;
function Pos_JOH_PAS_6(const SubStr : AnsiString; const Str : AnsiString) : Integer; {$IFDEF Inline} inline; {$ENDIF}
begin
Result := StrUtils.PosEx(SubStr, Str, 1);
end;
function PosStub(const substr: AnsiString; const s: AnsiString): Integer;
asm
{$IFDEF DELPHI2005Plus}
call System.Pos;
{$ELSE}
call System.@LStrPos;
{$ENDIF}
end;
end.