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/>. */
22 /* int [r3] memrchr (char *s [r3], int byte [r4], int size [r5]) */
28 add r3,r7,r5 /* Calculate the last acceptable address. */
29 cmpld cr7,r3,r7 /* Is the address equal or less than r3? */
31 /* Replicate BYTE to doubleword. */
37 li r3,-1 /* Make r11 the biggest if r4 <= 0. */
44 rlwinm r0,r0,3,26,28 /* Calculate padding. */
49 ldbrx r12,r8,r6 /* Load reversed doubleword from memory. */
50 cmpb r10,r12,r4 /* Check for BYTE in DWORD1. */
53 cmpldi cr7,r10,0 /* If r10 == 0, no BYTEs have been found. */
56 /* Are we done already? */
62 /* Are we now aligned to a doubleword boundary? If so, skip to
63 the main loop. Otherwise, go through the alignment code. */
67 /* Handle DWORD2 of pair. */
73 /* Are we done already. */
81 srdi r9,r5,4 /* Number of loop iterations. */
82 mtctr r9 /* Setup the counter. */
84 /* Main loop to look for BYTE backwards in the string. Since it's a
85 small loop (< 8 instructions), align it to 32-bytes. */
88 /* Load two doublewords, 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. */
97 or r5,r9,r10 /* Merge everything in one doubleword. */
102 /* We're here because the counter reached 0, and that means we
103 didn't have any matches for BYTE in the whole range. Just return
104 the original range. */
107 bgt cr6,L(loop_small)
110 /* OK, one (or both) of the words contains BYTE. Check
111 the first word and decrement the address in case the first
112 word really contains BYTE. */
119 /* BYTE must be in the second word. Adjust the address
120 again and move the result of cmpb to r10 so we can calculate the
126 /* r10 has the output of the cmpb instruction, that is, it contains
127 0xff in the same position as the BYTE in the original
128 word from the string. Use that to calculate the pointer.
129 We need to make sure BYTE is *before* the end of the
132 cntlzd r0,r10 /* Count leading zeroes before the match. */
133 srdi r6,r0,3 /* Convert leading zeroes to bytes. */
145 /* Deals with size <= 32. */
151 ldbrx r12,r8,r6 /* Load reversed doubleword from memory. */
152 cmpb r10,r12,r4 /* Check for BYTE in DWORD1. */
158 /* Are we done already? */
176 weak_alias (__memrchr, memrchr)
177 libc_hidden_builtin_def (memrchr)