-
Notifications
You must be signed in to change notification settings - Fork 3
/
FastcodeUpperCaseUnit.pas
571 lines (549 loc) · 15.5 KB
/
FastcodeUpperCaseUnit.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
unit FastcodeUpperCaseUnit;
(* ***** 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>
* Dennis Kjaer Christensen
*
* BV Version: 3.65
* ***** END LICENSE BLOCK ***** *)
interface
{$I Fastcode.inc}
uses
SysUtils;
type
FastcodeUpperCaseFunction = function(const s: string): string;
{Functions shared between Targets}
function UpperCase_JOH_SSE2_2(const S: string): string;
function UpperCase_JOH_SSE_2(const S: string): string;
function UpperCase_JOH_MMX_3(const S: string): string;
function UpperCase_JOH_IA32_5(const S: string): string;
function UpperCase_DKC_Pas_32(const S: string): string;
{Functions not shared between Targets}
{Functions}
const
FastcodeUpperCaseP4R: FastcodeUpperCaseFunction = UpperCase_JOH_SSE2_2;
FastcodeUpperCaseP4N: FastcodeUpperCaseFunction = UpperCase_JOH_SSE_2;
FastcodeUpperCasePMY: FastcodeUpperCaseFunction = UpperCase_JOH_MMX_3;
FastcodeUpperCasePMD: FastcodeUpperCaseFunction = UpperCase_JOH_MMX_3;
FastcodeUpperCaseAMD64: FastcodeUpperCaseFunction = UpperCase_JOH_MMX_3;
FastcodeUpperCaseAMD64_SSE3: FastcodeUpperCaseFunction = UpperCase_JOH_SSE_2;
FastCodeUpperCaseIA32SizePenalty: FastCodeUpperCaseFunction = UpperCase_JOH_IA32_5;
FastcodeUpperCaseIA32: FastcodeUpperCaseFunction = SysUtils.UpperCase;
FastcodeUpperCaseMMX: FastcodeUpperCaseFunction = UpperCase_JOH_MMX_3;
FastCodeUpperCaseSSESizePenalty: FastCodeUpperCaseFunction = UpperCase_JOH_IA32_5;
FastcodeUpperCaseSSE: FastcodeUpperCaseFunction = UpperCase_JOH_SSE_2;
FastcodeUpperCaseSSE2: FastcodeUpperCaseFunction = UpperCase_JOH_SSE_2;
FastCodeUpperCasePascalSizePenalty: FastCodeUpperCaseFunction = UpperCase_DKC_Pas_32;
FastCodeUpperCasePascal: FastCodeUpperCaseFunction = UpperCase_DKC_Pas_32;
procedure UpperCaseStub;
implementation
var
LookUpTable : array of Char;
function UpperCase_JOH_SSE2_2(const S: string): string;
const
B05 : array[1..2] of Int64 = ($0505050505050505, $0505050505050505);
B65 : array[1..2] of Int64 = ($6565656565656565, $6565656565656565);
B20 : array[1..2] of Int64 = ($2020202020202020, $2020202020202020);
asm
push ebx
push edi
push esi
test eax, eax {Test for S = NIL}
mov esi, eax {@S}
mov edi, edx {@Result}
mov eax, edx {@Result}
jz @@Null {S = NIL}
mov edx, [esi-4] {Length(S)}
test edx, edx
je @@Null {Length(S) = 0}
mov ebx, edx
call system.@LStrSetLength {Create Result String}
mov edi, [edi] {@Result}
cmp ebx, 32
jg @@Large
@@Small:
mov eax, [esi+ebx-4] {Convert the Last 4 Characters of String}
mov ecx, $7f7f7f7f {Note - For Strings of Length 1, 2 or 3,
this}
mov edx, eax {will Read/Write the last 1, 2 or 3 bytes
of }
not edx {the Length Preaamble, but since these
bytes }
and ecx, eax {will always contain 0 in these cases,
hey }
and edx, $80808080 {will never be modified}
add ecx, $05050505
and ecx, $7f7f7f7f
add ecx, $1a1a1a1a
and ecx, edx
shr ecx, 2
xor eax, ecx
mov [edi+ebx-4], eax
sub ebx, 1
and ebx, -4
jz @@Done {Exit if Length <= 4}
add esi, ebx
add edi, ebx
neg ebx
@@SmallLoop: {Loop converting 4 Character per Loop}
mov eax, [esi+ebx]
mov ecx, $7f7f7f7f
mov edx, eax
not edx
and ecx, eax
and edx, $80808080
add ecx, $05050505
and ecx, $7f7f7f7f
add ecx, $1a1a1a1a
and ecx, edx
shr ecx, 2
xor eax, ecx
mov [edi+ebx], eax
add ebx, 4
jnz @@SmallLoop
@@Done:
pop esi
pop edi
pop ebx
ret
@@Null:
pop esi
pop edi
pop ebx
jmp System.@LStrClr
@@Large:
movdqu xmm2, B05
movdqu xmm3, B65
movdqu xmm4, B20
movdqu xmm0, [esi] {Translate First 16 Chars}
movdqu xmm1, xmm0
paddb xmm1, xmm2
pcmpgtb xmm1, xmm3
pand xmm1, xmm4
psubb xmm0, xmm1
movdqu [edi], xmm0
sub ebx, 16
movdqu xmm0, [esi+ebx] {Translate Last 16 Chars}
movdqu xmm1, xmm0
paddb xmm1, xmm2
pcmpgtb xmm1, xmm3
pand xmm1, xmm4
psubb xmm0, xmm1
movdqu [edi+ebx], xmm0
mov ecx, edi {Align Writes}
add esi, ebx
add ebx, edi
and edi, -16
sub ebx, edi
add edi, ebx
sub ebx, 16
neg ebx
@@LargeLoop:
movdqu xmm0, [esi+ebx] {Translate Next 16 Chars}
movdqa xmm1, xmm0
paddb xmm1, xmm2
pcmpgtb xmm1, xmm3
pand xmm1, xmm4
psubb xmm0, xmm1
movdqa [edi+ebx], xmm0
add ebx, 16
jl @@LargeLoop
pop esi
pop edi
pop ebx
end;
function UpperCase_JOH_SSE_2(const S: string): string;
const
B05 : Int64 = $0505050505050505;
B65 : Int64 = $6565656565656565;
B20 : Int64 = $2020202020202020;
asm
push ebx
push edi
push esi
test eax, eax {Test for S = NIL}
mov esi, eax {@S}
mov edi, edx {@Result}
mov eax, edx {@Result}
jz @@Null {S = NIL}
mov edx, [esi-4] {Length(S)}
test edx, edx
je @@Null {Length(S) = 0}
mov ebx, edx
call system.@LStrSetLength {Create Result String}
mov edi, [edi] {@Result}
cmp ebx, 32
jg @@Large
@@Small:
mov eax, [esi+ebx-4] {Convert the Last 4 Characters of String}
mov ecx, $7f7f7f7f {Note - For Strings of Length 1, 2 or 3,
this}
mov edx, eax {will Read/Write the last 1, 2 or 3 bytes
of }
not edx {the Length Preaamble, but since these
bytes }
and ecx, eax {will always contain 0 in these cases,
hey }
and edx, $80808080 {will never be modified}
add ecx, $05050505
and ecx, $7f7f7f7f
add ecx, $1a1a1a1a
and ecx, edx
shr ecx, 2
xor eax, ecx
mov [edi+ebx-4], eax
sub ebx, 1
and ebx, -4
jz @@Done {Exit if Length <= 4}
add esi, ebx
add edi, ebx
neg ebx
@@SmallLoop: {Loop converting 4 Character per Loop}
mov eax, [esi+ebx]
mov ecx, $7f7f7f7f
mov edx, eax
not edx
and ecx, eax
and edx, $80808080
add ecx, $05050505
and ecx, $7f7f7f7f
add ecx, $1a1a1a1a
and ecx, edx
shr ecx, 2
xor eax, ecx
mov [edi+ebx], eax
add ebx, 4
jnz @@SmallLoop
@@Done:
pop esi
pop edi
pop ebx
ret
@@Null:
pop esi
pop edi
pop ebx
jmp System.@LStrClr
@@Large:
movq mm4, B05
movq mm5, B65
movq mm6, B20
movq mm0, [esi ] {Translate First 16 Chars}
movq mm1, [esi+8]
pshufw mm2, mm0, $E4 {Faster Version of movq mm2, mm0}
pshufw mm3, mm1, $E4 {Faster Version of movq mm3, mm1}
paddb mm2, mm4
paddb mm3, mm4
pcmpgtb mm2, mm5
pcmpgtb mm3, mm5
pand mm2, mm6
pand mm3, mm6
psubb mm0, mm2
psubb mm1, mm3
movq [edi ], mm0
movq [edi+8], mm1
sub ebx, 16
movq mm0, [esi+ebx ] {Translate Last 16 Chars}
movq mm1, [esi+ebx+8]
pshufw mm2, mm0, $E4 {Faster Version of movq mm2, mm0}
pshufw mm3, mm1, $E4 {Faster Version of movq mm3, mm1}
paddb mm2, mm4
paddb mm3, mm4
pcmpgtb mm2, mm5
pcmpgtb mm3, mm5
pand mm2, mm6
pand mm3, mm6
psubb mm0, mm2
psubb mm1, mm3
movq [edi+ebx ], mm0
movq [edi+ebx+8], mm1
mov ecx, edi {Align Writes}
add esi, ebx
add ebx, edi
and edi, -16
sub ebx, edi
add edi, ebx
sub ebx, 16
neg ebx
@@LargeLoop:
movq mm0, [esi+ebx ] {Translate Next 16 Chars}
movq mm1, [esi+ebx+8]
pshufw mm2, mm0, $E4 {Faster Version of movq mm2, mm0}
pshufw mm3, mm1, $E4 {Faster Version of movq mm3, mm1}
paddb mm2, mm4
paddb mm3, mm4
pcmpgtb mm2, mm5
pcmpgtb mm3, mm5
pand mm2, mm6
pand mm3, mm6
psubb mm0, mm2
psubb mm1, mm3
movq [edi+ebx ], mm0
movq [edi+ebx+8], mm1
add ebx, 16
jl @@LargeLoop
emms
pop esi
pop edi
pop ebx
end;
function UpperCase_JOH_MMX_3(const S: string): string;
const
B05 : Int64 = $0505050505050505;
B65 : Int64 = $6565656565656565;
B20 : Int64 = $2020202020202020;
asm
push ebx
push edi
push esi
test eax, eax {Test for S = NIL}
mov esi, eax {@S}
mov edi, edx {@Result}
mov eax, edx {@Result}
jz @@Null {S = NIL}
mov edx, [esi-4] {Length(S)}
test edx, edx
je @@Null {Length(S) = 0}
mov ebx, edx
call system.@LStrSetLength {Create Result String}
mov edi, [edi] {@Result}
cmp ebx, 32
jg @@Large
@@Small:
mov eax, [esi+ebx-4] {Convert the Last 4 Characters of String}
mov ecx, $7f7f7f7f {Note - For Strings of Length 1, 2 or 3,
this}
mov edx, eax {will Read/Write the last 1, 2 or 3 bytes
of }
not edx {the Length Preaamble, but since these
bytes }
and ecx, eax {will always contain 0 in these cases,
hey }
and edx, $80808080 {will never be modified}
add ecx, $05050505
and ecx, $7f7f7f7f
add ecx, $1a1a1a1a
and ecx, edx
shr ecx, 2
xor eax, ecx
mov [edi+ebx-4], eax
sub ebx, 1
and ebx, -4
jz @@Done {Exit if Length <= 4}
add esi, ebx
add edi, ebx
neg ebx
@@SmallLoop: {Loop converting 4 Character per Loop}
mov eax, [esi+ebx]
mov ecx, $7f7f7f7f
mov edx, eax
not edx
and ecx, eax
and edx, $80808080
add ecx, $05050505
and ecx, $7f7f7f7f
add ecx, $1a1a1a1a
and ecx, edx
shr ecx, 2
xor eax, ecx
mov [edi+ebx], eax
add ebx, 4
jnz @@SmallLoop
@@Done:
pop esi
pop edi
pop ebx
ret
@@Null:
pop esi
pop edi
pop ebx
jmp System.@LStrClr
@@Large:
movq mm4, B05
movq mm5, B65
movq mm6, B20
movq mm0, [esi ] {Translate First 16 Chars}
movq mm1, [esi+8]
movq mm2, mm0
movq mm3, mm1
paddb mm2, mm4
paddb mm3, mm4
pcmpgtb mm2, mm5
pcmpgtb mm3, mm5
pand mm2, mm6
pand mm3, mm6
psubb mm0, mm2
psubb mm1, mm3
movq [edi ], mm0
movq [edi+8], mm1
sub ebx, 16
movq mm0, [esi+ebx ] {Translate Last 16 Chars}
movq mm1, [esi+ebx+8]
movq mm2, mm0
movq mm3, mm1
paddb mm2, mm4
paddb mm3, mm4
pcmpgtb mm2, mm5
pcmpgtb mm3, mm5
pand mm2, mm6
pand mm3, mm6
psubb mm0, mm2
psubb mm1, mm3
movq [edi+ebx ], mm0
movq [edi+ebx+8], mm1
mov ecx, edi {Align Writes}
add esi, ebx
add ebx, edi
and edi, -16
sub ebx, edi
add edi, ebx
sub ebx, 16
neg ebx
@@LargeLoop:
movq mm0, [esi+ebx ] {Translate Next 16 Chars}
movq mm1, [esi+ebx+8]
movq mm2, mm0
movq mm3, mm1
paddb mm2, mm4
paddb mm3, mm4
pcmpgtb mm2, mm5
pcmpgtb mm3, mm5
pand mm2, mm6
pand mm3, mm6
psubb mm0, mm2
psubb mm1, mm3
movq [edi+ebx ], mm0
movq [edi+ebx+8], mm1
add ebx, 16
jl @@LargeLoop
emms
pop esi
pop edi
pop ebx
end;
function UpperCase_JOH_IA32_5(const S: string): string;
asm {Size = 134 Bytes}
push ebx
push edi
push esi
test eax, eax {Test for S = NIL}
mov esi, eax {@S}
mov edi, edx {@Result}
mov eax, edx {@Result}
jz @@Null {S = NIL}
mov edx, [esi-4] {Length(S)}
test edx, edx
je @@Null {Length(S) = 0}
mov ebx, edx
call system.@LStrSetLength {Create Result String}
mov edi, [edi] {@Result}
mov eax, [esi+ebx-4] {Convert the Last 4 Characters of String}
mov ecx, eax {4 Original Bytes}
or eax, $80808080 {Set High Bit of each Byte}
mov edx, eax {Comments Below apply to each Byte...}
sub eax, $7B7B7B7B {Set High Bit if Original <= Ord('z')}
xor edx, ecx {80h if Original < 128 else 00h}
or eax, $80808080 {Set High Bit}
sub eax, $66666666 {Set High Bit if Original >= Ord('a')}
and eax, edx {80h if Orig in 'a'..'z' else 00h}
shr eax, 2 {80h > 20h ('a'-'A')}
sub ecx, eax {Clear Bit 5 if Original in 'a'..'z'}
mov [edi+ebx-4], ecx
sub ebx, 1
and ebx, -4
jmp @@CheckDone
@@Null:
pop esi
pop edi
pop ebx
jmp System.@LStrClr
@@Loop: {Loop converting 4 Character per Loop}
mov eax, [esi+ebx]
mov ecx, eax {4 Original Bytes}
or eax, $80808080 {Set High Bit of each Byte}
mov edx, eax {Comments Below apply to each Byte...}
sub eax, $7B7B7B7B {Set High Bit if Original <= Ord('z')}
xor edx, ecx {80h if Original < 128 else 00h}
or eax, $80808080 {Set High Bit}
sub eax, $66666666 {Set High Bit if Original >= Ord('a')}
and eax, edx {80h if Orig in 'a'..'z' else 00h}
shr eax, 2 {80h > 20h ('a'-'A')}
sub ecx, eax {Clear Bit 5 if Original in 'a'..'z'}
mov [edi+ebx], ecx
@@CheckDone:
sub ebx, 4
jnc @@Loop
pop esi
pop edi
pop ebx
end;
procedure InitializeLookUpTable;
var
I : Byte;
S1, S2 : AnsiString;
begin
SetLength(LookUpTable, 256);
for I := 0 to 255 do
begin
S1 := Char(I);
S2 := UpperCase(S1);
LookUpTable[I] := S2[1];
end;
end;
function UpperCase_DKC_Pas_32(const S: string): string;
var
Max, CharNo : Cardinal;
pResult : PChar;
begin
Max := Length(S);
SetLength(Result, Max);
if Max > 0 then
begin
pResult := PChar(Result);
CharNo := 0;
repeat
pResult[CharNo] := LookUpTable[Ord(S[CharNo+1])];
Inc(CharNo);
if CharNo >= Max then
Break;
pResult[CharNo] := LookUpTable[Ord(S[CharNo+1])];
Inc(CharNo);
if CharNo >= Max then
Break;
pResult[CharNo] := LookUpTable[Ord(S[CharNo+1])];
Inc(CharNo);
if CharNo >= Max then
Break;
pResult[CharNo] := LookUpTable[Ord(S[CharNo+1])];
Inc(CharNo);
until(CharNo >= Max);
end;
end;
procedure UpperCaseStub;
asm
call SysUtils.UpperCase;
end;
initialization
InitializeLookUpTable;
end.