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