1 /* Optimized memrchr implementation for PowerPC64/POWER7 using cmpb insn.
2 Copyright (C) 2010-2013 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/>. */
24 /* int [r3] memrchr (char *s [r3], int byte [r4], int size [r5]) */
26 ENTRY (BP_SYM (__memrchr))
30 add r3,r7,r5 /* Calculate the last acceptable address. */
31 cmpld cr7,r3,r7 /* Is the address equal or less than r3? */
33 /* Replicate BYTE to doubleword. */
39 li r3,-1 /* Make r11 the biggest if r4 <= 0. */
46 rlwinm r0,r0,3,26,28 /* Calculate padding. */
51 ldbrx r12,r8,r6 /* Load reversed doubleword from memory. */
52 cmpb r10,r12,r4 /* Check for BYTE in DWORD1. */
55 cmpldi cr7,r10,0 /* If r10 == 0, no BYTEs have been found. */
58 /* Are we done already? */
64 /* Are we now aligned to a doubleword boundary? If so, skip to
65 the main loop. Otherwise, go through the alignment code. */
69 /* Handle DWORD2 of pair. */
75 /* Are we done already. */
83 srdi r9,r5,4 /* Number of loop iterations. */
84 mtctr r9 /* Setup the counter. */
86 /* Main loop to look for BYTE backwards in the string. Since it's a
87 small loop (< 8 instructions), align it to 32-bytes. */
90 /* Load two doublewords, compare and merge in a
91 single register for speed. This is an attempt
92 to speed up the byte-checking process for bigger strings. */
99 or r5,r9,r10 /* Merge everything in one doubleword. */
104 /* We're here because the counter reached 0, and that means we
105 didn't have any matches for BYTE in the whole range. Just return
106 the original range. */
109 bgt cr6,L(loop_small)
112 /* OK, one (or both) of the words contains BYTE. Check
113 the first word and decrement the address in case the first
114 word really contains BYTE. */
121 /* BYTE must be in the second word. Adjust the address
122 again and move the result of cmpb to r10 so we can calculate the
128 /* r10 has the output of the cmpb instruction, that is, it contains
129 0xff in the same position as the BYTE in the original
130 word from the string. Use that to calculate the pointer.
131 We need to make sure BYTE is *before* the end of the
134 cntlzd r0,r10 /* Count leading zeroes before the match. */
135 srdi r6,r0,3 /* Convert leading zeroes to bytes. */
147 /* Deals with size <= 32. */
153 ldbrx r12,r8,r6 /* Load reversed doubleword from memory. */
154 cmpb r10,r12,r4 /* Check for BYTE in DWORD1. */
160 /* Are we done already? */
177 END (BP_SYM (__memrchr))
178 weak_alias (BP_SYM (__memrchr), BP_SYM(memrchr))
179 libc_hidden_builtin_def (memrchr)