(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / x86_64 / strcspn.S
blob63af04aeabd73a667a7f64ce94d279f661bf217b
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 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>.
8    Adopted for x86-64 by Andreas Jaeger <aj@suse.de>.
10    The GNU C Library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2.1 of the License, or (at your option) any later version.
15    The GNU C Library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
20    You should have received a copy of the GNU Lesser General Public
21    License along with the GNU C Library; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23    02111-1307 USA.  */
25 #include <sysdep.h>
26 #include "asm-syntax.h"
28 /* BEWARE: `#ifdef strcspn' means that strcspn is redefined as `strpbrk' */
29 #define STRPBRK_P (defined strcspn)
31         .text
32 ENTRY (strcspn)
34         movq %rdi, %rdx         /* Save SRC.  */
36         /* First we create a table with flags for all possible characters.
37            For the ASCII (7bit/8bit) or ISO-8859-X character sets which are
38            supported by the C string functions we have 256 characters.
39            Before inserting marks for the stop characters we clear the whole
40            table.  */
41         movq %rdi, %r8                  /* Save value.  */
42         subq $256, %rsp                 /* Make space for 256 bytes.  */
43         cfi_adjust_cfa_offset(256)
44         movq $32,  %rcx                 /* 32*8 bytes = 256 bytes.  */
45         movq %rsp, %rdi
46         xorq %rax, %rax                 /* We store 0s.  */
47         cld
48         rep
49         stosq
51         movq %rsi, %rax                 /* Setup skipset.  */
53 /* For understanding the following code remember that %rcx == 0 now.
54    Although all the following instruction only modify %cl we always
55    have a correct zero-extended 64-bit value in %rcx.  */
57         .p2align 4
58 L(2):   movb (%rax), %cl        /* get byte from skipset */
59         testb %cl, %cl          /* is NUL char? */
60         jz L(1)                 /* yes => start compare loop */
61         movb %cl, (%rsp,%rcx)   /* set corresponding byte in skipset table */
63         movb 1(%rax), %cl       /* get byte from skipset */
64         testb $0xff, %cl        /* is NUL char? */
65         jz L(1)                 /* yes => start compare loop */
66         movb %cl, (%rsp,%rcx)   /* set corresponding byte in skipset table */
68         movb 2(%rax), %cl       /* get byte from skipset */
69         testb $0xff, %cl        /* is NUL char? */
70         jz L(1)                 /* yes => start compare loop */
71         movb %cl, (%rsp,%rcx)   /* set corresponding byte in skipset table */
73         movb 3(%rax), %cl       /* get byte from skipset */
74         addq $4, %rax           /* increment skipset pointer */
75         movb %cl, (%rsp,%rcx)   /* set corresponding byte in skipset table */
76         testb $0xff, %cl        /* is NUL char? */
77         jnz L(2)                /* no => process next dword from skipset */
79 L(1):   leaq -4(%rdx), %rax     /* prepare loop */
81         /* We use a neat trick for the following loop.  Normally we would
82            have to test for two termination conditions
83            1. a character in the skipset was found
84            and
85            2. the end of the string was found
86            But as a sign that the character is in the skipset we store its
87            value in the table.  But the value of NUL is NUL so the loop
88            terminates for NUL in every case.  */
90         .p2align 4
91 L(3):   addq $4, %rax           /* adjust pointer for full loop round */
93         movb (%rax), %cl        /* get byte from string */
94         cmpb %cl, (%rsp,%rcx)   /* is it contained in skipset? */
95         je L(4)                 /* yes => return */
97         movb 1(%rax), %cl       /* get byte from string */
98         cmpb %cl, (%rsp,%rcx)   /* is it contained in skipset? */
99         je L(5)                 /* yes => return */
101         movb 2(%rax), %cl       /* get byte from string */
102         cmpb %cl, (%rsp,%rcx)   /* is it contained in skipset? */
103         jz L(6)                 /* yes => return */
105         movb 3(%rax), %cl       /* get byte from string */
106         cmpb %cl, (%rsp,%rcx)   /* is it contained in skipset? */
107         jne L(3)                /* no => start loop again */
109         incq %rax               /* adjust pointer */
110 L(6):   incq %rax
111 L(5):   incq %rax
113 L(4):   addq $256, %rsp         /* remove skipset */
114         cfi_adjust_cfa_offset(-256)
115 #if STRPBRK_P
116         xorq %rdx,%rdx
117         orb %cl, %cl            /* was last character NUL? */
118         cmovzq %rdx, %rax       /* Yes: return NULL */
119 #else   
120         subq %rdx, %rax         /* we have to return the number of valid
121                                    characters, so compute distance to first
122                                    non-valid character */
123 #endif
124         ret
125 END (strcspn)
126 libc_hidden_builtin_def (strcspn)