1 /* Copyright (C) 1991, 1993, 1995, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Torbjorn Granlund (tege@sics.se).
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
25 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
26 #define __ptr_t void *
27 #else /* Not C++ or ANSI C. */
30 #define __ptr_t char *
31 #endif /* C++ or ANSI C. */
33 #if defined (HAVE_STRING_H) || defined (_LIBC)
43 #else /* Not in the GNU C library. */
45 #include <sys/types.h>
47 /* Type to use for aligned memory operations.
48 This should normally be the biggest type supported by a single load
49 and store. Must be an unsigned type. */
50 #define op_t unsigned long int
51 #define OPSIZ (sizeof(op_t))
53 /* Threshold value for when to enter the unrolled loops. */
56 /* Type to use for unaligned operations. */
57 typedef unsigned char byte
;
59 #ifndef WORDS_BIGENDIAN
60 #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
62 #define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
65 #endif /* In the GNU C library. */
67 #ifdef WORDS_BIGENDIAN
68 #define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
70 #define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
73 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
75 /* The strategy of this memcmp is:
77 1. Compare bytes until one of the block pointers is aligned.
79 2. Compare using memcmp_common_alignment or
80 memcmp_not_common_alignment, regarding the alignment of the other
81 block after the initial byte operations. The maximum number of
82 full words (of type op_t) are compared in this way.
84 3. Compare the few remaining bytes. */
86 #ifndef WORDS_BIGENDIAN
87 /* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
88 A and B are known to be different.
89 This is needed only on little-endian machines. */
91 static int memcmp_bytes
__P((op_t
, op_t
));
100 long int srcp1
= (long int) &a
;
101 long int srcp2
= (long int) &b
;
106 a0
= ((byte
*) srcp1
)[0];
107 b0
= ((byte
*) srcp2
)[0];
116 static int memcmp_common_alignment
__P((long, long, size_t));
118 /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
119 objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
120 memory operations on `op_t's. */
125 memcmp_common_alignment (srcp1
, srcp2
, len
)
135 default: /* Avoid warning about uninitialized local variables. */
137 a0
= ((op_t
*) srcp1
)[0];
138 b0
= ((op_t
*) srcp2
)[0];
144 a1
= ((op_t
*) srcp1
)[0];
145 b1
= ((op_t
*) srcp2
)[0];
151 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
153 a0
= ((op_t
*) srcp1
)[0];
154 b0
= ((op_t
*) srcp2
)[0];
157 a1
= ((op_t
*) srcp1
)[0];
158 b1
= ((op_t
*) srcp2
)[0];
162 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
169 a0
= ((op_t
*) srcp1
)[0];
170 b0
= ((op_t
*) srcp2
)[0];
172 return CMP_LT_OR_GT (a1
, b1
);
175 a1
= ((op_t
*) srcp1
)[1];
176 b1
= ((op_t
*) srcp2
)[1];
178 return CMP_LT_OR_GT (a0
, b0
);
181 a0
= ((op_t
*) srcp1
)[2];
182 b0
= ((op_t
*) srcp2
)[2];
184 return CMP_LT_OR_GT (a1
, b1
);
187 a1
= ((op_t
*) srcp1
)[3];
188 b1
= ((op_t
*) srcp2
)[3];
190 return CMP_LT_OR_GT (a0
, b0
);
198 /* This is the right position for do0. Please don't move
202 return CMP_LT_OR_GT (a1
, b1
);
206 static int memcmp_not_common_alignment
__P((long, long, size_t));
208 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
209 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
210 operations on `op_t', but SRCP1 *should be unaligned*. */
215 memcmp_not_common_alignment (srcp1
, srcp2
, len
)
225 /* Calculate how to shift a word read at the memory operation
226 aligned srcp1 to make it aligned for comparison. */
228 shl
= 8 * (srcp1
% OPSIZ
);
229 shr
= 8 * OPSIZ
- shl
;
231 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
232 it points in the middle of. */
237 default: /* Avoid warning about uninitialized local variables. */
239 a1
= ((op_t
*) srcp1
)[0];
240 a2
= ((op_t
*) srcp1
)[1];
241 b2
= ((op_t
*) srcp2
)[0];
247 a0
= ((op_t
*) srcp1
)[0];
248 a1
= ((op_t
*) srcp1
)[1];
249 b1
= ((op_t
*) srcp2
)[0];
254 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
256 a3
= ((op_t
*) srcp1
)[0];
257 a0
= ((op_t
*) srcp1
)[1];
258 b0
= ((op_t
*) srcp2
)[0];
262 a2
= ((op_t
*) srcp1
)[0];
263 a3
= ((op_t
*) srcp1
)[1];
264 b3
= ((op_t
*) srcp2
)[0];
268 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
275 a0
= ((op_t
*) srcp1
)[0];
276 b0
= ((op_t
*) srcp2
)[0];
277 x
= MERGE(a2
, shl
, a3
, shr
);
279 return CMP_LT_OR_GT (x
, b3
);
282 a1
= ((op_t
*) srcp1
)[1];
283 b1
= ((op_t
*) srcp2
)[1];
284 x
= MERGE(a3
, shl
, a0
, shr
);
286 return CMP_LT_OR_GT (x
, b0
);
289 a2
= ((op_t
*) srcp1
)[2];
290 b2
= ((op_t
*) srcp2
)[2];
291 x
= MERGE(a0
, shl
, a1
, shr
);
293 return CMP_LT_OR_GT (x
, b1
);
296 a3
= ((op_t
*) srcp1
)[3];
297 b3
= ((op_t
*) srcp2
)[3];
298 x
= MERGE(a1
, shl
, a2
, shr
);
300 return CMP_LT_OR_GT (x
, b2
);
308 /* This is the right position for do0. Please don't move
311 x
= MERGE(a2
, shl
, a3
, shr
);
313 return CMP_LT_OR_GT (x
, b3
);
325 long int srcp1
= (long int) s1
;
326 long int srcp2
= (long int) s2
;
329 if (len
>= OP_T_THRES
)
331 /* There are at least some bytes to compare. No need to test
332 for LEN == 0 in this alignment loop. */
333 while (srcp2
% OPSIZ
!= 0)
335 a0
= ((byte
*) srcp1
)[0];
336 b0
= ((byte
*) srcp2
)[0];
345 /* SRCP2 is now aligned for memory operations on `op_t'.
346 SRCP1 alignment determines if we can do a simple,
347 aligned compare or need to shuffle bits. */
349 if (srcp1
% OPSIZ
== 0)
350 res
= memcmp_common_alignment (srcp1
, srcp2
, len
/ OPSIZ
);
352 res
= memcmp_not_common_alignment (srcp1
, srcp2
, len
/ OPSIZ
);
356 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
357 srcp1
+= len
& -OPSIZ
;
358 srcp2
+= len
& -OPSIZ
;
362 /* There are just a few bytes to compare. Use byte memory operations. */
365 a0
= ((byte
*) srcp1
)[0];
366 b0
= ((byte
*) srcp2
)[0];
380 weak_alias (memcmp
, bcmp
)