1 | /* |
---|
2 | FUNCTION |
---|
3 | <<memcmp>>---compare two memory areas |
---|
4 | |
---|
5 | INDEX |
---|
6 | memcmp |
---|
7 | |
---|
8 | SYNOPSIS |
---|
9 | #include <string.h> |
---|
10 | int memcmp(const void *<[s1]>, const void *<[s2]>, size_t <[n]>); |
---|
11 | |
---|
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]>. |
---|
15 | |
---|
16 | |
---|
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]>. |
---|
22 | |
---|
23 | PORTABILITY |
---|
24 | <<memcmp>> is ANSI C. |
---|
25 | |
---|
26 | <<memcmp>> requires no supporting OS subroutines. |
---|
27 | |
---|
28 | QUICKREF |
---|
29 | memcmp ansi pure |
---|
30 | */ |
---|
31 | |
---|
32 | #include <string.h> |
---|
33 | |
---|
34 | |
---|
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))) |
---|
38 | |
---|
39 | /* How many bytes are copied each iteration of the word copy loop. */ |
---|
40 | #define LBLOCKSIZE (sizeof (long)) |
---|
41 | |
---|
42 | /* Threshhold for punting to the byte copier. */ |
---|
43 | #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE) |
---|
44 | |
---|
45 | int |
---|
46 | memcmp (const void *m1, |
---|
47 | const void *m2, |
---|
48 | size_t n) |
---|
49 | { |
---|
50 | #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) |
---|
51 | unsigned char *s1 = (unsigned char *) m1; |
---|
52 | unsigned char *s2 = (unsigned char *) m2; |
---|
53 | |
---|
54 | while (n--) |
---|
55 | { |
---|
56 | if (*s1 != *s2) |
---|
57 | { |
---|
58 | return *s1 - *s2; |
---|
59 | } |
---|
60 | s1++; |
---|
61 | s2++; |
---|
62 | } |
---|
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; |
---|
69 | |
---|
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)) |
---|
74 | { |
---|
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) |
---|
80 | { |
---|
81 | if (*a1 != *a2) |
---|
82 | break; |
---|
83 | a1++; |
---|
84 | a2++; |
---|
85 | n -= LBLOCKSIZE; |
---|
86 | } |
---|
87 | |
---|
88 | /* check m mod LBLOCKSIZE remaining characters */ |
---|
89 | |
---|
90 | s1 = (unsigned char*)a1; |
---|
91 | s2 = (unsigned char*)a2; |
---|
92 | } |
---|
93 | |
---|
94 | while (n--) |
---|
95 | { |
---|
96 | if (*s1 != *s2) |
---|
97 | return *s1 - *s2; |
---|
98 | s1++; |
---|
99 | s2++; |
---|
100 | } |
---|
101 | |
---|
102 | return 0; |
---|
103 | #endif /* not PREFER_SIZE_OVER_SPEED */ |
---|
104 | } |
---|
105 | |
---|