Define write_profiling functions only in profile library [BZ #31756]
[glibc.git] / sysdeps / powerpc / powerpc64 / power7 / strrchr.S
blobb8b389a275ce44b47bd9db6bb0eb6b1734673ae3
1 /* Optimized strrchr implementation for PowerPC64/POWER7 using cmpb insn.
2    Copyright (C) 2014-2024 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    <https://www.gnu.org/licenses/>.  */
19 #include <sysdep.h>
21 /* int [r3] strrchr (char *s [r3], int c [r4])  */
23 #ifndef STRRCHR
24 # define STRRCHR strrchr
25 #endif
27         .machine  power7
28 ENTRY_TOCLESS (STRRCHR)
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      r9,0          /* used to store last occurrence */
35         li      r0,0          /* Doubleword with null chars to use
36                                  with cmpb.  */
38         rlwinm  r6,r3,3,26,28 /* Calculate padding.  */
40         beq     cr7,L(null_match)
42         /* Replicate byte to doubleword.  */
43         insrdi  r4,r4,8,48
44         insrdi  r4,r4,16,32
45         insrdi  r4,r4,32,0
47         /* r4 is changed now ,if its passed as more chars
48            check for null again */
49         cmpdi   cr7,r4,0
50         beq     cr7,L(null_match)
51         /* Now r4 has a doubleword of c bytes and r0 has
52            a doubleword of null bytes.  */
54         cmpb    r10,r12,r4     /* Compare each byte against c byte.  */
55         cmpb    r11,r12,r0     /* Compare each byte against null byte.  */
57         /* Move the doublewords left and right to discard the bits that are
58            not part of the string and bring them back as zeros.  */
59 #ifdef __LITTLE_ENDIAN__
60         srd     r10,r10,r6
61         srd     r11,r11,r6
62         sld     r10,r10,r6
63         sld     r11,r11,r6
64 #else
65         sld     r10,r10,r6
66         sld     r11,r11,r6
67         srd     r10,r10,r6
68         srd     r11,r11,r6
69 #endif
70         or      r5,r10,r11    /* OR the results to speed things up.  */
71         cmpdi   cr7,r5,0      /* If r5 == 0, no c or null bytes
72                                  have been found.  */
73         bne     cr7,L(done)
75 L(align):
76         mtcrf   0x01,r8
78         /* Are we now aligned to a doubleword boundary?  If so, skip to
79            the main loop.  Otherwise, go through the alignment code.  */
81         bt      28,L(loop)
83         /* Handle WORD2 of pair.  */
84         ldu     r12,8(r8)
85         cmpb    r10,r12,r4
86         cmpb    r11,r12,r0
87         or      r5,r10,r11
88         cmpdi   cr7,r5,0
89         bne     cr7,L(done)
90         b       L(loop)       /* We branch here (rather than falling through)
91                                  to skip the nops due to heavy alignment
92                                  of the loop below.  */
93         .p2align  5
94 L(loop):
95         /* Load two doublewords, compare and merge in a
96            single register for speed.  This is an attempt
97            to speed up the null-checking process for bigger strings.  */
98         ld      r12,8(r8)
99         ldu     r7,16(r8)
100         cmpb    r10,r12,r4
101         cmpb    r11,r12,r0
102         cmpb    r6,r7,r4
103         cmpb    r7,r7,r0
104         or      r12,r10,r11
105         or      r5,r6,r7
106         or      r5,r12,r5
107         cmpdi   cr7,r5,0
108         beq     cr7,L(loop)
110         /* OK, one (or both) of the doublewords contains a c/null byte.  Check
111            the first doubleword and decrement the address in case the first
112            doubleword really contains a c/null byte.  */
113         cmpdi   cr6,r12,0
114         addi    r8,r8,-8
115         bne     cr6,L(done)
117         /* The c/null byte must be in the second doubleword.  Adjust the
118            address again and move the result of cmpb to r10 so we can calculate
119            the pointer.  */
121         mr      r10,r6
122         mr      r11,r7
123         addi    r8,r8,8
125         /* r10/r11 have the output of the cmpb instructions, that is,
126            0xff in the same position as the c/null byte in the original
127            doubleword from the string.  Use that to calculate the pointer.  */
129 L(done):
130         /* if there are more than one 0xff in r11, find the first pos of ff
131            in r11 and fill r10 with 0 from that position */
132         cmpdi   cr7,r11,0
133         beq     cr7,L(no_null)
134 #ifdef __LITTLE_ENDIAN__
135         addi    r3,r11,-1
136         andc    r3,r3,r11
137         popcntd r0,r3
138 #else
139         cntlzd  r0,r11
140 #endif
141         subfic  r0,r0,63
142         li      r6,-1
143 #ifdef __LITTLE_ENDIAN__
144         srd     r0,r6,r0
145 #else
146         sld     r0,r6,r0
147 #endif
148         and     r10,r0,r10
149 L(no_null):
150 #ifdef __LITTLE_ENDIAN__
151         cntlzd  r0,r10          /* Count leading zeros before c matches.  */
152         addi    r3,r10,-1
153         andc    r3,r3,r10
154         addi    r10,r11,-1
155         andc    r10,r10,r11
156         cmpld   cr7,r3,r10
157         bgt     cr7,L(no_match)
158 #else
159         addi    r3,r10,-1       /* Count trailing zeros before c matches.  */
160         andc    r3,r3,r10
161         popcntd r0,r3
162         cmpld   cr7,r11,r10
163         bgt     cr7,L(no_match)
164 #endif
165         srdi    r0,r0,3         /* Convert trailing zeros to bytes.  */
166         subfic  r0,r0,7
167         add     r9,r8,r0      /* Return address of the matching c byte
168                                  or null in case c was not found.  */
169         li      r0,0
170         cmpdi   cr7,r11,0     /* If r11 == 0, no null's have been found.  */
171         beq     cr7,L(align)
173         .align  4
174 L(no_match):
175         mr      r3,r9
176         blr
178 /* We are here because strrchr was called with a null byte.  */
179         .align  4
180 L(null_match):
181         /* r0 has a doubleword of null bytes.  */
183         cmpb    r5,r12,r0     /* Compare each byte against null bytes.  */
185         /* Move the doublewords left and right to discard the bits that are
186            not part of the string and bring them back as zeros.  */
187 #ifdef __LITTLE_ENDIAN__
188         srd     r5,r5,r6
189         sld     r5,r5,r6
190 #else
191         sld     r5,r5,r6
192         srd     r5,r5,r6
193 #endif
194         cmpdi   cr7,r5,0      /* If r10 == 0, no c or null bytes
195                                  have been found.  */
196         bne     cr7,L(done_null)
198         mtcrf   0x01,r8
200         /* Are we now aligned to a quadword boundary?  If so, skip to
201            the main loop.  Otherwise, go through the alignment code.  */
203         bt      28,L(loop_null)
205         /* Handle WORD2 of pair.  */
206         ldu     r12,8(r8)
207         cmpb    r5,r12,r0
208         cmpdi   cr7,r5,0
209         bne     cr7,L(done_null)
210         b       L(loop_null)  /* We branch here (rather than falling through)
211                                  to skip the nops due to heavy alignment
212                                  of the loop below.  */
214         /* Main loop to look for the end of the string.  Since it's a
215            small loop (< 8 instructions), align it to 32-bytes.  */
216         .p2align  5
217 L(loop_null):
218         /* Load two doublewords, compare and merge in a
219            single register for speed.  This is an attempt
220            to speed up the null-checking process for bigger strings.  */
221         ld      r12,8(r8)
222         ldu     r11,16(r8)
223         cmpb    r5,r12,r0
224         cmpb    r10,r11,r0
225         or      r6,r5,r10
226         cmpdi   cr7,r6,0
227         beq     cr7,L(loop_null)
229         /* OK, one (or both) of the doublewords contains a null byte.  Check
230            the first doubleword and decrement the address in case the first
231            doubleword really contains a null byte.  */
233         cmpdi   cr6,r5,0
234         addi    r8,r8,-8
235         bne     cr6,L(done_null)
237         /* The null byte must be in the second doubleword.  Adjust the address
238            again and move the result of cmpb to r10 so we can calculate the
239            pointer.  */
241         mr      r5,r10
242         addi    r8,r8,8
244         /* r5 has the output of the cmpb instruction, that is, it contains
245            0xff in the same position as the null byte in the original
246            doubleword from the string.  Use that to calculate the pointer.  */
247 L(done_null):
248 #ifdef __LITTLE_ENDIAN__
249         addi    r0,r5,-1
250         andc    r0,r0,r5
251         popcntd r0,r0
252 #else
253         cntlzd  r0,r5         /* Count leading zeros before the match.  */
254 #endif
255         srdi    r0,r0,3       /* Convert trailing zeros to bytes.  */
256         add     r3,r8,r0      /* Return address of the matching null byte.  */
257         blr
258 END (STRRCHR)
259 weak_alias (strrchr, rindex)
260 libc_hidden_builtin_def (strrchr)