Remove powerpc64 bounded-pointers code.
[glibc.git] / sysdeps / powerpc / powerpc64 / power7 / memrchr.S
blobd24fbbb1b940b70e08a40231725862bd5db67c6e
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/>.  */
20 #include <sysdep.h>
22 /* int [r3] memrchr (char *s [r3], int byte [r4], int size [r5])  */
23         .machine  power7
24 ENTRY (__memrchr)
25         CALL_MCOUNT
26         dcbt    0,r3
27         mr      r7,r3
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.  */
32         rlwimi  r4,r4,8,16,23
33         rlwimi  r4,r4,16,0,15
34         insrdi  r4,r4,32,0
35         bge     cr7,L(proceed)
37         li      r3,-1         /* Make r11 the biggest if r4 <= 0.  */
38 L(proceed):
39         li      r6,-8
40         addi    r9,r3,-1
41         clrrdi  r8,r9,3
42         addi    r8,r8,8
43         neg     r0,r3
44         rlwinm  r0,r0,3,26,28 /* Calculate padding.  */
46         cmpldi  r5,32
47         ble     L(small_range)
49         ldbrx   r12,r8,r6     /* Load reversed doubleword from memory.  */
50         cmpb    r10,r12,r4    /* Check for BYTE in DWORD1.  */
51         sld     r10,r10,r0
52         srd     r10,r10,r0
53         cmpldi  cr7,r10,0     /* If r10 == 0, no BYTEs have been found.  */
54         bne     cr7,L(done)
56         /* Are we done already?  */
57         addi    r9,r8,-8
58         cmpld   cr6,r9,r7
59         ble     cr6,L(null)
61         mtcrf   0x01,r8
62         /* Are we now aligned to a doubleword boundary?  If so, skip to
63            the main loop.  Otherwise, go through the alignment code.  */
64         mr      r8,r9
65         bt      28,L(loop_setup)
67         /* Handle DWORD2 of pair.  */
68         ldbrx   r12,r8,r6
69         cmpb    r10,r12,r4
70         cmpldi  cr7,r10,0
71         bne     cr7,L(done)
73         /* Are we done already.  */
74         addi    r8,r8,-8
75         cmpld   cr6,r8,r7
76         ble     cr6,L(null)
78 L(loop_setup):
79         li      r0,-16
80         sub     r5,r8,r7
81         srdi    r9,r5,4       /* Number of loop iterations.  */
82         mtctr   r9            /* Setup the counter.  */
83         b       L(loop)
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.  */
86         .p2align  5
87 L(loop):
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.  */
92         ldbrx   r12,r8,r6
93         ldbrx   r11,r8,r0
94         addi    r8,r8,-8
95         cmpb    r10,r12,r4
96         cmpb    r9,r11,r4
97         or      r5,r9,r10     /* Merge everything in one doubleword.  */
98         cmpldi  cr7,r5,0
99         bne     cr7,L(found)
100         addi    r8,r8,-8
101         bdnz    L(loop)
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.  */
105         addi    r9,r8,8
106         cmpld   cr6,r9,r7
107         bgt     cr6,L(loop_small)
108         b       L(null)
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.  */
113         .align  4
114 L(found):
115         cmpldi  cr6,r10,0
116         addi    r8,r8,8
117         bne     cr6,L(done)
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
121            pointer.  */
123         mr      r10,r9
124         addi    r8,r8,-8
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
130            range.  */
131 L(done):
132         cntlzd  r0,r10        /* Count leading zeroes before the match.  */
133         srdi    r6,r0,3       /* Convert leading zeroes to bytes.  */
134         addi    r0,r6,1
135         sub     r3,r8,r0
136         cmpld   r3,r7
137         blt     L(null)
138         blr
140         .align  4
141 L(null):
142         li      r3,0
143         blr
145 /* Deals with size <= 32.  */
146         .align  4
147 L(small_range):
148         cmpldi  r5,0
149         beq     L(null)
151         ldbrx   r12,r8,r6     /* Load reversed doubleword from memory.  */
152         cmpb    r10,r12,r4    /* Check for BYTE in DWORD1.  */
153         sld     r10,r10,r0
154         srd     r10,r10,r0
155         cmpldi  cr7,r10,0
156         bne     cr7,L(done)
158         /* Are we done already?  */
159         addi    r8,r8,-8
160         cmpld   r8,r7
161         ble     L(null)
162         b       L(loop_small)
164         .p2align  5
165 L(loop_small):
166         ldbrx   r12,r8,r6
167         cmpb    r10,r12,r4
168         cmpldi  cr6,r10,0
169         bne     cr6,L(done)
170         addi    r8,r8,-8
171         cmpld   r8,r7
172         ble     L(null)
173         b       L(loop_small)
175 END (__memrchr)
176 weak_alias (__memrchr, memrchr)
177 libc_hidden_builtin_def (memrchr)