xref: /titanic_50/usr/src/lib/libc/sparcv9/gen/strcmp.s (revision 54925bf60766fbb4f1f2d7c843721406a7b7a3fb)
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, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27
28.ident	"%Z%%M%	%I%	%E% SMI"
29
30	.file	"%M%"
31
32/* strcmp(s1, s2)
33 *
34 * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
35 *
36 * Fast assembler language version of the following C-program for strcmp
37 * which represents the `standard' for the C-library.
38 *
39 *	int
40 *	strcmp(s1, s2)
41 *	register const char *s1;
42 *	register const char *s2;
43 *	{
44 *
45 *		if(s1 == s2)
46 *			return(0);
47 *		while(*s1 == *s2++)
48 *			if(*s1++ == '\0')
49 *				return(0);
50 *		return(*s1 - s2[-1]);
51 *	}
52 */
53
54#include <sys/asm_linkage.h>
55#include "synonyms.h"
56
57	! This strcmp implementation first determines whether s1 is aligned.
58	! If it is not, it attempts to align it and then checks the
59	! alignment of the destination string.  If it is possible to
60	! align s2, this also happens and then the compare begins.  Otherwise,
61	! a different compare for non-aligned strings is used.
62
63	ENTRY(strcmp)
64
65	.align 32
66
67	subcc	%o0, %o1, %o2		! s1 == s2 ?
68	bz,pn	%xcc, .stringsequal	! yup, same string, done
69 	sethi	%hi(0x01010101), %o5	! magic2<31:13>
70	andcc	%o0, 7, %o3		! s1 sword-aligned ?
71	or	%o5, %lo(0x01010101),%o5! magic2<31:0>
72	bz,pn	%xcc, .s1aligned	! yup
73	sllx	%o5, 32, %o4		! magic2<63:32>
74	sub 	%o3, 8, %o3		! number of bytes till s1 aligned
75
76.aligns1:
77	ldub	[%o1 + %o2], %o0	! s1[]
78	ldub	[%o1], %g1		! s2[]
79	subcc	%o0, %g1, %o0		! s1[] != s2[] ?
80	bne,pn	%xcc, .done		! yup, done
81	addcc	%o0, %g1, %g0		! s1[] == 0 ?
82	bz,pn	%xcc, .done		! yup, done
83	inccc	%o3			! s1 aligned yet?
84	bnz,pt	%xcc, .aligns1		! nope, compare another pair of bytes
85	inc	%o1			! s1++, s2++
86
87.s1aligned:
88	andcc	%o1, 7, %o3		! s2 dword aligned ?
89	or	%o5, %o4, %o5		! magic2<63:0>
90	bz,pn	%xcc, .s2aligned	! yup
91	sllx	%o5, 7, %o4		! magic1
92	sllx	%o3, 3, %g5		! leftshift = 8*ofs
93	orn	%g0, %g0, %g1		! all ones
94	and	%o1, -8, %o1		! round s1 down to next aligned dword
95	srlx	%g1, %g5, %g1		! mask for fixing up bytes
96	ldx	[%o1], %o0		! new lower dword in s2
97	orn	%o0, %g1, %o0 		! force start bytes to non-zero
98	sub	%g0, %g5, %g4		! rightshift = -(8*ofs) mod 64
99	sllx	%o0, %g5, %g1		! partial unaligned word from s2
100	add	%o2, %o3, %o2		! adjust pointers
101	nop				! align loop to 16-byte boundary
102	nop				! align loop to 16-byte boundary
103
104.cmp:
105	andn	%o4, %o0, %o3		! ~word & 0x80808080
106	sub	%o0, %o5, %o0		! word - 0x01010101
107	andcc	%o0, %o3, %g0		! (word - 0x01010101) & ~word & 0x80808080
108	bz,a,pt	%xcc, .doload		! no null byte in previous word from s2
109 	ldx	[%o1+8], %o0		! next aligned word in s2
110.doload:
111	srlx	%o0, %g4, %o3		! bytes from aligned word from s2
112	or	%g1, %o3, %g1		! merge to get unaligned word from s2
113	ldx	[%o1 + %o2], %o3	! word from s1
114	cmp	%o3, %g1		! *s1 != *s2 ?
115	bne,pn	%xcc, .wordsdiffer	! yup, find the byte that is different
116	add	%o1, 8, %o1		! s1+=8, s2+=8
117	andn	%o4, %o3, %g1		! ~word & 0x80808080
118	sub	%o3, %o5, %o3		! word - 0x01010101
119	andcc	%o3, %g1, %g0		! (word - 0x01010101) & ~word & 0x80808080
120	bz,pt	%xcc, .cmp		! no null-byte in s1 yet
121	sllx	%o0, %g5, %g1		! partial unaligned word from s2
122
123	! words are equal but the end of s1 has been reached
124	! this means the strings must be equal
125.strequal:
126	retl				! return from leaf function
127	mov	%g0, %o0		! return 0, i.e. strings are equal
128	nop
129
130.s2aligned:
131	ldx	[%o1 + %o2], %o3	! load word from s1
132
133.cmpaligned:
134	ldx	[%o1], %g1		! load word from s2
135	cmp	%o3, %g1		! *scr1 == *src2 ?
136	bne,pn	%xcc, .wordsdiffer	! nope, find mismatching character
137	add	%o1, 8, %o1		! src1 += 8, src2 += 8
138	andn	%o4, %o3, %o0		! ~word & 0x80808080
139	sub	%o3, %o5, %o3		! word - 0x01010101
140	andcc	%o3, %o0, %g0		! (word - 0x01010101) & ~word & 0x80808080
141	bz,a,pt	%xcc, .cmpaligned	! no null-byte in s1 yet
142	ldx	[%o1 + %o2], %o3	! load word from s1
143
144	! words are equal but the end of s1 has been reached
145	! this means the strings must be equal
146
147.stringsequal:
148	retl				! return from leaf function
149	mov	%g0, %o0		! return 0, i.e. strings are equal
150	nop				! align loop on 16-byte boundary
151	nop				! align loop on 16-byte boundary
152	nop				! align loop on 16-byte boundary
153
154.wordsdiffer:
155	mov	56, %o4			! initial shift count
156	srlx	%g1, %o4, %o2		! first byte of mismatching word in s2
157.cmpbytes:
158	srlx	%o3, %o4, %o1		! first byte of mismatching word in s1
159	subcc	%o1, %o2, %o0		! *s1-*s2
160	bnz,pn	%xcc, .done		! bytes differ, return difference
161	nop
162	andcc	%o1, 0xff, %o0		! *s1 == 0 ?
163	bz,pn	%xcc, .done		! yup, strings match
164	subcc	%o4, 8, %o4		! shift_count -= 8
165	bpos,pt	%xcc, .cmpbytes		! until all bytes processed
166	srlx	%g1, %o4, %o2		! first byte of mismatching word in s2
167
168.done:
169	retl				! return from leaf routine
170	nop				! padding
171
172	SET_SIZE(strcmp)
173