1
/* Copyright (C) 1991,1993,1995,1997,1998,2003,2004,2012
2
Free Software Foundation, Inc.
3
This file is part of the GNU C Library.
4
Contributed by Torbjorn Granlund (tege@sics.se).
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.
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.
16
You should have received a copy of the GNU Lesser General Public
17
License along with the GNU C Library; if not, see
18
<http://www.gnu.org/licenses/>. */
25
#define __ptr_t void *
27
#if defined HAVE_STRING_H || defined _LIBC
38
# if __BYTE_ORDER == __BIG_ENDIAN
39
# define WORDS_BIGENDIAN
42
#else /* Not in the GNU C library. */
44
# include <sys/types.h>
46
/* Type to use for aligned memory operations.
47
This should normally be the biggest type supported by a single load
48
and store. Must be an unsigned type. */
49
# define op_t unsigned long int
50
# define OPSIZ (sizeof(op_t))
52
/* Threshold value for when to enter the unrolled loops. */
53
# define OP_T_THRES 16
55
/* Type to use for unaligned operations. */
56
typedef unsigned char byte;
58
# ifndef WORDS_BIGENDIAN
59
# define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
61
# define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
64
#endif /* In the GNU C library. */
66
#ifdef WORDS_BIGENDIAN
67
# define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
69
# define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
72
/* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
74
/* The strategy of this memcmp is:
76
1. Compare bytes until one of the block pointers is aligned.
78
2. Compare using memcmp_common_alignment or
79
memcmp_not_common_alignment, regarding the alignment of the other
80
block after the initial byte operations. The maximum number of
81
full words (of type op_t) are compared in this way.
83
3. Compare the few remaining bytes. */
85
#ifndef WORDS_BIGENDIAN
86
/* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
87
A and B are known to be different.
88
This is needed only on little-endian machines. */
90
static int memcmp_bytes (op_t, op_t) __THROW;
99
long int srcp1 = (long int) &a;
100
long int srcp2 = (long int) &b;
105
a0 = ((byte *) srcp1)[0];
106
b0 = ((byte *) srcp2)[0];
115
static int memcmp_common_alignment (long, long, size_t) __THROW;
117
/* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
118
objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
119
memory operations on `op_t's. */
121
memcmp_common_alignment (srcp1, srcp2, len)
131
default: /* Avoid warning about uninitialized local variables. */
133
a0 = ((op_t *) srcp1)[0];
134
b0 = ((op_t *) srcp2)[0];
140
a1 = ((op_t *) srcp1)[0];
141
b1 = ((op_t *) srcp2)[0];
147
if (OP_T_THRES <= 3 * OPSIZ && len == 0)
149
a0 = ((op_t *) srcp1)[0];
150
b0 = ((op_t *) srcp2)[0];
153
a1 = ((op_t *) srcp1)[0];
154
b1 = ((op_t *) srcp2)[0];
158
if (OP_T_THRES <= 3 * OPSIZ && len == 0)
165
a0 = ((op_t *) srcp1)[0];
166
b0 = ((op_t *) srcp2)[0];
168
return CMP_LT_OR_GT (a1, b1);
171
a1 = ((op_t *) srcp1)[1];
172
b1 = ((op_t *) srcp2)[1];
174
return CMP_LT_OR_GT (a0, b0);
177
a0 = ((op_t *) srcp1)[2];
178
b0 = ((op_t *) srcp2)[2];
180
return CMP_LT_OR_GT (a1, b1);
183
a1 = ((op_t *) srcp1)[3];
184
b1 = ((op_t *) srcp2)[3];
186
return CMP_LT_OR_GT (a0, b0);
194
/* This is the right position for do0. Please don't move
198
return CMP_LT_OR_GT (a1, b1);
202
static int memcmp_not_common_alignment (long, long, size_t) __THROW;
204
/* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
205
`op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
206
operations on `op_t', but SRCP1 *should be unaligned*. */
208
memcmp_not_common_alignment (srcp1, srcp2, len)
218
/* Calculate how to shift a word read at the memory operation
219
aligned srcp1 to make it aligned for comparison. */
221
shl = 8 * (srcp1 % OPSIZ);
222
shr = 8 * OPSIZ - shl;
224
/* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
225
it points in the middle of. */
230
default: /* Avoid warning about uninitialized local variables. */
232
a1 = ((op_t *) srcp1)[0];
233
a2 = ((op_t *) srcp1)[1];
234
b2 = ((op_t *) srcp2)[0];
240
a0 = ((op_t *) srcp1)[0];
241
a1 = ((op_t *) srcp1)[1];
242
b1 = ((op_t *) srcp2)[0];
247
if (OP_T_THRES <= 3 * OPSIZ && len == 0)
249
a3 = ((op_t *) srcp1)[0];
250
a0 = ((op_t *) srcp1)[1];
251
b0 = ((op_t *) srcp2)[0];
255
a2 = ((op_t *) srcp1)[0];
256
a3 = ((op_t *) srcp1)[1];
257
b3 = ((op_t *) srcp2)[0];
261
if (OP_T_THRES <= 3 * OPSIZ && len == 0)
268
a0 = ((op_t *) srcp1)[0];
269
b0 = ((op_t *) srcp2)[0];
270
x = MERGE(a2, shl, a3, shr);
272
return CMP_LT_OR_GT (x, b3);
275
a1 = ((op_t *) srcp1)[1];
276
b1 = ((op_t *) srcp2)[1];
277
x = MERGE(a3, shl, a0, shr);
279
return CMP_LT_OR_GT (x, b0);
282
a2 = ((op_t *) srcp1)[2];
283
b2 = ((op_t *) srcp2)[2];
284
x = MERGE(a0, shl, a1, shr);
286
return CMP_LT_OR_GT (x, b1);
289
a3 = ((op_t *) srcp1)[3];
290
b3 = ((op_t *) srcp2)[3];
291
x = MERGE(a1, shl, a2, shr);
293
return CMP_LT_OR_GT (x, b2);
301
/* This is the right position for do0. Please don't move
304
x = MERGE(a2, shl, a3, shr);
306
return CMP_LT_OR_GT (x, b3);
318
long int srcp1 = (long int) s1;
319
long int srcp2 = (long int) s2;
322
if (len >= OP_T_THRES)
324
/* There are at least some bytes to compare. No need to test
325
for LEN == 0 in this alignment loop. */
326
while (srcp2 % OPSIZ != 0)
328
a0 = ((byte *) srcp1)[0];
329
b0 = ((byte *) srcp2)[0];
338
/* SRCP2 is now aligned for memory operations on `op_t'.
339
SRCP1 alignment determines if we can do a simple,
340
aligned compare or need to shuffle bits. */
342
if (srcp1 % OPSIZ == 0)
343
res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ);
345
res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ);
349
/* Number of bytes remaining in the interval [0..OPSIZ-1]. */
350
srcp1 += len & -OPSIZ;
351
srcp2 += len & -OPSIZ;
355
/* There are just a few bytes to compare. Use byte memory operations. */
358
a0 = ((byte *) srcp1)[0];
359
b0 = ((byte *) srcp2)[0];