-
Notifications
You must be signed in to change notification settings - Fork 3
/
FastcodeStrToInt32Unit.pas
394 lines (355 loc) · 11.3 KB
/
FastcodeStrToInt32Unit.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
unit FastcodeStrToInt32Unit;
(* ***** 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: 1.07
* ***** END LICENSE BLOCK ***** *)
interface
{$I Fastcode.inc}
uses
SysUtils;
type
FastcodeStrToInt32Function = function(const S: string): Integer;
{Functions shared between Targets}
function StrToInt32_JOH_IA32_7(const S: string) : Integer;
{Functions not shared between Targets}
{Functions}
const
FastcodeStrToInt32P4R: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32P4N: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32PMY: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32PMD: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32AMD64: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32AMD64_SSE3: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32IA32SizePenalty: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32IA32: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32MMX: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32SSESizePenalty: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32SSE: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32SSE2: FastcodeStrToInt32Function = StrToInt32_JOH_IA32_7;
FastcodeStrToInt32PascalSizePenalty: FastcodeStrToInt32Function = StrToInt;
FastcodeStrToInt32Pascal: FastcodeStrToInt32Function = StrToInt;
function StrToInt32Stub(const S: string): Integer;
implementation
uses
SysConst;
procedure ConvertErrorFmt(ResString: PResStringRec; const Args: array of const); local;
begin
raise EConvertError.CreateResFmt(ResString, Args);
end;
procedure RaiseConvertError(S : AnsiString);
begin
ConvertErrorFmt(@SInvalidInteger, [S]);
end;
procedure StrToIntError(const S: string);
begin
raise EConvertError.CreateResFmt(@SInvalidInteger, [S]);
end;
function StrToInt32_JOH_IA32_7(const S: string) : Integer;
asm
test eax, eax
jz @@Failed
push eax
push ebx
push edi
push esi
mov edx, eax {String Pointer}
xor ebx, ebx {Clear Sign Flag (top bit) and Valid Flag}
xor eax, eax {Clear Result}
mov edi, '0'
mov esi, 9
@@Trim: {Strip Leading Spaces}
movzx ecx, [edx]
inc edx
cmp cl, ' '
je @@Trim
cmp ecx, edi {cl <= '0'?}
jle @@CheckFirstChar {Yes, Check +, -, $, 0x, 0X}
test cl, not 'x'
jz @@CheckX {May start with 'x' or 'X'}
@@Numeric:
sub ecx, edi
cmp ecx, esi
ja @@TestValid {Not '0'..'9'}
mov eax, ecx {Result := Digit}
movzx ecx, [edx]
sub ecx, edi
cmp ecx, esi
ja @@CheckDone {Not '0'..'9'}
lea eax, [eax*4+eax]
lea eax, [eax*2+ecx] {Result = Result * 10 + Digit}
movzx ecx, [edx+1]
sub ecx, edi
cmp ecx, esi
ja @@CheckDone {Not '0'..'9'}
lea eax, [eax*4+eax]
lea eax, [eax*2+ecx] {Result = Result * 10 + Digit}
movzx ecx, [edx+2]
sub ecx, edi
cmp ecx, esi
ja @@CheckDone {Not '0'..'9'}
lea eax, [eax*4+eax]
lea eax, [eax*2+ecx] {Result = Result * 10 + Digit}
movzx ecx, [edx+3]
sub ecx, edi
cmp ecx, esi
ja @@CheckDone {Not '0'..'9'}
lea eax, [eax*4+eax]
lea eax, [eax*2+ecx] {Result = Result * 10 + Digit}
movzx ecx, [edx+4]
sub ecx, edi
cmp ecx, esi
ja @@CheckDone {Not '0'..'9'}
lea eax, [eax*4+eax]
lea eax, [eax*2+ecx] {Result = Result * 10 + Digit}
movzx ecx, [edx+5]
sub ecx, edi
cmp ecx, esi
ja @@CheckDone {Not '0'..'9'}
lea eax, [eax*4+eax]
lea eax, [eax*2+ecx] {Result = Result * 10 + Digit}
movzx ecx, [edx+6]
sub ecx, edi
cmp ecx, esi
ja @@CheckDone {Not '0'..'9'}
lea eax, [eax*4+eax]
lea eax, [eax*2+ecx] {Result = Result * 10 + Digit}
movzx ecx, [edx+7]
sub ecx, edi
cmp ecx, esi
ja @@CheckDone {Not '0'..'9'}
lea eax, [eax*4+eax]
lea eax, [eax*2+ecx] {Result = Result * 10 + Digit}
@@NumLoop:
movzx ecx, [edx+8]
sub ecx, edi
cmp ecx, esi
ja @@NumDone {Not '0'..'9'}
cmp eax, MaxInt/10
ja @@Error
inc edx
lea eax, [eax*4+eax]
lea eax, [eax*2+ecx] {Result = Result * 10 + Digit}
jmp @@NumLoop
@@TestValid:
test bl, bl {Got Valid Number?}
jz @@Error {No, Error}
@@CheckDone:
add ecx, edi {Last Character = Null Terminator?}
jnz @@Error {No, Error}
sar ebx, 31 {Set Each Bit to Top Bit (Sign Flag)}
xor eax, ebx {Negate Result if Necessary}
sub eax, ebx
pop esi
pop edi
pop ebx
pop ecx
ret
@@NumDone:
cmp eax, $80000000
jb @@CheckDone {No Overflow}
jne @@Error {Overflow}
test ebx, ebx {Sign Flag Set?}
js @@CheckDone {Yes, Result is Valid (-MaxInt-1)}
@@Error:
pop esi
pop edi
pop ebx
pop eax
@@Failed:
jmp StrToIntError
@@CheckFirstChar: {First Char <= '0'}
cmp cl, '$'
je @@Hex
cmp cl, '-'
je @@Minus
cmp cl, '+'
je @@Plus
cmp ecx, edi {Starts with '0'?}
jne @@Error {No, Error}
movzx ecx, [edx] {Character after '0'}
mov bl, 1 {Valid := True}
inc edx
jmp @@CheckX
@@Minus:
mov ebx, $80000000 {Set Sign Flag (Top Bit), Valid := False}
@@Plus:
movzx ecx, [edx] {Character after '+' or '-'}
inc edx
cmp cl, '$'
je @@Hex {Starts with '+$' or '-$'}
cmp ecx, edi {Starts with '+0' or '-0'?}
jne @@CheckAlpha {No, May start with '+x', '-x', '+X' or '-X'}
movzx ecx, [edx] {Character after '+0' or '-0'}
inc ebx {Starts with '+0' or '-0', Valid := True}
inc edx
@@CheckAlpha:
test cl, not 'x' {Could Char be 'x' or 'X'?}
jnz @@Numeric {No, Assume Numeric}
@@CheckX:
or cl, $20 {'X' -> 'x'}
cmp cl, 'x' {Char = 'X' or 'x'?}
movzx ecx, [edx-1] {Reload Character}
jne @@Numeric {Does Not start with +/-('x', 'X', '0x' or '0X')}
mov bl, 0 {Reset Valid to False}
@@Hex:
movzx ecx, [edx]
inc edx
sub ecx, edi
cmp ecx, esi
jna @@Hex1 {'0'..'9'}
cmp cl, 'z'-'0'
ja @@TestValid {Check for Valid and Null Terminator}
or cl, $20 {'A'..'F' -> 'a'..'f'}
sub cl, 'a'-'0'-10 {'A'..'F' or 'a'..'f' -> 10..15}
cmp cl, 15
ja @@Error {Not Hex Character}
@@Hex1:
mov eax, ecx {Result = Digit}
movzx ecx, [edx]
inc edx
sub ecx, edi
cmp ecx, esi
jna @@Hex2 {'0'..'9'}
cmp cl, 'z'-'0'
ja @@CheckDone {Check for Null Terminator}
or cl, $20 {'A'..'F' -> 'a'..'f'}
sub cl, 'a'-'0'-10 {'A'..'F' or 'a'..'f' -> 10..15}
cmp cl, 15
ja @@Error {Not Hex Character}
@@Hex2:
shl eax, 4
add eax, ecx {Result = Result * 16 + Digit}
movzx ecx, [edx]
inc edx
sub ecx, edi
cmp ecx, esi
jna @@Hex3 {'0'..'9'}
cmp cl, 'z'-'0'
ja @@CheckDone {Check for Null Terminator}
or cl, $20 {'A'..'F' -> 'a'..'f'}
sub cl, 'a'-'0'-10 {'A'..'F' or 'a'..'f' -> 10..15}
cmp cl, 15
ja @@Error {Not Hex Character}
@@Hex3:
shl eax, 4
add eax, ecx {Result = Result * 16 + Digit}
movzx ecx, [edx]
inc edx
sub ecx, edi
cmp ecx, esi
jna @@Hex4 {'0'..'9'}
cmp cl, 'z'-'0'
ja @@CheckDone {Check for Null Terminator}
or cl, $20 {'A'..'F' -> 'a'..'f'}
sub cl, 'a'-'0'-10 {'A'..'F' or 'a'..'f' -> 10..15}
cmp cl, 15
ja @@Error {Not Hex Character}
@@Hex4:
shl eax, 4
add eax, ecx {Result = Result * 16 + Digit}
movzx ecx, [edx]
inc edx
sub ecx, edi
cmp ecx, esi
jna @@Hex5 {'0'..'9'}
cmp cl, 'z'-'0'
ja @@CheckDone {Check for Null Terminator}
or cl, $20 {'A'..'F' -> 'a'..'f'}
sub cl, 'a'-'0'-10 {'A'..'F' or 'a'..'f' -> 10..15}
cmp cl, 15
ja @@Error {Not Hex Character}
@@Hex5:
shl eax, 4
add eax, ecx {Result = Result * 16 + Digit}
movzx ecx, [edx]
inc edx
sub ecx, edi
cmp ecx, esi
jna @@Hex6 {'0'..'9'}
cmp cl, 'z'-'0'
ja @@CheckDone {Check for Null Terminator}
or cl, $20 {'A'..'F' -> 'a'..'f'}
sub cl, 'a'-'0'-10 {'A'..'F' or 'a'..'f' -> 10..15}
cmp cl, 15
ja @@Error {Not Hex Character}
@@Hex6:
shl eax, 4
add eax, ecx {Result = Result * 16 + Digit}
movzx ecx, [edx]
inc edx
sub ecx, edi
cmp ecx, esi
jna @@Hex7 {'0'..'9'}
cmp cl, 'z'-'0'
ja @@CheckDone {Check for Null Terminator}
or cl, $20 {'A'..'F' -> 'a'..'f'}
sub cl, 'a'-'0'-10 {'A'..'F' or 'a'..'f' -> 10..15}
cmp cl, 15
ja @@Error {Not Hex Character}
@@Hex7:
shl eax, 4
add eax, ecx {Result = Result * 16 + Digit}
movzx ecx, [edx]
inc edx
sub ecx, edi
cmp ecx, esi
jna @@Hex8 {'0'..'9'}
cmp cl, 'z'-'0'
ja @@CheckDone {Check for Null Terminator}
or cl, $20 {'A'..'F' -> 'a'..'f'}
sub cl, 'a'-'0'-10 {'A'..'F' or 'a'..'f' -> 10..15}
cmp cl, 15
ja @@Error {Not Hex Character}
@@Hex8:
shl eax, 4
add eax, ecx {Result = Result * 16 + Digit}
@@HexLoop:
movzx ecx, [edx]
sub ecx, edi
cmp ecx, esi
jna @@CheckRange {'0'..'9'}
cmp cl, 'z'-'0'
ja @@CheckDone {Check for Null Terminator}
or cl, $20 {'A'..'F' -> 'a'..'f'}
sub cl, 'a'-'0'-10 {'A'..'F' or 'a'..'f' -> 10..15}
cmp cl, 15
ja @@Error {Not Hex Character}
@@CheckRange:
cmp eax, MaxInt/8 {High(ULONG) div 16}
ja @@Error {Overflow}
shl eax, 4
(*
shl eax, 4 //Using this instead of the above 3 lines wrongly
jc @@Error // passes validation with S='$200000000000000'
*)
add eax, ecx {Result = Result * 16 + Digit}
inc edx
jmp @@HexLoop
end;
function StrToInt32Stub(const S: string): Integer;
asm
call SysUtils.StrToInt;
end;
end.