1 /* Copyright (C) 1991, 1993, 1995, 1997, 1998, 2003, 2006, 2009
2 Free Software Foundation, Inc.
4 Contributed by Torbjorn Granlund (tege@sics.se).
6 NOTE: The canonical source of this file is maintained with the GNU C Library.
7 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
9 This program is free software: you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
35 # if __BYTE_ORDER == __BIG_ENDIAN
36 # define WORDS_BIGENDIAN
39 #else /* Not in the GNU C library. */
41 # include <sys/types.h>
43 /* Type to use for aligned memory operations.
44 This should normally be the biggest type supported by a single load
45 and store. Must be an unsigned type. */
46 # define op_t unsigned long int
47 # define OPSIZ (sizeof(op_t))
49 /* Threshold value for when to enter the unrolled loops. */
50 # define OP_T_THRES 16
52 /* Type to use for unaligned operations. */
53 typedef unsigned char byte
;
55 # ifndef WORDS_BIGENDIAN
56 # define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
58 # define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
61 #endif /* In the GNU C library. */
63 #ifdef WORDS_BIGENDIAN
64 # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
66 # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
69 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
71 /* The strategy of this memcmp is:
73 1. Compare bytes until one of the block pointers is aligned.
75 2. Compare using memcmp_common_alignment or
76 memcmp_not_common_alignment, regarding the alignment of the other
77 block after the initial byte operations. The maximum number of
78 full words (of type op_t) are compared in this way.
80 3. Compare the few remaining bytes. */
82 #ifndef WORDS_BIGENDIAN
83 /* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
84 A and B are known to be different.
85 This is needed only on little-endian machines. */
91 memcmp_bytes (long unsigned int a
, long unsigned int b
)
93 long int srcp1
= (long int) &a
;
94 long int srcp2
= (long int) &b
;
99 a0
= ((byte
*) srcp1
)[0];
100 b0
= ((byte
*) srcp2
)[0];
109 /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
110 objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
111 memory operations on `op_t's. */
116 memcmp_common_alignment (long int srcp1
, long int srcp2
, size_t len
)
123 default: /* Avoid warning about uninitialized local variables. */
125 a0
= ((op_t
*) srcp1
)[0];
126 b0
= ((op_t
*) srcp2
)[0];
132 a1
= ((op_t
*) srcp1
)[0];
133 b1
= ((op_t
*) srcp2
)[0];
139 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
141 a0
= ((op_t
*) srcp1
)[0];
142 b0
= ((op_t
*) srcp2
)[0];
145 a1
= ((op_t
*) srcp1
)[0];
146 b1
= ((op_t
*) srcp2
)[0];
150 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
157 a0
= ((op_t
*) srcp1
)[0];
158 b0
= ((op_t
*) srcp2
)[0];
160 return CMP_LT_OR_GT (a1
, b1
);
163 a1
= ((op_t
*) srcp1
)[1];
164 b1
= ((op_t
*) srcp2
)[1];
166 return CMP_LT_OR_GT (a0
, b0
);
169 a0
= ((op_t
*) srcp1
)[2];
170 b0
= ((op_t
*) srcp2
)[2];
172 return CMP_LT_OR_GT (a1
, b1
);
175 a1
= ((op_t
*) srcp1
)[3];
176 b1
= ((op_t
*) srcp2
)[3];
178 return CMP_LT_OR_GT (a0
, b0
);
186 /* This is the right position for do0. Please don't move
190 return CMP_LT_OR_GT (a1
, b1
);
194 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
195 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
196 operations on `op_t', but SRCP1 *should be unaligned*. */
201 memcmp_not_common_alignment (long int srcp1
, long int srcp2
, size_t len
)
208 /* Calculate how to shift a word read at the memory operation
209 aligned srcp1 to make it aligned for comparison. */
211 shl
= 8 * (srcp1
% OPSIZ
);
212 shr
= 8 * OPSIZ
- shl
;
214 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
215 it points in the middle of. */
220 default: /* Avoid warning about uninitialized local variables. */
222 a1
= ((op_t
*) srcp1
)[0];
223 a2
= ((op_t
*) srcp1
)[1];
224 b2
= ((op_t
*) srcp2
)[0];
230 a0
= ((op_t
*) srcp1
)[0];
231 a1
= ((op_t
*) srcp1
)[1];
232 b1
= ((op_t
*) srcp2
)[0];
237 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
239 a3
= ((op_t
*) srcp1
)[0];
240 a0
= ((op_t
*) srcp1
)[1];
241 b0
= ((op_t
*) srcp2
)[0];
245 a2
= ((op_t
*) srcp1
)[0];
246 a3
= ((op_t
*) srcp1
)[1];
247 b3
= ((op_t
*) srcp2
)[0];
251 if (OP_T_THRES
<= 3 * OPSIZ
&& len
== 0)
258 a0
= ((op_t
*) srcp1
)[0];
259 b0
= ((op_t
*) srcp2
)[0];
260 x
= MERGE(a2
, shl
, a3
, shr
);
262 return CMP_LT_OR_GT (x
, b3
);
265 a1
= ((op_t
*) srcp1
)[1];
266 b1
= ((op_t
*) srcp2
)[1];
267 x
= MERGE(a3
, shl
, a0
, shr
);
269 return CMP_LT_OR_GT (x
, b0
);
272 a2
= ((op_t
*) srcp1
)[2];
273 b2
= ((op_t
*) srcp2
)[2];
274 x
= MERGE(a0
, shl
, a1
, shr
);
276 return CMP_LT_OR_GT (x
, b1
);
279 a3
= ((op_t
*) srcp1
)[3];
280 b3
= ((op_t
*) srcp2
)[3];
281 x
= MERGE(a1
, shl
, a2
, shr
);
283 return CMP_LT_OR_GT (x
, b2
);
291 /* This is the right position for do0. Please don't move
294 x
= MERGE(a2
, shl
, a3
, shr
);
296 return CMP_LT_OR_GT (x
, b3
);
301 rpl_memcmp (const void *s1
, const void *s2
, size_t len
)
305 long int srcp1
= (long int) s1
;
306 long int srcp2
= (long int) s2
;
309 if (len
>= OP_T_THRES
)
311 /* There are at least some bytes to compare. No need to test
312 for LEN == 0 in this alignment loop. */
313 while (srcp2
% OPSIZ
!= 0)
315 a0
= ((byte
*) srcp1
)[0];
316 b0
= ((byte
*) srcp2
)[0];
325 /* SRCP2 is now aligned for memory operations on `op_t'.
326 SRCP1 alignment determines if we can do a simple,
327 aligned compare or need to shuffle bits. */
329 if (srcp1
% OPSIZ
== 0)
330 res
= memcmp_common_alignment (srcp1
, srcp2
, len
/ OPSIZ
);
332 res
= memcmp_not_common_alignment (srcp1
, srcp2
, len
/ OPSIZ
);
336 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
337 srcp1
+= len
& -OPSIZ
;
338 srcp2
+= len
& -OPSIZ
;
342 /* There are just a few bytes to compare. Use byte memory operations. */
345 a0
= ((byte
*) srcp1
)[0];
346 b0
= ((byte
*) srcp2
)[0];
360 weak_alias (memcmp
, bcmp
)
This page took 0.037382 seconds and 4 git commands to generate.