]>
Commit | Line | Data |
---|---|---|
fe2f79db LM |
1 | /* Optimized memrchr implementation for PowerPC64/POWER7 using cmpb insn. |
2 | Copyright (C) 2010 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 | |
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> | |
21 | #include <bp-sym.h> | |
22 | #include <bp-asm.h> | |
23 | ||
24 | /* int [r3] memrchr (char *s [r3], int byte [r4], int size [r5]) */ | |
25 | .machine power7 | |
26 | ENTRY (BP_SYM (__memrchr)) | |
27 | CALL_MCOUNT | |
28 | dcbt 0,r3 | |
29 | mr r7,r3 | |
30 | add r3,r7,r5 /* Calculate the last acceptable address. */ | |
31 | cmpld cr7,r3,r7 /* Is the address equal or less than r3? */ | |
32 | ||
33 | /* Replicate BYTE to doubleword. */ | |
34 | rlwimi r4,r4,8,16,23 | |
35 | rlwimi r4,r4,16,0,15 | |
36 | insrdi r4,r4,32,0 | |
37 | bge cr7,L(proceed) | |
38 | ||
39 | li r3,-1 /* Make r11 the biggest if r4 <= 0. */ | |
40 | L(proceed): | |
41 | li r6,-8 | |
42 | addi r9,r3,-1 | |
43 | clrrdi r8,r9,3 | |
44 | addi r8,r8,8 | |
45 | neg r0,r3 | |
46 | rlwinm r0,r0,3,26,28 /* Calculate padding. */ | |
47 | ||
48 | cmpldi r5,32 | |
49 | ble L(small_range) | |
50 | ||
51 | ldbrx r12,r8,r6 /* Load reversed doubleword from memory. */ | |
52 | cmpb r10,r12,r4 /* Check for BYTE in DWORD1. */ | |
53 | sld r10,r10,r0 | |
54 | srd r10,r10,r0 | |
55 | cmpldi cr7,r10,0 /* If r10 == 0, no BYTE's have been found. */ | |
56 | bne cr7,L(done) | |
57 | ||
58 | /* Are we done already? */ | |
59 | addi r9,r8,-8 | |
60 | cmpld cr6,r9,r7 | |
61 | ble cr6,L(null) | |
62 | ||
63 | mtcrf 0x01,r8 | |
64 | /* Are we now aligned to a doubleword boundary? If so, skip to | |
65 | the main loop. Otherwise, go through the alignment code. */ | |
66 | mr r8,r9 | |
67 | bt 28,L(loop_setup) | |
68 | ||
69 | /* Handle DWORD2 of pair. */ | |
70 | ldbrx r12,r8,r6 | |
71 | cmpb r10,r12,r4 | |
72 | cmpldi cr7,r10,0 | |
73 | bne cr7,L(done) | |
74 | ||
75 | /* Are we done already. */ | |
76 | addi r8,r8,-8 | |
77 | cmpld cr6,r8,r7 | |
78 | ble cr6,L(null) | |
79 | ||
80 | L(loop_setup): | |
81 | li r0,-16 | |
82 | sub r5,r8,r7 | |
83 | srdi r9,r5,4 /* Number of loop iterations. */ | |
84 | mtctr r9 /* Setup the counter. */ | |
85 | b L(loop) | |
86 | /* Main loop to look for BYTE backwards in the string. Since it's a | |
87 | small loop (< 8 instructions), align it to 32-bytes. */ | |
88 | .p2align 5 | |
89 | L(loop): | |
90 | /* Load two doublewords, compare and merge in a | |
91 | single register for speed. This is an attempt | |
92 | to speed up the byte-checking process for bigger strings. */ | |
93 | ||
94 | ldbrx r12,r8,r6 | |
95 | ldbrx r11,r8,r0 | |
96 | addi r8,r8,-8 | |
97 | cmpb r10,r12,r4 | |
98 | cmpb r9,r11,r4 | |
99 | or r5,r9,r10 /* Merge everything in one doubleword. */ | |
100 | cmpldi cr7,r5,0 | |
101 | bne cr7,L(found) | |
102 | addi r8,r8,-8 | |
103 | bdnz L(loop) | |
104 | /* We're here because the counter reached 0, and that means we | |
105 | didn't have any matches for BYTE in the whole range. Just return | |
106 | the original range. */ | |
107 | addi r9,r8,8 | |
108 | cmpld cr6,r9,r7 | |
109 | bgt cr6,L(loop_small) | |
110 | b L(null) | |
111 | ||
112 | /* OK, one (or both) of the words contains BYTE. Check | |
113 | the first word and decrement the address in case the first | |
114 | word really contains BYTE. */ | |
115 | .align 4 | |
116 | L(found): | |
117 | cmpldi cr6,r10,0 | |
118 | addi r8,r8,8 | |
119 | bne cr6,L(done) | |
120 | ||
121 | /* BYTE must be in the second word. Adjust the address | |
122 | again and move the result of cmpb to r10 so we can calculate the | |
123 | pointer. */ | |
124 | ||
125 | mr r10,r9 | |
126 | addi r8,r8,-8 | |
127 | ||
128 | /* r10 has the output of the cmpb instruction, that is, it contains | |
129 | 0xff in the same position as the BYTE in the original | |
130 | word from the string. Use that to calculate the pointer. | |
131 | We need to make sure BYTE is *before* the end of the | |
132 | range. */ | |
133 | L(done): | |
134 | cntlzd r0,r10 /* Count leading zeroes before the match. */ | |
135 | srdi r6,r0,3 /* Convert leading zeroes to bytes. */ | |
136 | addi r0,r6,1 | |
137 | sub r3,r8,r0 | |
138 | cmpld r3,r7 | |
139 | blt L(null) | |
140 | blr | |
141 | ||
142 | .align 4 | |
143 | L(null): | |
144 | li r3,0 | |
145 | blr | |
146 | ||
147 | /* Deals with size <= 32. */ | |
148 | .align 4 | |
149 | L(small_range): | |
150 | cmpldi r5,0 | |
151 | beq L(null) | |
152 | ||
153 | ldbrx r12,r8,r6 /* Load reversed doubleword from memory. */ | |
154 | cmpb r10,r12,r4 /* Check for BYTE in DWORD1. */ | |
155 | sld r10,r10,r0 | |
156 | srd r10,r10,r0 | |
157 | cmpldi cr7,r10,0 | |
158 | bne cr7,L(done) | |
159 | ||
160 | /* Are we done already? */ | |
161 | addi r8,r8,-8 | |
162 | cmpld r8,r7 | |
163 | ble L(null) | |
164 | b L(loop_small) | |
165 | ||
166 | .p2align 5 | |
167 | L(loop_small): | |
168 | ldbrx r12,r8,r6 | |
169 | cmpb r10,r12,r4 | |
170 | cmpldi cr6,r10,0 | |
171 | bne cr6,L(done) | |
172 | addi r8,r8,-8 | |
173 | cmpld r8,r7 | |
174 | ble L(null) | |
175 | b L(loop_small) | |
176 | ||
177 | END (BP_SYM (__memrchr)) | |
178 | weak_alias (BP_SYM (__memrchr), BP_SYM(memrchr)) | |
179 | libc_hidden_builtin_def (memrchr) |