Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / powerpc / powerpc32 / power7 / strlen.S
blob0d2d6b54f02feac2a0778d46093093dc5769f500
1 /* Optimized strlen 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] strlen (char *s [r3])  */
23         .machine  power7
24 ENTRY (strlen)
25         CALL_MCOUNT
26         dcbt    0,r3
27         clrrwi  r4,r3,2       /* Align the address to word boundary.  */
28         rlwinm  r6,r3,3,27,28 /* Calculate padding.  */
29         li      r0,0          /* Word with null chars to use with cmpb.  */
30         li      r5,-1         /* MASK = 0xffffffffffffffff.  */
31         lwz     r12,0(r4)     /* Load word from memory.  */
32 #ifdef __LITTLE_ENDIAN__
33         slw     r5,r5,r6
34 #else
35         srw     r5,r5,r6      /* MASK = MASK >> padding.  */
36 #endif
37         orc     r9,r12,r5     /* Mask bits that are not part of the string.  */
38         cmpb    r10,r9,r0     /* Check for null bytes in WORD1.  */
39         cmpwi   cr7,r10,0     /* If r10 == 0, no null's have been found.  */
40         bne     cr7,L(done)
42         mtcrf   0x01,r4
44         /* Are we now aligned to a doubleword boundary?  If so, skip to
45            the main loop.  Otherwise, go through the alignment code.  */
47         bt      29,L(loop)
49         /* Handle WORD2 of pair.  */
50         lwzu    r12,4(r4)
51         cmpb    r10,r12,r0
52         cmpwi   cr7,r10,0
53         bne     cr7,L(done)
55         /* Main loop to look for the end of the string.  Since it's a
56            small loop (< 8 instructions), align it to 32-bytes.  */
57         .p2align  5
58 L(loop):
59         /* Load two words, compare and merge in a
60            single register for speed.  This is an attempt
61            to speed up the null-checking process for bigger strings.  */
63         lwz     r12, 4(r4)
64         lwzu    r11, 8(r4)
65         cmpb    r10,r12,r0
66         cmpb    r9,r11,r0
67         or      r8,r9,r10     /* Merge everything in one word.  */
68         cmpwi   cr7,r8,0
69         beq     cr7,L(loop)
71         /* OK, one (or both) of the words contains a null byte.  Check
72            the first word and decrement the address in case the first
73            word really contains a null byte.  */
75         cmpwi   cr6,r10,0
76         addi    r4,r4,-4
77         bne     cr6,L(done)
79         /* The null byte must be in the second word.  Adjust the address
80            again and move the result of cmpb to r10 so we can calculate the
81            length.  */
83         mr      r10,r9
84         addi    r4,r4,4
86         /* r10 has the output of the cmpb instruction, that is, it contains
87            0xff in the same position as the null byte in the original
88            word from the string.  Use that to calculate the length.  */
89 L(done):
90 #ifdef __LITTLE_ENDIAN__
91         addi    r9, r10, -1   /* Form a mask from trailing zeros.  */
92         andc    r9, r9, r10
93         popcntw r0, r9        /* Count the bits in the mask.  */
94 #else
95         cntlzw  r0,r10        /* Count leading zeros before the match.  */
96 #endif
97         subf    r5,r3,r4
98         srwi    r0,r0,3       /* Convert leading zeros to bytes.  */
99         add     r3,r5,r0      /* Compute final length.  */
100         blr
101 END (strlen)
102 libc_hidden_builtin_def (strlen)