1 /* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
2 Contributed by Torbjorn Granlund (tege@sics.se).
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
24 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
25 #define __ptr_t void *
26 #else /* Not C++ or ANSI C. */
29 #define __ptr_t char *
30 #endif /* C++ or ANSI C. */
32 #if defined (HAVE_STRING_H) || defined (_LIBC)
40 #else /* Not in the GNU C library. */
42 #include <sys/types.h>
44 /* Type to use for aligned memory operations.
45 This should normally be the biggest type supported by a single load
46 and store. Must be an unsigned type. */
47 #define op_t unsigned long int
48 #define OPSIZ (sizeof(op_t))
50 /* Threshold value for when to enter the unrolled loops. */
53 /* Type to use for unaligned operations. */
54 typedef unsigned char byte;
56 #ifndef WORDS_BIGENDIAN
57 #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
59 #define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
62 #endif /* In the GNU C library. */
64 #ifdef WORDS_BIGENDIAN
65 #define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
67 #define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
70 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
72 /* The strategy of this memcmp is:
74 1. Compare bytes until one of the block pointers is aligned.
76 2. Compare using memcmp_common_alignment or
77 memcmp_not_common_alignment, regarding the alignment of the other
78 block after the initial byte operations. The maximum number of
79 full words (of type op_t) are compared in this way.
81 3. Compare the few remaining bytes. */
83 #ifndef WORDS_BIGENDIAN
84 /* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
85 A and B are known to be different.
86 This is needed only on little-endian machines. */
94 long int srcp1 = (long int) &a;
95 long int srcp2 = (long int) &b;
100 a0 = ((byte *) srcp1)[0];
101 b0 = ((byte *) srcp2)[0];
110 /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
111 objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
112 memory operations on `op_t's. */
117 memcmp_common_alignment (srcp1, srcp2, len)
128 a0 = ((op_t *) srcp1)[0];
129 b0 = ((op_t *) srcp2)[0];
135 a1 = ((op_t *) srcp1)[0];
136 b1 = ((op_t *) srcp2)[0];
142 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
144 a0 = ((op_t *) srcp1)[0];
145 b0 = ((op_t *) srcp2)[0];
148 a1 = ((op_t *) srcp1)[0];
149 b1 = ((op_t *) srcp2)[0];
153 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
160 a0 = ((op_t *) srcp1)[0];
161 b0 = ((op_t *) srcp2)[0];
163 return CMP_LT_OR_GT (a1, b1);
166 a1 = ((op_t *) srcp1)[1];
167 b1 = ((op_t *) srcp2)[1];
169 return CMP_LT_OR_GT (a0, b0);
172 a0 = ((op_t *) srcp1)[2];
173 b0 = ((op_t *) srcp2)[2];
175 return CMP_LT_OR_GT (a1, b1);
178 a1 = ((op_t *) srcp1)[3];
179 b1 = ((op_t *) srcp2)[3];
181 return CMP_LT_OR_GT (a0, b0);
189 /* This is the right position for do0. Please don't move
193 return CMP_LT_OR_GT (a1, b1);
197 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
198 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
199 operations on `op_t', but SRCP1 *should be unaligned*. */
204 memcmp_not_common_alignment (srcp1, srcp2, len)
214 /* Calculate how to shift a word read at the memory operation
215 aligned srcp1 to make it aligned for comparison. */
217 shl = 8 * (srcp1 % OPSIZ);
218 shr = 8 * OPSIZ - shl;
220 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
221 it points in the middle of. */
227 a1 = ((op_t *) srcp1)[0];
228 a2 = ((op_t *) srcp1)[1];
229 b2 = ((op_t *) srcp2)[0];
235 a0 = ((op_t *) srcp1)[0];
236 a1 = ((op_t *) srcp1)[1];
237 b1 = ((op_t *) srcp2)[0];
242 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
244 a3 = ((op_t *) srcp1)[0];
245 a0 = ((op_t *) srcp1)[1];
246 b0 = ((op_t *) srcp2)[0];
250 a2 = ((op_t *) srcp1)[0];
251 a3 = ((op_t *) srcp1)[1];
252 b3 = ((op_t *) srcp2)[0];
256 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
263 a0 = ((op_t *) srcp1)[0];
264 b0 = ((op_t *) srcp2)[0];
265 x = MERGE (a2, shl, a3, shr);
267 return CMP_LT_OR_GT (x, b3);
270 a1 = ((op_t *) srcp1)[1];
271 b1 = ((op_t *) srcp2)[1];
272 x = MERGE (a3, shl, a0, shr);
274 return CMP_LT_OR_GT (x, b0);
277 a2 = ((op_t *) srcp1)[2];
278 b2 = ((op_t *) srcp2)[2];
279 x = MERGE (a0, shl, a1, shr);
281 return CMP_LT_OR_GT (x, b1);
284 a3 = ((op_t *) srcp1)[3];
285 b3 = ((op_t *) srcp2)[3];
286 x = MERGE (a1, shl, a2, shr);
288 return CMP_LT_OR_GT (x, b2);
296 /* This is the right position for do0. Please don't move
299 x = MERGE (a2, shl, a3, shr);
301 return CMP_LT_OR_GT (x, b3);
313 long int srcp1 = (long int) s1;
314 long int srcp2 = (long int) s2;
317 if (len >= OP_T_THRES)
319 /* There are at least some bytes to compare. No need to test
320 for LEN == 0 in this alignment loop. */
321 while (srcp2 % OPSIZ != 0)
323 a0 = ((byte *) srcp1)[0];
324 b0 = ((byte *) srcp2)[0];
333 /* SRCP2 is now aligned for memory operations on `op_t'.
334 SRCP1 alignment determines if we can do a simple,
335 aligned compare or need to shuffle bits. */
337 if (srcp1 % OPSIZ == 0)
338 res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ);
340 res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ);
344 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
345 srcp1 += len & -OPSIZ;
346 srcp2 += len & -OPSIZ;
350 /* There are just a few bytes to compare. Use byte memory operations. */
353 a0 = ((byte *) srcp1)[0];
354 b0 = ((byte *) srcp2)[0];