]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/x86_64/strspn.S
* sysdeps/unix/sysv/linux/x86_64/getcontext.S: Use functionally
[thirdparty/glibc.git] / sysdeps / x86_64 / strspn.S
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>.
10
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.
15
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.
20
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. */
25
26 #include <sysdep.h>
27
28 .text
29 ENTRY (strspn)
30
31 movq %rdi, %rdx /* Save SRC. */
32
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
47
48 movq %rsi, %rax /* Setup stopset. */
49
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. */
53
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 */
59
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 */
64
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 */
69
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 */
75
76 L(1): leaq -4(%rdx), %rax /* prepare loop */
77
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. */
86
87 .p2align 4
88 L(3): addq $4, %rax /* adjust pointer for full loop round */
89
90 movb (%rax), %cl /* get byte from string */
91 testb %cl, (%rsp,%rcx) /* is it contained in skipset? */
92 jz L(4) /* no => return */
93
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 */
97
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 */
101
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 */
105
106 incq %rax /* adjust pointer */
107 L(6): incq %rax
108 L(5): incq %rax
109
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)