Fix spelling errors in sysdeps/powerpc files.
[glibc.git] / sysdeps / powerpc / powerpc32 / power7 / memchr.S
blob3d8389e95c7dc2ce395ba9be376334b0dc85dd84
1 /* Optimized memchr implementation for PowerPC32/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>
21 #include <bp-sym.h>
22 #include <bp-asm.h>
24 /* int [r3] memchr (char *s [r3], int byte [r4], int size [r5])  */
25         .machine  power7
26 ENTRY (BP_SYM (__memchr))
27         CALL_MCOUNT
28         dcbt    0,r3
29         clrrwi  r8,r3,2
30         rlwimi  r4,r4,8,16,23
31         rlwimi  r4,r4,16,0,15
32         add     r7,r3,r5      /* Calculate the last acceptable address.  */
33         cmplwi  r5,16
34         ble     L(small_range)
36         cmplw   cr7,r3,r7     /* Compare the starting address (r3) with the
37                                  ending address (r7).  If (r3 >= r7), the size
38                                  passed in is zero or negative.  */
39         ble     cr7,L(proceed)
41         li      r7,-1         /* Artificially set our ending address (r7)
42                                  such that we will exit early. */
43 L(proceed):
44         rlwinm  r6,r3,3,27,28 /* Calculate padding.  */
45         cmpli   cr6,r6,0      /* cr6 == Do we have padding?  */
46         lwz     r12,0(r8)     /* Load word from memory.  */
47         cmpb    r10,r12,r4    /* Check for BYTEs in WORD1.  */
48         beq     cr6,L(proceed_no_padding)
49         slw     r10,r10,r6
50         srw     r10,r10,r6
51 L(proceed_no_padding):
52         cmplwi  cr7,r10,0     /* If r10 == 0, no BYTEs have been found.  */
53         bne     cr7,L(done)
55         /* Are we done already?  */
56         addi    r9,r8,4
57         cmplw   cr6,r9,r7
58         bge     cr6,L(null)
60         mtcrf   0x01,r8
61         /* Are we now aligned to a doubleword boundary?  If so, skip to
62            the main loop.  Otherwise, go through the alignment code.  */
64         bt      29,L(loop_setup)
66         /* Handle WORD2 of pair.  */
67         lwzu    r12,4(r8)
68         cmpb    r10,r12,r4
69         cmplwi  cr7,r10,0
70         bne     cr7,L(done)
72         /* Are we done already?  */
73         addi    r9,r8,4
74         cmplw   cr6,r9,r7
75         bge     cr6,L(null)
77 L(loop_setup):
78         sub     r5,r7,r9
79         srwi    r6,r5,3       /* Number of loop iterations.  */
80         mtctr   r6            /* Setup the counter.  */
81         b       L(loop)
82         /* Main loop to look for BYTE backwards in the string.  Since
83            it's a small loop (< 8 instructions), align it to 32-bytes.  */
84         .p2align  5
85 L(loop):
86         /* Load two words, compare and merge in a
87            single register for speed.  This is an attempt
88            to speed up the byte-checking process for bigger strings.  */
89         lwz     r12,4(r8)
90         lwzu    r11,8(r8)
91         cmpb    r10,r12,r4
92         cmpb    r9,r11,r4
93         or      r5,r9,r10     /* Merge everything in one word.  */
94         cmplwi  cr7,r5,0
95         bne     cr7,L(found)
96         bdnz    L(loop)
98         /* We're here because the counter reached 0, and that means we
99            didn't have any matches for BYTE in the whole range.  */
100         subi    r11,r7,4
101         cmplw   cr6,r8,r11
102         blt     cr6,L(loop_small)
103         b       L(null)
105         /* OK, one (or both) of the words contains BYTE.  Check
106            the first word and decrement the address in case the first
107            word really contains BYTE.  */
108         .align  4
109 L(found):
110         cmplwi  cr6,r10,0
111         addi    r8,r8,-4
112         bne     cr6,L(done)
114         /* BYTE must be in the second word.  Adjust the address
115            again and move the result of cmpb to r10 so we can calculate the
116            pointer.  */
118         mr      r10,r9
119         addi    r8,r8,4
121         /* r10 has the output of the cmpb instruction, that is, it contains
122            0xff in the same position as BYTE in the original
123            word from the string.  Use that to calculate the pointer.
124            We need to make sure BYTE is *before* the end of the range.  */
125 L(done):
126         cntlzw  r0,r10        /* Count leading zeroes before the match.  */
127         srwi    r0,r0,3       /* Convert leading zeroes to bytes.  */
128         add     r3,r8,r0
129         cmplw   r3,r7
130         bge     L(null)
131         blr
133         .align  4
134 L(null):
135         li      r3,0
136         blr
138 /* Deals with size <= 16.  */
139         .align  4
140 L(small_range):
141         cmplwi  r5,0
142         rlwinm  r6,r3,3,27,28 /* Calculate padding.  */
143         beq     L(null)       /* This branch is for the cmplwi r5,0 above */
144         lwz     r12,0(r8)     /* Load word from memory.  */
145         cmplwi  cr6,r6,0      /* cr6 == Do we have padding?  */
146         cmpb    r10,r12,r4    /* Check for BYTE in DWORD1.  */
147         beq     cr6,L(small_no_padding)
148         slw     r10,r10,r6
149         srw     r10,r10,r6
150 L(small_no_padding):
151         cmplwi  cr7,r10,0
152         bne     cr7,L(done)
154         /* Are we done already?  */
155         addi    r9,r8,4
156         cmplw   r9,r7
157         bge     L(null)
159 L(loop_small):                /* loop_small has been unrolled.  */
160         lwzu    r12,4(r8)
161         cmpb    r10,r12,r4
162         addi    r9,r8,4
163         cmplwi  cr6,r10,0
164         cmplw   r9,r7
165         bne     cr6,L(done)
166         bge     L(null)
168         lwzu    r12,4(r8)
169         cmpb    r10,r12,r4
170         addi    r9,r8,4
171         cmplwi  cr6,r10,0
172         cmplw   r9,r7
173         bne     cr6,L(done)
174         bge     L(null)
176         lwzu    r12,4(r8)
177         cmpb    r10,r12,r4
178         addi    r9,r8,4
179         cmplwi  cr6,r10,0
180         cmplw   r9,r7
181         bne     cr6,L(done)
182         bge     L(null)
184         lwzu    r12,4(r8)
185         cmpb    r10,r12,r4
186         addi    r9,r8,4
187         cmplwi  cr6,r10,0
188         cmplw   r9,r7
189         bne     cr6,L(done)
190         bge     L(null)
192         /* For most cases we will never get here.  Under some combinations of
193            padding + length there is a leftover word that still needs to be
194            checked.  */
195         lwzu    r12,4(r8)
196         cmpb    r10,r12,r4
197         addi    r9,r8,4
198         cmplwi  cr6,r10,0
199         bne     cr6,L(done)
201         /* save a branch and exit directly */
202         li      r3,0
203         blr
205 END (BP_SYM (__memchr))
206 weak_alias (BP_SYM (__memchr), BP_SYM(memchr))
207 libc_hidden_builtin_def (memchr)