xref: /titanic_51/usr/src/lib/libc/sparcv9/gen/strcmp.s (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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	.file	"strcmp.s"
28
29/* strcmp(s1, s2)
30 *
31 * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
32 *
33 * Fast assembler language version of the following C-program for strcmp
34 * which represents the `standard' for the C-library.
35 *
36 *	int
37 *	strcmp(s1, s2)
38 *	register const char *s1;
39 *	register const char *s2;
40 *	{
41 *
42 *		if(s1 == s2)
43 *			return(0);
44 *		while(*s1 == *s2++)
45 *			if(*s1++ == '\0')
46 *				return(0);
47 *		return(*s1 - s2[-1]);
48 *	}
49 */
50
51#include <sys/asm_linkage.h>
52
53	! This strcmp implementation first determines whether s1 is aligned.
54	! If it is not, it attempts to align it and then checks the
55	! alignment of the destination string.  If it is possible to
56	! align s2, this also happens and then the compare begins.  Otherwise,
57	! a different compare for non-aligned strings is used.
58
59	ENTRY(strcmp)
60
61	.align 32
62
63	subcc	%o0, %o1, %o2		! s1 == s2 ?
64	bz,pn	%xcc, .stringsequal	! yup, same string, done
65 	sethi	%hi(0x01010101), %o5	! magic2<31:13>
66	andcc	%o0, 7, %o3		! s1 sword-aligned ?
67	or	%o5, %lo(0x01010101),%o5! magic2<31:0>
68	bz,pn	%xcc, .s1aligned	! yup
69	sllx	%o5, 32, %o4		! magic2<63:32>
70	sub 	%o3, 8, %o3		! number of bytes till s1 aligned
71
72.aligns1:
73	ldub	[%o1 + %o2], %o0	! s1[]
74	ldub	[%o1], %g1		! s2[]
75	subcc	%o0, %g1, %o0		! s1[] != s2[] ?
76	bne,pn	%xcc, .done		! yup, done
77	addcc	%o0, %g1, %g0		! s1[] == 0 ?
78	bz,pn	%xcc, .done		! yup, done
79	inccc	%o3			! s1 aligned yet?
80	bnz,pt	%xcc, .aligns1		! nope, compare another pair of bytes
81	inc	%o1			! s1++, s2++
82
83.s1aligned:
84	andcc	%o1, 7, %o3		! s2 dword aligned ?
85	or	%o5, %o4, %o5		! magic2<63:0>
86	bz,pn	%xcc, .s2aligned	! yup
87	sllx	%o5, 7, %o4		! magic1
88	sllx	%o3, 3, %g5		! leftshift = 8*ofs
89	orn	%g0, %g0, %g1		! all ones
90	and	%o1, -8, %o1		! round s1 down to next aligned dword
91	srlx	%g1, %g5, %g1		! mask for fixing up bytes
92	ldx	[%o1], %o0		! new lower dword in s2
93	orn	%o0, %g1, %o0 		! force start bytes to non-zero
94	sub	%g0, %g5, %g4		! rightshift = -(8*ofs) mod 64
95	sllx	%o0, %g5, %g1		! partial unaligned word from s2
96	add	%o2, %o3, %o2		! adjust pointers
97	nop				! align loop to 16-byte boundary
98	nop				! align loop to 16-byte boundary
99
100.cmp:
101	andn	%o4, %o0, %o3		! ~word & 0x80808080
102	sub	%o0, %o5, %o0		! word - 0x01010101
103	andcc	%o0, %o3, %g0		! (word - 0x01010101) & ~word & 0x80808080
104	bz,a,pt	%xcc, .doload		! no null byte in previous word from s2
105 	ldx	[%o1+8], %o0		! next aligned word in s2
106.doload:
107	srlx	%o0, %g4, %o3		! bytes from aligned word from s2
108	or	%g1, %o3, %g1		! merge to get unaligned word from s2
109	ldx	[%o1 + %o2], %o3	! word from s1
110	cmp	%o3, %g1		! *s1 != *s2 ?
111	bne,pn	%xcc, .wordsdiffer	! yup, find the byte that is different
112	add	%o1, 8, %o1		! s1+=8, s2+=8
113	andn	%o4, %o3, %g1		! ~word & 0x80808080
114	sub	%o3, %o5, %o3		! word - 0x01010101
115	andcc	%o3, %g1, %g0		! (word - 0x01010101) & ~word & 0x80808080
116	bz,pt	%xcc, .cmp		! no null-byte in s1 yet
117	sllx	%o0, %g5, %g1		! partial unaligned word from s2
118
119	! words are equal but the end of s1 has been reached
120	! this means the strings must be equal
121.strequal:
122	retl				! return from leaf function
123	mov	%g0, %o0		! return 0, i.e. strings are equal
124	nop
125
126.s2aligned:
127	ldx	[%o1 + %o2], %o3	! load word from s1
128
129.cmpaligned:
130	ldx	[%o1], %g1		! load word from s2
131	cmp	%o3, %g1		! *scr1 == *src2 ?
132	bne,pn	%xcc, .wordsdiffer	! nope, find mismatching character
133	add	%o1, 8, %o1		! src1 += 8, src2 += 8
134	andn	%o4, %o3, %o0		! ~word & 0x80808080
135	sub	%o3, %o5, %o3		! word - 0x01010101
136	andcc	%o3, %o0, %g0		! (word - 0x01010101) & ~word & 0x80808080
137	bz,a,pt	%xcc, .cmpaligned	! no null-byte in s1 yet
138	ldx	[%o1 + %o2], %o3	! load word from s1
139
140	! words are equal but the end of s1 has been reached
141	! this means the strings must be equal
142
143.stringsequal:
144	retl				! return from leaf function
145	mov	%g0, %o0		! return 0, i.e. strings are equal
146	nop				! align loop on 16-byte boundary
147	nop				! align loop on 16-byte boundary
148	nop				! align loop on 16-byte boundary
149
150.wordsdiffer:
151	mov	56, %o4			! initial shift count
152	srlx	%g1, %o4, %o2		! first byte of mismatching word in s2
153.cmpbytes:
154	srlx	%o3, %o4, %o1		! first byte of mismatching word in s1
155	subcc	%o1, %o2, %o0		! *s1-*s2
156	bnz,pn	%xcc, .done		! bytes differ, return difference
157	nop
158	andcc	%o1, 0xff, %o0		! *s1 == 0 ?
159	bz,pn	%xcc, .done		! yup, strings match
160	subcc	%o4, 8, %o4		! shift_count -= 8
161	bpos,pt	%xcc, .cmpbytes		! until all bytes processed
162	srlx	%g1, %o4, %o2		! first byte of mismatching word in s2
163
164.done:
165	retl				! return from leaf routine
166	nop				! padding
167
168	SET_SIZE(strcmp)
169