]>
Commit | Line | Data |
---|---|---|
fe2f79db | 1 | /* Optimized strlen implementation for PowerPC64/POWER7 using cmpb insn. |
04277e02 | 2 | Copyright (C) 2010-2019 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 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
fe2f79db LM |
19 | |
20 | #include <sysdep.h> | |
fe2f79db LM |
21 | |
22 | /* int [r3] strlen (char *s [r3]) */ | |
001649fd WSM |
23 | |
24 | #ifndef STRLEN | |
25 | # define STRLEN strlen | |
26 | #endif | |
fe2f79db | 27 | .machine power7 |
d5b41185 | 28 | ENTRY_TOCLESS (STRLEN) |
fe2f79db LM |
29 | CALL_MCOUNT 1 |
30 | dcbt 0,r3 | |
31 | clrrdi r4,r3,3 /* Align the address to doubleword boundary. */ | |
32 | rlwinm r6,r3,3,26,28 /* Calculate padding. */ | |
33 | li r0,0 /* Doubleword with null chars to use | |
34 | with cmpb. */ | |
35 | li r5,-1 /* MASK = 0xffffffffffffffff. */ | |
36 | ld r12,0(r4) /* Load doubleword from memory. */ | |
db9b4570 AM |
37 | #ifdef __LITTLE_ENDIAN__ |
38 | sld r5,r5,r6 | |
39 | #else | |
fe2f79db | 40 | srd r5,r5,r6 /* MASK = MASK >> padding. */ |
db9b4570 | 41 | #endif |
fe2f79db LM |
42 | orc r9,r12,r5 /* Mask bits that are not part of the string. */ |
43 | cmpb r10,r9,r0 /* Check for null bytes in DWORD1. */ | |
44 | cmpdi cr7,r10,0 /* If r10 == 0, no null's have been found. */ | |
45 | bne cr7,L(done) | |
46 | ||
47 | mtcrf 0x01,r4 | |
48 | ||
49 | /* Are we now aligned to a quadword boundary? If so, skip to | |
50 | the main loop. Otherwise, go through the alignment code. */ | |
51 | ||
52 | bt 28,L(loop) | |
53 | ||
54 | /* Handle DWORD2 of pair. */ | |
55 | ldu r12,8(r4) | |
56 | cmpb r10,r12,r0 | |
57 | cmpdi cr7,r10,0 | |
58 | bne cr7,L(done) | |
fe2f79db LM |
59 | |
60 | /* Main loop to look for the end of the string. Since it's a | |
61 | small loop (< 8 instructions), align it to 32-bytes. */ | |
62 | .p2align 5 | |
63 | L(loop): | |
64 | /* Load two doublewords, compare and merge in a | |
65 | single register for speed. This is an attempt | |
66 | to speed up the null-checking process for bigger strings. */ | |
67 | ||
68 | ld r12, 8(r4) | |
69 | ldu r11, 16(r4) | |
70 | cmpb r10,r12,r0 | |
71 | cmpb r9,r11,r0 | |
72 | or r8,r9,r10 /* Merge everything in one doubleword. */ | |
73 | cmpdi cr7,r8,0 | |
74 | beq cr7,L(loop) | |
75 | ||
76 | /* OK, one (or both) of the doublewords contains a null byte. Check | |
77 | the first doubleword and decrement the address in case the first | |
78 | doubleword really contains a null byte. */ | |
79 | ||
80 | cmpdi cr6,r10,0 | |
81 | addi r4,r4,-8 | |
82 | bne cr6,L(done) | |
83 | ||
84 | /* The null byte must be in the second doubleword. Adjust the address | |
85 | again and move the result of cmpb to r10 so we can calculate the | |
86 | length. */ | |
87 | ||
88 | mr r10,r9 | |
89 | addi r4,r4,8 | |
90 | ||
91 | /* r10 has the output of the cmpb instruction, that is, it contains | |
92 | 0xff in the same position as the null byte in the original | |
93 | doubleword from the string. Use that to calculate the length. */ | |
94 | L(done): | |
db9b4570 AM |
95 | #ifdef __LITTLE_ENDIAN__ |
96 | addi r9, r10, -1 /* Form a mask from trailing zeros. */ | |
97 | andc r9, r9, r10 | |
98 | popcntd r0, r9 /* Count the bits in the mask. */ | |
99 | #else | |
100 | cntlzd r0,r10 /* Count leading zeros before the match. */ | |
101 | #endif | |
fe2f79db | 102 | subf r5,r3,r4 |
db9b4570 | 103 | srdi r0,r0,3 /* Convert leading/trailing zeros to bytes. */ |
fe2f79db LM |
104 | add r3,r5,r0 /* Compute final length. */ |
105 | blr | |
001649fd | 106 | END (STRLEN) |
fe2f79db | 107 | libc_hidden_builtin_def (strlen) |