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, 2002, 2003, 2004, 2005
5 Free Software Foundation, Inc.
6 This file is part of the GNU C Library.
7 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>.
8 Bug fixes by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>.
9 Adopted for x86-64 by Andreas Jaeger <aj@suse.de>.
11 The GNU C Library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
16 The GNU C Library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with the GNU C Library; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include "_glibc_inc.h"
28 /* Seems to be unrolled too much */
30 /* BEWARE: `#ifdef strcspn' means that strcspn is redefined as `strpbrk' */
31 #define STRPBRK_P (defined strcspn)
36 movq %rdi, %rdx /* Save SRC. */
38 /* First we create a table with flags for all possible characters.
39 For the ASCII (7bit/8bit) or ISO-8859-X character sets which are
40 supported by the C string functions we have 256 characters.
41 Before inserting marks for the stop characters we clear the whole
43 movq %rdi, %r8 /* Save value. */
44 subq $256, %rsp /* Make space for 256 bytes. */
45 movl $32, %ecx /* 32*8 bytes = 256 bytes. */
47 xorl %eax, %eax /* We store 0s. */
52 movq %rsi, %rax /* Setup skipset. */
54 /* For understanding the following code remember that %rcx == 0 now.
55 Although all the following instruction only modify %cl we always
56 have a correct zero-extended 64-bit value in %rcx. */
58 /* Next 3 insns are 6 bytes total, make sure we decode them in one go */
61 L(2): movb (%rax), %cl /* get byte from skipset */
62 testb %cl, %cl /* is NUL char? */
63 jz L(1) /* yes => start compare loop */
64 movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
66 movb 1(%rax), %cl /* get byte from skipset */
67 testb %cl, %cl /* is NUL char? */
68 jz L(1) /* yes => start compare loop */
69 movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
71 movb 2(%rax), %cl /* get byte from skipset */
72 testb %cl, %cl /* is NUL char? */
73 jz L(1) /* yes => start compare loop */
74 movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
76 movb 3(%rax), %cl /* get byte from skipset */
77 addq $4, %rax /* increment skipset pointer */
78 movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
79 testb %cl, %cl /* is NUL char? */
80 jnz L(2) /* no => process next dword from skipset */
82 L(1): leaq -4(%rdx), %rax /* prepare loop */
84 /* We use a neat trick for the following loop. Normally we would
85 have to test for two termination conditions
86 1. a character in the skipset was found
88 2. the end of the string was found
89 But as a sign that the character is in the skipset we store its
90 value in the table. But the value of NUL is NUL so the loop
91 terminates for NUL in every case. */
93 /* Next 3 insns are 9 bytes total. */
94 /* .p2align 4,,9 would make sure we decode them in one go, */
95 /* but it will also align entire function to 16 bytes, */
96 /* potentially creating largish padding at link time. */
97 /* We are aligning to 8 bytes instead: */
100 L(3): addq $4, %rax /* adjust pointer for full loop round */
102 movb (%rax), %cl /* get byte from string */
103 cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
104 je L(4) /* yes => return */
106 movb 1(%rax), %cl /* get byte from string */
107 cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
108 je L(5) /* yes => return */
110 movb 2(%rax), %cl /* get byte from string */
111 cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
112 jz L(6) /* yes => return */
114 movb 3(%rax), %cl /* get byte from string */
115 cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
116 jne L(3) /* no => start loop again */
118 incq %rax /* adjust pointer */
122 L(4): addq $256, %rsp /* remove skipset */
125 orb %cl, %cl /* was last character NUL? */
126 cmovzq %rdx, %rax /* Yes: return NULL */
128 subq %rdx, %rax /* we have to return the number of valid
129 characters, so compute distance to first
130 non-valid character */
136 libc_hidden_def(strcspn)