|
0
|
1 //=========================================================================== |
|
|
2 // |
|
|
3 // memcmp.cxx |
|
|
4 // |
|
|
5 // ANSI standard memcmp() routine |
|
|
6 // |
|
|
7 //=========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //=========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): jlarmour |
|
2
|
33 // Contributors: jlarmour |
|
0
|
34 // Date: 1998-02-13 |
|
|
35 // Purpose: |
|
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //=========================================================================== |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libc.h> // Configuration header |
|
|
46 |
|
|
47 // Include the C library? |
|
|
48 #ifdef CYGPKG_LIBC |
|
|
49 |
|
|
50 // INCLUDES |
|
|
51 |
|
|
52 #include <cyg/infra/cyg_type.h> // Common type definitions |
|
|
53 #include <cyg/infra/cyg_trac.h> // Tracing support |
|
|
54 #include <cyg/infra/cyg_ass.h> // Assertion support |
|
|
55 #include <string.h> // Header for this file |
|
|
56 #include <stddef.h> // Compiler definitions such as size_t, NULL etc. |
|
|
57 #include "clibincl/stringsupp.hxx" // Useful string function support and |
|
|
58 // prototypes |
|
|
59 |
|
|
60 |
|
|
61 // EXPORTED SYMBOLS |
|
|
62 |
|
|
63 externC int |
|
|
64 memcmp( const void *s1, const void *s2, size_t n ) \ |
|
|
65 CYGPRI_LIBC_WEAK_ALIAS("_memcmp"); |
|
|
66 |
|
|
67 // FUNCTIONS |
|
|
68 |
|
|
69 int |
|
|
70 _memcmp( const void *s1, const void *s2, size_t n ) |
|
|
71 { |
|
|
72 int retval; |
|
|
73 |
|
|
74 CYG_REPORT_FUNCNAMETYPE( "_memcmp", "returning %d" ); |
|
|
75 CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, n=%d", s1, s2, n ); |
|
|
76 |
|
|
77 CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" ); |
|
|
78 CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" ); |
|
|
79 |
|
|
80 #if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__) |
|
|
81 const unsigned char *m1 = (const unsigned char *) s1; |
|
|
82 const unsigned char *m2 = (const unsigned char *) s2; |
|
|
83 |
|
|
84 while (n--) |
|
|
85 { |
|
|
86 if (*m1 != *m2) |
|
|
87 { |
|
|
88 retval = *m1 - *m2; |
|
|
89 CYG_REPORT_RETVAL( retval ); |
|
|
90 return retval; |
|
|
91 } |
|
|
92 m1++; |
|
|
93 m2++; |
|
|
94 } |
|
|
95 CYG_REPORT_RETVAL( 0 ); |
|
|
96 return 0; |
|
|
97 #else |
|
|
98 const unsigned char *m1 = (const unsigned char *) s1; |
|
|
99 const unsigned char *m2 = (const unsigned char *) s2; |
|
|
100 const CYG_WORD *aligned_m1; |
|
|
101 const CYG_WORD *aligned_m2; |
|
|
102 |
|
|
103 // If the size is too small, or either pointer is unaligned, |
|
|
104 // then we punt to the byte compare loop. Hopefully this will |
|
|
105 // not turn up in inner loops. |
|
|
106 if (CYG_LIBC_STR_OPT_TOO_SMALL(n) || CYG_LIBC_STR_UNALIGNED2(m1,m2)) |
|
|
107 { |
|
|
108 while (n--) |
|
|
109 { |
|
|
110 if (*m1 != *m2) |
|
|
111 { |
|
|
112 retval = *m1 - *m2; |
|
|
113 CYG_REPORT_RETVAL( retval ); |
|
|
114 return retval; |
|
|
115 } |
|
|
116 m1++; |
|
|
117 m2++; |
|
|
118 } |
|
|
119 CYG_REPORT_RETVAL( 0 ); |
|
|
120 return 0; |
|
|
121 } |
|
|
122 |
|
|
123 // Otherwise, load and compare the blocks of memory one |
|
|
124 // word at a time. |
|
|
125 aligned_m1 = (const CYG_WORD *) m1; |
|
|
126 aligned_m2 = (const CYG_WORD *) m2; |
|
|
127 while (n > CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE) |
|
|
128 { |
|
|
129 if (*aligned_m1 != *aligned_m2) |
|
|
130 { |
|
|
131 // This block of characters has a mismatch, somewhere. |
|
|
132 // xoring them together and then testing for null would |
|
|
133 // be fastest, but this is clearer code. |
|
|
134 m1 = (const unsigned char *)aligned_m1; |
|
|
135 m2 = (const unsigned char *)aligned_m2; |
|
|
136 while (n--) |
|
|
137 { |
|
|
138 if (*m1 != *m2) |
|
|
139 { |
|
|
140 retval = *m1 - *m2; |
|
|
141 CYG_REPORT_RETVAL( retval ); |
|
|
142 return retval; |
|
|
143 } |
|
|
144 m1++; |
|
|
145 m2++; |
|
|
146 } |
|
|
147 // NOT REACHED |
|
|
148 } |
|
|
149 aligned_m1++; |
|
|
150 aligned_m2++; |
|
|
151 n -= CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; |
|
|
152 } |
|
|
153 |
|
|
154 // checking the last few characters |
|
|
155 |
|
|
156 m1 = (const unsigned char *)aligned_m1; |
|
|
157 m2 = (const unsigned char *)aligned_m2; |
|
|
158 |
|
|
159 while (n--) |
|
|
160 { |
|
|
161 if (*m1 != *m2) |
|
|
162 { |
|
|
163 retval = *m1 - *m2; |
|
|
164 CYG_REPORT_RETVAL( retval ); |
|
|
165 return retval; |
|
|
166 } |
|
|
167 m1++; |
|
|
168 m2++; |
|
|
169 } |
|
|
170 |
|
|
171 CYG_REPORT_RETVAL( 0 ); |
|
|
172 return 0; |
|
|
173 #endif // not defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || |
|
|
174 // defined(__OPTIMIZE_SIZE__) |
|
|
175 } // _memcmp() |
|
|
176 |
|
|
177 #endif // CYGPKG_LIBC |
|
|
178 |
|
|
179 // EOF memcmp.cxx |