1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc.  Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /*
18 FUNCTION
19 	<<memcmp>>---compare two memory areas
20 
21 INDEX
22 	memcmp
23 
24 SYNOPSIS
25 	#include <string.h>
26 	int memcmp(const void *<[s1]>, const void *<[s2]>, size_t <[n]>);
27 
28 DESCRIPTION
29 	This function compares not more than <[n]> characters of the
30 	object pointed to by <[s1]> with the object pointed to by <[s2]>.
31 
32 
33 RETURNS
34 	The function returns an integer greater than, equal to or
35 	less than zero 	according to whether the object pointed to by
36 	<[s1]> is greater than, equal to or less than the object
37 	pointed to by <[s2]>.
38 
39 PORTABILITY
40 <<memcmp>> is ANSI C.
41 
42 <<memcmp>> requires no supporting OS subroutines.
43 
44 QUICKREF
45 	memcmp ansi pure
46 */
47 
48 #include <string.h>
49 #include <stdint.h>
50 
51 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
52 #define UNALIGNED(X, Y) \
53   (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
54 
55 /* How many bytes are copied each iteration of the word copy loop.  */
56 #define LBLOCKSIZE (sizeof (long))
57 
58 /* Threshhold for punting to the byte copier.  */
59 #define TOO_SMALL(LEN)  ((LEN) < LBLOCKSIZE)
60 
61 int
memcmp(const void * m1,const void * m2,size_t n)62 memcmp (const void *m1,
63 	const void *m2,
64 	size_t n)
65 {
66 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
67   unsigned char *s1 = (unsigned char *) m1;
68   unsigned char *s2 = (unsigned char *) m2;
69 
70   while (n--)
71     {
72       if (*s1 != *s2)
73 	{
74 	  return *s1 - *s2;
75 	}
76       s1++;
77       s2++;
78     }
79   return 0;
80 #else
81   unsigned char *s1 = (unsigned char *) m1;
82   unsigned char *s2 = (unsigned char *) m2;
83   unsigned long *a1;
84   unsigned long *a2;
85 
86   /* If the size is too small, or either pointer is unaligned,
87      then we punt to the byte compare loop.  Hopefully this will
88      not turn up in inner loops.  */
89   if (!TOO_SMALL(n) && !UNALIGNED(s1,s2))
90     {
91       /* Otherwise, load and compare the blocks of memory one
92          word at a time.  */
93       a1 = (unsigned long*) s1;
94       a2 = (unsigned long*) s2;
95       while (n >= LBLOCKSIZE)
96         {
97           if (*a1 != *a2)
98    	    break;
99           a1++;
100           a2++;
101           n -= LBLOCKSIZE;
102         }
103 
104       /* check m mod LBLOCKSIZE remaining characters */
105 
106       s1 = (unsigned char*)a1;
107       s2 = (unsigned char*)a2;
108     }
109 
110   while (n--)
111     {
112       if (*s1 != *s2)
113 	return *s1 - *s2;
114       s1++;
115       s2++;
116     }
117 
118   return 0;
119 #endif /* not PREFER_SIZE_OVER_SPEED */
120 }
121 
122