1 /* Optimized strchr implementation for PowerPC32/POWER7 using cmpb insn.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Luis Machado <luisgpm@br.ibm.com>.
4 This file is part of the GNU C Library.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 /* int [r3] strchr (char *s [r3], int c [r4]) */
27 clrrwi r8,r3,2 /* Align the address to word boundary. */
29 lwz r12,0(r8) /* Load word from memory. */
30 li r0,0 /* Word with null chars to use
33 rlwinm r6,r3,3,27,28 /* Calculate padding. */
37 /* Replicate byte to word. */
41 /* Now r4 has a word of c bytes and r0 has
42 a word of null bytes. */
44 cmpb r10,r12,r4 /* Compare each byte against c byte. */
45 cmpb r11,r12,r0 /* Compare each byte against null byte. */
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 #ifdef __LITTLE_ENDIAN__
60 or r5,r10,r11 /* OR the results to speed things up. */
61 cmpwi cr7,r5,0 /* If r5 == 0, no c or null bytes
67 /* Are we now aligned to a doubleword boundary? If so, skip to
68 the main loop. Otherwise, go through the alignment code. */
72 /* Handle WORD2 of pair. */
79 b L(loop) /* We branch here (rather than falling through)
80 to skip the nops due to heavy alignment
85 /* Load two words, compare and merge in a
86 single register for speed. This is an attempt
87 to speed up the null-checking process for bigger strings. */
100 /* OK, one (or both) of the words contains a c/null byte. Check
101 the first word and decrement the address in case the first
102 word really contains a c/null byte. */
108 /* The c/null byte must be in the second word. Adjust the address
109 again and move the result of cmpb to r10/r11 so we can calculate
116 /* r10/r11 have the output of the cmpb instructions, that is,
117 0xff in the same position as the c/null byte in the original
118 word from the string. Use that to calculate the pointer. */
120 #ifdef __LITTLE_ENDIAN__
129 cntlzw r0,r10 /* Count leading zeros before c matches. */
133 srwi r0,r0,3 /* Convert leading zeros to bytes. */
134 add r3,r8,r0 /* Return address of the matching c byte
135 or null in case c was not found. */
143 /* We are here because strchr was called with a null byte. */
146 /* r0 has a word of null bytes. */
148 cmpb r5,r12,r0 /* Compare each byte against null bytes. */
150 /* Move the words left and right to discard the bits that are
151 not part of the string and bring them back as zeros. */
152 #ifdef __LITTLE_ENDIAN__
159 cmpwi cr7,r5,0 /* If r10 == 0, no c or null bytes
165 /* Are we now aligned to a doubleword boundary? If so, skip to
166 the main loop. Otherwise, go through the alignment code. */
170 /* Handle WORD2 of pair. */
175 b L(loop_null) /* We branch here (rather than falling through)
176 to skip the nops due to heavy alignment
177 of the loop below. */
179 /* Main loop to look for the end of the string. Since it's a
180 small loop (< 8 instructions), align it to 32-bytes. */
183 /* Load two words, compare and merge in a
184 single register for speed. This is an attempt
185 to speed up the null-checking process for bigger strings. */
194 /* OK, one (or both) of the words contains a null byte. Check
195 the first word and decrement the address in case the first
196 word really contains a null byte. */
202 /* The null byte must be in the second word. Adjust the address
203 again and move the result of cmpb to r10 so we can calculate the
209 /* r5 has the output of the cmpb instruction, that is, it contains
210 0xff in the same position as the null byte in the original
211 word from the string. Use that to calculate the pointer. */
213 #ifdef __LITTLE_ENDIAN__
218 cntlzw r0,r5 /* Count leading zeros before the match. */
220 srwi r0,r0,3 /* Convert leading zeros to bytes. */
221 add r3,r8,r0 /* Return address of the matching null byte. */
224 weak_alias (strchr, index)
225 libc_hidden_builtin_def (strchr)