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