1 /* Optimized strchr implementation for PowerPC32/POWER7 using cmpb insn.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
21 /* int [r3] strchr (char *s [r3], int c [r4]) */
26 clrrwi r8,r3,2 /* Align the address to word boundary. */
28 lwz r12,0(r8) /* Load word from memory. */
29 li r0,0 /* Word with null chars to use
32 rlwinm r6,r3,3,27,28 /* Calculate padding. */
36 /* Replicate byte to word. */
40 /* Now r4 has a word of c bytes and r0 has
41 a word of null bytes. */
43 cmpb r10,r12,r4 /* Compare each byte against c byte. */
44 cmpb r11,r12,r0 /* Compare each byte against null byte. */
46 /* Move the words left and right to discard the bits that are
47 not part of the string and to bring them back as zeros. */
48 #ifdef __LITTLE_ENDIAN__
59 or r5,r10,r11 /* OR the results to speed things up. */
60 cmpwi cr7,r5,0 /* If r5 == 0, no c or null bytes
66 /* Are we now aligned to a doubleword boundary? If so, skip to
67 the main loop. Otherwise, go through the alignment code. */
71 /* Handle WORD2 of pair. */
78 b L(loop) /* We branch here (rather than falling through)
79 to skip the nops due to heavy alignment
84 /* Load two words, compare and merge in a
85 single register for speed. This is an attempt
86 to speed up the null-checking process for bigger strings. */
99 /* OK, one (or both) of the words contains a c/null byte. Check
100 the first word and decrement the address in case the first
101 word really contains a c/null byte. */
107 /* The c/null byte must be in the second word. Adjust the address
108 again and move the result of cmpb to r10/r11 so we can calculate
115 /* r10/r11 have the output of the cmpb instructions, that is,
116 0xff in the same position as the c/null byte in the original
117 word from the string. Use that to calculate the pointer. */
119 #ifdef __LITTLE_ENDIAN__
128 cntlzw r0,r10 /* Count leading zeros before c matches. */
132 srwi r0,r0,3 /* Convert leading zeros to bytes. */
133 add r3,r8,r0 /* Return address of the matching c byte
134 or null in case c was not found. */
142 /* We are here because strchr was called with a null byte. */
145 /* r0 has a word of null bytes. */
147 cmpb r5,r12,r0 /* Compare each byte against null bytes. */
149 /* Move the words left and right to discard the bits that are
150 not part of the string and bring them back as zeros. */
151 #ifdef __LITTLE_ENDIAN__
158 cmpwi cr7,r5,0 /* If r10 == 0, no c or null bytes
164 /* Are we now aligned to a doubleword boundary? If so, skip to
165 the main loop. Otherwise, go through the alignment code. */
169 /* Handle WORD2 of pair. */
174 b L(loop_null) /* We branch here (rather than falling through)
175 to skip the nops due to heavy alignment
176 of the loop below. */
178 /* Main loop to look for the end of the string. Since it's a
179 small loop (< 8 instructions), align it to 32-bytes. */
182 /* Load two words, compare and merge in a
183 single register for speed. This is an attempt
184 to speed up the null-checking process for bigger strings. */
193 /* OK, one (or both) of the words contains a null byte. Check
194 the first word and decrement the address in case the first
195 word really contains a null byte. */
201 /* The null byte must be in the second word. Adjust the address
202 again and move the result of cmpb to r10 so we can calculate the
208 /* r5 has the output of the cmpb instruction, that is, it contains
209 0xff in the same position as the null byte in the original
210 word from the string. Use that to calculate the pointer. */
212 #ifdef __LITTLE_ENDIAN__
217 cntlzw r0,r5 /* Count leading zeros before the match. */
219 srwi r0,r0,3 /* Convert leading zeros to bytes. */
220 add r3,r8,r0 /* Return address of the matching null byte. */
223 weak_alias (strchr, index)
224 libc_hidden_builtin_def (strchr)