xref: /titanic_41/usr/src/lib/libc/sparc/gen/strcmp.s (revision 940d71d237794874e18a0eb72f6564821a823517)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31/* strcmp(s1, s2)
32 *
33 * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
34 *
35 * Fast assembler language version of the following C-program for strcmp
36 * which represents the `standard' for the C-library.
37 *
38 *	int
39 *	strcmp(s1, s2)
40 *	register const char *s1;
41 *	register const char *s2;
42 *	{
43 *
44 *		if(s1 == s2)
45 *			return(0);
46 *		while(*s1 == *s2++)
47 *			if(*s1++ == '\0')
48 *				return(0);
49 *		return(*s1 - s2[-1]);
50 *	}
51 */
52
53#include <sys/asm_linkage.h>
54
55	! This strcmp implementation first determines whether s1 is aligned.
56	! If it is not, it attempts to align it and then checks the
57	! alignment of the destination string.  If it is possible to
58	! align s2, this also happens and then the compare begins.  Otherwise,
59	! a different compare for non-aligned strings is used.
60	! In this case, we have multiple routines depending upon the
61	! degree to which a string is mis-aligned.
62
63	ENTRY(strcmp)
64
65	.align 32
66
67	subcc	%o0, %o1, %o2		! s1 == s2 ?
68	bz	.stringsequal1		! yup, same string, done
69	sethi	%hi(0x01010101), %o5	! start loading Mycroft's magic2
70	andcc	%o0, 3, %o3		! s1 word-aligned ?
71	or	%o5, %lo(0x01010101),%o5! finish loading Mycroft's magic2
72	bz	.s1aligned		! yup
73	sll	%o5, 7, %o4		! load Mycroft's magic1
74	sub	%o3, 4, %o3		! number of bytes till aligned
75
76.aligns1:
77	ldub	[%o1 + %o2], %o0	! s1[]
78	ldub	[%o1], %g1		! s2[]
79	subcc	%o0, %g1, %o0		! s1[] != s2[] ?
80	bne	.done			! yup, done
81	addcc	%o0, %g1, %g0		! s1[] == 0 ?
82	bz	.done			! yup, done
83	inccc	%o3			! s1 aligned yet?
84	bnz	.aligns1		! nope, compare another pair of bytes
85	inc	%o1			! s1++, s2++
86
87.s1aligned:
88	andcc	%o1, 3, %o3		! s2 word aligned ?
89	bz	.word4			! yup
90	cmp	%o3, 2			! s2 half-word aligned ?
91	be	.word2			! yup
92	cmp	%o3, 3			! s2 offset to dword == 3 ?
93	be,a	.word3			! yup
94	ldub	[%o1], %o0		! new lower word in s2
95
96.word1:
97	lduw	[%o1 - 1], %o0		! new lower word in s2
98	sethi	%hi(0xff000000), %o3	! mask for forcing byte 1 non-zero
99	sll	%o0, 8, %g1		! partial unaligned word from s2
100	or	%o0, %o3, %o0		! force byte 1 non-zero
101
102.cmp1:
103	andn	%o4, %o0, %o3		! ~word & 0x80808080
104	sub	%o0, %o5, %o0		! word - 0x01010101
105	andcc	%o0, %o3, %g0		! (word - 0x01010101) & ~word & 0x80808080
106	bz,a	.doload1		! no null byte in previous word from s2
107	lduw	[%o1 + 3], %o0		! load next aligned word from s2
108.doload1:
109	srl	%o0, 24, %o3		! byte 1 of new aligned word from s2
110	or	%g1, %o3, %g1		! merge to get unaligned word from s2
111	lduw	[%o1 + %o2], %o3	! word from s1
112	cmp	%o3, %g1		! *s1 != *s2 ?
113	bne	.wordsdiffer		! yup, find the byte that is different
114	add	%o1, 4, %o1		! s1+=4, s2+=4
115	andn	%o4, %o3, %g1		! ~word & 0x80808080
116	sub	%o3, %o5, %o3		! word - 0x01010101
117	andcc	%o3, %g1, %g0		! (word - 0x01010101) & ~word & 0x80808080
118	bz	.cmp1			! no null-byte in s1 yet
119	sll	%o0, 8, %g1		! partial unaligned word from s2
120
121	! words are equal but the end of s1 has been reached
122	! this means the strings must be equal
123.stringsequal1:
124	retl				! return from leaf function
125	mov	%g0, %o0		! return 0, i.e. strings are equal
126	nop				! pad for optimal alignment of .cmp2
127	nop				! pad for optimal alignment of .cmp2
128
129.word2:
130	lduh	[%o1], %o0		! new lower word in s2
131	sethi	%hi(0xffff0000), %o3	! mask for forcing bytes 1,2 non-zero
132	sll	%o0, 16, %g1		! partial unaligned word from s2
133	or	%o0, %o3, %o0		! force bytes 1,2 non-zero
134
135.cmp2:
136	andn	%o4, %o0, %o3		! ~word & 0x80808080
137	sub	%o0, %o5, %o0		! word - 0x01010101
138	andcc	%o0, %o3, %g0		! (word - 0x01010101) & ~word & 0x80808080
139	bz,a	.doload2		! no null byte in previous word from s2
140	lduw	[%o1 + 2], %o0		! load next aligned word from s2
141.doload2:
142	srl	%o0, 16, %o3		! bytes 1,2 of new aligned word from s2
143	or	%g1, %o3, %g1		! merge to get unaligned word from s2
144	lduw	[%o1 + %o2], %o3	! word from s1
145	cmp	%o3, %g1		! *s1 != *s2 ?
146	bne	.wordsdiffer		! yup, find the byte that is different
147	add	%o1, 4, %o1		! s1+=4, s2+=4
148	andn	%o4, %o3, %g1		! ~word & 0x80808080
149	sub	%o3, %o5, %o3		! word - 0x01010101
150	andcc	%o3, %g1, %g0		! (word - 0x01010101) & ~word & 0x80808080
151	bz	.cmp2			! no null-byte in s1 yet
152	sll	%o0, 16, %g1		! partial unaligned word from s2
153
154	! words are equal but the end of s1 has been reached
155	! this means the strings must be equal
156.stringsequal2:
157	retl				! return from leaf function
158	mov	%g0, %o0		! return 0, i.e. strings are equal
159
160.word3:
161	sll	%o0, 24, %g1		! partial unaligned word from s2
162	nop				! pad for optimal alignment of .cmp3
163.cmp3:
164	andcc	%o0, 0xff, %g0		! did previous word contain null-byte ?
165	bnz,a	.doload3		! nope, load next word from s2
166	lduw	[%o1 + 1], %o0		! load next aligned word from s2
167.doload3:
168	srl	%o0, 8, %o3		! bytes 1,2,3 from new aligned s2 word
169	or	%g1, %o3, %g1		! merge to get unaligned word from s2
170	lduw	[%o1 + %o2], %o3	! word from s1
171	cmp	%o3, %g1		! *s1 != *s2 ?
172	bne	.wordsdiffer		! yup, find the byte that is different
173	add	%o1, 4, %o1		! s1+=4, s2+=4
174	andn	%o4, %o3, %g1		! ~word & 0x80808080
175	sub	%o3, %o5, %o3		! word - 0x01010101
176	andcc	%o3, %g1, %g0		! (word - 0x01010101) & ~word & 0x80808080
177	bz	.cmp3			! no null-byte in s1 yet
178	sll	%o0, 24, %g1		! partial unaligned word from s2
179
180	! words are equal but the end of s1 has been reached
181	! this means the strings must be equal
182.stringsequal3:
183	retl				! return from leaf function
184	mov	%g0, %o0		! return 0, i.e. strings are equal
185
186.word4:
187	lduw	[%o1 + %o2], %o3	! load word from s1
188	nop				! pad for optimal alignment of .cmp4
189	nop				! pad for optimal alignment of .cmp4
190	nop				! pad for optimal alignment of .cmp4
191
192.cmp4:
193	lduw	[%o1], %g1		! load word from s2
194	cmp	%o3, %g1		! *scr1 == *src2 ?
195	bne	.wordsdiffer		! nope, find mismatching character
196	add	%o1, 4, %o1		! src1 += 4, src2 += 4
197	andn	%o4, %o3, %o0		! ~word & 0x80808080
198	sub	%o3, %o5, %o3		! word - 0x01010101
199	andcc	%o3, %o0, %g0		! (word - 0x01010101) & ~word & 0x80808080
200	bz,a	.cmp4			! no null-byte in s1 yet
201	lduw	[%o1 + %o2], %o3	! load word from s1
202
203	! words are equal but the end of s1 has been reached
204	! this means the strings must be equal
205.stringsequal4:
206	retl				! return from leaf function
207	mov	%g0, %o0		! return 0, i.e. strings are equal
208
209.wordsdiffer:
210	srl	%g1, 24, %o2		! first byte of mismatching word in s2
211	srl	%o3, 24, %o1		! first byte of mismatching word in s1
212	subcc	%o1, %o2, %o0		! *s1-*s2
213	bnz	.done			! bytes differ, return difference
214	srl	%g1, 16, %o2		! second byte of mismatching word in s2
215	andcc	%o1, 0xff, %o0		! *s1 == 0 ?
216	bz	.done			! yup
217
218	! we know byte 1 is equal, so can compare bytes 1,2 as a group
219
220	srl	%o3, 16, %o1		! second byte of mismatching word in s1
221	subcc	%o1, %o2, %o0		! *s1-*s2
222	bnz	.done			! bytes differ, return difference
223	srl	%g1, 8, %o2		! third byte of mismatching word in s2
224	andcc	%o1, 0xff, %o0		! *s1 == 0 ?
225	bz	.done			! yup
226
227	! we know bytes 1, 2 are equal, so can compare bytes 1,2,3 as a group
228
229	srl	%o3, 8, %o1		! third byte of mismatching word in s1
230	subcc	%o1, %o2, %o0		! *s1-*s2
231	bnz	.done			! bytes differ, return difference
232	andcc	%o1, 0xff, %g0		! *s1 == 0 ?
233	bz	.stringsequal1		! yup
234
235	! we know bytes 1,2,3 are equal, so can compare bytes 1,2,3,4 as group
236
237	subcc	%o3, %g1, %o0		! *s1-*s2
238	bz,a	.done			! bytes differ, return difference
239	andcc	%o3, 0xff, %o0		! *s1 == 0 ?
240
241.done:
242	retl				! return from leaf routine
243	nop				! padding
244
245
246	SET_SIZE(strcmp)
247