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.
26 #ifdef HAVE_INTTYPES_H
31 #if defined __cplusplus || (defined __STDC__ && __STDC__)
32 # define __ptr_t void *
33 #else /* Not C++ or ANSI C. */
36 # define __ptr_t char *
37 #endif /* C++ or ANSI C. */
40 # if defined __GNUC__ || (defined __STDC__ && __STDC__)
41 # define __P(args) args
47 #if defined HAVE_STRING_H || defined _LIBC
57 #else /* Not in the GNU C library. */
59 # include <sys/types.h>
61 /* Type to use for aligned memory operations.
62 This should normally be the biggest type supported by a single load
63 and store. Must be an unsigned type. */
64 # define op_t unsigned long int
65 # define OPSIZ (sizeof(op_t))
67 /* Threshold value for when to enter the unrolled loops. */
68 # define OP_T_THRES 16
70 /* Type to use for unaligned operations. */
71 typedef unsigned char byte;
73 # ifndef WORDS_BIGENDIAN
74 # define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
76 # define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
79 #endif /* In the GNU C library. */
81 #ifdef WORDS_BIGENDIAN
82 # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
84 # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
87 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
89 /* The strategy of this memcmp is:
91 1. Compare bytes until one of the block pointers is aligned.
93 2. Compare using memcmp_common_alignment or
94 memcmp_not_common_alignment, regarding the alignment of the other
95 block after the initial byte operations. The maximum number of
96 full words (of type op_t) are compared in this way.
98 3. Compare the few remaining bytes. */
100 #ifndef WORDS_BIGENDIAN
101 /* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
102 A and B are known to be different.
103 This is needed only on little-endian machines. */
105 static int memcmp_bytes __P((op_t, op_t));
114 intptr_t srcp1 = (intptr_t) &a;
115 intptr_t srcp2 = (intptr_t) &b;
120 a0 = ((byte *) srcp1)[0];
121 b0 = ((byte *) srcp2)[0];
130 static int memcmp_common_alignment __P((intptr_t, intptr_t, 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 (srcp1, srcp2, len)
149 default: /* Avoid warning about uninitialized local variables. */
151 a0 = ((op_t *) srcp1)[0];
152 b0 = ((op_t *) srcp2)[0];
158 a1 = ((op_t *) srcp1)[0];
159 b1 = ((op_t *) srcp2)[0];
165 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
167 a0 = ((op_t *) srcp1)[0];
168 b0 = ((op_t *) srcp2)[0];
171 a1 = ((op_t *) srcp1)[0];
172 b1 = ((op_t *) srcp2)[0];
176 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
183 a0 = ((op_t *) srcp1)[0];
184 b0 = ((op_t *) srcp2)[0];
186 return CMP_LT_OR_GT (a1, b1);
189 a1 = ((op_t *) srcp1)[1];
190 b1 = ((op_t *) srcp2)[1];
192 return CMP_LT_OR_GT (a0, b0);
195 a0 = ((op_t *) srcp1)[2];
196 b0 = ((op_t *) srcp2)[2];
198 return CMP_LT_OR_GT (a1, b1);
201 a1 = ((op_t *) srcp1)[3];
202 b1 = ((op_t *) srcp2)[3];
204 return CMP_LT_OR_GT (a0, b0);
212 /* This is the right position for do0. Please don't move
216 return CMP_LT_OR_GT (a1, b1);
220 static int memcmp_not_common_alignment __P((intptr_t, intptr_t, size_t));
222 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
223 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
224 operations on `op_t', but SRCP1 *should be unaligned*. */
229 memcmp_not_common_alignment (srcp1, srcp2, len)
239 /* Calculate how to shift a word read at the memory operation
240 aligned srcp1 to make it aligned for comparison. */
242 shl = 8 * (srcp1 % OPSIZ);
243 shr = 8 * OPSIZ - shl;
245 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
246 it points in the middle of. */
251 default: /* Avoid warning about uninitialized local variables. */
253 a1 = ((op_t *) srcp1)[0];
254 a2 = ((op_t *) srcp1)[1];
255 b2 = ((op_t *) srcp2)[0];
261 a0 = ((op_t *) srcp1)[0];
262 a1 = ((op_t *) srcp1)[1];
263 b1 = ((op_t *) srcp2)[0];
268 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
270 a3 = ((op_t *) srcp1)[0];
271 a0 = ((op_t *) srcp1)[1];
272 b0 = ((op_t *) srcp2)[0];
276 a2 = ((op_t *) srcp1)[0];
277 a3 = ((op_t *) srcp1)[1];
278 b3 = ((op_t *) srcp2)[0];
282 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
289 a0 = ((op_t *) srcp1)[0];
290 b0 = ((op_t *) srcp2)[0];
291 x = MERGE(a2, shl, a3, shr);
293 return CMP_LT_OR_GT (x, b3);
296 a1 = ((op_t *) srcp1)[1];
297 b1 = ((op_t *) srcp2)[1];
298 x = MERGE(a3, shl, a0, shr);
300 return CMP_LT_OR_GT (x, b0);
303 a2 = ((op_t *) srcp1)[2];
304 b2 = ((op_t *) srcp2)[2];
305 x = MERGE(a0, shl, a1, shr);
307 return CMP_LT_OR_GT (x, b1);
310 a3 = ((op_t *) srcp1)[3];
311 b3 = ((op_t *) srcp2)[3];
312 x = MERGE(a1, shl, a2, shr);
314 return CMP_LT_OR_GT (x, b2);
322 /* This is the right position for do0. Please don't move
325 x = MERGE(a2, shl, a3, shr);
327 return CMP_LT_OR_GT (x, b3);
332 rpl_memcmp (s1, s2, len)
339 intptr_t srcp1 = (intptr_t) s1;
340 intptr_t srcp2 = (intptr_t) s2;
343 if (len >= OP_T_THRES)
345 /* There are at least some bytes to compare. No need to test
346 for LEN == 0 in this alignment loop. */
347 while (srcp2 % OPSIZ != 0)
349 a0 = ((byte *) srcp1)[0];
350 b0 = ((byte *) srcp2)[0];
359 /* SRCP2 is now aligned for memory operations on `op_t'.
360 SRCP1 alignment determines if we can do a simple,
361 aligned compare or need to shuffle bits. */
363 if (srcp1 % OPSIZ == 0)
364 res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ);
366 res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ);
370 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
371 srcp1 += len & -OPSIZ;
372 srcp2 += len & -OPSIZ;
376 /* There are just a few bytes to compare. Use byte memory operations. */
379 a0 = ((byte *) srcp1)[0];
380 b0 = ((byte *) srcp2)[0];
394 weak_alias (memcmp, bcmp)