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, write to the Free
21 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include "asm-syntax.h"
29 #define PARMS LINKAGE /* no space for saved regs */
31 #define STOP STR+PTR_SIZE
34 ENTRY (BP_SYM (strcspn))
39 CHECK_BOUNDS_LOW (%edx, STR(%esp))
41 /* First we create a table with flags for all possible characters.
42 For the ASCII (7bit/8bit) or ISO-8859-X character sets which are
43 supported by the C string functions we have 256 characters.
44 Before inserting marks for the stop characters we clear the whole
45 table. The unrolled form is much faster than a loop. */
46 xorl %ecx, %ecx /* %ecx = 0 !!! */
48 pushl %ecx /* make a 256 bytes long block filled with 0 */
49 cfi_adjust_cfa_offset (4)
51 cfi_adjust_cfa_offset (4)
53 cfi_adjust_cfa_offset (4)
55 cfi_adjust_cfa_offset (4)
57 cfi_adjust_cfa_offset (4)
59 cfi_adjust_cfa_offset (4)
61 cfi_adjust_cfa_offset (4)
63 cfi_adjust_cfa_offset (4)
65 cfi_adjust_cfa_offset (4)
67 cfi_adjust_cfa_offset (4)
69 cfi_adjust_cfa_offset (4)
71 cfi_adjust_cfa_offset (4)
73 cfi_adjust_cfa_offset (4)
75 cfi_adjust_cfa_offset (4)
77 cfi_adjust_cfa_offset (4)
79 cfi_adjust_cfa_offset (4)
81 cfi_adjust_cfa_offset (4)
83 cfi_adjust_cfa_offset (4)
85 cfi_adjust_cfa_offset (4)
87 cfi_adjust_cfa_offset (4)
89 cfi_adjust_cfa_offset (4)
91 cfi_adjust_cfa_offset (4)
93 cfi_adjust_cfa_offset (4)
95 cfi_adjust_cfa_offset (4)
97 cfi_adjust_cfa_offset (4)
99 cfi_adjust_cfa_offset (4)
101 cfi_adjust_cfa_offset (4)
103 cfi_adjust_cfa_offset (4)
105 cfi_adjust_cfa_offset (4)
107 cfi_adjust_cfa_offset (4)
109 cfi_adjust_cfa_offset (4)
111 cfi_adjust_cfa_offset (4)
113 cfi_adjust_cfa_offset (4)
115 cfi_adjust_cfa_offset (4)
117 cfi_adjust_cfa_offset (4)
119 cfi_adjust_cfa_offset (4)
121 cfi_adjust_cfa_offset (4)
123 cfi_adjust_cfa_offset (4)
125 cfi_adjust_cfa_offset (4)
127 cfi_adjust_cfa_offset (4)
129 cfi_adjust_cfa_offset (4)
131 cfi_adjust_cfa_offset (4)
133 cfi_adjust_cfa_offset (4)
135 cfi_adjust_cfa_offset (4)
137 cfi_adjust_cfa_offset (4)
139 cfi_adjust_cfa_offset (4)
141 cfi_adjust_cfa_offset (4)
143 cfi_adjust_cfa_offset (4)
145 cfi_adjust_cfa_offset (4)
147 cfi_adjust_cfa_offset (4)
149 cfi_adjust_cfa_offset (4)
151 cfi_adjust_cfa_offset (4)
153 cfi_adjust_cfa_offset (4)
155 cfi_adjust_cfa_offset (4)
157 cfi_adjust_cfa_offset (4)
159 cfi_adjust_cfa_offset (4)
161 cfi_adjust_cfa_offset (4)
163 cfi_adjust_cfa_offset (4)
164 pushl $0 /* These immediate values make the label 2 */
165 cfi_adjust_cfa_offset (4)
166 pushl $0 /* to be aligned on a 16 byte boundary to */
167 cfi_adjust_cfa_offset (4)
168 pushl $0 /* get a better performance of the loop. */
169 cfi_adjust_cfa_offset (4)
171 cfi_adjust_cfa_offset (4)
173 cfi_adjust_cfa_offset (4)
175 cfi_adjust_cfa_offset (4)
177 /* For understanding the following code remember that %ecx == 0 now.
178 Although all the following instruction only modify %cl we always
179 have a correct zero-extended 32-bit value in %ecx. */
181 /* Don't change the "testb $0xff,%%cl" to "testb %%cl,%%cl". We want
182 longer instructions so that the next loop aligns without adding nops. */
184 L(2): movb (%eax), %cl /* get byte from stopset */
185 testb %cl, %cl /* is NUL char? */
186 jz L(1) /* yes => start compare loop */
187 movb %cl, (%esp,%ecx) /* set corresponding byte in stopset table */
189 movb 1(%eax), %cl /* get byte from stopset */
190 testb $0xff, %cl /* is NUL char? */
191 jz L(1) /* yes => start compare loop */
192 movb %cl, (%esp,%ecx) /* set corresponding byte in stopset table */
194 movb 2(%eax), %cl /* get byte from stopset */
195 testb $0xff, %cl /* is NUL char? */
196 jz L(1) /* yes => start compare loop */
197 movb %cl, (%esp,%ecx) /* set corresponding byte in stopset table */
199 movb 3(%eax), %cl /* get byte from stopset */
200 addl $4, %eax /* increment stopset pointer */
201 movb %cl, (%esp,%ecx) /* set corresponding byte in stopset table */
202 testb $0xff, %cl /* is NUL char? */
203 jnz L(2) /* no => process next dword from stopset */
205 L(1): leal -4(%edx), %eax /* prepare loop */
207 /* We use a neat trick for the following loop. Normally we would
208 have to test for two termination conditions
209 1. a character in the stopset was found
211 2. the end of the string was found
212 But as a sign that the character is in the stopset we store its
213 value in the table. But the value of NUL is NUL so the loop
214 terminates for NUL in every case. */
216 L(3): addl $4, %eax /* adjust pointer for full loop round */
218 movb (%eax), %cl /* get byte from string */
219 cmpb %cl, (%esp,%ecx) /* is it contained in stopset? */
220 je L(4) /* yes => return */
222 movb 1(%eax), %cl /* get byte from string */
223 cmpb %cl, (%esp,%ecx) /* is it contained in stopset? */
224 je L(5) /* yes => return */
226 movb 2(%eax), %cl /* get byte from string */
227 cmpb %cl, (%esp,%ecx) /* is it contained in stopset? */
228 je L(6) /* yes => return */
230 movb 3(%eax), %cl /* get byte from string */
231 cmpb %cl, (%esp,%ecx) /* is it contained in stopset? */
232 jne L(3) /* yes => return */
234 incl %eax /* adjust pointer */
238 L(4): addl $256, %esp /* remove stopset */
239 cfi_adjust_cfa_offset (-256)
240 CHECK_BOUNDS_HIGH (%eax, STR(%esp), jb)
241 subl %edx, %eax /* we have to return the number of valid
242 characters, so compute distance to first
243 non-valid character */
246 END (BP_SYM (strcspn))
247 libc_hidden_builtin_def (strcspn)