]>
Commit | Line | Data |
---|---|---|
fe2f79db | 1 | /* Optimized strchr implementation for PowerPC32/POWER7 using cmpb insn. |
568035b7 | 2 | Copyright (C) 2010-2013 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 | |
b5510883 | 24 | ENTRY (strchr) |
fe2f79db LM |
25 | CALL_MCOUNT |
26 | dcbt 0,r3 | |
27 | clrrwi r8,r3,2 /* Align the address to word boundary. */ | |
28 | cmpwi cr7,r4,0 | |
29 | lwz r12,0(r8) /* Load word from memory. */ | |
30 | li r0,0 /* Word with null chars to use | |
31 | with cmpb. */ | |
32 | ||
33 | rlwinm r6,r3,3,27,28 /* Calculate padding. */ | |
34 | ||
35 | beq cr7,L(null_match) | |
36 | ||
37 | /* Replicate byte to word. */ | |
38 | rlwimi r4,r4,8,16,23 | |
39 | rlwimi r4,r4,16,0,15 | |
40 | ||
41 | /* Now r4 has a word of c bytes and r0 has | |
42 | a word of null bytes. */ | |
43 | ||
44 | cmpb r10,r12,r4 /* Compare each byte against c byte. */ | |
45 | cmpb r11,r12,r0 /* Compare each byte against null byte. */ | |
46 | ||
47 | /* Move the words left and right to discard the bits that are | |
48 | not part of the string and to bring them back as zeros. */ | |
49 | ||
50 | slw r10,r10,r6 | |
51 | slw r11,r11,r6 | |
52 | srw r10,r10,r6 | |
53 | srw r11,r11,r6 | |
54 | or r5,r10,r11 /* OR the results to speed things up. */ | |
55 | cmpwi cr7,r5,0 /* If r5 == 0, no c or null bytes | |
56 | have been found. */ | |
57 | bne cr7,L(done) | |
58 | ||
59 | mtcrf 0x01,r8 | |
60 | ||
61 | /* Are we now aligned to a doubleword boundary? If so, skip to | |
62 | the main loop. Otherwise, go through the alignment code. */ | |
63 | ||
64 | bt 29,L(loop) | |
65 | ||
66 | /* Handle WORD2 of pair. */ | |
67 | lwzu r12,4(r8) | |
68 | cmpb r10,r12,r4 | |
69 | cmpb r11,r12,r0 | |
70 | or r5,r10,r11 | |
71 | cmpwi cr7,r5,0 | |
72 | bne cr7,L(done) | |
73 | b L(loop) /* We branch here (rather than falling through) | |
74 | to skip the nops due to heavy alignment | |
75 | of the loop below. */ | |
76 | ||
77 | .p2align 5 | |
78 | L(loop): | |
79 | /* Load two words, compare and merge in a | |
80 | single register for speed. This is an attempt | |
81 | to speed up the null-checking process for bigger strings. */ | |
82 | lwz r12,4(r8) | |
83 | lwzu r9,8(r8) | |
84 | cmpb r10,r12,r4 | |
85 | cmpb r11,r12,r0 | |
86 | cmpb r6,r9,r4 | |
87 | cmpb r7,r9,r0 | |
88 | or r12,r10,r11 | |
89 | or r9,r6,r7 | |
90 | or r5,r12,r9 | |
91 | cmpwi cr7,r5,0 | |
92 | beq cr7,L(loop) | |
93 | ||
94 | /* OK, one (or both) of the words contains a c/null byte. Check | |
95 | the first word and decrement the address in case the first | |
96 | word really contains a c/null byte. */ | |
97 | ||
98 | cmpwi cr6,r12,0 | |
99 | addi r8,r8,-4 | |
100 | bne cr6,L(done) | |
101 | ||
102 | /* The c/null byte must be in the second word. Adjust the address | |
103 | again and move the result of cmpb to r10 so we can calculate the | |
104 | pointer. */ | |
105 | ||
106 | mr r10,r6 | |
107 | mr r11,r7 | |
108 | addi r8,r8,4 | |
109 | ||
110 | /* r5 has the output of the cmpb instruction, that is, it contains | |
111 | 0xff in the same position as the c/null byte in the original | |
112 | word from the string. Use that to calculate the pointer. */ | |
113 | L(done): | |
114 | cntlzw r4,r10 /* Count leading zeroes before c matches. */ | |
115 | cntlzw r0,r11 /* Count leading zeroes before null matches. */ | |
116 | cmplw cr7,r4,r0 | |
117 | bgt cr7,L(no_match) | |
118 | srwi r0,r4,3 /* Convert leading zeroes to bytes. */ | |
119 | add r3,r8,r0 /* Return address of the matching c byte | |
120 | or null in case c was not found. */ | |
121 | blr | |
122 | ||
123 | .align 4 | |
124 | L(no_match): | |
125 | li r3,0 | |
126 | blr | |
127 | ||
128 | /* We are here because strchr was called with a null byte. */ | |
129 | .align 4 | |
130 | L(null_match): | |
131 | /* r0 has a word of null bytes. */ | |
132 | ||
133 | cmpb r5,r12,r0 /* Compare each byte against null bytes. */ | |
134 | ||
135 | /* Move the words left and right to discard the bits that are | |
136 | not part of the string and to bring them back as zeros. */ | |
137 | ||
138 | slw r5,r5,r6 | |
139 | srw r5,r5,r6 | |
140 | cmpwi cr7,r5,0 /* If r10 == 0, no c or null bytes | |
141 | have been found. */ | |
142 | bne cr7,L(done_null) | |
143 | ||
144 | mtcrf 0x01,r8 | |
145 | ||
146 | /* Are we now aligned to a doubleword boundary? If so, skip to | |
147 | the main loop. Otherwise, go through the alignment code. */ | |
148 | ||
149 | bt 29,L(loop_null) | |
150 | ||
151 | /* Handle WORD2 of pair. */ | |
152 | lwzu r12,4(r8) | |
153 | cmpb r5,r12,r0 | |
154 | cmpwi cr7,r5,0 | |
155 | bne cr7,L(done_null) | |
156 | b L(loop_null) /* We branch here (rather than falling through) | |
157 | to skip the nops due to heavy alignment | |
158 | of the loop below. */ | |
159 | ||
160 | /* Main loop to look for the end of the string. Since it's a | |
161 | small loop (< 8 instructions), align it to 32-bytes. */ | |
162 | .p2align 5 | |
163 | L(loop_null): | |
164 | /* Load two words, compare and merge in a | |
165 | single register for speed. This is an attempt | |
166 | to speed up the null-checking process for bigger strings. */ | |
167 | lwz r12,4(r8) | |
168 | lwzu r11,8(r8) | |
169 | cmpb r5,r12,r0 | |
170 | cmpb r10,r11,r0 | |
171 | or r6,r5,r10 | |
172 | cmpwi cr7,r6,0 | |
173 | beq cr7,L(loop_null) | |
174 | ||
175 | /* OK, one (or both) of the words contains a null byte. Check | |
176 | the first word and decrement the address in case the first | |
177 | word really contains a null byte. */ | |
178 | ||
179 | cmpwi cr6,r5,0 | |
180 | addi r8,r8,-4 | |
181 | bne cr6,L(done_null) | |
182 | ||
183 | /* The null byte must be in the second word. Adjust the address | |
184 | again and move the result of cmpb to r10 so we can calculate the | |
185 | pointer. */ | |
186 | ||
187 | mr r5,r10 | |
188 | addi r8,r8,4 | |
189 | ||
190 | /* r5 has the output of the cmpb instruction, that is, it contains | |
191 | 0xff in the same position as the null byte in the original | |
192 | word from the string. Use that to calculate the pointer. */ | |
193 | L(done_null): | |
194 | cntlzw r0,r5 /* Count leading zeros before the match. */ | |
195 | srwi r0,r0,3 /* Convert leading zeros to bytes. */ | |
196 | add r3,r8,r0 /* Return address of the matching null byte. */ | |
197 | blr | |
b5510883 JM |
198 | END (strchr) |
199 | weak_alias (strchr, index) | |
fe2f79db | 200 | libc_hidden_builtin_def (strchr) |