2.9
[glibc/nacl-glibc.git] / sysdeps / x86_64 / strcspn.S
blob467201388ba1da31081f6a2353467cd6532ba6e1
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>.
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
24    02111-1307 USA.  */
26 #include <sysdep.h>
27 #include "asm-syntax.h"
29 /* BEWARE: `#ifdef strcspn' means that strcspn is redefined as `strpbrk' */
30 #define STRPBRK_P (defined strcspn)
32         .text
33 ENTRY (strcspn)
35         movq %rdi, %rdx         /* Save SRC.  */
37         /* First we create a table with flags for all possible characters.
38            For the ASCII (7bit/8bit) or ISO-8859-X character sets which are
39            supported by the C string functions we have 256 characters.
40            Before inserting marks for the stop characters we clear the whole
41            table.  */
42         movq %rdi, %r8                  /* Save value.  */
43         subq $256, %rsp                 /* Make space for 256 bytes.  */
44         cfi_adjust_cfa_offset(256)
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
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         .p2align 4
59 L(2):   movb (%rax), %cl        /* get byte from skipset */
60         testb %cl, %cl          /* is NUL char? */
61         jz L(1)                 /* yes => start compare loop */
62         movb %cl, (%rsp,%rcx)   /* set corresponding byte in skipset table */
64         movb 1(%rax), %cl       /* get byte from skipset */
65         testb $0xff, %cl        /* is NUL char? */
66         jz L(1)                 /* yes => start compare loop */
67         movb %cl, (%rsp,%rcx)   /* set corresponding byte in skipset table */
69         movb 2(%rax), %cl       /* get byte from skipset */
70         testb $0xff, %cl        /* is NUL char? */
71         jz L(1)                 /* yes => start compare loop */
72         movb %cl, (%rsp,%rcx)   /* set corresponding byte in skipset table */
74         movb 3(%rax), %cl       /* get byte from skipset */
75         addq $4, %rax           /* increment skipset pointer */
76         movb %cl, (%rsp,%rcx)   /* set corresponding byte in skipset table */
77         testb $0xff, %cl        /* is NUL char? */
78         jnz L(2)                /* no => process next dword from skipset */
80 L(1):   leaq -4(%rdx), %rax     /* prepare loop */
82         /* We use a neat trick for the following loop.  Normally we would
83            have to test for two termination conditions
84            1. a character in the skipset was found
85            and
86            2. the end of the string was found
87            But as a sign that the character is in the skipset we store its
88            value in the table.  But the value of NUL is NUL so the loop
89            terminates for NUL in every case.  */
91         .p2align 4
92 L(3):   addq $4, %rax           /* adjust pointer for full loop round */
94         movb (%rax), %cl        /* get byte from string */
95         cmpb %cl, (%rsp,%rcx)   /* is it contained in skipset? */
96         je L(4)                 /* yes => return */
98         movb 1(%rax), %cl       /* get byte from string */
99         cmpb %cl, (%rsp,%rcx)   /* is it contained in skipset? */
100         je L(5)                 /* yes => return */
102         movb 2(%rax), %cl       /* get byte from string */
103         cmpb %cl, (%rsp,%rcx)   /* is it contained in skipset? */
104         jz L(6)                 /* yes => return */
106         movb 3(%rax), %cl       /* get byte from string */
107         cmpb %cl, (%rsp,%rcx)   /* is it contained in skipset? */
108         jne L(3)                /* no => start loop again */
110         incq %rax               /* adjust pointer */
111 L(6):   incq %rax
112 L(5):   incq %rax
114 L(4):   addq $256, %rsp         /* remove skipset */
115         cfi_adjust_cfa_offset(-256)
116 #if STRPBRK_P
117         xorl %edx,%edx
118         orb %cl, %cl            /* was last character NUL? */
119         cmovzq %rdx, %rax       /* Yes: return NULL */
120 #else   
121         subq %rdx, %rax         /* we have to return the number of valid
122                                    characters, so compute distance to first
123                                    non-valid character */
124 #endif
125         ret
126 END (strcspn)
127 libc_hidden_builtin_def (strcspn)