Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / powerpc / powerpc32 / power7 / strnlen.S
blob71e2aa87bd81417536f34ddaa54a7a6ad3ea5980
1 /* Optimized strnlen implementation for PowerPC32/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
26         dcbt    0,r3
27         clrrwi  r8,r3,2       /* Align the address to word boundary.  */
28         add     r7,r3,r4      /* Calculate the last acceptable address.  */
29         cmplwi  r4,16
30         li      r0,0          /* Word with null chars.  */
31         addi    r7,r7,-1
32         ble     L(small_range)
34         rlwinm  r6,r3,3,27,28 /* Calculate padding.  */
35         lwz     r12,0(r8)     /* Load word from memory.  */
36         cmpb    r10,r12,r0    /* Check for null bytes in DWORD1.  */
37 #ifdef __LITTLE_ENDIAN__
38         srw     r10,r10,r6
39         slw     r10,r10,r6
40 #else
41         slw     r10,r10,r6
42         srw     r10,r10,r6
43 #endif
44         cmplwi  cr7,r10,0     /* If r10 == 0, no null's have been found.  */
45         bne     cr7,L(done)
47         clrrwi  r7,r7,2       /* Address of last word.  */
48         mtcrf   0x01,r8
49         /* Are we now aligned to a doubleword boundary?  If so, skip to
50            the main loop.  Otherwise, go through the alignment code.  */
52         bt      29,L(loop_setup)
54         /* Handle WORD2 of pair.  */
55         lwzu    r12,4(r8)
56         cmpb    r10,r12,r0
57         cmplwi  cr7,r10,0
58         bne     cr7,L(done)
60 L(loop_setup):
61         /* The last word we want to read in the loop below is the one
62            containing the last byte of the string, ie. the word at
63            (s + size - 1) & ~3, or r7.  The first word read is at
64            r8 + 4, we read 2 * cnt words, so the last word read will
65            be at r8 + 4 + 8 * cnt - 4.  Solving for cnt gives
66            cnt = (r7 - r8) / 8  */
67         sub     r5,r7,r8
68         srwi    r6,r5,3       /* Number of loop iterations.  */
69         mtctr   r6            /* Setup the counter.  */
71         /* Main loop to look for the null byte in the string.  Since
72            it's a small loop (< 8 instructions), align it to 32-bytes.  */
73         .p2align  5
74 L(loop):
75         /* Load two words, compare and merge in a
76            single register for speed.  This is an attempt
77            to speed up the null-checking process for bigger strings.  */
79         lwz     r12,4(r8)
80         lwzu    r11,8(r8)
81         cmpb    r10,r12,r0
82         cmpb    r9,r11,r0
83         or      r5,r9,r10     /* Merge everything in one word.  */
84         cmplwi  cr7,r5,0
85         bne     cr7,L(found)
86         bdnz    L(loop)
88         /* We may have one more word to read.  */
89         cmplw   cr6,r8,r7
90         beq     cr6,L(end_max)
92         lwzu    r12,4(r8)
93         cmpb    r10,r12,r0
94         cmplwi  cr6,r10,0
95         bne     cr6,L(done)
97 L(end_max):
98         mr      r3,r4
99         blr
101         /* OK, one (or both) of the words contains a null byte.  Check
102            the first word and decrement the address in case the first
103            word really contains a null byte.  */
104         .align  4
105 L(found):
106         cmplwi  cr6,r10,0
107         addi    r8,r8,-4
108         bne     cr6,L(done)
110         /* The null byte must be in the second word.  Adjust the address
111            again and move the result of cmpb to r10 so we can calculate the
112            length.  */
114         mr      r10,r9
115         addi    r8,r8,4
117         /* r10 has the output of the cmpb instruction, that is, it contains
118            0xff in the same position as the null byte in the original
119            word from the string.  Use that to calculate the length.
120            We need to make sure the null char is *before* the end of the
121            range.  */
122 L(done):
123 #ifdef __LITTLE_ENDIAN__
124         addi    r0,r10,-1
125         andc    r0,r0,r10
126         popcntw r0,r0
127 #else
128         cntlzw  r0,r10        /* Count leading zeros before the match.  */
129 #endif
130         sub     r3,r8,r3
131         srwi    r0,r0,3       /* Convert leading/trailing zeros to bytes.  */
132         add     r3,r3,r0      /* Length until the match.  */
133         cmplw   r3,r4
134         blelr
135         mr      r3,r4
136         blr
138 /* Deals with size <= 16.  */
139         .align  4
140 L(small_range):
141         cmplwi  r4,0
142         beq     L(end_max)
144         clrrwi  r7,r7,2       /* Address of last word.  */
146         rlwinm  r6,r3,3,27,28 /* Calculate padding.  */
147         lwz     r12,0(r8)     /* Load word from memory.  */
148         cmpb    r10,r12,r0    /* Check for null bytes in WORD1.  */
149 #ifdef __LITTLE_ENDIAN__
150         srw     r10,r10,r6
151         slw     r10,r10,r6
152 #else
153         slw     r10,r10,r6
154         srw     r10,r10,r6
155 #endif
156         cmplwi  cr7,r10,0
157         bne     cr7,L(done)
159         cmplw   r8,r7
160         beq     L(end_max)
162         .p2align  5
163 L(loop_small):
164         lwzu    r12,4(r8)
165         cmpb    r10,r12,r0
166         cmplwi  cr6,r10,0
167         bne     cr6,L(done)
168         cmplw   r8,r7
169         bne     L(loop_small)
170         mr      r3,r4
171         blr
173 END (__strnlen)
174 weak_alias (__strnlen, strnlen)
175 libc_hidden_builtin_def (strnlen)