Fix tests-printers handling for cross compiling.
[glibc.git] / string / memcmp.c
blob135bb689ec4163da84107dcab68bd83aa029a6e8
1 /* Copyright (C) 1991-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Torbjorn Granlund (tege@sics.se).
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #undef __ptr_t
24 #define __ptr_t void *
26 #if defined HAVE_STRING_H || defined _LIBC
27 # include <string.h>
28 #endif
30 #undef memcmp
32 #ifndef MEMCMP
33 # define MEMCMP memcmp
34 #endif
36 #ifdef _LIBC
38 # include <memcopy.h>
39 # include <endian.h>
41 # if __BYTE_ORDER == __BIG_ENDIAN
42 # define WORDS_BIGENDIAN
43 # endif
45 #else /* Not in the GNU C library. */
47 # include <sys/types.h>
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. */
52 # define op_t unsigned long int
53 # define OPSIZ (sizeof(op_t))
55 /* Threshold value for when to enter the unrolled loops. */
56 # define OP_T_THRES 16
58 /* Type to use for unaligned operations. */
59 typedef unsigned char byte;
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
67 #endif /* In the GNU C library. */
69 #ifdef WORDS_BIGENDIAN
70 # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
71 #else
72 # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
73 #endif
75 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
77 /* The strategy of this memcmp is:
79 1. Compare bytes until one of the block pointers is aligned.
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.
86 3. Compare the few remaining bytes. */
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. */
93 static int memcmp_bytes (op_t, op_t) __THROW;
95 static int
96 memcmp_bytes (op_t a, op_t b)
98 long int srcp1 = (long int) &a;
99 long int srcp2 = (long int) &b;
100 op_t a0, b0;
104 a0 = ((byte *) srcp1)[0];
105 b0 = ((byte *) srcp2)[0];
106 srcp1 += 1;
107 srcp2 += 1;
109 while (a0 == b0);
110 return a0 - b0;
112 #endif
114 static int memcmp_common_alignment (long, long, size_t) __THROW;
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. */
119 static int
120 memcmp_common_alignment (long int srcp1, long int srcp2, size_t len)
122 op_t a0, a1;
123 op_t b0, b1;
125 switch (len % 4)
127 default: /* Avoid warning about uninitialized local variables. */
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. */
161 a0 = ((op_t *) srcp1)[0];
162 b0 = ((op_t *) srcp2)[0];
163 if (a1 != b1)
164 return CMP_LT_OR_GT (a1, b1);
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);
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);
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);
184 srcp1 += 4 * OPSIZ;
185 srcp2 += 4 * OPSIZ;
186 len -= 4;
188 while (len != 0);
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;
198 static int memcmp_not_common_alignment (long, long, size_t) __THROW;
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*. */
203 static int
204 memcmp_not_common_alignment (long int srcp1, long int srcp2, size_t len)
206 op_t a0, a1, a2, a3;
207 op_t b0, b1, b2, b3;
208 op_t x;
209 int shl, shr;
211 /* Calculate how to shift a word read at the memory operation
212 aligned srcp1 to make it aligned for comparison. */
214 shl = 8 * (srcp1 % OPSIZ);
215 shr = 8 * OPSIZ - shl;
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;
221 switch (len % 4)
223 default: /* Avoid warning about uninitialized local variables. */
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. */
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);
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);
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);
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);
288 srcp1 += 4 * OPSIZ;
289 srcp2 += 4 * OPSIZ;
290 len -= 4;
292 while (len != 0);
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;
304 MEMCMP (const __ptr_t s1, const __ptr_t s2, size_t len)
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;
312 if (len >= OP_T_THRES)
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)
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;
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. */
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;
339 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
340 srcp1 += len & -OPSIZ;
341 srcp2 += len & -OPSIZ;
342 len %= OPSIZ;
345 /* There are just a few bytes to compare. Use byte memory operations. */
346 while (len != 0)
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;
358 return 0;
360 libc_hidden_builtin_def(memcmp)
361 #ifdef weak_alias
362 # undef bcmp
363 weak_alias (memcmp, bcmp)
364 #endif