]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/powerpc/powerpc64/power7/strlen.S
powerpc: refactor strcasecmp, strcmp, and strncmp IFUNC.
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc64 / power7 / strlen.S
CommitLineData
fe2f79db 1/* Optimized strlen implementation for PowerPC64/POWER7 using cmpb insn.
bfff8b1b 2 Copyright (C) 2010-2017 Free Software Foundation, Inc.
fe2f79db
LM
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
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
fe2f79db
LM
19
20#include <sysdep.h>
fe2f79db
LM
21
22/* int [r3] strlen (char *s [r3]) */
23 .machine power7
2d67d91a 24ENTRY (strlen)
fe2f79db
LM
25 CALL_MCOUNT 1
26 dcbt 0,r3
27 clrrdi r4,r3,3 /* Align the address to doubleword boundary. */
28 rlwinm r6,r3,3,26,28 /* Calculate padding. */
29 li r0,0 /* Doubleword with null chars to use
30 with cmpb. */
31 li r5,-1 /* MASK = 0xffffffffffffffff. */
32 ld r12,0(r4) /* Load doubleword from memory. */
db9b4570
AM
33#ifdef __LITTLE_ENDIAN__
34 sld r5,r5,r6
35#else
fe2f79db 36 srd r5,r5,r6 /* MASK = MASK >> padding. */
db9b4570 37#endif
fe2f79db
LM
38 orc r9,r12,r5 /* Mask bits that are not part of the string. */
39 cmpb r10,r9,r0 /* Check for null bytes in DWORD1. */
40 cmpdi cr7,r10,0 /* If r10 == 0, no null's have been found. */
41 bne cr7,L(done)
42
43 mtcrf 0x01,r4
44
45 /* Are we now aligned to a quadword boundary? If so, skip to
46 the main loop. Otherwise, go through the alignment code. */
47
48 bt 28,L(loop)
49
50 /* Handle DWORD2 of pair. */
51 ldu r12,8(r4)
52 cmpb r10,r12,r0
53 cmpdi cr7,r10,0
54 bne cr7,L(done)
fe2f79db
LM
55
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
59L(loop):
60 /* Load two doublewords, 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. */
63
64 ld r12, 8(r4)
65 ldu r11, 16(r4)
66 cmpb r10,r12,r0
67 cmpb r9,r11,r0
68 or r8,r9,r10 /* Merge everything in one doubleword. */
69 cmpdi cr7,r8,0
70 beq cr7,L(loop)
71
72 /* OK, one (or both) of the doublewords contains a null byte. Check
73 the first doubleword and decrement the address in case the first
74 doubleword really contains a null byte. */
75
76 cmpdi cr6,r10,0
77 addi r4,r4,-8
78 bne cr6,L(done)
79
80 /* The null byte must be in the second doubleword. Adjust the address
81 again and move the result of cmpb to r10 so we can calculate the
82 length. */
83
84 mr r10,r9
85 addi r4,r4,8
86
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 doubleword from the string. Use that to calculate the length. */
90L(done):
db9b4570
AM
91#ifdef __LITTLE_ENDIAN__
92 addi r9, r10, -1 /* Form a mask from trailing zeros. */
93 andc r9, r9, r10
94 popcntd r0, r9 /* Count the bits in the mask. */
95#else
96 cntlzd r0,r10 /* Count leading zeros before the match. */
97#endif
fe2f79db 98 subf r5,r3,r4
db9b4570 99 srdi r0,r0,3 /* Convert leading/trailing zeros to bytes. */
fe2f79db
LM
100 add r3,r5,r0 /* Compute final length. */
101 blr
2d67d91a 102END (strlen)
fe2f79db 103libc_hidden_builtin_def (strlen)