powerpc64: strchr/strchrnul optimization for power8
[glibc.git] / sysdeps / powerpc / powerpc64 / power8 / strchr.S
blob331d0a6c487e9c7138b09aa92d30d239c9f7d21c
1 /* Optimized strchr implementation for PowerPC64/POWER8.
2    Copyright (C) 2016 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 #ifdef USE_AS_STRCHRNUL
22 # define FUNC_NAME __strchrnul
23 #else
24 # define FUNC_NAME strchr
25 #endif
26 /* int [r3] strchr (char *s [r3], int c [r4])  */
27 /* TODO: change these to the actual instructions when the minimum required
28    binutils allows it.  */
29 #define MTVRD(v,r) .long (0x7c000167 | ((v)<<(32-11)) | ((r)<<(32-16)))
30 #define MFVRD(r,v) .long (0x7c000067 | ((v)<<(32-11)) | ((r)<<(32-16)))
31 #define VBPERMQ(t,a,b)  .long (0x1000054c \
32                         | ((t)<<(32-11)) \
33                         | ((a)<<(32-16)) \
34                         | ((b)<<(32-21)) )
35 /* TODO: change this to .machine power8 when the minimum required binutils
36    allows it.  */
37         .machine  power7
38 ENTRY (FUNC_NAME)
39         CALL_MCOUNT 2
40         dcbt    0,r3
41         clrrdi  r8,r3,3       /* Align the address to doubleword boundary.  */
42         cmpdi   cr7,r4,0
43         ld      r12,0(r8)     /* Load doubleword from memory.  */
44         li      r0,0          /* Doubleword with null chars to use
45                                  with cmpb.  */
47         rlwinm  r6,r3,3,26,28 /* Calculate padding.  */
49         beq     cr7,L(null_match)
51         /* Replicate byte to doubleword.  */
52         insrdi  r4,r4,8,48
53         insrdi  r4,r4,16,32
54         insrdi  r4,r4,32,0
56         /* Now r4 has a doubleword of c bytes and r0 has
57            a doubleword of null bytes.  */
59         cmpb    r10,r12,r4     /* Compare each byte against c byte.  */
60         cmpb    r11,r12,r0     /* Compare each byte against null byte.  */
62         /* Move the doublewords left and right to discard the bits that are
63            not part of the string and bring them back as zeros.  */
64 #ifdef __LITTLE_ENDIAN__
65         srd     r10,r10,r6
66         srd     r11,r11,r6
67         sld     r10,r10,r6
68         sld     r11,r11,r6
69 #else
70         sld     r10,r10,r6
71         sld     r11,r11,r6
72         srd     r10,r10,r6
73         srd     r11,r11,r6
74 #endif
75         or      r5,r10,r11    /* OR the results to speed things up.  */
76         cmpdi   cr7,r5,0      /* If r5 == 0, no c or null bytes
77                                  have been found.  */
78         bne     cr7,L(done)
80         mtcrf   0x01,r8
82         /* Are we now aligned to a doubleword boundary?  If so, skip to
83            the main loop.  Otherwise, go through the alignment code.  */
85         bt      28,L(loop)
87         /* Handle WORD2 of pair.  */
88         ldu     r12,8(r8)
89         cmpb    r10,r12,r4
90         cmpb    r11,r12,r0
91         or      r5,r10,r11
92         cmpdi   cr7,r5,0
93         bne     cr7,L(done)
94         b       L(loop)       /* We branch here (rather than falling through)
95                                  to skip the nops due to heavy alignment
96                                  of the loop below.  */
98         .p2align  5
99 L(loop):
100         /* Load two doublewords, compare and merge in a
101            single register for speed.  This is an attempt
102            to speed up the null-checking process for bigger strings.  */
103         ld      r12,8(r8)
104         ldu     r9,16(r8)
105         cmpb    r10,r12,r4
106         cmpb    r11,r12,r0
107         cmpb    r6,r9,r4
108         cmpb    r7,r9,r0
109         or      r5,r10,r11
110         or      r9,r6,r7
111         or      r12,r5,r9
112         cmpdi   cr7,r12,0
113         beq     cr7,L(vector)
114         /* OK, one (or both) of the doublewords contains a c/null byte.  Check
115            the first doubleword and decrement the address in case the first
116            doubleword really contains a c/null byte.  */
118         cmpdi   cr6,r5,0
119         addi    r8,r8,-8
120         bne     cr6,L(done)
122         /* The c/null byte must be in the second doubleword.  Adjust the
123            address again and move the result of cmpb to r10 so we can calculate
124            the pointer.  */
126         mr      r10,r6
127         mr      r11,r7
128         addi    r8,r8,8
129 #ifdef USE_AS_STRCHRNUL
130         mr      r5, r9
131 #endif
132         /* r10/r11 have the output of the cmpb instructions, that is,
133            0xff in the same position as the c/null byte in the original
134            doubleword from the string.  Use that to calculate the pointer.  */
135 L(done):
136 #ifdef USE_AS_STRCHRNUL
137         mr      r10, r5
138 #endif
139 #ifdef __LITTLE_ENDIAN__
140         addi    r3,r10,-1
141         andc    r3,r3,r10
142         popcntd r0,r3
143 # ifndef USE_AS_STRCHRNUL
144         addi    r4,r11,-1
145         andc    r4,r4,r11
146         cmpld   cr7,r3,r4
147         bgt     cr7,L(no_match)
148 # endif
149 #else
150         cntlzd  r0,r10        /* Count leading zeros before c matches.  */
151 # ifndef USE_AS_STRCHRNUL
152         cmpld   cr7,r11,r10
153         bgt     cr7,L(no_match)
154 # endif
155 #endif
156         srdi    r0,r0,3       /* Convert leading zeros to bytes.  */
157         add     r3,r8,r0      /* Return address of the matching c byte
158                                  or null in case c was not found.  */
159         blr
161         /* Check the first 32B in GPR's and move to vectorized loop.  */
162         .p2align  5
163 L(vector):
164         addi    r3, r8, 8
165         andi.   r10, r3, 31
166         bne     cr0, L(loop)
167         vspltisb        v0, 0
168         /* Precompute vbpermq constant.  */
169         vspltisb        v10, 3
170         lvsl    v11, r0, r0
171         vslb    v10, v11, v10
172         MTVRD(v1,r4)
173         li      r5, 16
174         vspltb  v1, v1, 7
175         /* Compare 32 bytes in each loop.  */
176 L(continue):
177         lvx     v4, 0, r3
178         lvx     v5, r3, r5
179         vcmpequb        v2, v0, v4
180         vcmpequb        v3, v0, v5
181         vcmpequb        v6, v1, v4
182         vcmpequb        v7, v1, v5
183         vor     v8, v2, v3
184         vor     v9, v6, v7
185         vor     v11, v8, v9
186         vcmpequb.       v11, v0, v11
187         addi    r3, r3, 32
188         blt     cr6, L(continue)
189         /* One (or both) of the quadwords contains a c/null byte.  */
190         addi    r3, r3, -32
191 #ifndef USE_AS_STRCHRNUL
192         vcmpequb.       v11, v0, v9
193         blt     cr6, L(no_match)
194 #endif
195         /* Permute the first bit of each byte into bits 48-63.  */
196         VBPERMQ(v2, v2, v10)
197         VBPERMQ(v3, v3, v10)
198         VBPERMQ(v6, v6, v10)
199         VBPERMQ(v7, v7, v10)
200         /* Shift each component into its correct position for merging.  */
201 #ifdef __LITTLE_ENDIAN__
202         vsldoi  v3, v3, v3, 2
203         vsldoi  v7, v7, v7, 2
204 #else
205         vsldoi  v2, v2, v2, 6
206         vsldoi  v3, v3, v3, 4
207         vsldoi  v6, v6, v6, 6
208         vsldoi  v7, v7, v7, 4
209 #endif
211         /* Merge the results and move to a GPR.  */
212         vor     v1, v3, v2
213         vor     v2, v6, v7
214         vor     v4, v1, v2
215         MFVRD(r5, v4)
216 #ifdef __LITTLE_ENDIAN__
217         addi    r6, r5, -1
218         andc    r6, r6, r5
219         popcntd r6, r6
220 #else
221         cntlzd  r6, r5  /* Count leading zeros before the match.  */
222 #endif
223         add     r3, r3, r6      /* Compute final length.  */
224         /* Return NULL if null found before c.  */
225 #ifndef USE_AS_STRCHRNUL
226         lbz     r4, 0(r3)
227         cmpdi   cr7, r4, 0
228         beq     cr7, L(no_match)
229 #endif
230         blr
232 #ifndef USE_AS_STRCHRNUL
233         .align  4
234 L(no_match):
235         li      r3,0
236         blr
237 #endif
239 /* We are here because strchr was called with a null byte.  */
240         .align  4
241 L(null_match):
242         /* r0 has a doubleword of null bytes.  */
244         cmpb    r5,r12,r0     /* Compare each byte against null bytes.  */
246         /* Move the doublewords left and right to discard the bits that are
247            not part of the string and bring them back as zeros.  */
248 #ifdef __LITTLE_ENDIAN__
249         srd     r5,r5,r6
250         sld     r5,r5,r6
251 #else
252         sld     r5,r5,r6
253         srd     r5,r5,r6
254 #endif
255         cmpdi   cr7,r5,0      /* If r10 == 0, no c or null bytes
256                                  have been found.  */
257         bne     cr7,L(done_null)
259         mtcrf   0x01,r8
261         /* Are we now aligned to a quadword boundary?  If so, skip to
262            the main loop.  Otherwise, go through the alignment code.  */
264         bt      28,L(loop_null)
266         /* Handle WORD2 of pair.  */
267         ldu     r12,8(r8)
268         cmpb    r5,r12,r0
269         cmpdi   cr7,r5,0
270         bne     cr7,L(done_null)
271         b       L(loop_null)  /* We branch here (rather than falling through)
272                                  to skip the nops due to heavy alignment
273                                  of the loop below.  */
275         /* Main loop to look for the end of the string.  Since it's a
276            small loop (< 8 instructions), align it to 32-bytes.  */
277         .p2align  5
278 L(loop_null):
279         /* Load two doublewords, compare and merge in a
280            single register for speed.  This is an attempt
281            to speed up the null-checking process for bigger strings.  */
282         ld      r12,8(r8)
283         ldu     r11,16(r8)
284         cmpb    r5,r12,r0
285         cmpb    r10,r11,r0
286         or      r6,r5,r10
287         cmpdi   cr7,r6,0
288         beq     cr7,L(vector1)
290         /* OK, one (or both) of the doublewords contains a null byte.  Check
291            the first doubleword and decrement the address in case the first
292            doubleword really contains a null byte.  */
294         cmpdi   cr6,r5,0
295         addi    r8,r8,-8
296         bne     cr6,L(done_null)
298         /* The null byte must be in the second doubleword.  Adjust the address
299            again and move the result of cmpb to r10 so we can calculate the
300            pointer.  */
302         mr      r5,r10
303         addi    r8,r8,8
305         /* r5 has the output of the cmpb instruction, that is, it contains
306            0xff in the same position as the null byte in the original
307            doubleword from the string.  Use that to calculate the pointer.  */
308 L(done_null):
309 #ifdef __LITTLE_ENDIAN__
310         addi    r0,r5,-1
311         andc    r0,r0,r5
312         popcntd r0,r0
313 #else
314         cntlzd  r0,r5         /* Count leading zeros before the match.  */
315 #endif
316         srdi    r0,r0,3       /* Convert leading zeros to bytes.  */
317         add     r3,r8,r0      /* Return address of the matching null byte.  */
318         blr
319         .p2align  5
320 L(vector1):
321         addi    r3, r8, 8
322         andi.   r10, r3, 31
323         bne     cr0, L(loop_null)
324         vspltisb        v8, -1
325         vspltisb        v0, 0
326         vspltisb        v10, 3
327         lvsl    v11, r0, r0
328         vslb    v10, v11, v10
329         li      r5, 16
330 L(continue1):
331         lvx     v4, 0, r3
332         lvx     v5, r3, r5
333         vcmpequb        v2, v0, v4
334         vcmpequb        v3, v0, v5
335         vor     v8, v2, v3
336         vcmpequb.       v11, v0, v8
337         addi    r3, r3, 32
338         blt     cr6, L(continue1)
339         addi    r3, r3, -32
340 L(end1):
341         VBPERMQ(v2, v2, v10)
342         VBPERMQ(v3, v3, v10)
343         /* Shift each component into its correct position for merging.  */
344 #ifdef __LITTLE_ENDIAN__
345         vsldoi  v3, v3, v3, 2
346 #else
347         vsldoi  v2, v2, v2, 6
348         vsldoi  v3, v3, v3, 4
349 #endif
351         /* Merge the results and move to a GPR.  */
352         vor     v4, v3, v2
353         MFVRD(r5, v4)
354 #ifdef __LITTLE_ENDIAN__
355         addi    r6, r5, -1
356         andc    r6, r6, r5
357         popcntd r6, r6
358 #else
359         cntlzd  r6, r5  /* Count leading zeros before the match.  */
360 #endif
361         add     r3, r3, r6      /* Compute final length.  */
362         blr
363 END (FUNC_NAME)
365 #ifndef USE_AS_STRCHRNUL
366 weak_alias (strchr, index)
367 libc_hidden_builtin_def (strchr)
368 #endif