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