Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / string / memcmp.c
blob342fb9fbc890bea04e10020d9c7f99b95772b203
1 /*
2 FUNCTION
3 <<memcmp>>---compare two memory areas
5 INDEX
6 memcmp
8 SYNOPSIS
9 #include <string.h>
10 int memcmp(const void *<[s1]>, const void *<[s2]>, size_t <[n]>);
12 DESCRIPTION
13 This function compares not more than <[n]> characters of the
14 object pointed to by <[s1]> with the object pointed to by <[s2]>.
17 RETURNS
18 The function returns an integer greater than, equal to or
19 less than zero according to whether the object pointed to by
20 <[s1]> is greater than, equal to or less than the object
21 pointed to by <[s2]>.
23 PORTABILITY
24 <<memcmp>> is ANSI C.
26 <<memcmp>> requires no supporting OS subroutines.
28 QUICKREF
29 memcmp ansi pure
32 #include <string.h>
35 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
36 #define UNALIGNED(X, Y) \
37 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
39 /* How many bytes are copied each iteration of the word copy loop. */
40 #define LBLOCKSIZE (sizeof (long))
42 /* Threshhold for punting to the byte copier. */
43 #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
45 int
46 memcmp (const void *m1,
47 const void *m2,
48 size_t n)
50 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
51 unsigned char *s1 = (unsigned char *) m1;
52 unsigned char *s2 = (unsigned char *) m2;
54 while (n--)
56 if (*s1 != *s2)
58 return *s1 - *s2;
60 s1++;
61 s2++;
63 return 0;
64 #else
65 unsigned char *s1 = (unsigned char *) m1;
66 unsigned char *s2 = (unsigned char *) m2;
67 unsigned long *a1;
68 unsigned long *a2;
70 /* If the size is too small, or either pointer is unaligned,
71 then we punt to the byte compare loop. Hopefully this will
72 not turn up in inner loops. */
73 if (!TOO_SMALL(n) && !UNALIGNED(s1,s2))
75 /* Otherwise, load and compare the blocks of memory one
76 word at a time. */
77 a1 = (unsigned long*) s1;
78 a2 = (unsigned long*) s2;
79 while (n >= LBLOCKSIZE)
81 if (*a1 != *a2)
82 break;
83 a1++;
84 a2++;
85 n -= LBLOCKSIZE;
88 /* check m mod LBLOCKSIZE remaining characters */
90 s1 = (unsigned char*)a1;
91 s2 = (unsigned char*)a2;
94 while (n--)
96 if (*s1 != *s2)
97 return *s1 - *s2;
98 s1++;
99 s2++;
102 return 0;
103 #endif /* not PREFER_SIZE_OVER_SPEED */