1 /* strcspn (str, ss) -- Return the length of the initial segment of STR
2 which contains no characters from SS.
4 Copyright (C) 1994-1997,2000,2003,2005 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
6 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>
7 Bug fixes by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
9 The GNU C Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 The GNU C Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with the GNU C Library; if not, see
21 <http://www.gnu.org/licenses/>. */
24 #include "asm-syntax.h"
28 #define PARMS LINKAGE /* no space for saved regs */
30 #define STOP STR+PTR_SIZE
33 ENTRY (BP_SYM (strcspn))
38 CHECK_BOUNDS_LOW (%edx, STR(%esp))
40 /* First we create a table with flags for all possible characters.
41 For the ASCII (7bit/8bit) or ISO-8859-X character sets which are
42 supported by the C string functions we have 256 characters.
43 Before inserting marks for the stop characters we clear the whole
44 table. The unrolled form is much faster than a loop. */
45 xorl %ecx, %ecx /* %ecx = 0 !!! */
47 pushl %ecx /* make a 256 bytes long block filled with 0 */
48 cfi_adjust_cfa_offset (4)
50 cfi_adjust_cfa_offset (4)
52 cfi_adjust_cfa_offset (4)
54 cfi_adjust_cfa_offset (4)
56 cfi_adjust_cfa_offset (4)
58 cfi_adjust_cfa_offset (4)
60 cfi_adjust_cfa_offset (4)
62 cfi_adjust_cfa_offset (4)
64 cfi_adjust_cfa_offset (4)
66 cfi_adjust_cfa_offset (4)
68 cfi_adjust_cfa_offset (4)
70 cfi_adjust_cfa_offset (4)
72 cfi_adjust_cfa_offset (4)
74 cfi_adjust_cfa_offset (4)
76 cfi_adjust_cfa_offset (4)
78 cfi_adjust_cfa_offset (4)
80 cfi_adjust_cfa_offset (4)
82 cfi_adjust_cfa_offset (4)
84 cfi_adjust_cfa_offset (4)
86 cfi_adjust_cfa_offset (4)
88 cfi_adjust_cfa_offset (4)
90 cfi_adjust_cfa_offset (4)
92 cfi_adjust_cfa_offset (4)
94 cfi_adjust_cfa_offset (4)
96 cfi_adjust_cfa_offset (4)
98 cfi_adjust_cfa_offset (4)
100 cfi_adjust_cfa_offset (4)
102 cfi_adjust_cfa_offset (4)
104 cfi_adjust_cfa_offset (4)
106 cfi_adjust_cfa_offset (4)
108 cfi_adjust_cfa_offset (4)
110 cfi_adjust_cfa_offset (4)
112 cfi_adjust_cfa_offset (4)
114 cfi_adjust_cfa_offset (4)
116 cfi_adjust_cfa_offset (4)
118 cfi_adjust_cfa_offset (4)
120 cfi_adjust_cfa_offset (4)
122 cfi_adjust_cfa_offset (4)
124 cfi_adjust_cfa_offset (4)
126 cfi_adjust_cfa_offset (4)
128 cfi_adjust_cfa_offset (4)
130 cfi_adjust_cfa_offset (4)
132 cfi_adjust_cfa_offset (4)
134 cfi_adjust_cfa_offset (4)
136 cfi_adjust_cfa_offset (4)
138 cfi_adjust_cfa_offset (4)
140 cfi_adjust_cfa_offset (4)
142 cfi_adjust_cfa_offset (4)
144 cfi_adjust_cfa_offset (4)
146 cfi_adjust_cfa_offset (4)
148 cfi_adjust_cfa_offset (4)
150 cfi_adjust_cfa_offset (4)
152 cfi_adjust_cfa_offset (4)
154 cfi_adjust_cfa_offset (4)
156 cfi_adjust_cfa_offset (4)
158 cfi_adjust_cfa_offset (4)
160 cfi_adjust_cfa_offset (4)
162 cfi_adjust_cfa_offset (4)
163 pushl $0 /* These immediate values make the label 2 */
164 cfi_adjust_cfa_offset (4)
165 pushl $0 /* to be aligned on a 16 byte boundary to */
166 cfi_adjust_cfa_offset (4)
167 pushl $0 /* get a better performance of the loop. */
168 cfi_adjust_cfa_offset (4)
170 cfi_adjust_cfa_offset (4)
172 cfi_adjust_cfa_offset (4)
174 cfi_adjust_cfa_offset (4)
176 /* For understanding the following code remember that %ecx == 0 now.
177 Although all the following instruction only modify %cl we always
178 have a correct zero-extended 32-bit value in %ecx. */
180 /* Don't change the "testb $0xff,%%cl" to "testb %%cl,%%cl". We want
181 longer instructions so that the next loop aligns without adding nops. */
183 L(2): movb (%eax), %cl /* get byte from stopset */
184 testb %cl, %cl /* is NUL char? */
185 jz L(1) /* yes => start compare loop */
186 movb %cl, (%esp,%ecx) /* set corresponding byte in stopset table */
188 movb 1(%eax), %cl /* get byte from stopset */
189 testb $0xff, %cl /* is NUL char? */
190 jz L(1) /* yes => start compare loop */
191 movb %cl, (%esp,%ecx) /* set corresponding byte in stopset table */
193 movb 2(%eax), %cl /* get byte from stopset */
194 testb $0xff, %cl /* is NUL char? */
195 jz L(1) /* yes => start compare loop */
196 movb %cl, (%esp,%ecx) /* set corresponding byte in stopset table */
198 movb 3(%eax), %cl /* get byte from stopset */
199 addl $4, %eax /* increment stopset pointer */
200 movb %cl, (%esp,%ecx) /* set corresponding byte in stopset table */
201 testb $0xff, %cl /* is NUL char? */
202 jnz L(2) /* no => process next dword from stopset */
204 L(1): leal -4(%edx), %eax /* prepare loop */
206 /* We use a neat trick for the following loop. Normally we would
207 have to test for two termination conditions
208 1. a character in the stopset was found
210 2. the end of the string was found
211 But as a sign that the character is in the stopset we store its
212 value in the table. But the value of NUL is NUL so the loop
213 terminates for NUL in every case. */
215 L(3): addl $4, %eax /* adjust pointer for full loop round */
217 movb (%eax), %cl /* get byte from string */
218 cmpb %cl, (%esp,%ecx) /* is it contained in stopset? */
219 je L(4) /* yes => return */
221 movb 1(%eax), %cl /* get byte from string */
222 cmpb %cl, (%esp,%ecx) /* is it contained in stopset? */
223 je L(5) /* yes => return */
225 movb 2(%eax), %cl /* get byte from string */
226 cmpb %cl, (%esp,%ecx) /* is it contained in stopset? */
227 je L(6) /* yes => return */
229 movb 3(%eax), %cl /* get byte from string */
230 cmpb %cl, (%esp,%ecx) /* is it contained in stopset? */
231 jne L(3) /* yes => return */
233 incl %eax /* adjust pointer */
237 L(4): addl $256, %esp /* remove stopset */
238 cfi_adjust_cfa_offset (-256)
239 CHECK_BOUNDS_HIGH (%eax, STR(%esp), jb)
240 subl %edx, %eax /* we have to return the number of valid
241 characters, so compute distance to first
242 non-valid character */
245 END (BP_SYM (strcspn))
246 libc_hidden_builtin_def (strcspn)