powerpc: POWER7 strcpy optimization for unaligned strings
[glibc.git] / sysdeps / powerpc / powerpc64 / power7 / strnlen.S
blob7993dae69ed8ca62b51fb4a91105e60c4aaf5be5
1 /* Optimized strnlen implementation for PowerPC64/POWER7 using cmpb insn.
2    Copyright (C) 2010-2014 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] strnlen (char *s [r3], int size [r4])  */
23         .machine  power7
24 ENTRY (__strnlen)
25         CALL_MCOUNT 2
26         dcbt    0,r3
27         clrrdi  r8,r3,3
28         add     r7,r3,r4      /* Calculate the last acceptable address.  */
29         cmpldi  r4,32
30         li      r0,0          /* Doubleword with null chars.  */
31         addi    r7,r7,-1
33         /* If we have less than 33 bytes to search, skip to a faster code.  */
34         ble     L(small_range)
36         rlwinm  r6,r3,3,26,28 /* Calculate padding.  */
37         ld      r12,0(r8)     /* Load doubleword from memory.  */
38         cmpb    r10,r12,r0    /* Check for null bytes in DWORD1.  */
39 #ifdef __LITTLE_ENDIAN__
40         srd     r10,r10,r6
41         sld     r10,r10,r6
42 #else
43         sld     r10,r10,r6
44         srd     r10,r10,r6
45 #endif
46         cmpldi  cr7,r10,0     /* If r10 == 0, no null's have been found.  */
47         bne     cr7,L(done)
49         clrrdi  r7,r7,3       /* Address of last doubleword.  */
50         mtcrf   0x01,r8
51         /* Are we now aligned to a quadword boundary?  If so, skip to
52            the main loop.  Otherwise, go through the alignment code.  */
54         bt      28,L(loop_setup)
56         /* Handle DWORD2 of pair.  */
57         ldu     r12,8(r8)
58         cmpb    r10,r12,r0
59         cmpldi  cr7,r10,0
60         bne     cr7,L(done)
62 L(loop_setup):
63         /* The last dword we want to read in the loop below is the one
64            containing the last byte of the string, ie. the dword at
65            (s + size - 1) & ~7, or r7.  The first dword read is at
66            r8 + 8, we read 2 * cnt dwords, so the last dword read will
67            be at r8 + 8 + 16 * cnt - 8.  Solving for cnt gives
68            cnt = (r7 - r8) / 16  */
69         sub     r5,r7,r8
70         srdi    r6,r5,4       /* Number of loop iterations.  */
71         mtctr   r6            /* Setup the counter.  */
73         /* Main loop to look for the null byte in the string.  Since
74            it's a small loop (< 8 instructions), align it to 32-bytes.  */
75         .p2align  5
76 L(loop):
77         /* Load two doublewords, compare and merge in a
78            single register for speed.  This is an attempt
79            to speed up the null-checking process for bigger strings.  */
81         ld      r12,8(r8)
82         ldu     r11,16(r8)
83         cmpb    r10,r12,r0
84         cmpb    r9,r11,r0
85         or      r5,r9,r10     /* Merge everything in one doubleword.  */
86         cmpldi  cr7,r5,0
87         bne     cr7,L(found)
88         bdnz    L(loop)
90         /* We may have one more dword to read.  */
91         cmpld   cr6,r8,r7
92         beq     cr6,L(end_max)
94         ldu     r12,8(r8)
95         cmpb    r10,r12,r0
96         cmpldi  cr6,r10,0
97         bne     cr6,L(done)
99 L(end_max):
100         mr      r3,r4
101         blr
103         /* OK, one (or both) of the doublewords contains a null byte.  Check
104            the first doubleword and decrement the address in case the first
105            doubleword really contains a null byte.  */
106         .align  4
107 L(found):
108         cmpldi  cr6,r10,0
109         addi    r8,r8,-8
110         bne     cr6,L(done)
112         /* The null byte must be in the second doubleword.  Adjust the address
113            again and move the result of cmpb to r10 so we can calculate the
114            length.  */
116         mr      r10,r9
117         addi    r8,r8,8
119         /* r10 has the output of the cmpb instruction, that is, it contains
120            0xff in the same position as the null byte in the original
121            doubleword from the string.  Use that to calculate the length.
122            We need to make sure the null char is *before* the end of the
123            range.  */
124 L(done):
125 #ifdef __LITTLE_ENDIAN__
126         addi    r0,r10,-1
127         andc    r0,r0,r10
128         popcntd r0,r0
129 #else
130         cntlzd  r0,r10        /* Count leading zeros before the match.  */
131 #endif
132         sub     r3,r8,r3
133         srdi    r0,r0,3       /* Convert leading/trailing zeros to bytes.  */
134         add     r3,r3,r0      /* Length until the match.  */
135         cmpld   r3,r4
136         blelr
137         mr      r3,r4
138         blr
140 /* Deals with size <= 32.  */
141         .align  4
142 L(small_range):
143         cmpldi  r4,0
144         beq     L(end_max)
146         clrrdi  r7,r7,3       /* Address of last doubleword.  */
148         rlwinm  r6,r3,3,26,28 /* Calculate padding.  */
149         ld      r12,0(r8)     /* Load doubleword from memory.  */
150         cmpb    r10,r12,r0    /* Check for null bytes in DWORD1.  */
151 #ifdef __LITTLE_ENDIAN__
152         srd     r10,r10,r6
153         sld     r10,r10,r6
154 #else
155         sld     r10,r10,r6
156         srd     r10,r10,r6
157 #endif
158         cmpldi  cr7,r10,0
159         bne     cr7,L(done)
161         cmpld   r8,r7
162         beq     L(end_max)
164         .p2align  5
165 L(loop_small):
166         ldu     r12,8(r8)
167         cmpb    r10,r12,r0
168         cmpldi  cr6,r10,0
169         bne     cr6,L(done)
170         cmpld   r8,r7
171         bne     L(loop_small)
172         mr      r3,r4
173         blr
175 END (__strnlen)
176 weak_alias (__strnlen, strnlen)
177 libc_hidden_builtin_def (strnlen)