1 /* Optimized strchr implementation for PowerPC64/POWER8.
2 Copyright (C) 2016-2019 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 <http://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]) */
36 /* TODO: change these to the actual instructions when the minimum required
37 binutils allows it. */
38 #define MTVRD(v,r) .long (0x7c000167 | ((v)<<(32-11)) | ((r)<<(32-16)))
39 #define MFVRD(r,v) .long (0x7c000067 | ((v)<<(32-11)) | ((r)<<(32-16)))
40 #define VBPERMQ(t,a,b) .long (0x1000054c \
44 /* TODO: change this to .machine power8 when the minimum required binutils
47 ENTRY_TOCLESS (FUNC_NAME)
50 clrrdi r8,r3,3 /* Align the address to doubleword boundary. */
52 ld r12,0(r8) /* Load doubleword from memory. */
53 li r0,0 /* Doubleword with null chars to use
56 rlwinm r6,r3,3,26,28 /* Calculate padding. */
60 /* Replicate byte to doubleword. */
65 /* Now r4 has a doubleword of c bytes and r0 has
66 a doubleword of null bytes. */
68 cmpb r10,r12,r4 /* Compare each byte against c byte. */
69 cmpb r11,r12,r0 /* Compare each byte against null byte. */
71 /* Move the doublewords left and right to discard the bits that are
72 not part of the string and bring them back as zeros. */
73 #ifdef __LITTLE_ENDIAN__
84 or r5,r10,r11 /* OR the results to speed things up. */
85 cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes
91 /* Are we now aligned to a doubleword boundary? If so, skip to
92 the main loop. Otherwise, go through the alignment code. */
96 /* Handle WORD2 of pair. */
103 b L(loop) /* We branch here (rather than falling through)
104 to skip the nops due to heavy alignment
105 of the loop below. */
109 /* Load two doublewords, compare and merge in a
110 single register for speed. This is an attempt
111 to speed up the null-checking process for bigger strings. */
123 /* OK, one (or both) of the doublewords contains a c/null byte. Check
124 the first doubleword and decrement the address in case the first
125 doubleword really contains a c/null byte. */
131 /* The c/null byte must be in the second doubleword. Adjust the
132 address again and move the result of cmpb to r10 so we can calculate
138 #ifdef USE_AS_STRCHRNUL
141 /* r10/r11 have the output of the cmpb instructions, that is,
142 0xff in the same position as the c/null byte in the original
143 doubleword from the string. Use that to calculate the pointer. */
145 #ifdef USE_AS_STRCHRNUL
148 #ifdef __LITTLE_ENDIAN__
152 # ifndef USE_AS_STRCHRNUL
159 cntlzd r0,r10 /* Count leading zeros before c matches. */
160 # ifndef USE_AS_STRCHRNUL
165 srdi r0,r0,3 /* Convert leading zeros to bytes. */
166 add r3,r8,r0 /* Return address of the matching c byte
167 or null in case c was not found. */
170 /* Check the first 32B in GPR's and move to vectorized loop. */
177 /* Precompute vbpermq constant. */
184 /* Compare 32 bytes in each loop. */
195 vcmpequb. v11, v0, v11
198 /* One (or both) of the quadwords contains a c/null byte. */
200 #ifndef USE_AS_STRCHRNUL
201 vcmpequb. v11, v0, v9
204 /* Permute the first bit of each byte into bits 48-63. */
209 /* Shift each component into its correct position for merging. */
210 #ifdef __LITTLE_ENDIAN__
220 /* Merge the results and move to a GPR. */
225 #ifdef __LITTLE_ENDIAN__
230 cntlzd r6, r5 /* Count leading zeros before the match. */
232 add r3, r3, r6 /* Compute final length. */
233 /* Return NULL if null found before c. */
234 #ifndef USE_AS_STRCHRNUL
241 #ifndef USE_AS_STRCHRNUL
248 /* We are here because strchr was called with a null byte. */
251 /* r0 has a doubleword of null bytes. */
253 cmpb r5,r12,r0 /* Compare each byte against null bytes. */
255 /* Move the doublewords left and right to discard the bits that are
256 not part of the string and bring them back as zeros. */
257 #ifdef __LITTLE_ENDIAN__
264 cmpdi cr7,r5,0 /* If r10 == 0, no c or null bytes
270 /* Are we now aligned to a quadword boundary? If so, skip to
271 the main loop. Otherwise, go through the alignment code. */
275 /* Handle WORD2 of pair. */
280 b L(loop_null) /* We branch here (rather than falling through)
281 to skip the nops due to heavy alignment
282 of the loop below. */
284 /* Main loop to look for the end of the string. Since it's a
285 small loop (< 8 instructions), align it to 32-bytes. */
288 /* Load two doublewords, compare and merge in a
289 single register for speed. This is an attempt
290 to speed up the null-checking process for bigger strings. */
299 /* OK, one (or both) of the doublewords contains a null byte. Check
300 the first doubleword and decrement the address in case the first
301 doubleword really contains a null byte. */
307 /* The null byte must be in the second doubleword. Adjust the address
308 again and move the result of cmpb to r10 so we can calculate the
314 /* r5 has the output of the cmpb instruction, that is, it contains
315 0xff in the same position as the null byte in the original
316 doubleword from the string. Use that to calculate the pointer. */
318 #ifdef __LITTLE_ENDIAN__
323 cntlzd r0,r5 /* Count leading zeros before the match. */
325 srdi r0,r0,3 /* Convert leading zeros to bytes. */
326 add r3,r8,r0 /* Return address of the matching null byte. */
332 bne cr0, L(loop_null)
345 vcmpequb. v11, v0, v8
347 blt cr6, L(continue1)
352 /* Shift each component into its correct position for merging. */
353 #ifdef __LITTLE_ENDIAN__
360 /* Merge the results and move to a GPR. */
363 #ifdef __LITTLE_ENDIAN__
368 cntlzd r6, r5 /* Count leading zeros before the match. */
370 add r3, r3, r6 /* Compute final length. */
374 #ifndef USE_AS_STRCHRNUL
375 weak_alias (strchr, index)
376 libc_hidden_builtin_def (strchr)