OSDN Git Service

fd9b09c4812e2676328663e4461a241b98c56e5b
[uclinux-h8/uClibc.git] / libc / string / x86_64 / strcspn.S
1 /* strcspn (str, ss) -- Return the length of the initial segment of STR
2                         which contains no characters from SS.
3    For AMD x86-64.
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>.
10
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.
15
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.
20
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
24    02111-1307 USA.  */
25
26 #include "_glibc_inc.h"
27
28 /* Seems to be unrolled too much */
29
30 /* BEWARE: `#ifdef strcspn' means that strcspn is redefined as `strpbrk' */
31 #define STRPBRK_P (defined strcspn)
32
33         .text
34 ENTRY (strcspn)
35
36         movq %rdi, %rdx         /* Save SRC.  */
37
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
42            table.  */
43         movq %rdi, %r8                  /* Save value.  */
44         subq $256, %rsp                 /* Make space for 256 bytes.  */
45         movl $32,  %ecx                 /* 32*8 bytes = 256 bytes.  */
46         movq %rsp, %rdi
47         xorl %eax, %eax                 /* We store 0s.  */
48         cld
49         rep
50         stosq
51
52         movq %rsi, %rax                 /* Setup skipset.  */
53
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.  */
57
58         /* Next 3 insns are 6 bytes total, make sure we decode them in one go */
59         .p2align 3,,6
60
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 */
65
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 */
70
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 */
75
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 */
81
82 L(1):   leaq -4(%rdx), %rax     /* prepare loop */
83
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
87            and
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.  */
92
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: */
98         .p2align 3,,8
99
100 L(3):   addq $4, %rax           /* adjust pointer for full loop round */
101
102         movb (%rax), %cl        /* get byte from string */
103         cmpb %cl, (%rsp,%rcx)   /* is it contained in skipset? */
104         je L(4)                 /* yes => return */
105
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 */
109
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 */
113
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 */
117
118         incq %rax               /* adjust pointer */
119 L(6):   incq %rax
120 L(5):   incq %rax
121
122 L(4):   addq $256, %rsp         /* remove skipset */
123 #if STRPBRK_P
124         xorl %edx,%edx
125         orb %cl, %cl            /* was last character NUL? */
126         cmovzq %rdx, %rax       /* Yes: return NULL */
127 #else   
128         subq %rdx, %rax         /* we have to return the number of valid
129                                    characters, so compute distance to first
130                                    non-valid character */
131 #endif
132         ret
133 END (strcspn)
134
135 #if !STRPBRK_P
136 libc_hidden_def(strcspn)
137 #endif