Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / powerpc / powerpc32 / strchr.S
blobec687ebca67995d1a4f5251a13191cc132f5b48c
1 /* Optimized strchr implementation for PowerPC.
2    Copyright (C) 1997, 1999, 2000, 2003 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    <http://www.gnu.org/licenses/>.  */
19 #include <sysdep.h>
20 #include <bp-sym.h>
21 #include <bp-asm.h>
23 /* See strlen.s for comments on how this works.  */
25 /* char * [r3] strchr (const char *s [r3] , int c [r4] )  */
27 ENTRY (BP_SYM (strchr))
29 #define rTMP1   r0
30 #define rRTN    r3      /* outgoing result */
31 #if __BOUNDED_POINTERS__
32 # define rSTR   r4
33 # define rCHR   r5      /* byte we're looking for, spread over the whole word */
34 # define rWORD  r8      /* the current word */
35 #else
36 # define rSTR   r8      /* current word pointer */
37 # define rCHR   r4      /* byte we're looking for, spread over the whole word */
38 # define rWORD  r5      /* the current word */
39 #endif
40 #define rCLZB   rCHR    /* leading zero byte count */
41 #define rFEFE   r6      /* constant 0xfefefeff (-0x01010101) */
42 #define r7F7F   r7      /* constant 0x7f7f7f7f */
43 #define rTMP2   r9
44 #define rIGN    r10     /* number of bits we should ignore in the first word */
45 #define rMASK   r11     /* mask with the bits to ignore set to 0 */
46 #define rTMP3   r12
48         CHECK_BOUNDS_LOW (rSTR, rTMP1, rTMP2)
49         STORE_RETURN_BOUNDS (rTMP1, rTMP2)
51         rlwimi  rCHR, rCHR, 8, 16, 23
52         li      rMASK, -1
53         rlwimi  rCHR, rCHR, 16, 0, 15
54         rlwinm  rIGN, rRTN, 3, 27, 28
55         lis     rFEFE, -0x101
56         lis     r7F7F, 0x7f7f
57         clrrwi  rSTR, rRTN, 2
58         addi    rFEFE, rFEFE, -0x101
59         addi    r7F7F, r7F7F, 0x7f7f
60 /* Test the first (partial?) word.  */
61         lwz     rWORD, 0(rSTR)
62         srw     rMASK, rMASK, rIGN
63         orc     rWORD, rWORD, rMASK
64         add     rTMP1, rFEFE, rWORD
65         nor     rTMP2, r7F7F, rWORD
66         and.    rTMP1, rTMP1, rTMP2
67         xor     rTMP3, rCHR, rWORD
68         orc     rTMP3, rTMP3, rMASK
69         b       L(loopentry)
71 /* The loop.  */
73 L(loop):lwzu rWORD, 4(rSTR)
74         and.    rTMP1, rTMP1, rTMP2
75 /* Test for 0.  */
76         add     rTMP1, rFEFE, rWORD
77         nor     rTMP2, r7F7F, rWORD
78         bne     L(foundit)
79         and.    rTMP1, rTMP1, rTMP2
80 /* Start test for the bytes we're looking for.  */
81         xor     rTMP3, rCHR, rWORD
82 L(loopentry):
83         add     rTMP1, rFEFE, rTMP3
84         nor     rTMP2, r7F7F, rTMP3
85         beq     L(loop)
86 /* There is a zero byte in the word, but may also be a matching byte (either
87    before or after the zero byte).  In fact, we may be looking for a
88    zero byte, in which case we return a match.  We guess that this hasn't
89    happened, though.  */
90 L(missed):
91         and.    rTMP1, rTMP1, rTMP2
92         li      rRTN, 0
93         STORE_RETURN_VALUE (rSTR)
94         beqlr
95 /* It did happen. Decide which one was first...
96    I'm not sure if this is actually faster than a sequence of
97    rotates, compares, and branches (we use it anyway because it's shorter).  */
98         and     rFEFE, r7F7F, rWORD
99         or      rMASK, r7F7F, rWORD
100         and     rTMP1, r7F7F, rTMP3
101         or      rIGN, r7F7F, rTMP3
102         add     rFEFE, rFEFE, r7F7F
103         add     rTMP1, rTMP1, r7F7F
104         nor     rWORD, rMASK, rFEFE
105         nor     rTMP2, rIGN, rTMP1
106         cmplw   rWORD, rTMP2
107         bgtlr
108         cntlzw  rCLZB, rTMP2
109         srwi    rCLZB, rCLZB, 3
110         add     rRTN, rSTR, rCLZB
111         CHECK_BOUNDS_HIGH_RTN (rSTR, rTMP2, twlge)
112         STORE_RETURN_VALUE (rSTR)
113         blr
115 L(foundit):
116         and     rTMP1, r7F7F, rTMP3
117         or      rIGN, r7F7F, rTMP3
118         add     rTMP1, rTMP1, r7F7F
119         nor     rTMP2, rIGN, rTMP1
120         cntlzw  rCLZB, rTMP2
121         subi    rSTR, rSTR, 4
122         srwi    rCLZB, rCLZB, 3
123         add     rRTN, rSTR, rCLZB
124         CHECK_BOUNDS_HIGH_RTN (rSTR, rTMP2, twlge)
125         STORE_RETURN_VALUE (rSTR)
126         blr
127 END (BP_SYM (strchr))
129 weak_alias (BP_SYM (strchr), BP_SYM (index))
130 libc_hidden_builtin_def (strchr)