]>
Commit | Line | Data |
---|---|---|
158db122 | 1 | /* Optimized strcmp implementation for POWER7/PowerPC64. |
b168057a | 2 | Copyright (C) 2010-2015 Free Software Foundation, Inc. |
158db122 LM |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
158db122 LM |
18 | |
19 | #include <sysdep.h> | |
158db122 LM |
20 | |
21 | /* See strlen.s for comments on how the end-of-string testing works. */ | |
22 | ||
23 | /* int [r3] strncmp (const char *s1 [r3], | |
24 | const char *s2 [r4], | |
25 | size_t size [r5]) */ | |
26 | ||
2d67d91a | 27 | EALIGN (strncmp,5,0) |
158db122 LM |
28 | CALL_MCOUNT 3 |
29 | ||
8a7413f9 | 30 | #define rTMP2 r0 |
158db122 LM |
31 | #define rRTN r3 |
32 | #define rSTR1 r3 /* first string arg */ | |
33 | #define rSTR2 r4 /* second string arg */ | |
34 | #define rN r5 /* max string length */ | |
158db122 LM |
35 | #define rWORD1 r6 /* current word in s1 */ |
36 | #define rWORD2 r7 /* current word in s2 */ | |
37 | #define rWORD3 r10 | |
38 | #define rWORD4 r11 | |
39 | #define rFEFE r8 /* constant 0xfefefefefefefeff (-0x0101010101010101) */ | |
40 | #define r7F7F r9 /* constant 0x7f7f7f7f7f7f7f7f */ | |
41 | #define rNEG r10 /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */ | |
42 | #define rBITDIF r11 /* bits that differ in s1 & s2 words */ | |
8a7413f9 | 43 | #define rTMP r12 |
158db122 LM |
44 | |
45 | dcbt 0,rSTR1 | |
22700377 | 46 | nop |
158db122 LM |
47 | or rTMP,rSTR2,rSTR1 |
48 | lis r7F7F,0x7f7f | |
49 | dcbt 0,rSTR2 | |
22700377 | 50 | nop |
158db122 LM |
51 | clrldi. rTMP,rTMP,61 |
52 | cmpldi cr1,rN,0 | |
53 | lis rFEFE,-0x101 | |
54 | bne L(unaligned) | |
2ccdea26 | 55 | /* We are doubleword aligned so set up for two loops. first a double word |
158db122 LM |
56 | loop, then fall into the byte loop if any residual. */ |
57 | srdi. rTMP,rN,3 | |
58 | clrldi rN,rN,61 | |
59 | addi rFEFE,rFEFE,-0x101 | |
60 | addi r7F7F,r7F7F,0x7f7f | |
61 | cmpldi cr1,rN,0 | |
62 | beq L(unaligned) | |
63 | ||
64 | mtctr rTMP | |
65 | ld rWORD1,0(rSTR1) | |
66 | ld rWORD2,0(rSTR2) | |
67 | sldi rTMP,rFEFE,32 | |
68 | insrdi r7F7F,r7F7F,32,0 | |
69 | add rFEFE,rFEFE,rTMP | |
70 | b L(g1) | |
71 | ||
72 | L(g0): | |
73 | ldu rWORD1,8(rSTR1) | |
74 | bne cr1,L(different) | |
75 | ldu rWORD2,8(rSTR2) | |
76 | L(g1): add rTMP,rFEFE,rWORD1 | |
77 | nor rNEG,r7F7F,rWORD1 | |
78 | bdz L(tail) | |
79 | and. rTMP,rTMP,rNEG | |
80 | cmpd cr1,rWORD1,rWORD2 | |
81 | beq L(g0) | |
82 | ||
83 | /* OK. We've hit the end of the string. We need to be careful that | |
84 | we don't compare two strings as different because of gunk beyond | |
85 | the end of the strings... */ | |
86 | ||
8a7413f9 AM |
87 | #ifdef __LITTLE_ENDIAN__ |
88 | L(endstring): | |
89 | addi rTMP2, rTMP, -1 | |
90 | beq cr1, L(equal) | |
91 | andc rTMP2, rTMP2, rTMP | |
92 | rldimi rTMP2, rTMP2, 1, 0 | |
93 | and rWORD2, rWORD2, rTMP2 /* Mask off gunk. */ | |
94 | and rWORD1, rWORD1, rTMP2 | |
95 | cmpd cr1, rWORD1, rWORD2 | |
96 | beq cr1, L(equal) | |
97 | cmpb rBITDIF, rWORD1, rWORD2 /* 0xff on equal bytes. */ | |
98 | addi rNEG, rBITDIF, 1 | |
99 | orc rNEG, rNEG, rBITDIF /* 0's below LS differing byte. */ | |
100 | sldi rNEG, rNEG, 8 /* 1's above LS differing byte. */ | |
101 | andc rWORD1, rWORD1, rNEG /* mask off MS bytes. */ | |
102 | andc rWORD2, rWORD2, rNEG | |
103 | xor. rBITDIF, rWORD1, rWORD2 | |
104 | sub rRTN, rWORD1, rWORD2 | |
105 | blt L(highbit) | |
106 | sradi rRTN, rRTN, 63 /* must return an int. */ | |
107 | ori rRTN, rRTN, 1 | |
108 | blr | |
109 | L(equal): | |
110 | li rRTN, 0 | |
111 | blr | |
112 | ||
113 | L(different): | |
114 | ld rWORD1, -8(rSTR1) | |
115 | cmpb rBITDIF, rWORD1, rWORD2 /* 0xff on equal bytes. */ | |
116 | addi rNEG, rBITDIF, 1 | |
117 | orc rNEG, rNEG, rBITDIF /* 0's below LS differing byte. */ | |
118 | sldi rNEG, rNEG, 8 /* 1's above LS differing byte. */ | |
119 | andc rWORD1, rWORD1, rNEG /* mask off MS bytes. */ | |
120 | andc rWORD2, rWORD2, rNEG | |
121 | xor. rBITDIF, rWORD1, rWORD2 | |
122 | sub rRTN, rWORD1, rWORD2 | |
123 | blt L(highbit) | |
124 | sradi rRTN, rRTN, 63 | |
125 | ori rRTN, rRTN, 1 | |
126 | blr | |
127 | L(highbit): | |
128 | sradi rRTN, rWORD2, 63 | |
129 | ori rRTN, rRTN, 1 | |
130 | blr | |
131 | ||
132 | #else | |
158db122 LM |
133 | L(endstring): |
134 | and rTMP,r7F7F,rWORD1 | |
135 | beq cr1,L(equal) | |
136 | add rTMP,rTMP,r7F7F | |
137 | xor. rBITDIF,rWORD1,rWORD2 | |
158db122 LM |
138 | andc rNEG,rNEG,rTMP |
139 | blt L(highbit) | |
140 | cntlzd rBITDIF,rBITDIF | |
141 | cntlzd rNEG,rNEG | |
142 | addi rNEG,rNEG,7 | |
143 | cmpd cr1,rNEG,rBITDIF | |
144 | sub rRTN,rWORD1,rWORD2 | |
145 | blt cr1,L(equal) | |
8a7413f9 | 146 | sradi rRTN,rRTN,63 /* must return an int. */ |
158db122 LM |
147 | ori rRTN,rRTN,1 |
148 | blr | |
149 | L(equal): | |
150 | li rRTN,0 | |
151 | blr | |
152 | ||
153 | L(different): | |
8a7413f9 | 154 | ld rWORD1,-8(rSTR1) |
158db122 LM |
155 | xor. rBITDIF,rWORD1,rWORD2 |
156 | sub rRTN,rWORD1,rWORD2 | |
157 | blt L(highbit) | |
158 | sradi rRTN,rRTN,63 | |
159 | ori rRTN,rRTN,1 | |
160 | blr | |
161 | L(highbit): | |
8a7413f9 AM |
162 | sradi rRTN,rWORD2,63 |
163 | ori rRTN,rRTN,1 | |
158db122 | 164 | blr |
8a7413f9 | 165 | #endif |
158db122 LM |
166 | |
167 | /* Oh well. In this case, we just do a byte-by-byte comparison. */ | |
168 | .align 4 | |
169 | L(tail): | |
170 | and. rTMP,rTMP,rNEG | |
171 | cmpd cr1,rWORD1,rWORD2 | |
172 | bne L(endstring) | |
173 | addi rSTR1,rSTR1,8 | |
174 | bne cr1,L(different) | |
175 | addi rSTR2,rSTR2,8 | |
176 | cmpldi cr1,rN,0 | |
177 | L(unaligned): | |
178 | mtctr rN | |
179 | ble cr1,L(ux) | |
180 | L(uz): | |
181 | lbz rWORD1,0(rSTR1) | |
182 | lbz rWORD2,0(rSTR2) | |
183 | .align 4 | |
184 | L(u1): | |
185 | cmpdi cr1,rWORD1,0 | |
186 | bdz L(u4) | |
187 | cmpd rWORD1,rWORD2 | |
188 | beq cr1,L(u4) | |
4420675c | 189 | bne L(u4) |
158db122 LM |
190 | lbzu rWORD3,1(rSTR1) |
191 | lbzu rWORD4,1(rSTR2) | |
158db122 LM |
192 | cmpdi cr1,rWORD3,0 |
193 | bdz L(u3) | |
194 | cmpd rWORD3,rWORD4 | |
195 | beq cr1,L(u3) | |
4420675c | 196 | bne L(u3) |
158db122 LM |
197 | lbzu rWORD1,1(rSTR1) |
198 | lbzu rWORD2,1(rSTR2) | |
158db122 LM |
199 | cmpdi cr1,rWORD1,0 |
200 | bdz L(u4) | |
201 | cmpd rWORD1,rWORD2 | |
202 | beq cr1,L(u4) | |
4420675c | 203 | bne L(u4) |
158db122 LM |
204 | lbzu rWORD3,1(rSTR1) |
205 | lbzu rWORD4,1(rSTR2) | |
158db122 LM |
206 | cmpdi cr1,rWORD3,0 |
207 | bdz L(u3) | |
208 | cmpd rWORD3,rWORD4 | |
209 | beq cr1,L(u3) | |
4420675c | 210 | bne L(u3) |
158db122 LM |
211 | lbzu rWORD1,1(rSTR1) |
212 | lbzu rWORD2,1(rSTR2) | |
4420675c | 213 | b L(u1) |
158db122 LM |
214 | |
215 | L(u3): sub rRTN,rWORD3,rWORD4 | |
216 | blr | |
217 | L(u4): sub rRTN,rWORD1,rWORD2 | |
218 | blr | |
219 | L(ux): | |
220 | li rRTN,0 | |
221 | blr | |
2d67d91a | 222 | END (strncmp) |
158db122 | 223 | libc_hidden_builtin_def (strncmp) |