Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUNCTION |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 3 | <<memcmp>>---compare two memory areas |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 4 | |
| 5 | INDEX |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 6 | memcmp |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 7 | |
| 8 | ANSI_SYNOPSIS |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 9 | #include <string.h> |
| 10 | int memcmp(const void *<[s1]>, const void *<[s2]>, size_t <[n]>); |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 11 | |
| 12 | TRAD_SYNOPSIS |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | int memcmp(<[s1]>, <[s2]>, <[n]>) |
| 15 | void *<[s1]>; |
| 16 | void *<[s2]>; |
| 17 | size_t <[n]>; |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 18 | |
| 19 | DESCRIPTION |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 20 | This function compares not more than <[n]> characters of the |
| 21 | object pointed to by <[s1]> with the object pointed to by <[s2]>. |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | RETURNS |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 25 | The function returns an integer greater than, equal to or |
| 26 | less than zero according to whether the object pointed to by |
| 27 | <[s1]> is greater than, equal to or less than the object |
| 28 | pointed to by <[s2]>. |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 29 | |
| 30 | PORTABILITY |
| 31 | <<memcmp>> is ANSI C. |
| 32 | |
| 33 | <<memcmp>> requires no supporting OS subroutines. |
| 34 | |
| 35 | QUICKREF |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 36 | memcmp ansi pure |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | #include <string.h> |
Thomas Martitz | f32bd59 | 2010-06-22 18:34:03 +0000 | [diff] [blame] | 40 | #include "_ansi.h" /* for _DEFUN */ |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 41 | |
| 42 | /* Nonzero if either X or Y is not aligned on a "long" boundary. */ |
| 43 | #define UNALIGNED(X, Y) \ |
| 44 | (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1))) |
| 45 | |
| 46 | /* How many bytes are copied each iteration of the word copy loop. */ |
| 47 | #define LBLOCKSIZE (sizeof (long)) |
| 48 | |
| 49 | /* Threshhold for punting to the byte copier. */ |
| 50 | #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE) |
| 51 | |
| 52 | int |
| 53 | _DEFUN (memcmp, (m1, m2, n), |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 54 | _CONST _PTR m1 _AND |
| 55 | _CONST _PTR m2 _AND |
| 56 | size_t n) |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 57 | { |
| 58 | #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) |
| 59 | unsigned char *s1 = (unsigned char *) m1; |
| 60 | unsigned char *s2 = (unsigned char *) m2; |
| 61 | |
| 62 | while (n--) |
| 63 | { |
| 64 | if (*s1 != *s2) |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 65 | { |
| 66 | return *s1 - *s2; |
| 67 | } |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 68 | s1++; |
| 69 | s2++; |
| 70 | } |
| 71 | return 0; |
| 72 | #else |
| 73 | unsigned char *s1 = (unsigned char *) m1; |
| 74 | unsigned char *s2 = (unsigned char *) m2; |
| 75 | unsigned long *a1; |
| 76 | unsigned long *a2; |
| 77 | |
| 78 | /* If the size is too small, or either pointer is unaligned, |
| 79 | then we punt to the byte compare loop. Hopefully this will |
| 80 | not turn up in inner loops. */ |
| 81 | if (!TOO_SMALL(n) && !UNALIGNED(s1,s2)) |
| 82 | { |
| 83 | /* Otherwise, load and compare the blocks of memory one |
| 84 | word at a time. */ |
| 85 | a1 = (unsigned long*) s1; |
| 86 | a2 = (unsigned long*) s2; |
| 87 | while (n >= LBLOCKSIZE) |
| 88 | { |
| 89 | if (*a1 != *a2) |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 90 | break; |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 91 | a1++; |
| 92 | a2++; |
| 93 | n -= LBLOCKSIZE; |
| 94 | } |
| 95 | |
| 96 | /* check m mod LBLOCKSIZE remaining characters */ |
| 97 | |
Daniel Stenberg | 1a29cfb | 2005-11-25 00:10:12 +0000 | [diff] [blame] | 98 | s1 = (unsigned char*)a1; |
| 99 | s2 = (unsigned char*)a2; |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | while (n--) |
| 103 | { |
| 104 | if (*s1 != *s2) |
Andree Buschmann | 0b7dcd6 | 2010-02-22 21:24:09 +0000 | [diff] [blame] | 105 | return *s1 - *s2; |
Felix Arends | 306c316 | 2002-09-16 21:01:06 +0000 | [diff] [blame] | 106 | s1++; |
| 107 | s2++; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | #endif /* not PREFER_SIZE_OVER_SPEED */ |
| 112 | } |
| 113 | |