powerpc: refactor memchr, memrchr, and rawmemchr IFUNC.
[glibc.git] / sysdeps / powerpc / powerpc64 / power7 / rawmemchr.S
blob48afb759430268890b63d6a2b12bde1dc690b70b
1 /* Optimized rawmemchr 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 /* int [r3] rawmemchr (void *s [r3], int c [r4])  */
24 #ifndef RAWMEMCHR
25 # define RAWMEMCHR __rawmemchr
26 #endif
27         .machine  power7
28 ENTRY (RAWMEMCHR)
29         CALL_MCOUNT 2
30         dcbt    0,r3
31         clrrdi  r8,r3,3       /* Align the address to doubleword boundary.  */
33         /* Replicate byte to doubleword.  */
34         insrdi  r4,r4,8,48
35         insrdi  r4,r4,16,32
36         insrdi  r4,r4,32,0
38         /* Now r4 has a doubleword of c bytes.  */
40         rlwinm  r6,r3,3,26,28 /* Calculate padding.  */
41         ld      r12,0(r8)     /* Load doubleword from memory.  */
42         cmpb    r5,r12,r4     /* Compare each byte against c byte.  */
43 #ifdef __LITTLE_ENDIAN__
44         srd     r5,r5,r6
45         sld     r5,r5,r6
46 #else
47         sld     r5,r5,r6      /* Move left to discard ignored bits.  */
48         srd     r5,r5,r6      /* Bring the bits back as zeros.  */
49 #endif
50         cmpdi   cr7,r5,0      /* If r5 == 0, no c bytes have been found.  */
51         bne     cr7,L(done)
53         mtcrf   0x01,r8
55         /* Are we now aligned to a quadword boundary?  If so, skip to
56            the main loop.  Otherwise, go through the alignment code.  */
58         bt      28,L(loop)
60         /* Handle DWORD2 of pair.  */
61         ldu     r12,8(r8)
62         cmpb    r5,r12,r4
63         cmpdi   cr7,r5,0
64         bne     cr7,L(done)
65         b       L(loop)       /* We branch here (rather than falling through)
66                                  to skip the nops due to heavy alignment
67                                  of the loop below.  */
69         /* Main loop to look for the end of the string.  Since it's a
70            small loop (< 8 instructions), align it to 32-bytes.  */
71         .p2align  5
72 L(loop):
73         /* Load two doublewords, compare and merge in a
74            single register for speed.  This is an attempt
75            to speed up the byte-checking process for bigger strings.  */
76         ld      r12,8(r8)
77         ldu     r11,16(r8)
78         cmpb    r5,r12,r4
79         cmpb    r6,r11,r4
80         or      r7,r5,r6
81         cmpdi   cr7,r7,0
82         beq     cr7,L(loop)
84         /* OK, one (or both) of the doublewords contains a 'c' byte.  Check
85            the first doubleword and decrement the address in case the first
86            doubleword really contains a c byte.  */
88         cmpdi   cr6,r5,0
89         addi    r8,r8,-8
90         bne     cr6,L(done)
92         /* The 'c' byte must be in the second doubleword.  Adjust the address
93            again and move the result of cmpb to r10 so we can calculate the
94            pointer.  */
95         mr      r5,r6
96         addi    r8,r8,8
98         /* r5 has the output of the cmpb instruction, that is, it contains
99            0xff in the same position as the 'c' byte in the original
100            doubleword from the string.  Use that fact to find out what is
101            the position of the byte inside the string.  */
102 L(done):
103 #ifdef __LITTLE_ENDIAN__
104         addi    r0,r5,-1
105         andc    r0,r0,r5
106         popcntd r0,r0         /* Count trailing zeros.  */
107 #else
108         cntlzd  r0,r5         /* Count leading zeros before the match.  */
109 #endif
110         srdi    r0,r0,3       /* Convert leading zeros to bytes.  */
111         add     r3,r8,r0      /* Return address of the matching char.  */
112         blr
113 END (RAWMEMCHR)
114 weak_alias (__rawmemchr,rawmemchr)
115 libc_hidden_builtin_def (__rawmemchr)