1 /* Copyright (C) 1991, 1993, 1995, 1997, 1998 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 along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #if defined __cplusplus || (defined __STDC__ && __STDC__)
28 # define __ptr_t void *
29 #else /* Not C++ or ANSI C. */
32 # define __ptr_t char *
33 #endif /* C++ or ANSI C. */
36 # if defined __GNUC__ || (defined __STDC__ && __STDC__)
37 # define __P(args) args
43 #if defined HAVE_STRING_H || defined _LIBC
53 #else /* Not in the GNU C library. */
55 # include <sys/types.h>
57 /* Type to use for aligned memory operations.
58 This should normally be the biggest type supported by a single load
59 and store. Must be an unsigned type. */
60 # define op_t unsigned long int
61 # define OPSIZ (sizeof(op_t))
63 /* Threshold value for when to enter the unrolled loops. */
64 # define OP_T_THRES 16
66 /* Type to use for unaligned operations. */
67 typedef unsigned char byte;
69 # ifndef WORDS_BIGENDIAN
70 # define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
72 # define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
75 #endif /* In the GNU C library. */
77 #ifdef WORDS_BIGENDIAN
78 # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
80 # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
83 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
85 /* The strategy of this memcmp is:
87 1. Compare bytes until one of the block pointers is aligned.
89 2. Compare using memcmp_common_alignment or
90 memcmp_not_common_alignment, regarding the alignment of the other
91 block after the initial byte operations. The maximum number of
92 full words (of type op_t) are compared in this way.
94 3. Compare the few remaining bytes. */
96 #ifndef WORDS_BIGENDIAN
97 /* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
98 A and B are known to be different.
99 This is needed only on little-endian machines. */
101 static int memcmp_bytes __P((op_t, op_t));
110 intptr_t srcp1 = (intptr_t) &a;
111 intptr_t srcp2 = (intptr_t) &b;
116 a0 = ((byte *) srcp1)[0];
117 b0 = ((byte *) srcp2)[0];
126 static int memcmp_common_alignment __P((intptr_t, intptr_t, size_t));
128 /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
129 objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
130 memory operations on `op_t's. */
135 memcmp_common_alignment (srcp1, srcp2, len)
145 default: /* Avoid warning about uninitialized local variables. */
147 a0 = ((op_t *) srcp1)[0];
148 b0 = ((op_t *) srcp2)[0];
154 a1 = ((op_t *) srcp1)[0];
155 b1 = ((op_t *) srcp2)[0];
161 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
163 a0 = ((op_t *) srcp1)[0];
164 b0 = ((op_t *) srcp2)[0];
167 a1 = ((op_t *) srcp1)[0];
168 b1 = ((op_t *) srcp2)[0];
172 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
179 a0 = ((op_t *) srcp1)[0];
180 b0 = ((op_t *) srcp2)[0];
182 return CMP_LT_OR_GT (a1, b1);
185 a1 = ((op_t *) srcp1)[1];
186 b1 = ((op_t *) srcp2)[1];
188 return CMP_LT_OR_GT (a0, b0);
191 a0 = ((op_t *) srcp1)[2];
192 b0 = ((op_t *) srcp2)[2];
194 return CMP_LT_OR_GT (a1, b1);
197 a1 = ((op_t *) srcp1)[3];
198 b1 = ((op_t *) srcp2)[3];
200 return CMP_LT_OR_GT (a0, b0);
208 /* This is the right position for do0. Please don't move
212 return CMP_LT_OR_GT (a1, b1);
216 static int memcmp_not_common_alignment __P((intptr_t, intptr_t, size_t));
218 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
219 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
220 operations on `op_t', but SRCP1 *should be unaligned*. */
225 memcmp_not_common_alignment (srcp1, srcp2, len)
235 /* Calculate how to shift a word read at the memory operation
236 aligned srcp1 to make it aligned for comparison. */
238 shl = 8 * (srcp1 % OPSIZ);
239 shr = 8 * OPSIZ - shl;
241 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
242 it points in the middle of. */
247 default: /* Avoid warning about uninitialized local variables. */
249 a1 = ((op_t *) srcp1)[0];
250 a2 = ((op_t *) srcp1)[1];
251 b2 = ((op_t *) srcp2)[0];
257 a0 = ((op_t *) srcp1)[0];
258 a1 = ((op_t *) srcp1)[1];
259 b1 = ((op_t *) srcp2)[0];
264 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
266 a3 = ((op_t *) srcp1)[0];
267 a0 = ((op_t *) srcp1)[1];
268 b0 = ((op_t *) srcp2)[0];
272 a2 = ((op_t *) srcp1)[0];
273 a3 = ((op_t *) srcp1)[1];
274 b3 = ((op_t *) srcp2)[0];
278 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
285 a0 = ((op_t *) srcp1)[0];
286 b0 = ((op_t *) srcp2)[0];
287 x = MERGE(a2, shl, a3, shr);
289 return CMP_LT_OR_GT (x, b3);
292 a1 = ((op_t *) srcp1)[1];
293 b1 = ((op_t *) srcp2)[1];
294 x = MERGE(a3, shl, a0, shr);
296 return CMP_LT_OR_GT (x, b0);
299 a2 = ((op_t *) srcp1)[2];
300 b2 = ((op_t *) srcp2)[2];
301 x = MERGE(a0, shl, a1, shr);
303 return CMP_LT_OR_GT (x, b1);
306 a3 = ((op_t *) srcp1)[3];
307 b3 = ((op_t *) srcp2)[3];
308 x = MERGE(a1, shl, a2, shr);
310 return CMP_LT_OR_GT (x, b2);
318 /* This is the right position for do0. Please don't move
321 x = MERGE(a2, shl, a3, shr);
323 return CMP_LT_OR_GT (x, b3);
328 rpl_memcmp (s1, s2, len)
335 intptr_t srcp1 = (intptr_t) s1;
336 intptr_t srcp2 = (intptr_t) s2;
339 if (len >= OP_T_THRES)
341 /* There are at least some bytes to compare. No need to test
342 for LEN == 0 in this alignment loop. */
343 while (srcp2 % OPSIZ != 0)
345 a0 = ((byte *) srcp1)[0];
346 b0 = ((byte *) srcp2)[0];
355 /* SRCP2 is now aligned for memory operations on `op_t'.
356 SRCP1 alignment determines if we can do a simple,
357 aligned compare or need to shuffle bits. */
359 if (srcp1 % OPSIZ == 0)
360 res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ);
362 res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ);
366 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
367 srcp1 += len & -OPSIZ;
368 srcp2 += len & -OPSIZ;
372 /* There are just a few bytes to compare. Use byte memory operations. */
375 a0 = ((byte *) srcp1)[0];
376 b0 = ((byte *) srcp2)[0];
390 weak_alias (memcmp, bcmp)