]>
Commit | Line | Data |
---|---|---|
fe2f79db | 1 | /* Optimized strchr implementation for PowerPC64/POWER7 using cmpb insn. |
b168057a | 2 | Copyright (C) 2010-2015 Free Software Foundation, Inc. |
fe2f79db LM |
3 | Contributed by Luis Machado <luisgpm@br.ibm.com>. |
4 | This file is part of the GNU C Library. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
fe2f79db LM |
19 | |
20 | #include <sysdep.h> | |
fe2f79db LM |
21 | |
22 | /* int [r3] strchr (char *s [r3], int c [r4]) */ | |
23 | .machine power7 | |
2d67d91a | 24 | ENTRY (strchr) |
fe2f79db LM |
25 | CALL_MCOUNT 2 |
26 | dcbt 0,r3 | |
27 | clrrdi r8,r3,3 /* Align the address to doubleword boundary. */ | |
28 | cmpdi cr7,r4,0 | |
29 | ld r12,0(r8) /* Load doubleword from memory. */ | |
30 | li r0,0 /* Doubleword with null chars to use | |
31 | with cmpb. */ | |
32 | ||
33 | rlwinm r6,r3,3,26,28 /* Calculate padding. */ | |
34 | ||
35 | beq cr7,L(null_match) | |
36 | ||
37 | /* Replicate byte to doubleword. */ | |
664318c3 AM |
38 | insrdi r4,r4,8,48 |
39 | insrdi r4,r4,16,32 | |
fe2f79db LM |
40 | insrdi r4,r4,32,0 |
41 | ||
42 | /* Now r4 has a doubleword of c bytes and r0 has | |
43 | a doubleword of null bytes. */ | |
44 | ||
45 | cmpb r10,r12,r4 /* Compare each byte against c byte. */ | |
46 | cmpb r11,r12,r0 /* Compare each byte against null byte. */ | |
47 | ||
48 | /* Move the doublewords left and right to discard the bits that are | |
49 | not part of the string and bring them back as zeros. */ | |
664318c3 AM |
50 | #ifdef __LITTLE_ENDIAN__ |
51 | srd r10,r10,r6 | |
52 | srd r11,r11,r6 | |
53 | sld r10,r10,r6 | |
54 | sld r11,r11,r6 | |
55 | #else | |
fe2f79db LM |
56 | sld r10,r10,r6 |
57 | sld r11,r11,r6 | |
58 | srd r10,r10,r6 | |
59 | srd r11,r11,r6 | |
664318c3 | 60 | #endif |
fe2f79db LM |
61 | or r5,r10,r11 /* OR the results to speed things up. */ |
62 | cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes | |
63 | have been found. */ | |
64 | bne cr7,L(done) | |
65 | ||
66 | mtcrf 0x01,r8 | |
67 | ||
68 | /* Are we now aligned to a doubleword boundary? If so, skip to | |
69 | the main loop. Otherwise, go through the alignment code. */ | |
70 | ||
71 | bt 28,L(loop) | |
72 | ||
73 | /* Handle WORD2 of pair. */ | |
74 | ldu r12,8(r8) | |
75 | cmpb r10,r12,r4 | |
76 | cmpb r11,r12,r0 | |
77 | or r5,r10,r11 | |
78 | cmpdi cr7,r5,0 | |
79 | bne cr7,L(done) | |
80 | b L(loop) /* We branch here (rather than falling through) | |
81 | to skip the nops due to heavy alignment | |
82 | of the loop below. */ | |
83 | ||
84 | .p2align 5 | |
85 | L(loop): | |
86 | /* Load two doublewords, compare and merge in a | |
87 | single register for speed. This is an attempt | |
88 | to speed up the null-checking process for bigger strings. */ | |
89 | ld r12,8(r8) | |
90 | ldu r9,16(r8) | |
91 | cmpb r10,r12,r4 | |
92 | cmpb r11,r12,r0 | |
93 | cmpb r6,r9,r4 | |
94 | cmpb r7,r9,r0 | |
95 | or r12,r10,r11 | |
96 | or r9,r6,r7 | |
97 | or r5,r12,r9 | |
98 | cmpdi cr7,r5,0 | |
99 | beq cr7,L(loop) | |
100 | ||
101 | /* OK, one (or both) of the doublewords contains a c/null byte. Check | |
102 | the first doubleword and decrement the address in case the first | |
103 | doubleword really contains a c/null byte. */ | |
104 | ||
105 | cmpdi cr6,r12,0 | |
106 | addi r8,r8,-8 | |
107 | bne cr6,L(done) | |
108 | ||
109 | /* The c/null byte must be in the second doubleword. Adjust the | |
110 | address again and move the result of cmpb to r10 so we can calculate | |
111 | the pointer. */ | |
112 | ||
113 | mr r10,r6 | |
114 | mr r11,r7 | |
115 | addi r8,r8,8 | |
116 | ||
664318c3 | 117 | /* r10/r11 have the output of the cmpb instructions, that is, |
fe2f79db LM |
118 | 0xff in the same position as the c/null byte in the original |
119 | doubleword from the string. Use that to calculate the pointer. */ | |
120 | L(done): | |
664318c3 AM |
121 | #ifdef __LITTLE_ENDIAN__ |
122 | addi r3,r10,-1 | |
123 | andc r3,r3,r10 | |
124 | popcntd r0,r3 | |
125 | addi r4,r11,-1 | |
126 | andc r4,r4,r11 | |
127 | cmpld cr7,r3,r4 | |
fe2f79db | 128 | bgt cr7,L(no_match) |
664318c3 AM |
129 | #else |
130 | cntlzd r0,r10 /* Count leading zeros before c matches. */ | |
131 | cmpld cr7,r11,r10 | |
132 | bgt cr7,L(no_match) | |
133 | #endif | |
134 | srdi r0,r0,3 /* Convert leading zeros to bytes. */ | |
fe2f79db LM |
135 | add r3,r8,r0 /* Return address of the matching c byte |
136 | or null in case c was not found. */ | |
137 | blr | |
138 | ||
139 | .align 4 | |
140 | L(no_match): | |
141 | li r3,0 | |
142 | blr | |
143 | ||
144 | /* We are here because strchr was called with a null byte. */ | |
145 | .align 4 | |
146 | L(null_match): | |
147 | /* r0 has a doubleword of null bytes. */ | |
148 | ||
149 | cmpb r5,r12,r0 /* Compare each byte against null bytes. */ | |
150 | ||
151 | /* Move the doublewords left and right to discard the bits that are | |
152 | not part of the string and bring them back as zeros. */ | |
664318c3 AM |
153 | #ifdef __LITTLE_ENDIAN__ |
154 | srd r5,r5,r6 | |
155 | sld r5,r5,r6 | |
156 | #else | |
fe2f79db LM |
157 | sld r5,r5,r6 |
158 | srd r5,r5,r6 | |
664318c3 | 159 | #endif |
fe2f79db LM |
160 | cmpdi cr7,r5,0 /* If r10 == 0, no c or null bytes |
161 | have been found. */ | |
162 | bne cr7,L(done_null) | |
163 | ||
164 | mtcrf 0x01,r8 | |
165 | ||
166 | /* Are we now aligned to a quadword boundary? If so, skip to | |
167 | the main loop. Otherwise, go through the alignment code. */ | |
168 | ||
169 | bt 28,L(loop_null) | |
170 | ||
171 | /* Handle WORD2 of pair. */ | |
172 | ldu r12,8(r8) | |
173 | cmpb r5,r12,r0 | |
174 | cmpdi cr7,r5,0 | |
175 | bne cr7,L(done_null) | |
176 | b L(loop_null) /* We branch here (rather than falling through) | |
177 | to skip the nops due to heavy alignment | |
178 | of the loop below. */ | |
179 | ||
180 | /* Main loop to look for the end of the string. Since it's a | |
181 | small loop (< 8 instructions), align it to 32-bytes. */ | |
182 | .p2align 5 | |
183 | L(loop_null): | |
184 | /* Load two doublewords, compare and merge in a | |
185 | single register for speed. This is an attempt | |
186 | to speed up the null-checking process for bigger strings. */ | |
187 | ld r12,8(r8) | |
188 | ldu r11,16(r8) | |
189 | cmpb r5,r12,r0 | |
190 | cmpb r10,r11,r0 | |
191 | or r6,r5,r10 | |
192 | cmpdi cr7,r6,0 | |
193 | beq cr7,L(loop_null) | |
194 | ||
195 | /* OK, one (or both) of the doublewords contains a null byte. Check | |
196 | the first doubleword and decrement the address in case the first | |
197 | doubleword really contains a null byte. */ | |
198 | ||
199 | cmpdi cr6,r5,0 | |
200 | addi r8,r8,-8 | |
201 | bne cr6,L(done_null) | |
202 | ||
203 | /* The null byte must be in the second doubleword. Adjust the address | |
204 | again and move the result of cmpb to r10 so we can calculate the | |
205 | pointer. */ | |
206 | ||
207 | mr r5,r10 | |
208 | addi r8,r8,8 | |
209 | ||
210 | /* r5 has the output of the cmpb instruction, that is, it contains | |
211 | 0xff in the same position as the null byte in the original | |
212 | doubleword from the string. Use that to calculate the pointer. */ | |
213 | L(done_null): | |
664318c3 AM |
214 | #ifdef __LITTLE_ENDIAN__ |
215 | addi r0,r5,-1 | |
216 | andc r0,r0,r5 | |
217 | popcntd r0,r0 | |
218 | #else | |
fe2f79db | 219 | cntlzd r0,r5 /* Count leading zeros before the match. */ |
664318c3 | 220 | #endif |
fe2f79db LM |
221 | srdi r0,r0,3 /* Convert leading zeros to bytes. */ |
222 | add r3,r8,r0 /* Return address of the matching null byte. */ | |
223 | blr | |
2d67d91a JM |
224 | END (strchr) |
225 | weak_alias (strchr, index) | |
fe2f79db | 226 | libc_hidden_builtin_def (strchr) |