1 /* Optimized memrchr implementation for PowerPC32/POWER7 using cmpb insn.
2 Copyright (C) 2010-2024 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 /* int [r3] memrchr (char *s [r3], int byte [r4], int size [r5]) */
25 add r7,r3,r5 /* Calculate the last acceptable address. */
31 dcbt r9,r6,16 /* Stream hint, decreasing addresses. */
33 /* Replicate BYTE to word. */
38 rlwinm r0,r0,3,27,28 /* Calculate padding. */
45 #ifdef __LITTLE_ENDIAN__
48 lwbrx r12,0,r8 /* Load reversed word from memory. */
50 cmpb r3,r12,r4 /* Check for BYTE in WORD1. */
52 cmplwi cr7,r3,0 /* If r3 == 0, no BYTEs have been found. */
56 /* Are we now aligned to a doubleword boundary? If so, skip to
57 the main loop. Otherwise, go through the alignment code. */
60 /* Handle WORD2 of pair. */
61 #ifdef __LITTLE_ENDIAN__
72 /* The last word we want to read in the loop below is the one
73 containing the first byte of the string, ie. the word at
74 s & ~3, or r0. The first word read is at r8 - 4, we
75 read 2 * cnt words, so the last word read will be at
76 r8 - 4 - 8 * cnt + 4. Solving for cnt gives
77 cnt = (r8 - r0) / 8 */
80 srwi r9,r5,3 /* Number of loop iterations. */
81 mtctr r9 /* Setup the counter. */
83 /* Main loop to look for BYTE backwards in the string.
84 FIXME: Investigate whether 32 byte align helps with this
85 9 instruction loop. */
88 /* Load two words, compare and merge in a
89 single register for speed. This is an attempt
90 to speed up the byte-checking process for bigger strings. */
92 #ifdef __LITTLE_ENDIAN__
101 or r5,r9,r3 /* Merge everything in one word. */
107 /* We may have one more word to read. */
111 #ifdef __LITTLE_ENDIAN__
123 /* OK, one (or both) of the words contains BYTE. Check
128 /* BYTE must be in the second word. Adjust the address
129 again and move the result of cmpb to r3 so we can calculate the
135 /* r3 has the output of the cmpb instruction, that is, it contains
136 0xff in the same position as BYTE in the original
137 word from the string. Use that to calculate the pointer.
138 We need to make sure BYTE is *before* the end of the
141 cntlzw r9,r3 /* Count leading zeros before the match. */
142 cmplw r8,r0 /* Are we on the last word? */
143 srwi r6,r9,3 /* Convert leading zeros to bytes. */
157 /* Deals with size <= 16. */
163 #ifdef __LITTLE_ENDIAN__
166 lwbrx r12,0,r8 /* Load reversed word from memory. */
168 cmpb r3,r12,r4 /* Check for BYTE in WORD1. */
173 /* Are we done already? */
180 #ifdef __LITTLE_ENDIAN__
194 libc_hidden_def (__memrchr)
195 weak_alias (__memrchr, memrchr)
196 libc_hidden_builtin_def (memrchr)