1 /* Copyright (C) 1991-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
23 #define __ptr_t void *
25 #if defined HAVE_STRING_H || defined _LIBC
32 # define MEMCMP memcmp
40 # if __BYTE_ORDER == __BIG_ENDIAN
41 # define WORDS_BIGENDIAN
44 #else /* Not in the GNU C library. */
46 # include <sys/types.h>
48 /* Type to use for aligned memory operations.
49 This should normally be the biggest type supported by a single load
50 and store. Must be an unsigned type. */
51 # define op_t unsigned long int
52 # define OPSIZ (sizeof(op_t))
54 /* Threshold value for when to enter the unrolled loops. */
55 # define OP_T_THRES 16
57 /* Type to use for unaligned operations. */
58 typedef unsigned char byte
;
60 #endif /* In the GNU C library. */
62 /* Provide the appropriate builtins to shift two registers based on
63 the alignment of a pointer held in a third register, and to reverse
64 the bytes in a word. */
66 #define DBLALIGN __insn_dblalign
67 #define REVBYTES __insn_revbytes
69 #define DBLALIGN __insn_dword_align
70 #define REVBYTES __insn_bytex
73 #ifdef WORDS_BIGENDIAN
74 # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
76 # define CMP_LT_OR_GT(a, b) (REVBYTES(a) > REVBYTES(b) ? 1 : -1)
79 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
81 /* The strategy of this memcmp is:
83 1. Compare bytes until one of the block pointers is aligned.
85 2. Compare using memcmp_common_alignment or
86 memcmp_not_common_alignment, regarding the alignment of the other
87 block after the initial byte operations. The maximum number of
88 full words (of type op_t) are compared in this way.
90 3. Compare the few remaining bytes. */
92 static int memcmp_common_alignment (long, long, size_t) __THROW
;
94 /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
95 objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
96 memory operations on `op_t's. */
98 memcmp_common_alignment (srcp1
, srcp2
, len
)
108 default: /* Avoid warning about uninitialized local variables. */
110 a0
= ((op_t
*) srcp1
)[0];
111 b0
= ((op_t
*) srcp2
)[0];
117 a1
= ((op_t
*) srcp1
)[0];
118 b1
= ((op_t
*) srcp2
)[0];
124 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
126 a0
= ((op_t
*) srcp1
)[0];
127 b0
= ((op_t
*) srcp2
)[0];
132 a1
= ((op_t
*) srcp1
)[0];
133 b1
= ((op_t
*) srcp2
)[0];
137 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
144 a0
= ((op_t
*) srcp1
)[0];
145 b0
= ((op_t
*) srcp2
)[0];
148 if (__glibc_likely (a1
!= b1
))
149 return CMP_LT_OR_GT (a1
, b1
);
152 a1
= ((op_t
*) srcp1
)[0];
153 b1
= ((op_t
*) srcp2
)[0];
156 if (__glibc_likely (a0
!= b0
))
157 return CMP_LT_OR_GT (a0
, b0
);
160 a0
= ((op_t
*) srcp1
)[0];
161 b0
= ((op_t
*) srcp2
)[0];
164 if (__glibc_likely (a1
!= b1
))
165 return CMP_LT_OR_GT (a1
, b1
);
168 a1
= ((op_t
*) srcp1
)[0];
169 b1
= ((op_t
*) srcp2
)[0];
172 if (__glibc_likely (a0
!= b0
))
173 return CMP_LT_OR_GT (a0
, b0
);
179 /* This is the right position for do0. Please don't move
182 if (__glibc_likely (a1
!= b1
))
183 return CMP_LT_OR_GT (a1
, b1
);
187 static int memcmp_not_common_alignment (long, long, size_t) __THROW
;
189 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
190 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
191 operations on `op_t', but SRCP1 *should be unaligned*. */
193 memcmp_not_common_alignment (srcp1
, srcp2
, len
)
203 /* Calculate how to shift a word read at the memory operation
204 aligned srcp1 to make it aligned for comparison. */
206 srcp1i
= (void *) srcp1
;
208 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
209 it points in the middle of. */
214 default: /* Avoid warning about uninitialized local variables. */
216 a1
= ((op_t
*) srcp1
)[0];
217 a2
= ((op_t
*) srcp1
)[1];
218 b2
= ((op_t
*) srcp2
)[0];
224 a0
= ((op_t
*) srcp1
)[0];
225 a1
= ((op_t
*) srcp1
)[1];
226 b1
= ((op_t
*) srcp2
)[0];
232 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
234 a3
= ((op_t
*) srcp1
)[0];
235 a0
= ((op_t
*) srcp1
)[1];
236 b0
= ((op_t
*) srcp2
)[0];
241 a2
= ((op_t
*) srcp1
)[0];
242 a3
= ((op_t
*) srcp1
)[1];
243 b3
= ((op_t
*) srcp2
)[0];
247 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
254 a0
= ((op_t
*) srcp1
)[0];
255 b0
= ((op_t
*) srcp2
)[0];
256 x
= DBLALIGN (a2
, a3
, srcp1i
);
259 if (__glibc_likely (x
!= b3
))
260 return CMP_LT_OR_GT (x
, b3
);
263 a1
= ((op_t
*) srcp1
)[0];
264 b1
= ((op_t
*) srcp2
)[0];
265 x
= DBLALIGN (a3
, a0
, srcp1i
);
268 if (__glibc_likely (x
!= b0
))
269 return CMP_LT_OR_GT (x
, b0
);
272 a2
= ((op_t
*) srcp1
)[0];
273 b2
= ((op_t
*) srcp2
)[0];
274 x
= DBLALIGN (a0
, a1
, srcp1i
);
277 if (__glibc_likely (x
!= b1
))
278 return CMP_LT_OR_GT (x
, b1
);
281 a3
= ((op_t
*) srcp1
)[0];
282 b3
= ((op_t
*) srcp2
)[0];
283 x
= DBLALIGN (a1
, a2
, srcp1i
);
286 if (__glibc_likely (x
!= b2
))
287 return CMP_LT_OR_GT (x
, b2
);
293 /* This is the right position for do0. Please don't move
296 x
= DBLALIGN (a2
, a3
, srcp1i
);
297 if (__glibc_likely (x
!= b3
))
298 return CMP_LT_OR_GT (x
, b3
);
310 long int srcp1
= (long int) s1
;
311 long int srcp2
= (long int) s2
;
314 if (len
>= OP_T_THRES
)
316 /* There are at least some bytes to compare. No need to test
317 for LEN == 0 in this alignment loop. */
318 while (srcp2
% OPSIZ
!= 0)
320 a0
= ((byte
*) srcp1
)[0];
321 b0
= ((byte
*) srcp2
)[0];
325 if (__glibc_likely (res
!= 0))
330 /* SRCP2 is now aligned for memory operations on `op_t'.
331 SRCP1 alignment determines if we can do a simple,
332 aligned compare or need to shuffle bits. */
334 if (srcp1
% OPSIZ
== 0)
335 res
= memcmp_common_alignment (srcp1
, srcp2
, len
/ OPSIZ
);
337 res
= memcmp_not_common_alignment (srcp1
, srcp2
, len
/ OPSIZ
);
341 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
342 srcp1
+= len
& -OPSIZ
;
343 srcp2
+= len
& -OPSIZ
;
347 /* There are just a few bytes to compare. Use byte memory operations. */
350 a0
= ((byte
*) srcp1
)[0];
351 b0
= ((byte
*) srcp2
)[0];
355 if (__glibc_likely (res
!= 0))
362 libc_hidden_builtin_def(memcmp
)
365 weak_alias (memcmp
, bcmp
)