1 /* Optimized strchr implementation for PowerPC64/POWER8.
2 Copyright (C) 2016-2020 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 #ifdef USE_AS_STRCHRNUL
23 # define FUNC_NAME __strchrnul
25 # define FUNC_NAME STRCHRNUL
29 # define FUNC_NAME strchr
31 # define FUNC_NAME STRCHR
33 #endif /* !USE_AS_STRCHRNUL */
35 /* int [r3] strchr (char *s [r3], int c [r4]) */
37 ENTRY_TOCLESS (FUNC_NAME)
40 clrrdi r8,r3,3 /* Align the address to doubleword boundary. */
42 ld r12,0(r8) /* Load doubleword from memory. */
43 li r0,0 /* Doubleword with null chars to use
46 rlwinm r6,r3,3,26,28 /* Calculate padding. */
50 /* Replicate byte to doubleword. */
55 /* Now r4 has a doubleword of c bytes and r0 has
56 a doubleword of null bytes. */
58 cmpb r10,r12,r4 /* Compare each byte against c byte. */
59 cmpb r11,r12,r0 /* Compare each byte against null byte. */
61 /* Move the doublewords left and right to discard the bits that are
62 not part of the string and bring them back as zeros. */
63 #ifdef __LITTLE_ENDIAN__
74 or r5,r10,r11 /* OR the results to speed things up. */
75 cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes
81 /* Are we now aligned to a doubleword boundary? If so, skip to
82 the main loop. Otherwise, go through the alignment code. */
86 /* Handle WORD2 of pair. */
93 b L(loop) /* We branch here (rather than falling through)
94 to skip the nops due to heavy alignment
99 /* Load two doublewords, compare and merge in a
100 single register for speed. This is an attempt
101 to speed up the null-checking process for bigger strings. */
113 /* OK, one (or both) of the doublewords contains a c/null byte. Check
114 the first doubleword and decrement the address in case the first
115 doubleword really contains a c/null byte. */
121 /* The c/null byte must be in the second doubleword. Adjust the
122 address again and move the result of cmpb to r10 so we can calculate
128 #ifdef USE_AS_STRCHRNUL
131 /* r10/r11 have the output of the cmpb instructions, that is,
132 0xff in the same position as the c/null byte in the original
133 doubleword from the string. Use that to calculate the pointer. */
135 #ifdef USE_AS_STRCHRNUL
138 #ifdef __LITTLE_ENDIAN__
142 # ifndef USE_AS_STRCHRNUL
149 cntlzd r0,r10 /* Count leading zeros before c matches. */
150 # ifndef USE_AS_STRCHRNUL
155 srdi r0,r0,3 /* Convert leading zeros to bytes. */
156 add r3,r8,r0 /* Return address of the matching c byte
157 or null in case c was not found. */
160 /* Check the first 32B in GPR's and move to vectorized loop. */
167 /* Precompute vbpermq constant. */
174 /* Compare 32 bytes in each loop. */
185 vcmpequb. v11, v0, v11
188 /* One (or both) of the quadwords contains a c/null byte. */
190 #ifndef USE_AS_STRCHRNUL
191 vcmpequb. v11, v0, v9
194 /* Permute the first bit of each byte into bits 48-63. */
199 /* Shift each component into its correct position for merging. */
200 #ifdef __LITTLE_ENDIAN__
210 /* Merge the results and move to a GPR. */
215 #ifdef __LITTLE_ENDIAN__
220 cntlzd r6, r5 /* Count leading zeros before the match. */
222 add r3, r3, r6 /* Compute final length. */
223 /* Return NULL if null found before c. */
224 #ifndef USE_AS_STRCHRNUL
231 #ifndef USE_AS_STRCHRNUL
238 /* We are here because strchr was called with a null byte. */
241 /* r0 has a doubleword of null bytes. */
243 cmpb r5,r12,r0 /* Compare each byte against null bytes. */
245 /* Move the doublewords left and right to discard the bits that are
246 not part of the string and bring them back as zeros. */
247 #ifdef __LITTLE_ENDIAN__
254 cmpdi cr7,r5,0 /* If r10 == 0, no c or null bytes
260 /* Are we now aligned to a quadword boundary? If so, skip to
261 the main loop. Otherwise, go through the alignment code. */
265 /* Handle WORD2 of pair. */
270 b L(loop_null) /* We branch here (rather than falling through)
271 to skip the nops due to heavy alignment
272 of the loop below. */
274 /* Main loop to look for the end of the string. Since it's a
275 small loop (< 8 instructions), align it to 32-bytes. */
278 /* Load two doublewords, compare and merge in a
279 single register for speed. This is an attempt
280 to speed up the null-checking process for bigger strings. */
289 /* OK, one (or both) of the doublewords contains a null byte. Check
290 the first doubleword and decrement the address in case the first
291 doubleword really contains a null byte. */
297 /* The null byte must be in the second doubleword. Adjust the address
298 again and move the result of cmpb to r10 so we can calculate the
304 /* r5 has the output of the cmpb instruction, that is, it contains
305 0xff in the same position as the null byte in the original
306 doubleword from the string. Use that to calculate the pointer. */
308 #ifdef __LITTLE_ENDIAN__
313 cntlzd r0,r5 /* Count leading zeros before the match. */
315 srdi r0,r0,3 /* Convert leading zeros to bytes. */
316 add r3,r8,r0 /* Return address of the matching null byte. */
322 bne cr0, L(loop_null)
335 vcmpequb. v11, v0, v8
337 blt cr6, L(continue1)
342 /* Shift each component into its correct position for merging. */
343 #ifdef __LITTLE_ENDIAN__
350 /* Merge the results and move to a GPR. */
353 #ifdef __LITTLE_ENDIAN__
358 cntlzd r6, r5 /* Count leading zeros before the match. */
360 add r3, r3, r6 /* Compute final length. */
364 #ifndef USE_AS_STRCHRNUL
365 weak_alias (strchr, index)
366 libc_hidden_builtin_def (strchr)