Update copyright notices with scripts/update-copyrights.
[glibc.git] / sysdeps / powerpc / powerpc32 / power7 / strnlen.S
blobec716e74a3bc0c779141b14eeab7593bae41ae64
1 /* Optimized strnlen implementation for PowerPC32/POWER7 using cmpb insn.
2    Copyright (C) 2010-2013 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>
21 #include <bp-sym.h>
22 #include <bp-asm.h>
24 /* int [r3] strnlen (char *s [r3], int size [r4])  */
25         .machine  power7
26 ENTRY (BP_SYM (__strnlen))
27         CALL_MCOUNT
28         dcbt    0,r3
29         clrrwi  r8,r3,2       /* Align the address to word boundary.  */
30         add     r7,r3,r4      /* Calculate the last acceptable address.  */
31         cmplwi  r4,16
32         li      r0,0          /* Word with null chars.  */
33         ble     L(small_range)
35         cmplw   cr7,r3,r7     /* Is the address equal or less than r3?  If
36                                  it's equal or less, it means size is either 0
37                                  or a negative number.  */
38         ble     cr7,L(proceed)
40         li      r7,-1         /* Make r11 the biggest if r4 <= 0.  */
41 L(proceed):
42         rlwinm  r6,r3,3,27,28 /* Calculate padding.  */
43         lwz     r12,0(r8)     /* Load word from memory.  */
44         cmpb    r10,r12,r0    /* Check for null bytes in DWORD1.  */
45         slw     r10,r10,r6
46         srw     r10,r10,r6
47         cmplwi  cr7,r10,0     /* If r10 == 0, no null's have been found.  */
48         bne     cr7,L(done)
50         /* Are we done already?  */
51         addi    r9,r8,4
52         cmplw   cr6,r9,r7
53         bge     cr6,L(end_max)
55         mtcrf   0x01,r8
56         /* Are we now aligned to a doubleword boundary?  If so, skip to
57            the main loop.  Otherwise, go through the alignment code.  */
59         bt      29,L(loop_setup)
61         /* Handle DWORD2 of pair.  */
62         lwzu    r12,4(r8)
63         cmpb    r10,r12,r0
64         cmplwi  cr7,r10,0
65         bne     cr7,L(done)
67         /* Are we done already?  */
68         addi    r9,r8,4
69         cmplw   cr6,r9,r7
70         bge     cr6,L(end_max)
72 L(loop_setup):
73         sub     r5,r7,r9
74         srwi    r6,r5,3       /* Number of loop iterations.  */
75         mtctr   r6            /* Setup the counter.  */
76         b       L(loop)
77         /* Main loop to look for the null byte backwards in the string.  Since
78            it's a small loop (< 8 instructions), align it to 32-bytes.  */
79         .p2align  5
80 L(loop):
81         /* Load two words, compare and merge in a
82            single register for speed.  This is an attempt
83            to speed up the null-checking process for bigger strings.  */
85         lwz     r12,4(r8)
86         lwzu    r11,8(r8)
87         cmpb    r10,r12,r0
88         cmpb    r9,r11,r0
89         or      r5,r9,r10     /* Merge everything in one word.  */
90         cmplwi  cr7,r5,0
91         bne     cr7,L(found)
92         bdnz    L(loop)
93         /* We're here because the counter reached 0, and that means we
94            didn't have any matches for null in the whole range.  Just return
95            the original size.  */
96         addi    r9,r8,4
97         cmplw   cr6,r9,r7
98         blt     cr6,L(loop_small)
100 L(end_max):
101         sub     r3,r7,r3
102         blr
104         /* OK, one (or both) of the words contains a null byte.  Check
105            the first word and decrement the address in case the first
106            word really contains a null byte.  */
107         .align  4
108 L(found):
109         cmplwi  cr6,r10,0
110         addi    r8,r8,-4
111         bne     cr6,L(done)
113         /* The null byte must be in the second word.  Adjust the address
114            again and move the result of cmpb to r10 so we can calculate the
115            length.  */
117         mr      r10,r9
118         addi    r8,r8,4
120         /* r10 has the output of the cmpb instruction, that is, it contains
121            0xff in the same position as the null byte in the original
122            word from the string.  Use that to calculate the length.
123            We need to make sure the null char is *before* the end of the
124            range.  */
125 L(done):
126         cntlzw  r0,r10        /* Count leading zeroes before the match.  */
127         srwi    r0,r0,3       /* Convert leading zeroes to bytes.  */
128         add     r9,r8,r0
129         sub     r6,r9,r3      /* Length until the match.  */
130         cmplw   r9,r7
131         bgt     L(end_max)
132         mr      r3,r6
133         blr
135         .align  4
136 L(zero):
137         li      r3,0
138         blr
140 /* Deals with size <= 32.  */
141         .align  4
142 L(small_range):
143         cmplwi  r4,0
144         beq     L(zero)
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         slw     r10,r10,r6
150         srw     r10,r10,r6
151         cmplwi  cr7,r10,0
152         bne     cr7,L(done)
154         addi    r9,r8,4
155         cmplw   r9,r7
156         bge     L(end_max)
157         b       L(loop_small)
159         .p2align  5
160 L(loop_small):
161         lwzu    r12,4(r8)
162         cmpb    r10,r12,r0
163         addi    r9,r8,4
164         cmplwi  cr6,r10,0
165         bne     cr6,L(done)
166         cmplw   r9,r7
167         bge     L(end_max)
168         b       L(loop_small)
169 END (BP_SYM (__strnlen))
170 weak_alias (BP_SYM (__strnlen), BP_SYM(strnlen))
171 libc_hidden_builtin_def (strnlen)