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