1 /* Optimized strchr implementation for PowerPC64/POWER7 using cmpb insn.
2 Copyright (C) 2010-2023 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/>. */
22 # define STRCHR strchr
25 /* int [r3] strchr (char *s [r3], int c [r4]) */
27 ENTRY_TOCLESS (STRCHR)
30 clrrdi r8,r3,3 /* Align the address to doubleword boundary. */
32 ld r12,0(r8) /* Load doubleword from memory. */
33 li r0,0 /* Doubleword with null chars to use
36 rlwinm r6,r3,3,26,28 /* Calculate padding. */
40 /* Replicate byte to doubleword. */
45 /* Now r4 has a doubleword of c bytes and r0 has
46 a doubleword of null bytes. */
48 cmpb r10,r12,r4 /* Compare each byte against c byte. */
49 cmpb r11,r12,r0 /* Compare each byte against null byte. */
51 /* Move the doublewords left and right to discard the bits that are
52 not part of the string and bring them back as zeros. */
53 #ifdef __LITTLE_ENDIAN__
64 or r5,r10,r11 /* OR the results to speed things up. */
65 cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes
71 /* Are we now aligned to a doubleword boundary? If so, skip to
72 the main loop. Otherwise, go through the alignment code. */
76 /* Handle WORD2 of pair. */
83 b L(loop) /* We branch here (rather than falling through)
84 to skip the nops due to heavy alignment
89 /* Load two doublewords, compare and merge in a
90 single register for speed. This is an attempt
91 to speed up the null-checking process for bigger strings. */
104 /* OK, one (or both) of the doublewords contains a c/null byte. Check
105 the first doubleword and decrement the address in case the first
106 doubleword really contains a c/null byte. */
112 /* The c/null byte must be in the second doubleword. Adjust the
113 address again and move the result of cmpb to r10 so we can calculate
120 /* r10/r11 have the output of the cmpb instructions, that is,
121 0xff in the same position as the c/null byte in the original
122 doubleword from the string. Use that to calculate the pointer. */
124 #ifdef __LITTLE_ENDIAN__
133 cntlzd r0,r10 /* Count leading zeros before c matches. */
137 srdi r0,r0,3 /* Convert leading zeros to bytes. */
138 add r3,r8,r0 /* Return address of the matching c byte
139 or null in case c was not found. */
147 /* We are here because strchr was called with a null byte. */
150 /* r0 has a doubleword of null bytes. */
152 cmpb r5,r12,r0 /* Compare each byte against null bytes. */
154 /* Move the doublewords left and right to discard the bits that are
155 not part of the string and bring them back as zeros. */
156 #ifdef __LITTLE_ENDIAN__
163 cmpdi cr7,r5,0 /* If r10 == 0, no c or null bytes
169 /* Are we now aligned to a quadword boundary? If so, skip to
170 the main loop. Otherwise, go through the alignment code. */
174 /* Handle WORD2 of pair. */
179 b L(loop_null) /* We branch here (rather than falling through)
180 to skip the nops due to heavy alignment
181 of the loop below. */
183 /* Main loop to look for the end of the string. Since it's a
184 small loop (< 8 instructions), align it to 32-bytes. */
187 /* Load two doublewords, compare and merge in a
188 single register for speed. This is an attempt
189 to speed up the null-checking process for bigger strings. */
198 /* OK, one (or both) of the doublewords contains a null byte. Check
199 the first doubleword and decrement the address in case the first
200 doubleword really contains a null byte. */
206 /* The null byte must be in the second doubleword. Adjust the address
207 again and move the result of cmpb to r10 so we can calculate the
213 /* r5 has the output of the cmpb instruction, that is, it contains
214 0xff in the same position as the null byte in the original
215 doubleword from the string. Use that to calculate the pointer. */
217 #ifdef __LITTLE_ENDIAN__
222 cntlzd r0,r5 /* Count leading zeros before the match. */
224 srdi r0,r0,3 /* Convert leading zeros to bytes. */
225 add r3,r8,r0 /* Return address of the matching null byte. */
228 weak_alias (strchr, index)
229 libc_hidden_builtin_def (strchr)