xref: /onnv-gate/usr/src/lib/libc/i386/gen/strcmp.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Fast strcmp.  This works one int at a time, using aligned pointers
31*0Sstevel@tonic-gate  * if possible, misaligned pointers if necessary.  To avoid taking
32*0Sstevel@tonic-gate  * faults from going off the end of a page, the code is careful to go
33*0Sstevel@tonic-gate  * a byte-at-a-time when a misaligned pointer is near a page boundary.
34*0Sstevel@tonic-gate  * The code is almost portable, but see the assumptions below.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * ASSUMPTIONS:
39*0Sstevel@tonic-gate  * sizeof (int) is not greater than 8.
40*0Sstevel@tonic-gate  * sizeof (int) is a power of 2.
41*0Sstevel@tonic-gate  * An int pointer can always be dereferenced even if it is not properly
42*0Sstevel@tonic-gate  *   aligned (though aligned references are assumed to be faster).
43*0Sstevel@tonic-gate  * It is OK to assign bogus values to a pointer (in particular, a
44*0Sstevel@tonic-gate  *   value that is before the beginning of the string) as long as that
45*0Sstevel@tonic-gate  *   pointer is only used with indices big enough to bring us back into
46*0Sstevel@tonic-gate  *   the string.
47*0Sstevel@tonic-gate  * It is OK to reference bytes past the end of a string as long as we
48*0Sstevel@tonic-gate  *   don't cross a page boundary.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include "lint.h"
52*0Sstevel@tonic-gate #include <limits.h>
53*0Sstevel@tonic-gate #include <unistd.h>
54*0Sstevel@tonic-gate #include <sys/sysconfig.h>
55*0Sstevel@tonic-gate #include "libc.h"
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate  * This strange expression will test to see if *any* byte in the int is
59*0Sstevel@tonic-gate  * a NUL.  The constants are big enough to allow for ints up to 8 bytes.
60*0Sstevel@tonic-gate  * The two arguments are actually two copies of the same value; this
61*0Sstevel@tonic-gate  * allows the compiler freedom to play with both values for efficiency.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate #define	ANYNUL(i1, i2)	(((i1) - (int)0x0101010101010101LL) & ~(i2) & \
64*0Sstevel@tonic-gate 		(int)0x8080808080808080ULL)
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate int
strcmp(const char * str1,const char * str2)67*0Sstevel@tonic-gate strcmp(const char *str1, const char *str2)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate 	int *s1, *s2;
70*0Sstevel@tonic-gate 	int i1, i2;
71*0Sstevel@tonic-gate 	int count;
72*0Sstevel@tonic-gate 	int b1, b2;
73*0Sstevel@tonic-gate 	static int pagesize;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if (str1 == str2)
76*0Sstevel@tonic-gate 		return (0);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	/*
79*0Sstevel@tonic-gate 	 * Go 1 byte at a time until at least one pointer is word aligned.
80*0Sstevel@tonic-gate 	 * Assumes that sizeof (int) is a power of 2.
81*0Sstevel@tonic-gate 	 */
82*0Sstevel@tonic-gate 	while ((((int) str1) & (sizeof (int) - 1)) &&
83*0Sstevel@tonic-gate 	    (((int) str2) & (sizeof (int) - 1))) {
84*0Sstevel@tonic-gate one_byte:
85*0Sstevel@tonic-gate 		if (*str1 != *str2)
86*0Sstevel@tonic-gate 			return ((unsigned char)*str1 - (unsigned char)*str2);
87*0Sstevel@tonic-gate 		if (*str1 == '\0')
88*0Sstevel@tonic-gate 			return (0);
89*0Sstevel@tonic-gate 		++str1;
90*0Sstevel@tonic-gate 		++str2;
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/*
94*0Sstevel@tonic-gate 	 * If one pointer is misaligned, we must be careful not to
95*0Sstevel@tonic-gate 	 * dereference it when it points across a page boundary.
96*0Sstevel@tonic-gate 	 * If we did, we might go past the end of the segment and
97*0Sstevel@tonic-gate 	 * get a SIGSEGV.  Set "count" to the number of ints we can
98*0Sstevel@tonic-gate 	 * scan before running into such a boundary.
99*0Sstevel@tonic-gate 	 */
100*0Sstevel@tonic-gate 	count = INT_MAX;
101*0Sstevel@tonic-gate 	if (((int) str1) & (sizeof (int) - 1)) {
102*0Sstevel@tonic-gate 		if (pagesize == 0)
103*0Sstevel@tonic-gate 			pagesize = _sysconfig(_CONFIG_PAGESIZE);
104*0Sstevel@tonic-gate 		count = (pagesize - ((int)str1 & (pagesize - 1))) /
105*0Sstevel@tonic-gate 			sizeof (int);
106*0Sstevel@tonic-gate 	} else if (((int) str2) & (sizeof (int) - 1)) {
107*0Sstevel@tonic-gate 		if (pagesize == 0)
108*0Sstevel@tonic-gate 			pagesize = _sysconfig(_CONFIG_PAGESIZE);
109*0Sstevel@tonic-gate 		count = (pagesize - ((int)str2 & (pagesize - 1))) /
110*0Sstevel@tonic-gate 			sizeof (int);
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	s1 = (void *) str1;
114*0Sstevel@tonic-gate 	s2 = (void *) str2;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	/*
117*0Sstevel@tonic-gate 	 * Go "sizeof (int)" bytes at a time until at least one pointer
118*0Sstevel@tonic-gate 	 * is word aligned.
119*0Sstevel@tonic-gate 	 *
120*0Sstevel@tonic-gate 	 * Unwrap the loop for even a bit more speed.
121*0Sstevel@tonic-gate 	 */
122*0Sstevel@tonic-gate 	for (;;) {
123*0Sstevel@tonic-gate 		/*
124*0Sstevel@tonic-gate 		 * Check whether we can test the next 4 ints without
125*0Sstevel@tonic-gate 		 * hitting a page boundary.  If we can only test 1, 2,
126*0Sstevel@tonic-gate 		 * or 3, go and do that first.  If we can't check any
127*0Sstevel@tonic-gate 		 * more, go and test one byte, realign, and start again.
128*0Sstevel@tonic-gate 		 */
129*0Sstevel@tonic-gate 		count -= 4;
130*0Sstevel@tonic-gate 		switch (count) {
131*0Sstevel@tonic-gate 		case -1:
132*0Sstevel@tonic-gate 			--s1;
133*0Sstevel@tonic-gate 			--s2;
134*0Sstevel@tonic-gate 			goto do3;	/* check only 3 ints */
135*0Sstevel@tonic-gate 		case -2:
136*0Sstevel@tonic-gate 			s1 -= 2;
137*0Sstevel@tonic-gate 			s2 -= 2;
138*0Sstevel@tonic-gate 			goto do2;	/* check only 2 ints */
139*0Sstevel@tonic-gate 		case -3:
140*0Sstevel@tonic-gate 			s1 -= 3;
141*0Sstevel@tonic-gate 			s2 -= 3;
142*0Sstevel@tonic-gate 			goto do1;	/* check only 1 int */
143*0Sstevel@tonic-gate 		case -4:
144*0Sstevel@tonic-gate 		case -5:		/* -5, -6, and -7 come up on the */
145*0Sstevel@tonic-gate 		case -6:		/* next time around after we do one */
146*0Sstevel@tonic-gate 		case -7:		/* of the 3 gotos above */
147*0Sstevel@tonic-gate 			str1 = (void *) s1;
148*0Sstevel@tonic-gate 			str2 = (void *) s2;
149*0Sstevel@tonic-gate 			goto one_byte;
150*0Sstevel@tonic-gate 			/*
151*0Sstevel@tonic-gate 			 * The goto above should be explained.  By going
152*0Sstevel@tonic-gate 			 * into the middle of the loop, it makes sure
153*0Sstevel@tonic-gate 			 * that we advance at least one byte.  We will
154*0Sstevel@tonic-gate 			 * stay in that loop until the misaligned pointer
155*0Sstevel@tonic-gate 			 * becomes aligned (at the page boundary).  We
156*0Sstevel@tonic-gate 			 * will then break out of that loop with the
157*0Sstevel@tonic-gate 			 * formerly misaligned pointer now aligned, the
158*0Sstevel@tonic-gate 			 * formerly aligned pointer now misaligned, and
159*0Sstevel@tonic-gate 			 * we will come back into this loop until the
160*0Sstevel@tonic-gate 			 * latter pointer reaches a page boundary.
161*0Sstevel@tonic-gate 			 */
162*0Sstevel@tonic-gate 		default:		/* at least 4 ints to go */
163*0Sstevel@tonic-gate 			break;
164*0Sstevel@tonic-gate 		}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 		i1 = s1[0];
167*0Sstevel@tonic-gate 		i2 = s2[0];
168*0Sstevel@tonic-gate 		if (i1 != i2)
169*0Sstevel@tonic-gate 			break;
170*0Sstevel@tonic-gate 		else if (ANYNUL(i1, i2))
171*0Sstevel@tonic-gate 			return (0);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate do3:
174*0Sstevel@tonic-gate 		i1 = s1[1];
175*0Sstevel@tonic-gate 		i2 = s2[1];
176*0Sstevel@tonic-gate 		if (i1 != i2)
177*0Sstevel@tonic-gate 			break;
178*0Sstevel@tonic-gate 		else if (ANYNUL(i1, i2))
179*0Sstevel@tonic-gate 			return (0);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate do2:
182*0Sstevel@tonic-gate 		i1 = s1[2];
183*0Sstevel@tonic-gate 		i2 = s2[2];
184*0Sstevel@tonic-gate 		if (i1 != i2)
185*0Sstevel@tonic-gate 			break;
186*0Sstevel@tonic-gate 		else if (ANYNUL(i1, i2))
187*0Sstevel@tonic-gate 			return (0);
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate do1:
190*0Sstevel@tonic-gate 		i1 = s1[3];
191*0Sstevel@tonic-gate 		i2 = s2[3];
192*0Sstevel@tonic-gate 		if (i1 != i2)
193*0Sstevel@tonic-gate 			break;
194*0Sstevel@tonic-gate 		else if (ANYNUL(i1, i2))
195*0Sstevel@tonic-gate 			return (0);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 		s1 += 4;
198*0Sstevel@tonic-gate 		s2 += 4;
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	/* We found a difference.  Go one byte at a time to find where. */
202*0Sstevel@tonic-gate 	b1 = i1;		/* save the ints in memory */
203*0Sstevel@tonic-gate 	b2 = i2;
204*0Sstevel@tonic-gate 	str1 = (void *) &b1;	/* point at them */
205*0Sstevel@tonic-gate 	str2 = (void *) &b2;
206*0Sstevel@tonic-gate 	while (*str1 == *str2) {
207*0Sstevel@tonic-gate 		if (*str1 == '\0')
208*0Sstevel@tonic-gate 			return (0);
209*0Sstevel@tonic-gate 		++str1;
210*0Sstevel@tonic-gate 		++str2;
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 	return ((unsigned char)*str1 - (unsigned char)*str2);
213*0Sstevel@tonic-gate }
214