1 /* Optimized strchr implementation for PowerPC64/POWER8.
2 Copyright (C) 2016 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
22 # define FUNC_NAME __strchrnul
24 # define FUNC_NAME strchr
26 /* int [r3] strchr (char *s [r3], int c [r4]) */
27 /* TODO: change these to the actual instructions when the minimum required
28 binutils allows it. */
29 #define MTVRD(v,r) .long (0x7c000167 | ((v)<<(32-11)) | ((r)<<(32-16)))
30 #define MFVRD(r,v) .long (0x7c000067 | ((v)<<(32-11)) | ((r)<<(32-16)))
31 #define VBPERMQ(t,a,b) .long (0x1000054c \
35 /* TODO: change this to .machine power8 when the minimum required binutils
41 clrrdi r8,r3,3 /* Align the address to doubleword boundary. */
43 ld r12,0(r8) /* Load doubleword from memory. */
44 li r0,0 /* Doubleword with null chars to use
47 rlwinm r6,r3,3,26,28 /* Calculate padding. */
51 /* Replicate byte to doubleword. */
56 /* Now r4 has a doubleword of c bytes and r0 has
57 a doubleword of null bytes. */
59 cmpb r10,r12,r4 /* Compare each byte against c byte. */
60 cmpb r11,r12,r0 /* Compare each byte against null byte. */
62 /* Move the doublewords left and right to discard the bits that are
63 not part of the string and bring them back as zeros. */
64 #ifdef __LITTLE_ENDIAN__
75 or r5,r10,r11 /* OR the results to speed things up. */
76 cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes
82 /* Are we now aligned to a doubleword boundary? If so, skip to
83 the main loop. Otherwise, go through the alignment code. */
87 /* Handle WORD2 of pair. */
94 b L(loop) /* We branch here (rather than falling through)
95 to skip the nops due to heavy alignment
100 /* Load two doublewords, compare and merge in a
101 single register for speed. This is an attempt
102 to speed up the null-checking process for bigger strings. */
114 /* OK, one (or both) of the doublewords contains a c/null byte. Check
115 the first doubleword and decrement the address in case the first
116 doubleword really contains a c/null byte. */
122 /* The c/null byte must be in the second doubleword. Adjust the
123 address again and move the result of cmpb to r10 so we can calculate
129 #ifdef USE_AS_STRCHRNUL
132 /* r10/r11 have the output of the cmpb instructions, that is,
133 0xff in the same position as the c/null byte in the original
134 doubleword from the string. Use that to calculate the pointer. */
136 #ifdef USE_AS_STRCHRNUL
139 #ifdef __LITTLE_ENDIAN__
143 # ifndef USE_AS_STRCHRNUL
150 cntlzd r0,r10 /* Count leading zeros before c matches. */
151 # ifndef USE_AS_STRCHRNUL
156 srdi r0,r0,3 /* Convert leading zeros to bytes. */
157 add r3,r8,r0 /* Return address of the matching c byte
158 or null in case c was not found. */
161 /* Check the first 32B in GPR's and move to vectorized loop. */
168 /* Precompute vbpermq constant. */
175 /* Compare 32 bytes in each loop. */
186 vcmpequb. v11, v0, v11
189 /* One (or both) of the quadwords contains a c/null byte. */
191 #ifndef USE_AS_STRCHRNUL
192 vcmpequb. v11, v0, v9
195 /* Permute the first bit of each byte into bits 48-63. */
200 /* Shift each component into its correct position for merging. */
201 #ifdef __LITTLE_ENDIAN__
211 /* Merge the results and move to a GPR. */
216 #ifdef __LITTLE_ENDIAN__
221 cntlzd r6, r5 /* Count leading zeros before the match. */
223 add r3, r3, r6 /* Compute final length. */
224 /* Return NULL if null found before c. */
225 #ifndef USE_AS_STRCHRNUL
232 #ifndef USE_AS_STRCHRNUL
239 /* We are here because strchr was called with a null byte. */
242 /* r0 has a doubleword of null bytes. */
244 cmpb r5,r12,r0 /* Compare each byte against null bytes. */
246 /* Move the doublewords left and right to discard the bits that are
247 not part of the string and bring them back as zeros. */
248 #ifdef __LITTLE_ENDIAN__
255 cmpdi cr7,r5,0 /* If r10 == 0, no c or null bytes
261 /* Are we now aligned to a quadword boundary? If so, skip to
262 the main loop. Otherwise, go through the alignment code. */
266 /* Handle WORD2 of pair. */
271 b L(loop_null) /* We branch here (rather than falling through)
272 to skip the nops due to heavy alignment
273 of the loop below. */
275 /* Main loop to look for the end of the string. Since it's a
276 small loop (< 8 instructions), align it to 32-bytes. */
279 /* Load two doublewords, compare and merge in a
280 single register for speed. This is an attempt
281 to speed up the null-checking process for bigger strings. */
290 /* OK, one (or both) of the doublewords contains a null byte. Check
291 the first doubleword and decrement the address in case the first
292 doubleword really contains a null byte. */
298 /* The null byte must be in the second doubleword. Adjust the address
299 again and move the result of cmpb to r10 so we can calculate the
305 /* r5 has the output of the cmpb instruction, that is, it contains
306 0xff in the same position as the null byte in the original
307 doubleword from the string. Use that to calculate the pointer. */
309 #ifdef __LITTLE_ENDIAN__
314 cntlzd r0,r5 /* Count leading zeros before the match. */
316 srdi r0,r0,3 /* Convert leading zeros to bytes. */
317 add r3,r8,r0 /* Return address of the matching null byte. */
323 bne cr0, L(loop_null)
336 vcmpequb. v11, v0, v8
338 blt cr6, L(continue1)
343 /* Shift each component into its correct position for merging. */
344 #ifdef __LITTLE_ENDIAN__
351 /* Merge the results and move to a GPR. */
354 #ifdef __LITTLE_ENDIAN__
359 cntlzd r6, r5 /* Count leading zeros before the match. */
361 add r3, r3, r6 /* Compute final length. */
365 #ifndef USE_AS_STRCHRNUL
366 weak_alias (strchr, index)
367 libc_hidden_builtin_def (strchr)