]>
Commit | Line | Data |
---|---|---|
f7a9f785 | 1 | /* Copyright (C) 1991-2016 Free Software Foundation, Inc. |
478b92f0 | 2 | This file is part of the GNU C Library. |
28f540f4 RM |
3 | Contributed by Torbjorn Granlund (tege@sics.se). |
4 | ||
478b92f0 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
28f540f4 | 9 | |
478b92f0 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
28f540f4 | 14 | |
41bdb6e2 | 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/>. */ | |
28f540f4 RM |
18 | |
19 | #ifdef HAVE_CONFIG_H | |
af6f3906 | 20 | # include "config.h" |
28f540f4 RM |
21 | #endif |
22 | ||
23 | #undef __ptr_t | |
c8a89e7d | 24 | #define __ptr_t void * |
28f540f4 | 25 | |
af6f3906 UD |
26 | #if defined HAVE_STRING_H || defined _LIBC |
27 | # include <string.h> | |
28f540f4 RM |
28 | #endif |
29 | ||
9a0a462c UD |
30 | #undef memcmp |
31 | ||
07253fcf AZ |
32 | #ifndef MEMCMP |
33 | # define MEMCMP memcmp | |
34 | #endif | |
35 | ||
28f540f4 RM |
36 | #ifdef _LIBC |
37 | ||
af6f3906 | 38 | # include <memcopy.h> |
789b13c4 UD |
39 | # include <endian.h> |
40 | ||
41 | # if __BYTE_ORDER == __BIG_ENDIAN | |
42 | # define WORDS_BIGENDIAN | |
43 | # endif | |
28f540f4 RM |
44 | |
45 | #else /* Not in the GNU C library. */ | |
46 | ||
af6f3906 | 47 | # include <sys/types.h> |
28f540f4 RM |
48 | |
49 | /* Type to use for aligned memory operations. | |
50 | This should normally be the biggest type supported by a single load | |
51 | and store. Must be an unsigned type. */ | |
af6f3906 UD |
52 | # define op_t unsigned long int |
53 | # define OPSIZ (sizeof(op_t)) | |
28f540f4 RM |
54 | |
55 | /* Threshold value for when to enter the unrolled loops. */ | |
af6f3906 | 56 | # define OP_T_THRES 16 |
28f540f4 RM |
57 | |
58 | /* Type to use for unaligned operations. */ | |
59 | typedef unsigned char byte; | |
60 | ||
af6f3906 UD |
61 | # ifndef WORDS_BIGENDIAN |
62 | # define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2))) | |
63 | # else | |
64 | # define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2))) | |
65 | # endif | |
28f540f4 RM |
66 | |
67 | #endif /* In the GNU C library. */ | |
68 | ||
69 | #ifdef WORDS_BIGENDIAN | |
af6f3906 | 70 | # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1) |
28f540f4 | 71 | #else |
af6f3906 | 72 | # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b)) |
28f540f4 RM |
73 | #endif |
74 | ||
75 | /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */ | |
76 | ||
77 | /* The strategy of this memcmp is: | |
78 | ||
79 | 1. Compare bytes until one of the block pointers is aligned. | |
80 | ||
81 | 2. Compare using memcmp_common_alignment or | |
82 | memcmp_not_common_alignment, regarding the alignment of the other | |
83 | block after the initial byte operations. The maximum number of | |
84 | full words (of type op_t) are compared in this way. | |
85 | ||
86 | 3. Compare the few remaining bytes. */ | |
87 | ||
88 | #ifndef WORDS_BIGENDIAN | |
89 | /* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine. | |
90 | A and B are known to be different. | |
91 | This is needed only on little-endian machines. */ | |
a1470b6f | 92 | |
79937577 | 93 | static int memcmp_bytes (op_t, op_t) __THROW; |
a1470b6f | 94 | |
28f540f4 | 95 | static int |
41075ae3 | 96 | memcmp_bytes (op_t a, op_t b) |
28f540f4 RM |
97 | { |
98 | long int srcp1 = (long int) &a; | |
99 | long int srcp2 = (long int) &b; | |
100 | op_t a0, b0; | |
101 | ||
102 | do | |
103 | { | |
104 | a0 = ((byte *) srcp1)[0]; | |
105 | b0 = ((byte *) srcp2)[0]; | |
106 | srcp1 += 1; | |
107 | srcp2 += 1; | |
108 | } | |
109 | while (a0 == b0); | |
110 | return a0 - b0; | |
111 | } | |
112 | #endif | |
113 | ||
79937577 | 114 | static int memcmp_common_alignment (long, long, size_t) __THROW; |
a1470b6f | 115 | |
28f540f4 RM |
116 | /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t' |
117 | objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for | |
118 | memory operations on `op_t's. */ | |
28f540f4 | 119 | static int |
9d46370c | 120 | memcmp_common_alignment (long int srcp1, long int srcp2, size_t len) |
28f540f4 RM |
121 | { |
122 | op_t a0, a1; | |
123 | op_t b0, b1; | |
124 | ||
125 | switch (len % 4) | |
126 | { | |
dfd2257a | 127 | default: /* Avoid warning about uninitialized local variables. */ |
28f540f4 RM |
128 | case 2: |
129 | a0 = ((op_t *) srcp1)[0]; | |
130 | b0 = ((op_t *) srcp2)[0]; | |
131 | srcp1 -= 2 * OPSIZ; | |
132 | srcp2 -= 2 * OPSIZ; | |
133 | len += 2; | |
134 | goto do1; | |
135 | case 3: | |
136 | a1 = ((op_t *) srcp1)[0]; | |
137 | b1 = ((op_t *) srcp2)[0]; | |
138 | srcp1 -= OPSIZ; | |
139 | srcp2 -= OPSIZ; | |
140 | len += 1; | |
141 | goto do2; | |
142 | case 0: | |
143 | if (OP_T_THRES <= 3 * OPSIZ && len == 0) | |
144 | return 0; | |
145 | a0 = ((op_t *) srcp1)[0]; | |
146 | b0 = ((op_t *) srcp2)[0]; | |
147 | goto do3; | |
148 | case 1: | |
149 | a1 = ((op_t *) srcp1)[0]; | |
150 | b1 = ((op_t *) srcp2)[0]; | |
151 | srcp1 += OPSIZ; | |
152 | srcp2 += OPSIZ; | |
153 | len -= 1; | |
154 | if (OP_T_THRES <= 3 * OPSIZ && len == 0) | |
155 | goto do0; | |
156 | /* Fall through. */ | |
157 | } | |
158 | ||
159 | do | |
160 | { | |
161 | a0 = ((op_t *) srcp1)[0]; | |
162 | b0 = ((op_t *) srcp2)[0]; | |
163 | if (a1 != b1) | |
164 | return CMP_LT_OR_GT (a1, b1); | |
165 | ||
166 | do3: | |
167 | a1 = ((op_t *) srcp1)[1]; | |
168 | b1 = ((op_t *) srcp2)[1]; | |
169 | if (a0 != b0) | |
170 | return CMP_LT_OR_GT (a0, b0); | |
171 | ||
172 | do2: | |
173 | a0 = ((op_t *) srcp1)[2]; | |
174 | b0 = ((op_t *) srcp2)[2]; | |
175 | if (a1 != b1) | |
176 | return CMP_LT_OR_GT (a1, b1); | |
177 | ||
178 | do1: | |
179 | a1 = ((op_t *) srcp1)[3]; | |
180 | b1 = ((op_t *) srcp2)[3]; | |
181 | if (a0 != b0) | |
182 | return CMP_LT_OR_GT (a0, b0); | |
183 | ||
184 | srcp1 += 4 * OPSIZ; | |
185 | srcp2 += 4 * OPSIZ; | |
186 | len -= 4; | |
187 | } | |
188 | while (len != 0); | |
189 | ||
190 | /* This is the right position for do0. Please don't move | |
191 | it into the loop. */ | |
192 | do0: | |
193 | if (a1 != b1) | |
194 | return CMP_LT_OR_GT (a1, b1); | |
195 | return 0; | |
196 | } | |
197 | ||
79937577 | 198 | static int memcmp_not_common_alignment (long, long, size_t) __THROW; |
a1470b6f | 199 | |
28f540f4 RM |
200 | /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN |
201 | `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory | |
202 | operations on `op_t', but SRCP1 *should be unaligned*. */ | |
28f540f4 | 203 | static int |
9d46370c | 204 | memcmp_not_common_alignment (long int srcp1, long int srcp2, size_t len) |
28f540f4 RM |
205 | { |
206 | op_t a0, a1, a2, a3; | |
207 | op_t b0, b1, b2, b3; | |
208 | op_t x; | |
209 | int shl, shr; | |
210 | ||
211 | /* Calculate how to shift a word read at the memory operation | |
212 | aligned srcp1 to make it aligned for comparison. */ | |
213 | ||
214 | shl = 8 * (srcp1 % OPSIZ); | |
215 | shr = 8 * OPSIZ - shl; | |
216 | ||
217 | /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t' | |
218 | it points in the middle of. */ | |
219 | srcp1 &= -OPSIZ; | |
220 | ||
221 | switch (len % 4) | |
222 | { | |
dfd2257a | 223 | default: /* Avoid warning about uninitialized local variables. */ |
28f540f4 RM |
224 | case 2: |
225 | a1 = ((op_t *) srcp1)[0]; | |
226 | a2 = ((op_t *) srcp1)[1]; | |
227 | b2 = ((op_t *) srcp2)[0]; | |
228 | srcp1 -= 1 * OPSIZ; | |
229 | srcp2 -= 2 * OPSIZ; | |
230 | len += 2; | |
231 | goto do1; | |
232 | case 3: | |
233 | a0 = ((op_t *) srcp1)[0]; | |
234 | a1 = ((op_t *) srcp1)[1]; | |
235 | b1 = ((op_t *) srcp2)[0]; | |
236 | srcp2 -= 1 * OPSIZ; | |
237 | len += 1; | |
238 | goto do2; | |
239 | case 0: | |
240 | if (OP_T_THRES <= 3 * OPSIZ && len == 0) | |
241 | return 0; | |
242 | a3 = ((op_t *) srcp1)[0]; | |
243 | a0 = ((op_t *) srcp1)[1]; | |
244 | b0 = ((op_t *) srcp2)[0]; | |
245 | srcp1 += 1 * OPSIZ; | |
246 | goto do3; | |
247 | case 1: | |
248 | a2 = ((op_t *) srcp1)[0]; | |
249 | a3 = ((op_t *) srcp1)[1]; | |
250 | b3 = ((op_t *) srcp2)[0]; | |
251 | srcp1 += 2 * OPSIZ; | |
252 | srcp2 += 1 * OPSIZ; | |
253 | len -= 1; | |
254 | if (OP_T_THRES <= 3 * OPSIZ && len == 0) | |
255 | goto do0; | |
256 | /* Fall through. */ | |
257 | } | |
258 | ||
259 | do | |
260 | { | |
261 | a0 = ((op_t *) srcp1)[0]; | |
262 | b0 = ((op_t *) srcp2)[0]; | |
263 | x = MERGE(a2, shl, a3, shr); | |
264 | if (x != b3) | |
265 | return CMP_LT_OR_GT (x, b3); | |
266 | ||
267 | do3: | |
268 | a1 = ((op_t *) srcp1)[1]; | |
269 | b1 = ((op_t *) srcp2)[1]; | |
270 | x = MERGE(a3, shl, a0, shr); | |
271 | if (x != b0) | |
272 | return CMP_LT_OR_GT (x, b0); | |
273 | ||
274 | do2: | |
275 | a2 = ((op_t *) srcp1)[2]; | |
276 | b2 = ((op_t *) srcp2)[2]; | |
277 | x = MERGE(a0, shl, a1, shr); | |
278 | if (x != b1) | |
279 | return CMP_LT_OR_GT (x, b1); | |
280 | ||
281 | do1: | |
282 | a3 = ((op_t *) srcp1)[3]; | |
283 | b3 = ((op_t *) srcp2)[3]; | |
284 | x = MERGE(a1, shl, a2, shr); | |
285 | if (x != b2) | |
286 | return CMP_LT_OR_GT (x, b2); | |
287 | ||
288 | srcp1 += 4 * OPSIZ; | |
289 | srcp2 += 4 * OPSIZ; | |
290 | len -= 4; | |
291 | } | |
292 | while (len != 0); | |
293 | ||
294 | /* This is the right position for do0. Please don't move | |
295 | it into the loop. */ | |
296 | do0: | |
297 | x = MERGE(a2, shl, a3, shr); | |
298 | if (x != b3) | |
299 | return CMP_LT_OR_GT (x, b3); | |
300 | return 0; | |
301 | } | |
302 | ||
303 | int | |
9d46370c | 304 | MEMCMP (const __ptr_t s1, const __ptr_t s2, size_t len) |
28f540f4 RM |
305 | { |
306 | op_t a0; | |
307 | op_t b0; | |
308 | long int srcp1 = (long int) s1; | |
309 | long int srcp2 = (long int) s2; | |
310 | op_t res; | |
311 | ||
312 | if (len >= OP_T_THRES) | |
313 | { | |
314 | /* There are at least some bytes to compare. No need to test | |
315 | for LEN == 0 in this alignment loop. */ | |
316 | while (srcp2 % OPSIZ != 0) | |
317 | { | |
318 | a0 = ((byte *) srcp1)[0]; | |
319 | b0 = ((byte *) srcp2)[0]; | |
320 | srcp1 += 1; | |
321 | srcp2 += 1; | |
322 | res = a0 - b0; | |
323 | if (res != 0) | |
324 | return res; | |
325 | len -= 1; | |
326 | } | |
327 | ||
328 | /* SRCP2 is now aligned for memory operations on `op_t'. | |
329 | SRCP1 alignment determines if we can do a simple, | |
330 | aligned compare or need to shuffle bits. */ | |
331 | ||
332 | if (srcp1 % OPSIZ == 0) | |
333 | res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ); | |
334 | else | |
335 | res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ); | |
336 | if (res != 0) | |
337 | return res; | |
338 | ||
339 | /* Number of bytes remaining in the interval [0..OPSIZ-1]. */ | |
340 | srcp1 += len & -OPSIZ; | |
341 | srcp2 += len & -OPSIZ; | |
342 | len %= OPSIZ; | |
343 | } | |
344 | ||
345 | /* There are just a few bytes to compare. Use byte memory operations. */ | |
346 | while (len != 0) | |
347 | { | |
348 | a0 = ((byte *) srcp1)[0]; | |
349 | b0 = ((byte *) srcp2)[0]; | |
350 | srcp1 += 1; | |
351 | srcp2 += 1; | |
352 | res = a0 - b0; | |
353 | if (res != 0) | |
354 | return res; | |
355 | len -= 1; | |
356 | } | |
357 | ||
358 | return 0; | |
359 | } | |
758b2153 | 360 | libc_hidden_builtin_def(memcmp) |
28f540f4 | 361 | #ifdef weak_alias |
af6f3906 | 362 | # undef bcmp |
28f540f4 RM |
363 | weak_alias (memcmp, bcmp) |
364 | #endif |