Convert 703 function definitions to prototype style.
[glibc.git] / string / memcmp.c
blob429c9c838d1760dcd648a0a73ac05932ac643334
1 /* Copyright (C) 1991-2015 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #undef __ptr_t
24 #define __ptr_t void *
26 #if defined HAVE_STRING_H || defined _LIBC
27 # include <string.h>
28 #endif
30 #undef memcmp
32 #ifndef MEMCMP
33 # define MEMCMP memcmp
34 #endif
36 #ifdef _LIBC
38 # include <memcopy.h>
39 # include <endian.h>
41 # if __BYTE_ORDER == __BIG_ENDIAN
42 # define WORDS_BIGENDIAN
43 # endif
45 #else /* Not in the GNU C library. */
47 # include <sys/types.h>
49 /* Type to use for aligned memory operations.
50 This should normally be the biggest type supported by a single load
51 and store. Must be an unsigned type. */
52 # define op_t unsigned long int
53 # define OPSIZ (sizeof(op_t))
55 /* Threshold value for when to enter the unrolled loops. */
56 # define OP_T_THRES 16
58 /* Type to use for unaligned operations. */
59 typedef unsigned char byte;
61 # ifndef WORDS_BIGENDIAN
62 # define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
63 # else
64 # define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
65 # endif
67 #endif /* In the GNU C library. */
69 #ifdef WORDS_BIGENDIAN
70 # define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
71 #else
72 # define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
73 #endif
75 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE! */
77 /* The strategy of this memcmp is:
79 1. Compare bytes until one of the block pointers is aligned.
81 2. Compare using memcmp_common_alignment or
82 memcmp_not_common_alignment, regarding the alignment of the other
83 block after the initial byte operations. The maximum number of
84 full words (of type op_t) are compared in this way.
86 3. Compare the few remaining bytes. */
88 #ifndef WORDS_BIGENDIAN
89 /* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
90 A and B are known to be different.
91 This is needed only on little-endian machines. */
93 static int memcmp_bytes (op_t, op_t) __THROW;
95 static int
96 memcmp_bytes (a, b)
97 op_t a, b;
99 long int srcp1 = (long int) &a;
100 long int srcp2 = (long int) &b;
101 op_t a0, b0;
105 a0 = ((byte *) srcp1)[0];
106 b0 = ((byte *) srcp2)[0];
107 srcp1 += 1;
108 srcp2 += 1;
110 while (a0 == b0);
111 return a0 - b0;
113 #endif
115 static int memcmp_common_alignment (long, long, size_t) __THROW;
117 /* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
118 objects (not LEN bytes!). Both SRCP1 and SRCP2 should be aligned for
119 memory operations on `op_t's. */
120 static int
121 memcmp_common_alignment (long int srcp1, long int srcp2, size_t len)
123 op_t a0, a1;
124 op_t b0, b1;
126 switch (len % 4)
128 default: /* Avoid warning about uninitialized local variables. */
129 case 2:
130 a0 = ((op_t *) srcp1)[0];
131 b0 = ((op_t *) srcp2)[0];
132 srcp1 -= 2 * OPSIZ;
133 srcp2 -= 2 * OPSIZ;
134 len += 2;
135 goto do1;
136 case 3:
137 a1 = ((op_t *) srcp1)[0];
138 b1 = ((op_t *) srcp2)[0];
139 srcp1 -= OPSIZ;
140 srcp2 -= OPSIZ;
141 len += 1;
142 goto do2;
143 case 0:
144 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
145 return 0;
146 a0 = ((op_t *) srcp1)[0];
147 b0 = ((op_t *) srcp2)[0];
148 goto do3;
149 case 1:
150 a1 = ((op_t *) srcp1)[0];
151 b1 = ((op_t *) srcp2)[0];
152 srcp1 += OPSIZ;
153 srcp2 += OPSIZ;
154 len -= 1;
155 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
156 goto do0;
157 /* Fall through. */
162 a0 = ((op_t *) srcp1)[0];
163 b0 = ((op_t *) srcp2)[0];
164 if (a1 != b1)
165 return CMP_LT_OR_GT (a1, b1);
167 do3:
168 a1 = ((op_t *) srcp1)[1];
169 b1 = ((op_t *) srcp2)[1];
170 if (a0 != b0)
171 return CMP_LT_OR_GT (a0, b0);
173 do2:
174 a0 = ((op_t *) srcp1)[2];
175 b0 = ((op_t *) srcp2)[2];
176 if (a1 != b1)
177 return CMP_LT_OR_GT (a1, b1);
179 do1:
180 a1 = ((op_t *) srcp1)[3];
181 b1 = ((op_t *) srcp2)[3];
182 if (a0 != b0)
183 return CMP_LT_OR_GT (a0, b0);
185 srcp1 += 4 * OPSIZ;
186 srcp2 += 4 * OPSIZ;
187 len -= 4;
189 while (len != 0);
191 /* This is the right position for do0. Please don't move
192 it into the loop. */
193 do0:
194 if (a1 != b1)
195 return CMP_LT_OR_GT (a1, b1);
196 return 0;
199 static int memcmp_not_common_alignment (long, long, size_t) __THROW;
201 /* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
202 `op_t' objects (not LEN bytes!). SRCP2 should be aligned for memory
203 operations on `op_t', but SRCP1 *should be unaligned*. */
204 static int
205 memcmp_not_common_alignment (long int srcp1, long int srcp2, size_t len)
207 op_t a0, a1, a2, a3;
208 op_t b0, b1, b2, b3;
209 op_t x;
210 int shl, shr;
212 /* Calculate how to shift a word read at the memory operation
213 aligned srcp1 to make it aligned for comparison. */
215 shl = 8 * (srcp1 % OPSIZ);
216 shr = 8 * OPSIZ - shl;
218 /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
219 it points in the middle of. */
220 srcp1 &= -OPSIZ;
222 switch (len % 4)
224 default: /* Avoid warning about uninitialized local variables. */
225 case 2:
226 a1 = ((op_t *) srcp1)[0];
227 a2 = ((op_t *) srcp1)[1];
228 b2 = ((op_t *) srcp2)[0];
229 srcp1 -= 1 * OPSIZ;
230 srcp2 -= 2 * OPSIZ;
231 len += 2;
232 goto do1;
233 case 3:
234 a0 = ((op_t *) srcp1)[0];
235 a1 = ((op_t *) srcp1)[1];
236 b1 = ((op_t *) srcp2)[0];
237 srcp2 -= 1 * OPSIZ;
238 len += 1;
239 goto do2;
240 case 0:
241 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
242 return 0;
243 a3 = ((op_t *) srcp1)[0];
244 a0 = ((op_t *) srcp1)[1];
245 b0 = ((op_t *) srcp2)[0];
246 srcp1 += 1 * OPSIZ;
247 goto do3;
248 case 1:
249 a2 = ((op_t *) srcp1)[0];
250 a3 = ((op_t *) srcp1)[1];
251 b3 = ((op_t *) srcp2)[0];
252 srcp1 += 2 * OPSIZ;
253 srcp2 += 1 * OPSIZ;
254 len -= 1;
255 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
256 goto do0;
257 /* Fall through. */
262 a0 = ((op_t *) srcp1)[0];
263 b0 = ((op_t *) srcp2)[0];
264 x = MERGE(a2, shl, a3, shr);
265 if (x != b3)
266 return CMP_LT_OR_GT (x, b3);
268 do3:
269 a1 = ((op_t *) srcp1)[1];
270 b1 = ((op_t *) srcp2)[1];
271 x = MERGE(a3, shl, a0, shr);
272 if (x != b0)
273 return CMP_LT_OR_GT (x, b0);
275 do2:
276 a2 = ((op_t *) srcp1)[2];
277 b2 = ((op_t *) srcp2)[2];
278 x = MERGE(a0, shl, a1, shr);
279 if (x != b1)
280 return CMP_LT_OR_GT (x, b1);
282 do1:
283 a3 = ((op_t *) srcp1)[3];
284 b3 = ((op_t *) srcp2)[3];
285 x = MERGE(a1, shl, a2, shr);
286 if (x != b2)
287 return CMP_LT_OR_GT (x, b2);
289 srcp1 += 4 * OPSIZ;
290 srcp2 += 4 * OPSIZ;
291 len -= 4;
293 while (len != 0);
295 /* This is the right position for do0. Please don't move
296 it into the loop. */
297 do0:
298 x = MERGE(a2, shl, a3, shr);
299 if (x != b3)
300 return CMP_LT_OR_GT (x, b3);
301 return 0;
305 MEMCMP (const __ptr_t s1, const __ptr_t s2, size_t len)
307 op_t a0;
308 op_t b0;
309 long int srcp1 = (long int) s1;
310 long int srcp2 = (long int) s2;
311 op_t res;
313 if (len >= OP_T_THRES)
315 /* There are at least some bytes to compare. No need to test
316 for LEN == 0 in this alignment loop. */
317 while (srcp2 % OPSIZ != 0)
319 a0 = ((byte *) srcp1)[0];
320 b0 = ((byte *) srcp2)[0];
321 srcp1 += 1;
322 srcp2 += 1;
323 res = a0 - b0;
324 if (res != 0)
325 return res;
326 len -= 1;
329 /* SRCP2 is now aligned for memory operations on `op_t'.
330 SRCP1 alignment determines if we can do a simple,
331 aligned compare or need to shuffle bits. */
333 if (srcp1 % OPSIZ == 0)
334 res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ);
335 else
336 res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ);
337 if (res != 0)
338 return res;
340 /* Number of bytes remaining in the interval [0..OPSIZ-1]. */
341 srcp1 += len & -OPSIZ;
342 srcp2 += len & -OPSIZ;
343 len %= OPSIZ;
346 /* There are just a few bytes to compare. Use byte memory operations. */
347 while (len != 0)
349 a0 = ((byte *) srcp1)[0];
350 b0 = ((byte *) srcp2)[0];
351 srcp1 += 1;
352 srcp2 += 1;
353 res = a0 - b0;
354 if (res != 0)
355 return res;
356 len -= 1;
359 return 0;
361 libc_hidden_builtin_def(memcmp)
362 #ifdef weak_alias
363 # undef bcmp
364 weak_alias (memcmp, bcmp)
365 #endif