1 /* Optimized strrchr implementation for PowerPC64/POWER7 using cmpb insn.
2 Copyright (C) 2017-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 /* char *[r3] strrchr (char *s [r3], int c [r4]) */
22 /* TODO: change these to the actual instructions when the minimum required
23 binutils allows it. */
24 #define MTVRD(v,r) .long (0x7c000167 | ((v)<<(32-11)) | ((r)<<(32-16)))
25 #define MFVRD(r,v) .long (0x7c000067 | ((v)<<(32-11)) | ((r)<<(32-16)))
26 #define VBPERMQ(t,a,b) .long (0x1000054c \
30 #define VCLZD(r,v) .long (0x100007c2 | ((r)<<(32-11)) | ((v)<<(32-21)))
31 #define VPOPCNTD(r,v) .long (0x100007c3 | ((r)<<(32-11)) | ((v)<<(32-21)))
32 #define VADDUQM(t,a,b) .long (0x10000100 \
36 #ifdef __LITTLE_ENDIAN__
37 /* Find the match position from v6 and place result in r6. */
38 # define CALCULATE_MATCH() \
39 VBPERMQ(v6, v6, v10); \
40 vsldoi v6, v6, v6, 6; \
45 * Find the first null position to mask bytes after null.
46 * (reg): vcmpequb result: v2 for 1st qw v3 for 2nd qw.
47 * Result placed at v2.
49 # define FIND_NULL_POS(reg) \
51 VADDUQM(v11, reg, v11); \
52 vandc v11, v11, reg; \
55 vcmpequb. v11, v11, v9; \
57 vsldoi v9, v0, v9, 1; \
62 # define CALCULATE_MATCH() \
63 VBPERMQ(v6, v6, v10); \
69 # define FIND_NULL_POS(reg) \
72 vcmpequb. v11, v11, v9; \
74 vsldoi v9, v0, v9, 1; \
78 #endif /* !__LITTLE_ENDIAN__ */
81 # define STRRCHR strrchr
84 ENTRY_TOCLESS (STRRCHR)
87 clrrdi r8,r3,3 /* Align the address to doubleword boundary. */
89 ld r12,0(r8) /* Load doubleword from memory. */
90 li r9,0 /* Used to store last occurence. */
91 li r0,0 /* Doubleword with null chars to use
94 rlwinm r6,r3,3,26,28 /* Calculate padding. */
98 /* Replicate byte to doubleword. */
103 /* r4 is changed now. If it's passed more chars, then
104 check for null again. */
106 beq cr7,L(null_match)
107 /* Now r4 has a doubleword of c bytes and r0 has
108 a doubleword of null bytes. */
110 cmpb r10,r12,r4 /* Compare each byte against c byte. */
111 cmpb r11,r12,r0 /* Compare each byte against null byte. */
113 /* Move the doublewords left and right to discard the bits that are
114 not part of the string and bring them back as zeros. */
115 #ifdef __LITTLE_ENDIAN__
126 or r5,r10,r11 /* OR the results to speed things up. */
127 cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes
134 /* Are we now aligned to a doubleword boundary? If so, skip to
135 the main loop. Otherwise, go through the alignment code. */
139 /* Handle WORD2 of pair. */
146 b L(loop) /* We branch here (rather than falling through)
147 to skip the nops due to heavy alignment
148 of the loop below. */
151 /* Load two doublewords, compare and merge in a
152 single register for speed. This is an attempt
153 to speed up the null-checking process for bigger strings. */
166 /* OK, one (or both) of the doublewords contains a c/null byte. Check
167 the first doubleword and decrement the address in case the first
168 doubleword really contains a c/null byte. */
173 /* The c/null byte must be in the second doubleword. Adjust the
174 address again and move the result of cmpb to r10 so we can calculate
181 /* r10/r11 have the output of the cmpb instructions, that is,
182 0xff in the same position as the c/null byte in the original
183 doubleword from the string. Use that to calculate the pointer. */
186 /* If there are more than one 0xff in r11, find the first position of
187 0xff in r11 and fill r10 with 0 from that position. */
190 #ifdef __LITTLE_ENDIAN__
199 #ifdef __LITTLE_ENDIAN__
206 #ifdef __LITTLE_ENDIAN__
207 cntlzd r0,r10 /* Count leading zeros before c matches. */
215 addi r3,r10,-1 /* Count trailing zeros before c matches. */
221 srdi r0,r0,3 /* Convert trailing zeros to bytes. */
223 add r9,r8,r0 /* Return address of the matching c byte
224 or null in case c was not found. */
226 cmpdi cr7,r11,0 /* If r11 == 0, no null's have been found. */
234 /* Check the first 32B in GPR's and move to vectorized loop. */
238 /* Make sure 32B aligned. */
242 /* Precompute vbpermq constant. */
249 /* Compare 32 bytes in each loop. */
260 vcmpequb. v11, v0, v11
266 /* One (or both) of the quadwords contains c/null. */
269 /* Precompute values used for comparison. */
270 vsl v9, v8, v9 /* v9 = 0x4040404040404040. */
272 vsldoi v8, v0, v8, 1 /* v8 = 0x80. */
274 /* Check if null is in second qw. */
275 vcmpequb. v11, v0, v2
278 /* Null found in first qw. */
280 /* Calculate the null position. */
282 /* Check if null is in the first byte. */
283 vcmpequb. v11, v0, v2
286 /* Mask unwanted bytes after null. */
287 #ifdef __LITTLE_ENDIAN__
294 vcmpequb. v11, v0, v6
296 /* Found a match before null. */
304 vcmpequb. v11, v0, v2
305 blt cr6, L(no_match1)
307 /* Mask unwanted bytes after null. */
308 #ifdef __LITTLE_ENDIAN__
315 vcmpequb. v11, v0, v7
316 blt cr6, L(no_match1)
321 vcmpequb. v11, v0, v6
323 /* Found a match before null. */
329 /* One (or both) of the quadwords contains a match. */
333 /* Match found in second qw. */
339 add r9, r8, r6 /* Compute final length. */
341 /* We are here because strrchr was called with a null byte. */
344 /* r0 has a doubleword of null bytes. */
346 cmpb r5,r12,r0 /* Compare each byte against null bytes. */
348 /* Move the doublewords left and right to discard the bits that are
349 not part of the string and bring them back as zeros. */
350 #ifdef __LITTLE_ENDIAN__
357 cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes
363 /* Are we now aligned to a quadword boundary? If so, skip to
364 the main loop. Otherwise, go through the alignment code. */
366 bne cr0, L(loop_null)
368 /* Handle WORD2 of pair. */
373 b L(loop_null) /* We branch here (rather than falling through)
374 to skip the nops due to heavy alignment
375 of the loop below. */
377 /* Main loop to look for the end of the string. Since it's a
378 small loop (< 8 instructions), align it to 32-bytes. */
381 /* Load two doublewords, compare and merge in a
382 single register for speed. This is an attempt
383 to speed up the null-checking process for bigger strings. */
392 /* OK, one (or both) of the doublewords contains a null byte. Check
393 the first doubleword and decrement the address in case the first
394 doubleword really contains a null byte. */
400 /* The null byte must be in the second doubleword. Adjust the address
401 again and move the result of cmpb to r10 so we can calculate the
407 /* r5 has the output of the cmpb instruction, that is, it contains
408 0xff in the same position as the null byte in the original
409 doubleword from the string. Use that to calculate the pointer. */
411 #ifdef __LITTLE_ENDIAN__
416 cntlzd r0,r5 /* Count leading zeros before the match. */
418 srdi r0,r0,3 /* Convert trailing zeros to bytes. */
419 add r3,r8,r0 /* Return address of the matching null byte. */
421 /* Check the first 32B in GPR's and move to vectorized loop. */
425 /* Make sure 32B aligned. */
427 bne cr0, L(loop_null)
429 /* Precompute vbpermq constant. */
434 /* Compare 32 bytes in each loop. */
441 vcmpequb. v11, v0, v8
443 blt cr6, L(continue1)
447 /* Shift each component into its correct position for merging. */
448 #ifdef __LITTLE_ENDIAN__
454 /* Merge the results and move to a GPR. */
457 #ifdef __LITTLE_ENDIAN__
462 cntlzd r6, r5 /* Count leading zeros before the match. */
464 add r3, r3, r6 /* Compute final length. */
466 END_GEN_TB (STRRCHR, TB_TOCLESS)
467 weak_alias (strrchr, rindex)
468 libc_hidden_builtin_def (strrchr)