1 /* Copyright (C) 1991-2016 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 (long int srcp1
, long int srcp2
, size_t len
)
105 default: /* Avoid warning about uninitialized local variables. */
107 a0
= ((op_t
*) srcp1
)[0];
108 b0
= ((op_t
*) srcp2
)[0];
114 a1
= ((op_t
*) srcp1
)[0];
115 b1
= ((op_t
*) srcp2
)[0];
121 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
123 a0
= ((op_t
*) srcp1
)[0];
124 b0
= ((op_t
*) srcp2
)[0];
129 a1
= ((op_t
*) srcp1
)[0];
130 b1
= ((op_t
*) srcp2
)[0];
134 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
141 a0
= ((op_t
*) srcp1
)[0];
142 b0
= ((op_t
*) srcp2
)[0];
145 if (__glibc_likely (a1
!= b1
))
146 return CMP_LT_OR_GT (a1
, b1
);
149 a1
= ((op_t
*) srcp1
)[0];
150 b1
= ((op_t
*) srcp2
)[0];
153 if (__glibc_likely (a0
!= b0
))
154 return CMP_LT_OR_GT (a0
, b0
);
157 a0
= ((op_t
*) srcp1
)[0];
158 b0
= ((op_t
*) srcp2
)[0];
161 if (__glibc_likely (a1
!= b1
))
162 return CMP_LT_OR_GT (a1
, b1
);
165 a1
= ((op_t
*) srcp1
)[0];
166 b1
= ((op_t
*) srcp2
)[0];
169 if (__glibc_likely (a0
!= b0
))
170 return CMP_LT_OR_GT (a0
, b0
);
176 /* This is the right position for do0. Please don't move
179 if (__glibc_likely (a1
!= b1
))
180 return CMP_LT_OR_GT (a1
, b1
);
184 static int memcmp_not_common_alignment (long, long, size_t) __THROW
;
186 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
187 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
188 operations on `op_t', but SRCP1 *should be unaligned*. */
190 memcmp_not_common_alignment (long int srcp1
, long int srcp2
, size_t len
)
197 /* Calculate how to shift a word read at the memory operation
198 aligned srcp1 to make it aligned for comparison. */
200 srcp1i
= (void *) srcp1
;
202 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
203 it points in the middle of. */
208 default: /* Avoid warning about uninitialized local variables. */
210 a1
= ((op_t
*) srcp1
)[0];
211 a2
= ((op_t
*) srcp1
)[1];
212 b2
= ((op_t
*) srcp2
)[0];
218 a0
= ((op_t
*) srcp1
)[0];
219 a1
= ((op_t
*) srcp1
)[1];
220 b1
= ((op_t
*) srcp2
)[0];
226 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
228 a3
= ((op_t
*) srcp1
)[0];
229 a0
= ((op_t
*) srcp1
)[1];
230 b0
= ((op_t
*) srcp2
)[0];
235 a2
= ((op_t
*) srcp1
)[0];
236 a3
= ((op_t
*) srcp1
)[1];
237 b3
= ((op_t
*) srcp2
)[0];
241 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
248 a0
= ((op_t
*) srcp1
)[0];
249 b0
= ((op_t
*) srcp2
)[0];
250 x
= DBLALIGN (a2
, a3
, srcp1i
);
253 if (__glibc_likely (x
!= b3
))
254 return CMP_LT_OR_GT (x
, b3
);
257 a1
= ((op_t
*) srcp1
)[0];
258 b1
= ((op_t
*) srcp2
)[0];
259 x
= DBLALIGN (a3
, a0
, srcp1i
);
262 if (__glibc_likely (x
!= b0
))
263 return CMP_LT_OR_GT (x
, b0
);
266 a2
= ((op_t
*) srcp1
)[0];
267 b2
= ((op_t
*) srcp2
)[0];
268 x
= DBLALIGN (a0
, a1
, srcp1i
);
271 if (__glibc_likely (x
!= b1
))
272 return CMP_LT_OR_GT (x
, b1
);
275 a3
= ((op_t
*) srcp1
)[0];
276 b3
= ((op_t
*) srcp2
)[0];
277 x
= DBLALIGN (a1
, a2
, srcp1i
);
280 if (__glibc_likely (x
!= b2
))
281 return CMP_LT_OR_GT (x
, b2
);
287 /* This is the right position for do0. Please don't move
290 x
= DBLALIGN (a2
, a3
, srcp1i
);
291 if (__glibc_likely (x
!= b3
))
292 return CMP_LT_OR_GT (x
, b3
);
297 MEMCMP (const __ptr_t s1
, const __ptr_t s2
, size_t len
)
301 long int srcp1
= (long int) s1
;
302 long int srcp2
= (long int) s2
;
305 if (len
>= OP_T_THRES
)
307 /* There are at least some bytes to compare. No need to test
308 for LEN == 0 in this alignment loop. */
309 while (srcp2
% OPSIZ
!= 0)
311 a0
= ((byte
*) srcp1
)[0];
312 b0
= ((byte
*) srcp2
)[0];
316 if (__glibc_likely (res
!= 0))
321 /* SRCP2 is now aligned for memory operations on `op_t'.
322 SRCP1 alignment determines if we can do a simple,
323 aligned compare or need to shuffle bits. */
325 if (srcp1
% OPSIZ
== 0)
326 res
= memcmp_common_alignment (srcp1
, srcp2
, len
/ OPSIZ
);
328 res
= memcmp_not_common_alignment (srcp1
, srcp2
, len
/ OPSIZ
);
332 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
333 srcp1
+= len
& -OPSIZ
;
334 srcp2
+= len
& -OPSIZ
;
338 /* There are just a few bytes to compare. Use byte memory operations. */
341 a0
= ((byte
*) srcp1
)[0];
342 b0
= ((byte
*) srcp2
)[0];
346 if (__glibc_likely (res
!= 0))
353 libc_hidden_builtin_def(memcmp
)
356 weak_alias (memcmp
, bcmp
)