1 /* Optimized memrchr implementation for PowerPC32/POWER7 using cmpb insn.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
3 Contributed by Luis Machado <luisgpm@br.ibm.com>.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 /* int [r3] memrchr (char *s [r3], int byte [r4], int size [r5]) */
26 add r7,r3,r5 /* Calculate the last acceptable address. */
32 dcbt r9,r6,16 /* Stream hint, decreasing addresses. */
34 /* Replicate BYTE to word. */
39 rlwinm r0,r0,3,27,28 /* Calculate padding. */
46 #ifdef __LITTLE_ENDIAN__
49 lwbrx r12,0,r8 /* Load reversed word from memory. */
51 cmpb r3,r12,r4 /* Check for BYTE in WORD1. */
53 cmplwi cr7,r3,0 /* If r3 == 0, no BYTEs have been found. */
57 /* Are we now aligned to a doubleword boundary? If so, skip to
58 the main loop. Otherwise, go through the alignment code. */
61 /* Handle WORD2 of pair. */
62 #ifdef __LITTLE_ENDIAN__
73 /* The last word we want to read in the loop below is the one
74 containing the first byte of the string, ie. the word at
75 s & ~3, or r0. The first word read is at r8 - 4, we
76 read 2 * cnt words, so the last word read will be at
77 r8 - 4 - 8 * cnt + 4. Solving for cnt gives
78 cnt = (r8 - r0) / 8 */
81 srwi r9,r5,3 /* Number of loop iterations. */
82 mtctr r9 /* Setup the counter. */
84 /* Main loop to look for BYTE backwards in the string.
85 FIXME: Investigate whether 32 byte align helps with this
86 9 instruction loop. */
89 /* Load two words, compare and merge in a
90 single register for speed. This is an attempt
91 to speed up the byte-checking process for bigger strings. */
93 #ifdef __LITTLE_ENDIAN__
102 or r5,r9,r3 /* Merge everything in one word. */
108 /* We may have one more word to read. */
112 #ifdef __LITTLE_ENDIAN__
124 /* OK, one (or both) of the words contains BYTE. Check
129 /* BYTE must be in the second word. Adjust the address
130 again and move the result of cmpb to r3 so we can calculate the
136 /* r3 has the output of the cmpb instruction, that is, it contains
137 0xff in the same position as BYTE in the original
138 word from the string. Use that to calculate the pointer.
139 We need to make sure BYTE is *before* the end of the
142 cntlzw r9,r3 /* Count leading zeros before the match. */
143 cmplw r8,r0 /* Are we on the last word? */
144 srwi r6,r9,3 /* Convert leading zeros to bytes. */
158 /* Deals with size <= 16. */
164 #ifdef __LITTLE_ENDIAN__
167 lwbrx r12,0,r8 /* Load reversed word from memory. */
169 cmpb r3,r12,r4 /* Check for BYTE in WORD1. */
174 /* Are we done already? */
181 #ifdef __LITTLE_ENDIAN__
195 weak_alias (__memrchr, memrchr)
196 libc_hidden_builtin_def (memrchr)