1 /* Optimized strchr implementation for PowerPC32/POWER7 using cmpb insn.
2 Copyright (C) 2010-2013 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/>. */
24 /* int [r3] strchr (char *s [r3], int c [r4]) */
26 ENTRY (BP_SYM(strchr))
29 clrrwi r8,r3,2 /* Align the address to word boundary. */
31 lwz r12,0(r8) /* Load word from memory. */
32 li r0,0 /* Word with null chars to use
35 rlwinm r6,r3,3,27,28 /* Calculate padding. */
39 /* Replicate byte to word. */
43 /* Now r4 has a word of c bytes and r0 has
44 a word of null bytes. */
46 cmpb r10,r12,r4 /* Compare each byte against c byte. */
47 cmpb r11,r12,r0 /* Compare each byte against null byte. */
49 /* Move the words left and right to discard the bits that are
50 not part of the string and to bring them back as zeros. */
56 or r5,r10,r11 /* OR the results to speed things up. */
57 cmpwi cr7,r5,0 /* If r5 == 0, no c or null bytes
63 /* Are we now aligned to a doubleword boundary? If so, skip to
64 the main loop. Otherwise, go through the alignment code. */
68 /* Handle WORD2 of pair. */
75 b L(loop) /* We branch here (rather than falling through)
76 to skip the nops due to heavy alignment
81 /* Load two words, compare and merge in a
82 single register for speed. This is an attempt
83 to speed up the null-checking process for bigger strings. */
96 /* OK, one (or both) of the words contains a c/null byte. Check
97 the first word and decrement the address in case the first
98 word really contains a c/null byte. */
104 /* The c/null byte must be in the second word. Adjust the address
105 again and move the result of cmpb to r10 so we can calculate the
112 /* r5 has the output of the cmpb instruction, that is, it contains
113 0xff in the same position as the c/null byte in the original
114 word from the string. Use that to calculate the pointer. */
116 cntlzw r4,r10 /* Count leading zeroes before c matches. */
117 cntlzw r0,r11 /* Count leading zeroes before null matches. */
120 srwi r0,r4,3 /* Convert leading zeroes to bytes. */
121 add r3,r8,r0 /* Return address of the matching c byte
122 or null in case c was not found. */
130 /* We are here because strchr was called with a null byte. */
133 /* r0 has a word of null bytes. */
135 cmpb r5,r12,r0 /* Compare each byte against null bytes. */
137 /* Move the words left and right to discard the bits that are
138 not part of the string and to bring them back as zeros. */
142 cmpwi cr7,r5,0 /* If r10 == 0, no c or null bytes
148 /* Are we now aligned to a doubleword boundary? If so, skip to
149 the main loop. Otherwise, go through the alignment code. */
153 /* Handle WORD2 of pair. */
158 b L(loop_null) /* We branch here (rather than falling through)
159 to skip the nops due to heavy alignment
160 of the loop below. */
162 /* Main loop to look for the end of the string. Since it's a
163 small loop (< 8 instructions), align it to 32-bytes. */
166 /* Load two words, compare and merge in a
167 single register for speed. This is an attempt
168 to speed up the null-checking process for bigger strings. */
177 /* OK, one (or both) of the words contains a null byte. Check
178 the first word and decrement the address in case the first
179 word really contains a null byte. */
185 /* The null byte must be in the second word. Adjust the address
186 again and move the result of cmpb to r10 so we can calculate the
192 /* r5 has the output of the cmpb instruction, that is, it contains
193 0xff in the same position as the null byte in the original
194 word from the string. Use that to calculate the pointer. */
196 cntlzw r0,r5 /* Count leading zeros before the match. */
197 srwi r0,r0,3 /* Convert leading zeros to bytes. */
198 add r3,r8,r0 /* Return address of the matching null byte. */
200 END (BP_SYM (strchr))
201 weak_alias (BP_SYM (strchr), BP_SYM (index))
202 libc_hidden_builtin_def (strchr)