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/>. */
24 #define __ptr_t void *
26 #if defined HAVE_STRING_H || defined _LIBC
33 # define MEMCMP memcmp
41 # if __BYTE_ORDER == __BIG_ENDIAN
42 # define WORDS_BIGENDIAN
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)))
64 # define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
67 #endif /* In the GNU C library. */
69 #ifdef WORDS_BIGENDIAN
70 # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
72 # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
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
;
96 memcmp_bytes (op_t a
, op_t b
)
98 long int srcp1
= (long int) &a
;
99 long int srcp2
= (long int) &b
;
104 a0
= ((byte
*) srcp1
)[0];
105 b0
= ((byte
*) srcp2
)[0];
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. */
120 memcmp_common_alignment (long int srcp1
, long int srcp2
, size_t len
)
127 default: /* Avoid warning about uninitialized local variables. */
129 a0
= ((op_t
*) srcp1
)[0];
130 b0
= ((op_t
*) srcp2
)[0];
136 a1
= ((op_t
*) srcp1
)[0];
137 b1
= ((op_t
*) srcp2
)[0];
143 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
145 a0
= ((op_t
*) srcp1
)[0];
146 b0
= ((op_t
*) srcp2
)[0];
149 a1
= ((op_t
*) srcp1
)[0];
150 b1
= ((op_t
*) srcp2
)[0];
154 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
161 a0
= ((op_t
*) srcp1
)[0];
162 b0
= ((op_t
*) srcp2
)[0];
164 return CMP_LT_OR_GT (a1
, b1
);
167 a1
= ((op_t
*) srcp1
)[1];
168 b1
= ((op_t
*) srcp2
)[1];
170 return CMP_LT_OR_GT (a0
, b0
);
173 a0
= ((op_t
*) srcp1
)[2];
174 b0
= ((op_t
*) srcp2
)[2];
176 return CMP_LT_OR_GT (a1
, b1
);
179 a1
= ((op_t
*) srcp1
)[3];
180 b1
= ((op_t
*) srcp2
)[3];
182 return CMP_LT_OR_GT (a0
, b0
);
190 /* This is the right position for do0. Please don't move
194 return CMP_LT_OR_GT (a1
, b1
);
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*. */
204 memcmp_not_common_alignment (long int srcp1
, long int srcp2
, size_t len
)
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. */
223 default: /* Avoid warning about uninitialized local variables. */
225 a1
= ((op_t
*) srcp1
)[0];
226 a2
= ((op_t
*) srcp1
)[1];
227 b2
= ((op_t
*) srcp2
)[0];
233 a0
= ((op_t
*) srcp1
)[0];
234 a1
= ((op_t
*) srcp1
)[1];
235 b1
= ((op_t
*) srcp2
)[0];
240 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
242 a3
= ((op_t
*) srcp1
)[0];
243 a0
= ((op_t
*) srcp1
)[1];
244 b0
= ((op_t
*) srcp2
)[0];
248 a2
= ((op_t
*) srcp1
)[0];
249 a3
= ((op_t
*) srcp1
)[1];
250 b3
= ((op_t
*) srcp2
)[0];
254 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
261 a0
= ((op_t
*) srcp1
)[0];
262 b0
= ((op_t
*) srcp2
)[0];
263 x
= MERGE(a2
, shl
, a3
, shr
);
265 return CMP_LT_OR_GT (x
, b3
);
268 a1
= ((op_t
*) srcp1
)[1];
269 b1
= ((op_t
*) srcp2
)[1];
270 x
= MERGE(a3
, shl
, a0
, shr
);
272 return CMP_LT_OR_GT (x
, b0
);
275 a2
= ((op_t
*) srcp1
)[2];
276 b2
= ((op_t
*) srcp2
)[2];
277 x
= MERGE(a0
, shl
, a1
, shr
);
279 return CMP_LT_OR_GT (x
, b1
);
282 a3
= ((op_t
*) srcp1
)[3];
283 b3
= ((op_t
*) srcp2
)[3];
284 x
= MERGE(a1
, shl
, a2
, shr
);
286 return CMP_LT_OR_GT (x
, b2
);
294 /* This is the right position for do0. Please don't move
297 x
= MERGE(a2
, shl
, a3
, shr
);
299 return CMP_LT_OR_GT (x
, b3
);
304 MEMCMP (const __ptr_t s1
, const __ptr_t s2
, size_t len
)
308 long int srcp1
= (long int) s1
;
309 long int srcp2
= (long int) s2
;
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];
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
);
335 res
= memcmp_not_common_alignment (srcp1
, srcp2
, len
/ OPSIZ
);
339 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
340 srcp1
+= len
& -OPSIZ
;
341 srcp2
+= len
& -OPSIZ
;
345 /* There are just a few bytes to compare. Use byte memory operations. */
348 a0
= ((byte
*) srcp1
)[0];
349 b0
= ((byte
*) srcp2
)[0];
360 libc_hidden_builtin_def(memcmp
)
363 weak_alias (memcmp
, bcmp
)