-
Notifications
You must be signed in to change notification settings - Fork 6
/
strspn32.asm
338 lines (291 loc) · 11.3 KB
/
strspn32.asm
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
;************************* strspn32.asm ************************************
; Author: Agner Fog
; Date created: 2011-07-19
; Last modified: 2011-08-21
; Description:
; Faster version of the standard strspn and strcspn functions:
; size_t A_strspn (const char * str, const char * set);
; size_t A_strcspn(const char * str, const char * set);
;
; A_strspn finds the length of the initial portion of str which consists only of
; characters that are part of set.
; A_strcspn finds the length of the initial portion of str which consists only of
; characters that are not part of set.
;
; Note that these functions may read up to 15 bytes beyond the end of the strings.
; This is rarely a problem but it can in principle generate a protection violation
; if a string is placed at the end of the data segment.
;
; Overriding standard functions strspn and strcspn:
; Overriding is disabled because the functions may read beyond the end of a string,
; while the standard strspn and strcspn functions are guaranteed to work in all cases.
;
; Position-independent code is generated if POSITIONINDEPENDENT is defined.
;
; CPU dispatching included for 386 and SSE4.2 instruction sets.
;
; Copyright (c) 2011 GNU General Public License www.gnu.org/licenses
;******************************************************************************
%define ALLOW_OVERRIDE 0 ; Set to one if override of standard function desired
global _A_strspn: function
global _A_strcspn: function
; Direct entries to CPU-specific versions
global _strspnGeneric: function
global _strcspnGeneric: function
global _strspnSSE42: function
global _strcspnSSE42: function
; Imported from instrset32.asm:
extern _InstructionSet ; Instruction set for CPU dispatcher
section .text
;******************************************************************************
; strspn function
;******************************************************************************
%if ALLOW_OVERRIDE
global ?OVR_strspn: function
?OVR_strspn:
%endif
_A_strspn: ; function dispatching
%IFNDEF POSITIONINDEPENDENT
jmp near [strspnDispatch] ; Go to appropriate version, depending on instruction set
%ELSE ; Position-independent code
call get_thunk_edx ; get reference point for position-independent code
RP1: ; reference point edx = offset RP1
; Make the following instruction with address relative to RP1:
jmp near [edx+strspnDispatch-RP1]
%ENDIF
align 16
_strspnSSE42: ; SSE4.2 version
push esi
push edi
mov esi, [esp+12] ; str
mov edi, [esp+16] ; set
xor ecx, ecx ; span counter
str_next:
movdqu xmm2, [esi] ; str
movdqu xmm1, [edi] ; set
pcmpistrm xmm1, xmm2, 00000000b; find in set, return bit mask in xmm0
movd eax, xmm0
jns set_extends
set_finished:
cmp ax, -1
jne str_finished
; first 16 characters matched, continue with next 16 characters (a terminating zero would never match)
add esi, 16 ; next 16 bytes of str
add ecx, 16 ; count span
jmp str_next
str_finished:
not eax
bsf eax, eax
add eax, ecx
pop edi
pop esi
ret
set_loop:
or eax, edx ; accumulate matches
set_extends: ; the set is more than 16 bytes
add edi, 16
movdqu xmm1, [edi] ; next part of set
pcmpistrm xmm1, xmm2, 00000000b; find in set, return bit mask in xmm0
movd edx, xmm0
jns set_loop
mov edi, [esp+16] ; restore set pointer
or eax, edx ; accumulate matches
jmp set_finished
;******************************************************************************
; strcspn function
;******************************************************************************
%if ALLOW_OVERRIDE
global ?OVR_strcspn: function
?OVR_strcspn:
%endif
_A_strcspn: ; function dispatching
%IFNDEF POSITIONINDEPENDENT
jmp near [strcspnDispatch] ; Go to appropriate version, depending on instruction set
%ELSE ; Position-independent code
call get_thunk_edx ; get reference point for position-independent code
RP2: ; reference point edx = offset RP2
; Make the following instruction with address relative to RP2:
jmp near [edx+strcspnDispatch-RP2]
%ENDIF
align 16
_strcspnSSE42: ; SSE4.2 version
push esi
push edi
mov esi, [esp+12] ; str
mov edi, [esp+16] ; set
xor ecx, ecx ; span counter
str_next2:
movdqu xmm2, [esi] ; str
movdqu xmm1, [edi] ; set
pcmpistrm xmm1, xmm2, 00110000b; find in set, invert valid bits, return bit mask in xmm0
movd eax, xmm0
jns set_extends2
set_finished2:
cmp ax, -1
jne str_finished2
; first 16 characters matched, continue with next 16 characters (a terminating zero would never match)
add esi, 16 ; next 16 bytes of str
add ecx, 16 ; count span
jmp str_next2
str_finished2:
not eax
bsf eax, eax
add eax, ecx
pop edi
pop esi
ret
set_loop2:
and eax, edx ; accumulate matches
set_extends2: ; the set is more than 16 bytes
add edi, 16
movdqu xmm1, [edi] ; next part of set
pcmpistrm xmm1, xmm2, 00110000b; find in set, invert valid bits, return bit mask in xmm0
movd edx, xmm0
jns set_loop2
mov edi, [esp+16] ; restore set pointer
and eax, edx ; accumulate matches
jmp set_finished2
;******************************************************************************
; strspn function generic
;******************************************************************************
align 8
_strspnGeneric: ; Generic version
push esi
push edi
mov esi, [esp+12] ; str pointer
str_next10:
mov edi, [esp+16] ; set pointer
mov al, [esi] ; read one byte from str
test al, al
jz str_finished10 ; str finished
set_next10:
mov dl, [edi]
test dl, dl
jz set_finished10
inc edi
cmp al, dl
jne set_next10
; character match found, goto next character
inc esi
jmp str_next10
str_finished10: ; end of str, all match
set_finished10: ; end of set, mismatch found
sub esi, [esp+12] ; calculate position
mov eax, esi
pop edi
pop esi
ret
;_strspnGeneric end
align 8
_strcspnGeneric: ; Generic version
push esi
push edi
mov esi, [esp+12] ; str pointer
str_next20:
mov edi, [esp+16] ; set pointer
mov al, [esi] ; read one byte from str
test al, al
jz str_finished20 ; str finished
set_next20:
mov dl, [edi]
test dl, dl
jz set_finished20
inc edi
cmp al, dl
jne set_next20
; character match found, stop search
jmp str_finished20
set_finished20: ; end of set, mismatch found
inc esi
jmp str_next20
str_finished20: ; end of str, all match
sub esi, [esp+12] ; calculate position
mov eax, esi
pop edi
pop esi
ret
;_strcspnGeneric end
; ********************************************************************************
%IFDEF POSITIONINDEPENDENT
get_thunk_edx: ; load caller address into edx for position-independent code
mov edx, [esp]
ret
%ENDIF
; ********************************************************************************
; CPU dispatching for strspn. This is executed only once
; ********************************************************************************
strspnCPUDispatch:
%IFNDEF POSITIONINDEPENDENT
; get supported instruction set
call _InstructionSet
; Point to generic version of strstr
mov ecx, _strspnGeneric
cmp eax, 10 ; check SSE4.2
jb Q100
; SSE4.2 supported
; Point to SSE4.2 version of strstr
mov ecx, _strspnSSE42
Q100: mov [strspnDispatch], ecx
; Continue in appropriate version
jmp ecx
%ELSE ; Position-independent version
; get supported instruction set
call _InstructionSet
call get_thunk_edx
RP11: ; reference point edx
; Point to generic version
lea ecx, [edx+_strspnGeneric-RP11]
cmp eax, 10 ; check SSE4.2
jb Q100
; SSE4.2 supported
; Point to SSE4.2 version of strstr
lea ecx, [edx+_strspnSSE42-RP11]
Q100: mov [edx+strspnDispatch-RP11], ecx
; Continue in appropriate version
jmp ecx
%ENDIF
strcspnCPUDispatch:
%IFNDEF POSITIONINDEPENDENT
; get supported instruction set
call _InstructionSet
; Point to generic version of strstr
mov ecx, _strcspnGeneric
cmp eax, 10 ; check SSE4.2
jb Q200
; SSE4.2 supported
; Point to SSE4.2 version of strstr
mov ecx, _strcspnSSE42
Q200: mov [strcspnDispatch], ecx
; Continue in appropriate version
jmp ecx
%ELSE ; Position-independent version
; get supported instruction set
call _InstructionSet
call get_thunk_edx
RP12: ; reference point edx
; Point to generic version
lea ecx, [edx+_strcspnGeneric-RP12]
cmp eax, 10 ; check SSE4.2
jb Q200
; SSE4.2 supported
; Point to SSE4.2 version of strstr
lea ecx, [edx+_strcspnSSE42-RP12]
Q200: mov [edx+strcspnDispatch-RP12], ecx
; Continue in appropriate version
jmp ecx
%ENDIF
SECTION .data
; Pointer to appropriate versions. Initially point to dispatcher
strspnDispatch DD strspnCPUDispatch
strcspnDispatch DD strcspnCPUDispatch
%IFDEF POSITIONINDEPENDENT
; Fix problem in Mac linker
DD 0,0,0,0
%ENDIF
SECTION .bss
; Append 16 bytes to end of last data section to allow reading past end of strings:
; (We might use names .bss$zzz etc. under Windows to make it is placed
; last, but the assembler gives sections with unknown names wrong attributes.
; Here, we are just relying on library data being placed after main data.
; This can be verified by making a link map file)
dq 0, 0