Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / powerpc / powerpc64 / power7 / strrchr.S
blob68565c68bcaad21b096ac7c2ad00c2c34c156746
1 /* Optimized strrchr implementation for PowerPC64/POWER7 using cmpb insn.
2    Copyright (C) 2014-2015 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>
21 /* int [r3] strrchr (char *s [r3], int c [r4])  */
22         .machine  power7
23 ENTRY (strrchr)
24         CALL_MCOUNT 2
25         dcbt    0,r3
26         clrrdi  r8,r3,3       /* Align the address to doubleword boundary.  */
27         cmpdi   cr7,r4,0
28         ld      r12,0(r8)     /* Load doubleword from memory.  */
29         li      r9,0          /* used to store last occurence */
30         li      r0,0          /* Doubleword with null chars to use
31                                  with cmpb.  */
33         rlwinm  r6,r3,3,26,28 /* Calculate padding.  */
35         beq     cr7,L(null_match)
37         /* Replicate byte to doubleword.  */
38         insrdi  r4,r4,8,48
39         insrdi  r4,r4,16,32
40         insrdi  r4,r4,32,0
42         /* r4 is changed now ,if its passed as more chars
43            check for null again */
44         cmpdi   cr7,r4,0
45         beq     cr7,L(null_match)
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 L(align):
71         mtcrf   0x01,r8
73         /* Are we now aligned to a doubleword boundary?  If so, skip to
74            the main loop.  Otherwise, go through the alignment code.  */
76         bt      28,L(loop)
78         /* Handle WORD2 of pair.  */
79         ldu     r12,8(r8)
80         cmpb    r10,r12,r4
81         cmpb    r11,r12,r0
82         or      r5,r10,r11
83         cmpdi   cr7,r5,0
84         bne     cr7,L(done)
85         b       L(loop)       /* We branch here (rather than falling through)
86                                  to skip the nops due to heavy alignment
87                                  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     r7,16(r8)
95         cmpb    r10,r12,r4
96         cmpb    r11,r12,r0
97         cmpb    r6,r7,r4
98         cmpb    r7,r7,r0
99         or      r12,r10,r11
100         or      r5,r6,r7
101         or      r5,r12,r5
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.  */
108         cmpdi   cr6,r12,0
109         addi    r8,r8,-8
110         bne     cr6,L(done)
112         /* The c/null byte must be in the second doubleword.  Adjust the
113            address again and move the result of cmpb to r10 so we can calculate
114            the pointer.  */
116         mr      r10,r6
117         mr      r11,r7
118         addi    r8,r8,8
120         /* r10/r11 have the output of the cmpb instructions, that is,
121            0xff in the same position as the c/null byte in the original
122            doubleword from the string.  Use that to calculate the pointer.  */
124 L(done):
125         /* if there are more than one 0xff in r11, find the first pos of ff
126            in r11 and fill r10 with 0 from that position */
127         cmpdi   cr7,r11,0
128         beq     cr7,L(no_null)
129 #ifdef __LITTLE_ENDIAN__
130         addi    r3,r11,-1
131         andc    r3,r3,r11
132         popcntd r0,r3
133 #else
134         cntlzd  r0,r11
135 #endif
136         subfic  r0,r0,63
137         li      r6,-1
138 #ifdef __LITTLE_ENDIAN__
139         srd     r0,r6,r0
140 #else
141         sld     r0,r6,r0
142 #endif
143         and     r10,r0,r10
144 L(no_null):
145 #ifdef __LITTLE_ENDIAN__
146         cntlzd  r0,r10          /* Count leading zeros before c matches.  */
147         addi    r3,r10,-1
148         andc    r3,r3,r10
149         addi    r10,r11,-1
150         andc    r10,r10,r11
151         cmpld   cr7,r3,r10
152         bgt     cr7,L(no_match)
153 #else
154         addi    r3,r10,-1       /* Count trailing zeros before c matches.  */
155         andc    r3,r3,r10
156         popcntd r0,r3
157         cmpld   cr7,r11,r10
158         bgt     cr7,L(no_match)
159 #endif
160         srdi    r0,r0,3         /* Convert trailing zeros to bytes.  */
161         subfic  r0,r0,7
162         add     r9,r8,r0      /* Return address of the matching c byte
163                                  or null in case c was not found.  */
164         li      r0,0
165         cmpdi   cr7,r11,0     /* If r11 == 0, no null's have been found.  */
166         beq     cr7,L(align)
168         .align  4
169 L(no_match):
170         mr      r3,r9
171         blr
173 /* We are here because strrchr was called with a null byte.  */
174         .align  4
175 L(null_match):
176         /* r0 has a doubleword of null bytes.  */
178         cmpb    r5,r12,r0     /* Compare each byte against null bytes.  */
180         /* Move the doublewords left and right to discard the bits that are
181            not part of the string and bring them back as zeros.  */
182 #ifdef __LITTLE_ENDIAN__
183         srd     r5,r5,r6
184         sld     r5,r5,r6
185 #else
186         sld     r5,r5,r6
187         srd     r5,r5,r6
188 #endif
189         cmpdi   cr7,r5,0      /* If r10 == 0, no c or null bytes
190                                  have been found.  */
191         bne     cr7,L(done_null)
193         mtcrf   0x01,r8
195         /* Are we now aligned to a quadword boundary?  If so, skip to
196            the main loop.  Otherwise, go through the alignment code.  */
198         bt      28,L(loop_null)
200         /* Handle WORD2 of pair.  */
201         ldu     r12,8(r8)
202         cmpb    r5,r12,r0
203         cmpdi   cr7,r5,0
204         bne     cr7,L(done_null)
205         b       L(loop_null)  /* We branch here (rather than falling through)
206                                  to skip the nops due to heavy alignment
207                                  of the loop below.  */
209         /* Main loop to look for the end of the string.  Since it's a
210            small loop (< 8 instructions), align it to 32-bytes.  */
211         .p2align  5
212 L(loop_null):
213         /* Load two doublewords, compare and merge in a
214            single register for speed.  This is an attempt
215            to speed up the null-checking process for bigger strings.  */
216         ld      r12,8(r8)
217         ldu     r11,16(r8)
218         cmpb    r5,r12,r0
219         cmpb    r10,r11,r0
220         or      r6,r5,r10
221         cmpdi   cr7,r6,0
222         beq     cr7,L(loop_null)
224         /* OK, one (or both) of the doublewords contains a null byte.  Check
225            the first doubleword and decrement the address in case the first
226            doubleword really contains a null byte.  */
228         cmpdi   cr6,r5,0
229         addi    r8,r8,-8
230         bne     cr6,L(done_null)
232         /* The null byte must be in the second doubleword.  Adjust the address
233            again and move the result of cmpb to r10 so we can calculate the
234            pointer.  */
236         mr      r5,r10
237         addi    r8,r8,8
239         /* r5 has the output of the cmpb instruction, that is, it contains
240            0xff in the same position as the null byte in the original
241            doubleword from the string.  Use that to calculate the pointer.  */
242 L(done_null):
243 #ifdef __LITTLE_ENDIAN__
244         addi    r0,r5,-1
245         andc    r0,r0,r5
246         popcntd r0,r0
247 #else
248         cntlzd  r0,r5         /* Count leading zeros before the match.  */
249 #endif
250         srdi    r0,r0,3       /* Convert trailing zeros to bytes.  */
251         add     r3,r8,r0      /* Return address of the matching null byte.  */
252         blr
253 END (strrchr)
254 weak_alias (strrchr, rindex)
255 libc_hidden_builtin_def (strrchr)