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-2022 Free Software Foundation, Inc. |
5 | This file is part of the GNU C Library. |
6 | |
7 | The GNU C Library is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Lesser General Public |
9 | License as published by the Free Software Foundation; either |
10 | version 2.1 of the License, or (at your option) any later version. |
11 | |
12 | The GNU C Library is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | Lesser General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Lesser General Public |
18 | License along with the GNU C Library; if not, see |
19 | <https://www.gnu.org/licenses/>. */ |
20 | |
21 | #include <sysdep.h> |
22 | #include "asm-syntax.h" |
23 | |
24 | .text |
25 | ENTRY (strcspn) |
26 | |
27 | movq %rdi, %rdx /* Save SRC. */ |
28 | |
29 | /* First we create a table with flags for all possible characters. |
30 | For the ASCII (7bit/8bit) or ISO-8859-X character sets which are |
31 | supported by the C string functions we have 256 characters. |
32 | Before inserting marks for the stop characters we clear the whole |
33 | table. */ |
34 | movq %rdi, %r8 /* Save value. */ |
35 | subq $256, %rsp /* Make space for 256 bytes. */ |
36 | cfi_adjust_cfa_offset(256) |
37 | movl $32, %ecx /* 32*8 bytes = 256 bytes. */ |
38 | movq %rsp, %rdi |
39 | xorl %eax, %eax /* We store 0s. */ |
40 | cld |
41 | rep |
42 | stosq |
43 | |
44 | movq %rsi, %rax /* Setup skipset. */ |
45 | |
46 | /* For understanding the following code remember that %rcx == 0 now. |
47 | Although all the following instruction only modify %cl we always |
48 | have a correct zero-extended 64-bit value in %rcx. */ |
49 | |
50 | .p2align 4 |
51 | L(2): movb (%rax), %cl /* get byte from skipset */ |
52 | testb %cl, %cl /* is NUL char? */ |
53 | jz L(1) /* yes => start compare loop */ |
54 | movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */ |
55 | |
56 | movb 1(%rax), %cl /* get byte from skipset */ |
57 | testb $0xff, %cl /* is NUL char? */ |
58 | jz L(1) /* yes => start compare loop */ |
59 | movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */ |
60 | |
61 | movb 2(%rax), %cl /* get byte from skipset */ |
62 | testb $0xff, %cl /* is NUL char? */ |
63 | jz L(1) /* yes => start compare loop */ |
64 | movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */ |
65 | |
66 | movb 3(%rax), %cl /* get byte from skipset */ |
67 | addq $4, %rax /* increment skipset pointer */ |
68 | movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */ |
69 | testb $0xff, %cl /* is NUL char? */ |
70 | jnz L(2) /* no => process next dword from skipset */ |
71 | |
72 | L(1): leaq -4(%rdx), %rax /* prepare loop */ |
73 | |
74 | /* We use a neat trick for the following loop. Normally we would |
75 | have to test for two termination conditions |
76 | 1. a character in the skipset was found |
77 | and |
78 | 2. the end of the string was found |
79 | But as a sign that the character is in the skipset we store its |
80 | value in the table. But the value of NUL is NUL so the loop |
81 | terminates for NUL in every case. */ |
82 | |
83 | .p2align 4 |
84 | L(3): addq $4, %rax /* adjust pointer for full loop round */ |
85 | |
86 | movb (%rax), %cl /* get byte from string */ |
87 | cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */ |
88 | je L(4) /* yes => return */ |
89 | |
90 | movb 1(%rax), %cl /* get byte from string */ |
91 | cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */ |
92 | je L(5) /* yes => return */ |
93 | |
94 | movb 2(%rax), %cl /* get byte from string */ |
95 | cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */ |
96 | jz L(6) /* yes => return */ |
97 | |
98 | movb 3(%rax), %cl /* get byte from string */ |
99 | cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */ |
100 | jne L(3) /* no => start loop again */ |
101 | |
102 | incq %rax /* adjust pointer */ |
103 | L(6): incq %rax |
104 | L(5): incq %rax |
105 | |
106 | L(4): addq $256, %rsp /* remove skipset */ |
107 | cfi_adjust_cfa_offset(-256) |
108 | #ifdef USE_AS_STRPBRK |
109 | xorl %edx,%edx |
110 | orb %cl, %cl /* was last character NUL? */ |
111 | cmovzq %rdx, %rax /* Yes: return NULL */ |
112 | #else |
113 | subq %rdx, %rax /* we have to return the number of valid |
114 | characters, so compute distance to first |
115 | non-valid character */ |
116 | #endif |
117 | ret |
118 | END (strcspn) |
119 | libc_hidden_builtin_def (strcspn) |
120 | |