2.9
[glibc/nacl-glibc.git] / sysdeps / x86_64 / strspn.S
blob54aac18d619f1443ee5eb44c670753bbdd486b99
1 /* strspn (str, ss) -- Return the length of the initial segment of STR
2                         which contains only 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>
28         .text
29 ENTRY (strspn)
31         movq %rdi, %rdx         /* Save SRC.  */
33         /* First we create a table with flags for all possible characters.
34            For the ASCII (7bit/8bit) or ISO-8859-X character sets which are
35            supported by the C string functions we have 256 characters.
36            Before inserting marks for the stop characters we clear the whole
37            table.  */
38         movq %rdi, %r8                  /* Save value.  */
39         subq $256, %rsp                 /* Make space for 256 bytes.  */
40         cfi_adjust_cfa_offset(256)
41         movl $32,  %ecx                 /* 32*8 bytes = 256 bytes.  */
42         movq %rsp, %rdi
43         xorl %eax, %eax                 /* We store 0s.  */
44         cld
45         rep
46         stosq
48         movq %rsi, %rax                 /* Setup stopset.  */
50 /* For understanding the following code remember that %rcx == 0 now.
51    Although all the following instruction only modify %cl we always
52    have a correct zero-extended 64-bit value in %rcx.  */
54         .p2align 4
55 L(2):   movb (%rax), %cl        /* get byte from stopset */
56         testb %cl, %cl          /* is NUL char? */
57         jz L(1)                 /* yes => start compare loop */
58         movb %cl, (%rsp,%rcx)   /* set corresponding byte in stopset table */
60         movb 1(%rax), %cl       /* get byte from stopset */
61         testb $0xff, %cl        /* is NUL char? */
62         jz L(1)                 /* yes => start compare loop */
63         movb %cl, (%rsp,%rcx)   /* set corresponding byte in stopset table */
65         movb 2(%rax), %cl       /* get byte from stopset */
66         testb $0xff, %cl        /* is NUL char? */
67         jz L(1)                 /* yes => start compare loop */
68         movb %cl, (%rsp,%rcx)   /* set corresponding byte in stopset table */
70         movb 3(%rax), %cl       /* get byte from stopset */
71         addq $4, %rax           /* increment stopset pointer */
72         movb %cl, (%rsp,%rcx)   /* set corresponding byte in stopset table */
73         testb $0xff, %cl        /* is NUL char? */
74         jnz L(2)                /* no => process next dword from stopset */
76 L(1):   leaq -4(%rdx), %rax     /* prepare loop */
78         /* We use a neat trick for the following loop.  Normally we would
79            have to test for two termination conditions
80            1. a character in the stopset was found
81            and
82            2. the end of the string was found
83            But as a sign that the character is in the stopset we store its
84            value in the table.  But the value of NUL is NUL so the loop
85            terminates for NUL in every case.  */
87         .p2align 4
88 L(3):   addq $4, %rax           /* adjust pointer for full loop round */
90         movb (%rax), %cl        /* get byte from string */
91         testb %cl, (%rsp,%rcx)  /* is it contained in skipset? */
92         jz L(4)                 /* no => return */
94         movb 1(%rax), %cl       /* get byte from string */
95         testb %cl, (%rsp,%rcx)  /* is it contained in skipset? */
96         jz L(5)                 /* no => return */
98         movb 2(%rax), %cl       /* get byte from string */
99         testb %cl, (%rsp,%rcx)  /* is it contained in skipset? */
100         jz L(6)                 /* no => return */
102         movb 3(%rax), %cl       /* get byte from string */
103         testb %cl, (%rsp,%rcx)  /* is it contained in skipset? */
104         jnz L(3)                /* yes => start loop again */
106         incq %rax               /* adjust pointer */
107 L(6):   incq %rax
108 L(5):   incq %rax
110 L(4):   addq $256, %rsp         /* remove stopset */
111         cfi_adjust_cfa_offset(-256)
112         subq %rdx, %rax         /* we have to return the number of valid
113                                    characters, so compute distance to first
114                                    non-valid character */
115         ret
116 END (strspn)
117 libc_hidden_builtin_def (strspn)