]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/powerpc/powerpc64/power7/strlen.S
powerpc: Various P7-optimized string functions
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc64 / power7 / strlen.S
1 /* Optimized strlen implementation for PowerPC64/POWER7 using cmpb insn.
2 Copyright (C) 2010 Free Software Foundation, Inc.
3 Contributed by Luis Machado <luisgpm@br.ibm.com>.
4 This file is part of the GNU C Library.
5
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.
10
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.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA
19 02110-1301 USA. */
20
21 #include <sysdep.h>
22 #include <bp-sym.h>
23 #include <bp-asm.h>
24
25 /* int [r3] strlen (char *s [r3]) */
26 .machine power7
27 ENTRY (BP_SYM (strlen))
28 CALL_MCOUNT 1
29 dcbt 0,r3
30 clrrdi r4,r3,3 /* Align the address to doubleword boundary. */
31 rlwinm r6,r3,3,26,28 /* Calculate padding. */
32 li r0,0 /* Doubleword with null chars to use
33 with cmpb. */
34 li r5,-1 /* MASK = 0xffffffffffffffff. */
35 ld r12,0(r4) /* Load doubleword from memory. */
36 srd r5,r5,r6 /* MASK = MASK >> padding. */
37 orc r9,r12,r5 /* Mask bits that are not part of the string. */
38 cmpb r10,r9,r0 /* Check for null bytes in DWORD1. */
39 cmpdi cr7,r10,0 /* If r10 == 0, no null's have been found. */
40 bne cr7,L(done)
41
42 mtcrf 0x01,r4
43
44 /* Are we now aligned to a quadword boundary? If so, skip to
45 the main loop. Otherwise, go through the alignment code. */
46
47 bt 28,L(loop)
48
49 /* Handle DWORD2 of pair. */
50 ldu r12,8(r4)
51 cmpb r10,r12,r0
52 cmpdi cr7,r10,0
53 bne cr7,L(done)
54 b L(loop) /* We branch here (rather than falling through)
55 to skip the nops due to heavy alignment
56 of the loop below. */
57
58 /* Main loop to look for the end of the string. Since it's a
59 small loop (< 8 instructions), align it to 32-bytes. */
60 .p2align 5
61 L(loop):
62 /* Load two doublewords, compare and merge in a
63 single register for speed. This is an attempt
64 to speed up the null-checking process for bigger strings. */
65
66 ld r12, 8(r4)
67 ldu r11, 16(r4)
68 cmpb r10,r12,r0
69 cmpb r9,r11,r0
70 or r8,r9,r10 /* Merge everything in one doubleword. */
71 cmpdi cr7,r8,0
72 beq cr7,L(loop)
73
74 /* OK, one (or both) of the doublewords contains a null byte. Check
75 the first doubleword and decrement the address in case the first
76 doubleword really contains a null byte. */
77
78 cmpdi cr6,r10,0
79 addi r4,r4,-8
80 bne cr6,L(done)
81
82 /* The null byte must be in the second doubleword. Adjust the address
83 again and move the result of cmpb to r10 so we can calculate the
84 length. */
85
86 mr r10,r9
87 addi r4,r4,8
88
89 /* r10 has the output of the cmpb instruction, that is, it contains
90 0xff in the same position as the null byte in the original
91 doubleword from the string. Use that to calculate the length. */
92 L(done):
93 cntlzd r0,r10 /* Count leading zeroes before the match. */
94 subf r5,r3,r4
95 srdi r0,r0,3 /* Convert leading zeroes to bytes. */
96 add r3,r5,r0 /* Compute final length. */
97 blr
98 END (BP_SYM (strlen))
99 libc_hidden_builtin_def (strlen)