]>
Commit | Line | Data |
---|---|---|
fe2f79db | 1 | /* Optimized memchr 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 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] memchr (char *s [r3], int byte [r4], int size [r5]) */ | |
ff06a55a WSM |
23 | |
24 | #ifndef MEMCHR | |
25 | # define MEMCHR __memchr | |
26 | #endif | |
fe2f79db | 27 | .machine power7 |
d5b41185 | 28 | ENTRY_TOCLESS (MEMCHR) |
83f5c32d | 29 | CALL_MCOUNT 3 |
fe2f79db LM |
30 | dcbt 0,r3 |
31 | clrrdi r8,r3,3 | |
466b0393 | 32 | insrdi r4,r4,8,48 |
b2246379 AZ |
33 | |
34 | /* Calculate the last acceptable address and check for possible | |
35 | addition overflow by using satured math: | |
36 | r7 = r3 + r5 | |
37 | r7 |= -(r7 < x) */ | |
38 | add r7,r3,r5 | |
39 | subfc r6,r3,r7 | |
40 | subfe r9,r9,r9 | |
41 | extsw r6,r9 | |
42 | or r7,r7,r6 | |
43 | ||
466b0393 | 44 | insrdi r4,r4,16,32 |
fe2f79db | 45 | cmpldi r5,32 |
466b0393 AM |
46 | li r9, -1 |
47 | rlwinm r6,r3,3,26,28 /* Calculate padding. */ | |
14a50c9d | 48 | insrdi r4,r4,32,0 |
466b0393 AM |
49 | addi r7,r7,-1 |
50 | #ifdef __LITTLE_ENDIAN__ | |
51 | sld r9,r9,r6 | |
52 | #else | |
53 | srd r9,r9,r6 | |
54 | #endif | |
fe2f79db LM |
55 | ble L(small_range) |
56 | ||
fe2f79db | 57 | ld r12,0(r8) /* Load doubleword from memory. */ |
466b0393 AM |
58 | cmpb r3,r12,r4 /* Check for BYTEs in DWORD1. */ |
59 | and r3,r3,r9 | |
60 | clrldi r5,r7,61 /* Byte count - 1 in last dword. */ | |
61 | clrrdi r7,r7,3 /* Address of last doubleword. */ | |
62 | cmpldi cr7,r3,0 /* Does r3 indicate we got a hit? */ | |
fe2f79db LM |
63 | bne cr7,L(done) |
64 | ||
fe2f79db LM |
65 | mtcrf 0x01,r8 |
66 | /* Are we now aligned to a quadword boundary? If so, skip to | |
67 | the main loop. Otherwise, go through the alignment code. */ | |
fe2f79db LM |
68 | bt 28,L(loop_setup) |
69 | ||
70 | /* Handle DWORD2 of pair. */ | |
71 | ldu r12,8(r8) | |
466b0393 AM |
72 | cmpb r3,r12,r4 |
73 | cmpldi cr7,r3,0 | |
fe2f79db LM |
74 | bne cr7,L(done) |
75 | ||
fe2f79db | 76 | L(loop_setup): |
466b0393 AM |
77 | /* The last dword we want to read in the loop below is the one |
78 | containing the last byte of the string, ie. the dword at | |
79 | (s + size - 1) & ~7, or r7. The first dword read is at | |
80 | r8 + 8, we read 2 * cnt dwords, so the last dword read will | |
81 | be at r8 + 8 + 16 * cnt - 8. Solving for cnt gives | |
82 | cnt = (r7 - r8) / 16 */ | |
83 | sub r6,r7,r8 | |
84 | srdi r6,r6,4 /* Number of loop iterations. */ | |
14a50c9d | 85 | mtctr r6 /* Setup the counter. */ |
466b0393 AM |
86 | |
87 | /* Main loop to look for BYTE in the string. Since | |
88 | it's a small loop (8 instructions), align it to 32-bytes. */ | |
89 | .align 5 | |
fe2f79db LM |
90 | L(loop): |
91 | /* Load two doublewords, compare and merge in a | |
92 | single register for speed. This is an attempt | |
93 | to speed up the byte-checking process for bigger strings. */ | |
fe2f79db LM |
94 | ld r12,8(r8) |
95 | ldu r11,16(r8) | |
466b0393 | 96 | cmpb r3,r12,r4 |
fe2f79db | 97 | cmpb r9,r11,r4 |
466b0393 AM |
98 | or r6,r9,r3 /* Merge everything in one doubleword. */ |
99 | cmpldi cr7,r6,0 | |
fe2f79db LM |
100 | bne cr7,L(found) |
101 | bdnz L(loop) | |
14a50c9d | 102 | |
466b0393 AM |
103 | /* We may have one more dword to read. */ |
104 | cmpld r8,r7 | |
105 | beqlr | |
fe2f79db | 106 | |
466b0393 AM |
107 | ldu r12,8(r8) |
108 | cmpb r3,r12,r4 | |
109 | cmpldi cr6,r3,0 | |
110 | bne cr6,L(done) | |
111 | blr | |
112 | ||
113 | .align 4 | |
114 | L(found): | |
fe2f79db LM |
115 | /* OK, one (or both) of the doublewords contains BYTE. Check |
116 | the first doubleword and decrement the address in case the first | |
117 | doubleword really contains BYTE. */ | |
466b0393 | 118 | cmpldi cr6,r3,0 |
fe2f79db LM |
119 | addi r8,r8,-8 |
120 | bne cr6,L(done) | |
121 | ||
122 | /* BYTE must be in the second doubleword. Adjust the address | |
466b0393 | 123 | again and move the result of cmpb to r3 so we can calculate the |
fe2f79db LM |
124 | pointer. */ |
125 | ||
466b0393 | 126 | mr r3,r9 |
fe2f79db LM |
127 | addi r8,r8,8 |
128 | ||
466b0393 | 129 | /* r3 has the output of the cmpb instruction, that is, it contains |
fe2f79db LM |
130 | 0xff in the same position as BYTE in the original |
131 | doubleword from the string. Use that to calculate the pointer. | |
14a50c9d | 132 | We need to make sure BYTE is *before* the end of the range. */ |
fe2f79db | 133 | L(done): |
466b0393 AM |
134 | #ifdef __LITTLE_ENDIAN__ |
135 | addi r0,r3,-1 | |
136 | andc r0,r0,r3 | |
137 | popcntd r0,r0 /* Count trailing zeros. */ | |
138 | #else | |
139 | cntlzd r0,r3 /* Count leading zeros before the match. */ | |
140 | #endif | |
141 | cmpld r8,r7 /* Are we on the last dword? */ | |
142 | srdi r0,r0,3 /* Convert leading/trailing zeros to bytes. */ | |
fe2f79db | 143 | add r3,r8,r0 |
466b0393 AM |
144 | cmpld cr7,r0,r5 /* If on the last dword, check byte offset. */ |
145 | bnelr | |
146 | blelr cr7 | |
147 | li r3,0 | |
fe2f79db LM |
148 | blr |
149 | ||
150 | .align 4 | |
151 | L(null): | |
152 | li r3,0 | |
153 | blr | |
154 | ||
155 | /* Deals with size <= 32. */ | |
156 | .align 4 | |
157 | L(small_range): | |
158 | cmpldi r5,0 | |
466b0393 | 159 | beq L(null) |
fe2f79db | 160 | ld r12,0(r8) /* Load word from memory. */ |
466b0393 AM |
161 | cmpb r3,r12,r4 /* Check for BYTE in DWORD1. */ |
162 | and r3,r3,r9 | |
163 | cmpldi cr7,r3,0 | |
164 | clrldi r5,r7,61 /* Byte count - 1 in last dword. */ | |
165 | clrrdi r7,r7,3 /* Address of last doubleword. */ | |
166 | cmpld r8,r7 /* Are we done already? */ | |
fe2f79db | 167 | bne cr7,L(done) |
466b0393 | 168 | beqlr |
fe2f79db | 169 | |
14a50c9d | 170 | ldu r12,8(r8) |
466b0393 AM |
171 | cmpb r3,r12,r4 |
172 | cmpldi cr6,r3,0 | |
173 | cmpld r8,r7 | |
14a50c9d | 174 | bne cr6,L(done) /* Found something. */ |
466b0393 | 175 | beqlr /* Hit end of string (length). */ |
fe2f79db | 176 | |
fe2f79db | 177 | ldu r12,8(r8) |
466b0393 AM |
178 | cmpb r3,r12,r4 |
179 | cmpldi cr6,r3,0 | |
180 | cmpld r8,r7 | |
181 | bne cr6,L(done) | |
182 | beqlr | |
14a50c9d WS |
183 | |
184 | ldu r12,8(r8) | |
466b0393 AM |
185 | cmpb r3,r12,r4 |
186 | cmpldi cr6,r3,0 | |
187 | cmpld r8,r7 | |
14a50c9d | 188 | bne cr6,L(done) |
466b0393 | 189 | beqlr |
14a50c9d | 190 | |
14a50c9d | 191 | ldu r12,8(r8) |
466b0393 AM |
192 | cmpb r3,r12,r4 |
193 | cmpldi cr6,r3,0 | |
194 | bne cr6,L(done) | |
14a50c9d WS |
195 | blr |
196 | ||
ff06a55a | 197 | END (MEMCHR) |
2d67d91a | 198 | weak_alias (__memchr, memchr) |
fe2f79db | 199 | libc_hidden_builtin_def (memchr) |