powerpc: refactor strchr, strchrnul, and strrchr IFUNC.
[glibc.git] / sysdeps / powerpc / powerpc64 / power7 / strchr.S
bloba18e2e101c8b6a0a9ed4ad5ebdea3274c112f942
1 /* Optimized strchr implementation for PowerPC64/POWER7 using cmpb insn.
2    Copyright (C) 2010-2017 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 #ifndef STRCHR
23 # define STRCHR strchr
24 #endif
26 /* int [r3] strchr (char *s [r3], int c [r4])  */
27         .machine  power7
28 ENTRY (STRCHR)
29         CALL_MCOUNT 2
30         dcbt    0,r3
31         clrrdi  r8,r3,3       /* Align the address to doubleword boundary.  */
32         cmpdi   cr7,r4,0
33         ld      r12,0(r8)     /* Load doubleword from memory.  */
34         li      r0,0          /* Doubleword with null chars to use
35                                  with cmpb.  */
37         rlwinm  r6,r3,3,26,28 /* Calculate padding.  */
39         beq     cr7,L(null_match)
41         /* Replicate byte to doubleword.  */
42         insrdi  r4,r4,8,48
43         insrdi  r4,r4,16,32
44         insrdi  r4,r4,32,0
46         /* Now r4 has a doubleword of c bytes and r0 has
47            a doubleword of null bytes.  */
49         cmpb    r10,r12,r4     /* Compare each byte against c byte.  */
50         cmpb    r11,r12,r0     /* Compare each byte against null byte.  */
52         /* Move the doublewords left and right to discard the bits that are
53            not part of the string and bring them back as zeros.  */
54 #ifdef __LITTLE_ENDIAN__
55         srd     r10,r10,r6
56         srd     r11,r11,r6
57         sld     r10,r10,r6
58         sld     r11,r11,r6
59 #else
60         sld     r10,r10,r6
61         sld     r11,r11,r6
62         srd     r10,r10,r6
63         srd     r11,r11,r6
64 #endif
65         or      r5,r10,r11    /* OR the results to speed things up.  */
66         cmpdi   cr7,r5,0      /* If r5 == 0, no c or null bytes
67                                  have been found.  */
68         bne     cr7,L(done)
70         mtcrf   0x01,r8
72         /* Are we now aligned to a doubleword boundary?  If so, skip to
73            the main loop.  Otherwise, go through the alignment code.  */
75         bt      28,L(loop)
77         /* Handle WORD2 of pair.  */
78         ldu     r12,8(r8)
79         cmpb    r10,r12,r4
80         cmpb    r11,r12,r0
81         or      r5,r10,r11
82         cmpdi   cr7,r5,0
83         bne     cr7,L(done)
84         b       L(loop)       /* We branch here (rather than falling through)
85                                  to skip the nops due to heavy alignment
86                                  of the loop below.  */
88         .p2align  5
89 L(loop):
90         /* Load two doublewords, compare and merge in a
91            single register for speed.  This is an attempt
92            to speed up the null-checking process for bigger strings.  */
93         ld      r12,8(r8)
94         ldu     r9,16(r8)
95         cmpb    r10,r12,r4
96         cmpb    r11,r12,r0
97         cmpb    r6,r9,r4
98         cmpb    r7,r9,r0
99         or      r12,r10,r11
100         or      r9,r6,r7
101         or      r5,r12,r9
102         cmpdi   cr7,r5,0
103         beq     cr7,L(loop)
105         /* OK, one (or both) of the doublewords contains a c/null byte.  Check
106            the first doubleword and decrement the address in case the first
107            doubleword really contains a c/null byte.  */
109         cmpdi   cr6,r12,0
110         addi    r8,r8,-8
111         bne     cr6,L(done)
113         /* The c/null byte must be in the second doubleword.  Adjust the
114            address again and move the result of cmpb to r10 so we can calculate
115            the pointer.  */
117         mr      r10,r6
118         mr      r11,r7
119         addi    r8,r8,8
121         /* r10/r11 have the output of the cmpb instructions, that is,
122            0xff in the same position as the c/null byte in the original
123            doubleword from the string.  Use that to calculate the pointer.  */
124 L(done):
125 #ifdef __LITTLE_ENDIAN__
126         addi    r3,r10,-1
127         andc    r3,r3,r10
128         popcntd r0,r3
129         addi    r4,r11,-1
130         andc    r4,r4,r11
131         cmpld   cr7,r3,r4
132         bgt     cr7,L(no_match)
133 #else
134         cntlzd  r0,r10        /* Count leading zeros before c matches.  */
135         cmpld   cr7,r11,r10
136         bgt     cr7,L(no_match)
137 #endif
138         srdi    r0,r0,3       /* Convert leading zeros to bytes.  */
139         add     r3,r8,r0      /* Return address of the matching c byte
140                                  or null in case c was not found.  */
141         blr
143         .align  4
144 L(no_match):
145         li      r3,0
146         blr
148 /* We are here because strchr was called with a null byte.  */
149         .align  4
150 L(null_match):
151         /* r0 has a doubleword of null bytes.  */
153         cmpb    r5,r12,r0     /* Compare each byte against null bytes.  */
155         /* Move the doublewords left and right to discard the bits that are
156            not part of the string and bring them back as zeros.  */
157 #ifdef __LITTLE_ENDIAN__
158         srd     r5,r5,r6
159         sld     r5,r5,r6
160 #else
161         sld     r5,r5,r6
162         srd     r5,r5,r6
163 #endif
164         cmpdi   cr7,r5,0      /* If r10 == 0, no c or null bytes
165                                  have been found.  */
166         bne     cr7,L(done_null)
168         mtcrf   0x01,r8
170         /* Are we now aligned to a quadword boundary?  If so, skip to
171            the main loop.  Otherwise, go through the alignment code.  */
173         bt      28,L(loop_null)
175         /* Handle WORD2 of pair.  */
176         ldu     r12,8(r8)
177         cmpb    r5,r12,r0
178         cmpdi   cr7,r5,0
179         bne     cr7,L(done_null)
180         b       L(loop_null)  /* We branch here (rather than falling through)
181                                  to skip the nops due to heavy alignment
182                                  of the loop below.  */
184         /* Main loop to look for the end of the string.  Since it's a
185            small loop (< 8 instructions), align it to 32-bytes.  */
186         .p2align  5
187 L(loop_null):
188         /* Load two doublewords, compare and merge in a
189            single register for speed.  This is an attempt
190            to speed up the null-checking process for bigger strings.  */
191         ld      r12,8(r8)
192         ldu     r11,16(r8)
193         cmpb    r5,r12,r0
194         cmpb    r10,r11,r0
195         or      r6,r5,r10
196         cmpdi   cr7,r6,0
197         beq     cr7,L(loop_null)
199         /* OK, one (or both) of the doublewords contains a null byte.  Check
200            the first doubleword and decrement the address in case the first
201            doubleword really contains a null byte.  */
203         cmpdi   cr6,r5,0
204         addi    r8,r8,-8
205         bne     cr6,L(done_null)
207         /* The null byte must be in the second doubleword.  Adjust the address
208            again and move the result of cmpb to r10 so we can calculate the
209            pointer.  */
211         mr      r5,r10
212         addi    r8,r8,8
214         /* r5 has the output of the cmpb instruction, that is, it contains
215            0xff in the same position as the null byte in the original
216            doubleword from the string.  Use that to calculate the pointer.  */
217 L(done_null):
218 #ifdef __LITTLE_ENDIAN__
219         addi    r0,r5,-1
220         andc    r0,r0,r5
221         popcntd r0,r0
222 #else
223         cntlzd  r0,r5         /* Count leading zeros before the match.  */
224 #endif
225         srdi    r0,r0,3       /* Convert leading zeros to bytes.  */
226         add     r3,r8,r0      /* Return address of the matching null byte.  */
227         blr
228 END (STRCHR)
229 weak_alias (strchr, index)
230 libc_hidden_builtin_def (strchr)