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
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
54 # if __BYTE_ORDER == __BIG_ENDIAN
55 # define WORDS_BIGENDIAN
58 #else /* Not in the GNU C library. */
60 # include <sys/types.h>
62 /* Type to use for aligned memory operations.
63 This should normally be the biggest type supported by a single load
64 and store. Must be an unsigned type. */
65 # define op_t unsigned long int
66 # define OPSIZ (sizeof(op_t))
68 /* Threshold value for when to enter the unrolled loops. */
69 # define OP_T_THRES 16
71 /* Type to use for unaligned operations. */
72 typedef unsigned char byte;
74 # ifndef WORDS_BIGENDIAN
75 # define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
77 # define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
80 #endif /* In the GNU C library. */
82 #ifdef WORDS_BIGENDIAN
83 # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
85 # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
88 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
90 /* The strategy of this memcmp is:
92 1. Compare bytes until one of the block pointers is aligned.
94 2. Compare using memcmp_common_alignment or
95 memcmp_not_common_alignment, regarding the alignment of the other
96 block after the initial byte operations. The maximum number of
97 full words (of type op_t) are compared in this way.
99 3. Compare the few remaining bytes. */
101 #ifndef WORDS_BIGENDIAN
102 /* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
103 A and B are known to be different.
104 This is needed only on little-endian machines. */
106 static int memcmp_bytes __P((op_t, op_t));
112 memcmp_bytes (long unsigned int a, long unsigned int b)
114 long int srcp1 = (long int) &a;
115 long int srcp2 = (long int) &b;
120 a0 = ((byte *) srcp1)[0];
121 b0 = ((byte *) srcp2)[0];
130 static int memcmp_common_alignment __P((long, long, size_t));
132 /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
133 objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
134 memory operations on `op_t's. */
139 memcmp_common_alignment (long int srcp1, long int srcp2, size_t len)
146 default: /* Avoid warning about uninitialized local variables. */
148 a0 = ((op_t *) srcp1)[0];
149 b0 = ((op_t *) srcp2)[0];
155 a1 = ((op_t *) srcp1)[0];
156 b1 = ((op_t *) srcp2)[0];
162 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
164 a0 = ((op_t *) srcp1)[0];
165 b0 = ((op_t *) srcp2)[0];
168 a1 = ((op_t *) srcp1)[0];
169 b1 = ((op_t *) srcp2)[0];
173 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
180 a0 = ((op_t *) srcp1)[0];
181 b0 = ((op_t *) srcp2)[0];
183 return CMP_LT_OR_GT (a1, b1);
186 a1 = ((op_t *) srcp1)[1];
187 b1 = ((op_t *) srcp2)[1];
189 return CMP_LT_OR_GT (a0, b0);
192 a0 = ((op_t *) srcp1)[2];
193 b0 = ((op_t *) srcp2)[2];
195 return CMP_LT_OR_GT (a1, b1);
198 a1 = ((op_t *) srcp1)[3];
199 b1 = ((op_t *) srcp2)[3];
201 return CMP_LT_OR_GT (a0, b0);
209 /* This is the right position for do0. Please don't move
213 return CMP_LT_OR_GT (a1, b1);
217 static int memcmp_not_common_alignment __P((long, long, size_t));
219 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
220 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
221 operations on `op_t', but SRCP1 *should be unaligned*. */
226 memcmp_not_common_alignment (long int srcp1, long int srcp2, size_t len)
233 /* Calculate how to shift a word read at the memory operation
234 aligned srcp1 to make it aligned for comparison. */
236 shl = 8 * (srcp1 % OPSIZ);
237 shr = 8 * OPSIZ - shl;
239 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
240 it points in the middle of. */
245 default: /* Avoid warning about uninitialized local variables. */
247 a1 = ((op_t *) srcp1)[0];
248 a2 = ((op_t *) srcp1)[1];
249 b2 = ((op_t *) srcp2)[0];
255 a0 = ((op_t *) srcp1)[0];
256 a1 = ((op_t *) srcp1)[1];
257 b1 = ((op_t *) srcp2)[0];
262 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
264 a3 = ((op_t *) srcp1)[0];
265 a0 = ((op_t *) srcp1)[1];
266 b0 = ((op_t *) srcp2)[0];
270 a2 = ((op_t *) srcp1)[0];
271 a3 = ((op_t *) srcp1)[1];
272 b3 = ((op_t *) srcp2)[0];
276 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
283 a0 = ((op_t *) srcp1)[0];
284 b0 = ((op_t *) srcp2)[0];
285 x = MERGE(a2, shl, a3, shr);
287 return CMP_LT_OR_GT (x, b3);
290 a1 = ((op_t *) srcp1)[1];
291 b1 = ((op_t *) srcp2)[1];
292 x = MERGE(a3, shl, a0, shr);
294 return CMP_LT_OR_GT (x, b0);
297 a2 = ((op_t *) srcp1)[2];
298 b2 = ((op_t *) srcp2)[2];
299 x = MERGE(a0, shl, a1, shr);
301 return CMP_LT_OR_GT (x, b1);
304 a3 = ((op_t *) srcp1)[3];
305 b3 = ((op_t *) srcp2)[3];
306 x = MERGE(a1, shl, a2, shr);
308 return CMP_LT_OR_GT (x, b2);
316 /* This is the right position for do0. Please don't move
319 x = MERGE(a2, shl, a3, shr);
321 return CMP_LT_OR_GT (x, b3);
326 rpl_memcmp (const void *s1, const void *s2, size_t len)
330 long int srcp1 = (long int) s1;
331 long int srcp2 = (long int) s2;
334 if (len >= OP_T_THRES)
336 /* There are at least some bytes to compare. No need to test
337 for LEN == 0 in this alignment loop. */
338 while (srcp2 % OPSIZ != 0)
340 a0 = ((byte *) srcp1)[0];
341 b0 = ((byte *) srcp2)[0];
350 /* SRCP2 is now aligned for memory operations on `op_t'.
351 SRCP1 alignment determines if we can do a simple,
352 aligned compare or need to shuffle bits. */
354 if (srcp1 % OPSIZ == 0)
355 res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ);
357 res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ);
361 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
362 srcp1 += len & -OPSIZ;
363 srcp2 += len & -OPSIZ;
367 /* There are just a few bytes to compare. Use byte memory operations. */
370 a0 = ((byte *) srcp1)[0];
371 b0 = ((byte *) srcp2)[0];
385 weak_alias (memcmp, bcmp)