]>
Commit | Line | Data |
---|---|---|
1 | /* Optimized strchrnul implementation for PowerPC64/POWER7 using cmpb insn. | |
2 | Copyright (C) 2010-2019 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, see | |
18 | <https://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include <sysdep.h> | |
21 | ||
22 | #ifndef STRCHRNUL | |
23 | # define STRCHRNUL __strchrnul | |
24 | #endif | |
25 | /* int [r3] strchrnul (char *s [r3], int c [r4]) */ | |
26 | .machine power7 | |
27 | ENTRY_TOCLESS (STRCHRNUL) | |
28 | CALL_MCOUNT 2 | |
29 | dcbt 0,r3 | |
30 | clrrdi r8,r3,3 /* Align the address to doubleword boundary. */ | |
31 | ||
32 | /* Replicate byte to doubleword. */ | |
33 | insrdi r4,r4,8,48 | |
34 | insrdi r4,r4,16,32 | |
35 | insrdi r4,r4,32,0 | |
36 | ||
37 | rlwinm r6,r3,3,26,28 /* Calculate padding. */ | |
38 | ld r12,0(r8) /* Load doubleword from memory. */ | |
39 | li r0,0 /* Doubleword with null chars to use | |
40 | with cmpb. */ | |
41 | ||
42 | /* Now r4 has a doubleword of c bytes and r0 has | |
43 | a doubleword of null bytes. */ | |
44 | ||
45 | cmpb r10,r12,r0 /* Compare each byte against c byte. */ | |
46 | cmpb r9,r12,r4 /* Compare each byte against null byte. */ | |
47 | ||
48 | /* Move the doublewords left and right to discard the bits that are | |
49 | not part of the string and to bring them back as zeros. */ | |
50 | #ifdef __LITTLE_ENDIAN__ | |
51 | srd r10,r10,r6 | |
52 | srd r9,r9,r6 | |
53 | sld r10,r10,r6 | |
54 | sld r9,r9,r6 | |
55 | #else | |
56 | sld r10,r10,r6 | |
57 | sld r9,r9,r6 | |
58 | srd r10,r10,r6 | |
59 | srd r9,r9,r6 | |
60 | #endif | |
61 | or r5,r9,r10 /* OR the results to speed things up. */ | |
62 | cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes | |
63 | have been found. */ | |
64 | bne cr7,L(done) | |
65 | ||
66 | mtcrf 0x01,r8 | |
67 | ||
68 | /* Are we now aligned to a quadword boundary? If so, skip to | |
69 | the main loop. Otherwise, go through the alignment code. */ | |
70 | ||
71 | bt 28,L(loop) | |
72 | ||
73 | /* Handle DWORD2 of pair. */ | |
74 | ldu r12,8(r8) | |
75 | cmpb r10,r12,r0 | |
76 | cmpb r9,r12,r4 | |
77 | or r5,r9,r10 | |
78 | cmpdi cr7,r5,0 | |
79 | bne cr7,L(done) | |
80 | b L(loop) /* We branch here (rather than falling through) | |
81 | to skip the nops due to heavy alignment | |
82 | of the loop below. */ | |
83 | ||
84 | .p2align 5 | |
85 | L(loop): | |
86 | /* Load two doublewords, compare and merge in a | |
87 | single register for speed. This is an attempt | |
88 | to speed up the null-checking process for bigger strings. */ | |
89 | ld r12,8(r8) | |
90 | ldu r11,16(r8) | |
91 | cmpb r10,r12,r0 | |
92 | cmpb r9,r12,r4 | |
93 | cmpb r6,r11,r0 | |
94 | cmpb r7,r11,r4 | |
95 | or r5,r9,r10 | |
96 | or r10,r6,r7 | |
97 | or r11,r5,r10 | |
98 | cmpdi cr7,r11,0 | |
99 | beq cr7,L(loop) | |
100 | ||
101 | /* OK, one (or both) of the doublewords contains a c/null byte. Check | |
102 | the first doubleword and decrement the address in case the first | |
103 | doubleword really contains a c/null byte. */ | |
104 | ||
105 | cmpdi cr6,r5,0 | |
106 | addi r8,r8,-8 | |
107 | bne cr6,L(done) | |
108 | ||
109 | /* The c/null byte must be in the second doubleword. Adjust the | |
110 | address again and move the result of cmpb to r5 so we can calculate | |
111 | the pointer. */ | |
112 | mr r5,r10 | |
113 | addi r8,r8,8 | |
114 | ||
115 | /* r5 has the output of the cmpb instruction, that is, it contains | |
116 | 0xff in the same position as the c/null byte in the original | |
117 | doubleword from the string. Use that to calculate the pointer. */ | |
118 | L(done): | |
119 | #ifdef __LITTLE_ENDIAN__ | |
120 | addi r0,r5,-1 | |
121 | andc r0,r0,r5 | |
122 | popcntd r0,r0 | |
123 | #else | |
124 | cntlzd r0,r5 /* Count leading zeros before the match. */ | |
125 | #endif | |
126 | srdi r0,r0,3 /* Convert leading zeros to bytes. */ | |
127 | add r3,r8,r0 /* Return address of matching c/null byte. */ | |
128 | blr | |
129 | END (STRCHRNUL) | |
130 | weak_alias (STRCHRNUL, strchrnul) | |
131 | libc_hidden_builtin_def (STRCHRNUL) |