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-2016 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>.
9
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.
14
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.
19
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/>. */
23
24#include <sysdep.h>
25#include "asm-syntax.h"
26
27/* BEWARE: `#ifdef strcspn' means that strcspn is redefined as `strpbrk' */
28#define STRPBRK_P (defined strcspn)
29
30 .text
31ENTRY (strcspn)
32
33 movq %rdi, %rdx /* Save SRC. */
34
35 /* First we create a table with flags for all possible characters.
36 For the ASCII (7bit/8bit) or ISO-8859-X character sets which are
37 supported by the C string functions we have 256 characters.
38 Before inserting marks for the stop characters we clear the whole
39 table. */
40 movq %rdi, %r8 /* Save value. */
41 subq $256, %rsp /* Make space for 256 bytes. */
42 cfi_adjust_cfa_offset(256)
43 movl $32, %ecx /* 32*8 bytes = 256 bytes. */
44 movq %rsp, %rdi
45 xorl %eax, %eax /* We store 0s. */
46 cld
47 rep
48 stosq
49
50 movq %rsi, %rax /* Setup skipset. */
51
52/* For understanding the following code remember that %rcx == 0 now.
53 Although all the following instruction only modify %cl we always
54 have a correct zero-extended 64-bit value in %rcx. */
55
56 .p2align 4
57L(2): movb (%rax), %cl /* get byte from skipset */
58 testb %cl, %cl /* is NUL char? */
59 jz L(1) /* yes => start compare loop */
60 movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
61
62 movb 1(%rax), %cl /* get byte from skipset */
63 testb $0xff, %cl /* is NUL char? */
64 jz L(1) /* yes => start compare loop */
65 movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
66
67 movb 2(%rax), %cl /* get byte from skipset */
68 testb $0xff, %cl /* is NUL char? */
69 jz L(1) /* yes => start compare loop */
70 movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
71
72 movb 3(%rax), %cl /* get byte from skipset */
73 addq $4, %rax /* increment skipset pointer */
74 movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
75 testb $0xff, %cl /* is NUL char? */
76 jnz L(2) /* no => process next dword from skipset */
77
78L(1): leaq -4(%rdx), %rax /* prepare loop */
79
80 /* We use a neat trick for the following loop. Normally we would
81 have to test for two termination conditions
82 1. a character in the skipset was found
83 and
84 2. the end of the string was found
85 But as a sign that the character is in the skipset we store its
86 value in the table. But the value of NUL is NUL so the loop
87 terminates for NUL in every case. */
88
89 .p2align 4
90L(3): addq $4, %rax /* adjust pointer for full loop round */
91
92 movb (%rax), %cl /* get byte from string */
93 cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
94 je L(4) /* yes => return */
95
96 movb 1(%rax), %cl /* get byte from string */
97 cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
98 je L(5) /* yes => return */
99
100 movb 2(%rax), %cl /* get byte from string */
101 cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
102 jz L(6) /* yes => return */
103
104 movb 3(%rax), %cl /* get byte from string */
105 cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
106 jne L(3) /* no => start loop again */
107
108 incq %rax /* adjust pointer */
109L(6): incq %rax
110L(5): incq %rax
111
112L(4): addq $256, %rsp /* remove skipset */
113 cfi_adjust_cfa_offset(-256)
114#if STRPBRK_P
115 xorl %edx,%edx
116 orb %cl, %cl /* was last character NUL? */
117 cmovzq %rdx, %rax /* Yes: return NULL */
118#else
119 subq %rdx, %rax /* we have to return the number of valid
120 characters, so compute distance to first
121 non-valid character */
122#endif
123 ret
124END (strcspn)
125libc_hidden_builtin_def (strcspn)
126