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