xref: /optee_os/lib/libutils/isoc/newlib/strcmp.c (revision b1469ba0bfd0371eb52bd50f5c52eeda7a8f5f1e)
1 /*
2  * Copyright (c) 1994-2009  Red Hat, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /* This file is copied from newlib-1.19 */
33 
34 /*
35 FUNCTION
36 	<<strcmp>>---character string compare
37 
38 INDEX
39 	strcmp
40 
41 ANSI_SYNOPSIS
42 	#include <string.h>
43 	int strcmp(const char *<[a]>, const char *<[b]>);
44 
45 TRAD_SYNOPSIS
46 	#include <string.h>
47 	int strcmp(<[a]>, <[b]>)
48 	char *<[a]>;
49 	char *<[b]>;
50 
51 DESCRIPTION
52 	<<strcmp>> compares the string at <[a]> to
53 	the string at <[b]>.
54 
55 RETURNS
56 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
57 	<<strcmp>> returns a number greater than zero.  If the two
58 	strings match, <<strcmp>> returns zero.  If <<*<[a]>>>
59 	sorts lexicographically before <<*<[b]>>>, <<strcmp>> returns a
60 	number less than zero.
61 
62 PORTABILITY
63 <<strcmp>> is ANSI C.
64 
65 <<strcmp>> requires no supporting OS subroutines.
66 
67 QUICKREF
68 	strcmp ansi pure
69 */
70 
71 #include "_ansi.h"
72 #include <string.h>
73 #include <limits.h>
74 
75 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
76 #define UNALIGNED(X, Y) \
77 	(((long)X & (sizeof(long) - 1)) | ((long)Y & (sizeof(long) - 1)))
78 
79 /* DETECTNULL returns nonzero if (long)X contains a NULL byte. */
80 #if LONG_MAX == 2147483647L
81 #define DETECTNULL(X) (((X) - 0x01010101L) & ~(X) & 0x80808080UL)
82 #else
83 #if LONG_MAX == 9223372036854775807L
84 #define DETECTNULL(X) (((X) - 0x0101010101010101L) & ~(X) & \
85 		       0x8080808080808080UL)
86 #else
87 #error long int is not a 32bit or 64bit type.
88 #endif
89 #endif
90 
91 #ifndef DETECTNULL
92 #error long int is not a 32bit or 64bit byte
93 #endif
94 
95 int _DEFUN(strcmp, (s1, s2), _CONST char *s1 _AND _CONST char *s2)
96 {
97 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
98 	while (*s1 != '\0' && *s1 == *s2) {
99 		s1++;
100 		s2++;
101 	}
102 
103 	return (*(unsigned char *)s1) - (*(unsigned char *)s2);
104 #else
105 	unsigned long *a1;
106 	unsigned long *a2;
107 
108 	/* If s1 or s2 are unaligned, then compare bytes. */
109 	if (!UNALIGNED(s1, s2)) {
110 		/*
111 		 * If s1 and s2 are word-aligned, compare them a word at a time.
112 		 */
113 		a1 = (unsigned long *)s1;
114 		a2 = (unsigned long *)s2;
115 		while (*a1 == *a2) {
116 			/*
117 			 * To get here, *a1 == *a2, thus if we find a null in
118 			 * *a1, then the strings must be equal, so return zero.
119 			 */
120 			if (DETECTNULL(*a1))
121 				return 0;
122 
123 			a1++;
124 			a2++;
125 		}
126 
127 		/*
128 		 * A difference was detected in last few bytes of s1, so search
129 		 * bytewise.
130 		 */
131 		s1 = (char *)a1;
132 		s2 = (char *)a2;
133 	}
134 
135 	while (*s1 != '\0' && *s1 == *s2) {
136 		s1++;
137 		s2++;
138 	}
139 	return (*(unsigned char *)s1) - (*(unsigned char *)s2);
140 #endif /* not PREFER_SIZE_OVER_SPEED */
141 }
142