1 /* Copyright (C) 1991, 1993, 1995 Free Software Foundation, Inc.
2 Contributed by Torbjorn Granlund (tege@sics.se).
4 NOTE: The canonical source of this file is maintained with the GNU C Library.
5 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
27 #define __ptr_t void *
28 #else /* Not C++ or ANSI C. */
31 #define __ptr_t char *
32 #endif /* C++ or ANSI C. */
34 #if defined (HAVE_STRING_H) || defined (_LIBC)
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. */
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. */
96 long int srcp1 = (long int) &a;
97 long int srcp2 = (long int) &b;
102 a0 = ((byte *) srcp1)[0];
103 b0 = ((byte *) srcp2)[0];
112 /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
113 objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
114 memory operations on `op_t's. */
119 memcmp_common_alignment (srcp1, srcp2, len)
130 a0 = ((op_t *) srcp1)[0];
131 b0 = ((op_t *) srcp2)[0];
137 a1 = ((op_t *) srcp1)[0];
138 b1 = ((op_t *) srcp2)[0];
144 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
146 a0 = ((op_t *) srcp1)[0];
147 b0 = ((op_t *) srcp2)[0];
150 a1 = ((op_t *) srcp1)[0];
151 b1 = ((op_t *) srcp2)[0];
155 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
162 a0 = ((op_t *) srcp1)[0];
163 b0 = ((op_t *) srcp2)[0];
165 return CMP_LT_OR_GT (a1, b1);
168 a1 = ((op_t *) srcp1)[1];
169 b1 = ((op_t *) srcp2)[1];
171 return CMP_LT_OR_GT (a0, b0);
174 a0 = ((op_t *) srcp1)[2];
175 b0 = ((op_t *) srcp2)[2];
177 return CMP_LT_OR_GT (a1, b1);
180 a1 = ((op_t *) srcp1)[3];
181 b1 = ((op_t *) srcp2)[3];
183 return CMP_LT_OR_GT (a0, b0);
191 /* This is the right position for do0. Please don't move
195 return CMP_LT_OR_GT (a1, b1);
199 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
200 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
201 operations on `op_t', but SRCP1 *should be unaligned*. */
206 memcmp_not_common_alignment (srcp1, srcp2, len)
216 /* Calculate how to shift a word read at the memory operation
217 aligned srcp1 to make it aligned for comparison. */
219 shl = 8 * (srcp1 % OPSIZ);
220 shr = 8 * OPSIZ - shl;
222 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
223 it points in the middle of. */
229 a1 = ((op_t *) srcp1)[0];
230 a2 = ((op_t *) srcp1)[1];
231 b2 = ((op_t *) srcp2)[0];
237 a0 = ((op_t *) srcp1)[0];
238 a1 = ((op_t *) srcp1)[1];
239 b1 = ((op_t *) srcp2)[0];
244 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
246 a3 = ((op_t *) srcp1)[0];
247 a0 = ((op_t *) srcp1)[1];
248 b0 = ((op_t *) srcp2)[0];
252 a2 = ((op_t *) srcp1)[0];
253 a3 = ((op_t *) srcp1)[1];
254 b3 = ((op_t *) srcp2)[0];
258 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
265 a0 = ((op_t *) srcp1)[0];
266 b0 = ((op_t *) srcp2)[0];
267 x = MERGE(a2, shl, a3, shr);
269 return CMP_LT_OR_GT (x, b3);
272 a1 = ((op_t *) srcp1)[1];
273 b1 = ((op_t *) srcp2)[1];
274 x = MERGE(a3, shl, a0, shr);
276 return CMP_LT_OR_GT (x, b0);
279 a2 = ((op_t *) srcp1)[2];
280 b2 = ((op_t *) srcp2)[2];
281 x = MERGE(a0, shl, a1, shr);
283 return CMP_LT_OR_GT (x, b1);
286 a3 = ((op_t *) srcp1)[3];
287 b3 = ((op_t *) srcp2)[3];
288 x = MERGE(a1, shl, a2, shr);
290 return CMP_LT_OR_GT (x, b2);
298 /* This is the right position for do0. Please don't move
301 x = MERGE(a2, shl, a3, shr);
303 return CMP_LT_OR_GT (x, b3);
315 long int srcp1 = (long int) s1;
316 long int srcp2 = (long int) s2;
319 if (len >= OP_T_THRES)
321 /* There are at least some bytes to compare. No need to test
322 for LEN == 0 in this alignment loop. */
323 while (srcp2 % OPSIZ != 0)
325 a0 = ((byte *) srcp1)[0];
326 b0 = ((byte *) srcp2)[0];
335 /* SRCP2 is now aligned for memory operations on `op_t'.
336 SRCP1 alignment determines if we can do a simple,
337 aligned compare or need to shuffle bits. */
339 if (srcp1 % OPSIZ == 0)
340 res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ);
342 res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ);
346 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
347 srcp1 += len & -OPSIZ;
348 srcp2 += len & -OPSIZ;
352 /* There are just a few bytes to compare. Use byte memory operations. */
355 a0 = ((byte *) srcp1)[0];
356 b0 = ((byte *) srcp2)[0];
370 weak_alias (memcmp, bcmp)