xref: /titanic_44/usr/src/lib/libc/sparc/gen/strcmp.s (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
17c478bd9Sstevel@tonic-gate/*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1b4Sraf * Common Development and Distribution License (the "License").
67257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate/*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*9a70fc3bSMark J. Nelson	.file	"strcmp.s"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate/* strcmp(s1, s2)
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate * Fast assembler language version of the following C-program for strcmp
347c478bd9Sstevel@tonic-gate * which represents the `standard' for the C-library.
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate *	int
377c478bd9Sstevel@tonic-gate *	strcmp(s1, s2)
387c478bd9Sstevel@tonic-gate *	register const char *s1;
397c478bd9Sstevel@tonic-gate *	register const char *s2;
407c478bd9Sstevel@tonic-gate *	{
417c478bd9Sstevel@tonic-gate *
427c478bd9Sstevel@tonic-gate *		if(s1 == s2)
437c478bd9Sstevel@tonic-gate *			return(0);
447c478bd9Sstevel@tonic-gate *		while(*s1 == *s2++)
457c478bd9Sstevel@tonic-gate *			if(*s1++ == '\0')
467c478bd9Sstevel@tonic-gate *				return(0);
477c478bd9Sstevel@tonic-gate *		return(*s1 - s2[-1]);
487c478bd9Sstevel@tonic-gate *	}
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate	! This strcmp implementation first determines whether s1 is aligned.
547c478bd9Sstevel@tonic-gate	! If it is not, it attempts to align it and then checks the
557c478bd9Sstevel@tonic-gate	! alignment of the destination string.  If it is possible to
567c478bd9Sstevel@tonic-gate	! align s2, this also happens and then the compare begins.  Otherwise,
577c478bd9Sstevel@tonic-gate	! a different compare for non-aligned strings is used.
587c478bd9Sstevel@tonic-gate	! In this case, we have multiple routines depending upon the
597c478bd9Sstevel@tonic-gate	! degree to which a string is mis-aligned.
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate	ENTRY(strcmp)
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate	.align 32
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate	subcc	%o0, %o1, %o2		! s1 == s2 ?
667c478bd9Sstevel@tonic-gate	bz	.stringsequal1		! yup, same string, done
677c478bd9Sstevel@tonic-gate	sethi	%hi(0x01010101), %o5	! start loading Mycroft's magic2
687c478bd9Sstevel@tonic-gate	andcc	%o0, 3, %o3		! s1 word-aligned ?
697c478bd9Sstevel@tonic-gate	or	%o5, %lo(0x01010101),%o5! finish loading Mycroft's magic2
707c478bd9Sstevel@tonic-gate	bz	.s1aligned		! yup
717c478bd9Sstevel@tonic-gate	sll	%o5, 7, %o4		! load Mycroft's magic1
727c478bd9Sstevel@tonic-gate	sub	%o3, 4, %o3		! number of bytes till aligned
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate.aligns1:
757c478bd9Sstevel@tonic-gate	ldub	[%o1 + %o2], %o0	! s1[]
767c478bd9Sstevel@tonic-gate	ldub	[%o1], %g1		! s2[]
777c478bd9Sstevel@tonic-gate	subcc	%o0, %g1, %o0		! s1[] != s2[] ?
787c478bd9Sstevel@tonic-gate	bne	.done			! yup, done
797c478bd9Sstevel@tonic-gate	addcc	%o0, %g1, %g0		! s1[] == 0 ?
807c478bd9Sstevel@tonic-gate	bz	.done			! yup, done
817c478bd9Sstevel@tonic-gate	inccc	%o3			! s1 aligned yet?
827c478bd9Sstevel@tonic-gate	bnz	.aligns1		! nope, compare another pair of bytes
837c478bd9Sstevel@tonic-gate	inc	%o1			! s1++, s2++
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate.s1aligned:
867c478bd9Sstevel@tonic-gate	andcc	%o1, 3, %o3		! s2 word aligned ?
877c478bd9Sstevel@tonic-gate	bz	.word4			! yup
887c478bd9Sstevel@tonic-gate	cmp	%o3, 2			! s2 half-word aligned ?
897c478bd9Sstevel@tonic-gate	be	.word2			! yup
907c478bd9Sstevel@tonic-gate	cmp	%o3, 3			! s2 offset to dword == 3 ?
917c478bd9Sstevel@tonic-gate	be,a	.word3			! yup
927c478bd9Sstevel@tonic-gate	ldub	[%o1], %o0		! new lower word in s2
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate.word1:
957c478bd9Sstevel@tonic-gate	lduw	[%o1 - 1], %o0		! new lower word in s2
967c478bd9Sstevel@tonic-gate	sethi	%hi(0xff000000), %o3	! mask for forcing byte 1 non-zero
977c478bd9Sstevel@tonic-gate	sll	%o0, 8, %g1		! partial unaligned word from s2
987c478bd9Sstevel@tonic-gate	or	%o0, %o3, %o0		! force byte 1 non-zero
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate.cmp1:
1017c478bd9Sstevel@tonic-gate	andn	%o4, %o0, %o3		! ~word & 0x80808080
1027c478bd9Sstevel@tonic-gate	sub	%o0, %o5, %o0		! word - 0x01010101
1037c478bd9Sstevel@tonic-gate	andcc	%o0, %o3, %g0		! (word - 0x01010101) & ~word & 0x80808080
1047c478bd9Sstevel@tonic-gate	bz,a	.doload1		! no null byte in previous word from s2
1057c478bd9Sstevel@tonic-gate	lduw	[%o1 + 3], %o0		! load next aligned word from s2
1067c478bd9Sstevel@tonic-gate.doload1:
1077c478bd9Sstevel@tonic-gate	srl	%o0, 24, %o3		! byte 1 of new aligned word from s2
1087c478bd9Sstevel@tonic-gate	or	%g1, %o3, %g1		! merge to get unaligned word from s2
1097c478bd9Sstevel@tonic-gate	lduw	[%o1 + %o2], %o3	! word from s1
1107c478bd9Sstevel@tonic-gate	cmp	%o3, %g1		! *s1 != *s2 ?
1117c478bd9Sstevel@tonic-gate	bne	.wordsdiffer		! yup, find the byte that is different
1127c478bd9Sstevel@tonic-gate	add	%o1, 4, %o1		! s1+=4, s2+=4
1137c478bd9Sstevel@tonic-gate	andn	%o4, %o3, %g1		! ~word & 0x80808080
1147c478bd9Sstevel@tonic-gate	sub	%o3, %o5, %o3		! word - 0x01010101
1157c478bd9Sstevel@tonic-gate	andcc	%o3, %g1, %g0		! (word - 0x01010101) & ~word & 0x80808080
1167c478bd9Sstevel@tonic-gate	bz	.cmp1			! no null-byte in s1 yet
1177c478bd9Sstevel@tonic-gate	sll	%o0, 8, %g1		! partial unaligned word from s2
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate	! words are equal but the end of s1 has been reached
1207c478bd9Sstevel@tonic-gate	! this means the strings must be equal
1217c478bd9Sstevel@tonic-gate.stringsequal1:
1227c478bd9Sstevel@tonic-gate	retl				! return from leaf function
1237c478bd9Sstevel@tonic-gate	mov	%g0, %o0		! return 0, i.e. strings are equal
1247c478bd9Sstevel@tonic-gate	nop				! pad for optimal alignment of .cmp2
1257c478bd9Sstevel@tonic-gate	nop				! pad for optimal alignment of .cmp2
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate.word2:
1287c478bd9Sstevel@tonic-gate	lduh	[%o1], %o0		! new lower word in s2
1297c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! mask for forcing bytes 1,2 non-zero
1307c478bd9Sstevel@tonic-gate	sll	%o0, 16, %g1		! partial unaligned word from s2
1317c478bd9Sstevel@tonic-gate	or	%o0, %o3, %o0		! force bytes 1,2 non-zero
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate.cmp2:
1347c478bd9Sstevel@tonic-gate	andn	%o4, %o0, %o3		! ~word & 0x80808080
1357c478bd9Sstevel@tonic-gate	sub	%o0, %o5, %o0		! word - 0x01010101
1367c478bd9Sstevel@tonic-gate	andcc	%o0, %o3, %g0		! (word - 0x01010101) & ~word & 0x80808080
1377c478bd9Sstevel@tonic-gate	bz,a	.doload2		! no null byte in previous word from s2
1387c478bd9Sstevel@tonic-gate	lduw	[%o1 + 2], %o0		! load next aligned word from s2
1397c478bd9Sstevel@tonic-gate.doload2:
1407c478bd9Sstevel@tonic-gate	srl	%o0, 16, %o3		! bytes 1,2 of new aligned word from s2
1417c478bd9Sstevel@tonic-gate	or	%g1, %o3, %g1		! merge to get unaligned word from s2
1427c478bd9Sstevel@tonic-gate	lduw	[%o1 + %o2], %o3	! word from s1
1437c478bd9Sstevel@tonic-gate	cmp	%o3, %g1		! *s1 != *s2 ?
1447c478bd9Sstevel@tonic-gate	bne	.wordsdiffer		! yup, find the byte that is different
1457c478bd9Sstevel@tonic-gate	add	%o1, 4, %o1		! s1+=4, s2+=4
1467c478bd9Sstevel@tonic-gate	andn	%o4, %o3, %g1		! ~word & 0x80808080
1477c478bd9Sstevel@tonic-gate	sub	%o3, %o5, %o3		! word - 0x01010101
1487c478bd9Sstevel@tonic-gate	andcc	%o3, %g1, %g0		! (word - 0x01010101) & ~word & 0x80808080
1497c478bd9Sstevel@tonic-gate	bz	.cmp2			! no null-byte in s1 yet
1507c478bd9Sstevel@tonic-gate	sll	%o0, 16, %g1		! partial unaligned word from s2
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate	! words are equal but the end of s1 has been reached
1537c478bd9Sstevel@tonic-gate	! this means the strings must be equal
1547c478bd9Sstevel@tonic-gate.stringsequal2:
1557c478bd9Sstevel@tonic-gate	retl				! return from leaf function
1567c478bd9Sstevel@tonic-gate	mov	%g0, %o0		! return 0, i.e. strings are equal
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate.word3:
1597c478bd9Sstevel@tonic-gate	sll	%o0, 24, %g1		! partial unaligned word from s2
1607c478bd9Sstevel@tonic-gate	nop				! pad for optimal alignment of .cmp3
1617c478bd9Sstevel@tonic-gate.cmp3:
1627c478bd9Sstevel@tonic-gate	andcc	%o0, 0xff, %g0		! did previous word contain null-byte ?
1637c478bd9Sstevel@tonic-gate	bnz,a	.doload3		! nope, load next word from s2
1647c478bd9Sstevel@tonic-gate	lduw	[%o1 + 1], %o0		! load next aligned word from s2
1657c478bd9Sstevel@tonic-gate.doload3:
1667c478bd9Sstevel@tonic-gate	srl	%o0, 8, %o3		! bytes 1,2,3 from new aligned s2 word
1677c478bd9Sstevel@tonic-gate	or	%g1, %o3, %g1		! merge to get unaligned word from s2
1687c478bd9Sstevel@tonic-gate	lduw	[%o1 + %o2], %o3	! word from s1
1697c478bd9Sstevel@tonic-gate	cmp	%o3, %g1		! *s1 != *s2 ?
1707c478bd9Sstevel@tonic-gate	bne	.wordsdiffer		! yup, find the byte that is different
1717c478bd9Sstevel@tonic-gate	add	%o1, 4, %o1		! s1+=4, s2+=4
1727c478bd9Sstevel@tonic-gate	andn	%o4, %o3, %g1		! ~word & 0x80808080
1737c478bd9Sstevel@tonic-gate	sub	%o3, %o5, %o3		! word - 0x01010101
1747c478bd9Sstevel@tonic-gate	andcc	%o3, %g1, %g0		! (word - 0x01010101) & ~word & 0x80808080
1757c478bd9Sstevel@tonic-gate	bz	.cmp3			! no null-byte in s1 yet
1767c478bd9Sstevel@tonic-gate	sll	%o0, 24, %g1		! partial unaligned word from s2
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate	! words are equal but the end of s1 has been reached
1797c478bd9Sstevel@tonic-gate	! this means the strings must be equal
1807c478bd9Sstevel@tonic-gate.stringsequal3:
1817c478bd9Sstevel@tonic-gate	retl				! return from leaf function
1827c478bd9Sstevel@tonic-gate	mov	%g0, %o0		! return 0, i.e. strings are equal
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate.word4:
1857c478bd9Sstevel@tonic-gate	lduw	[%o1 + %o2], %o3	! load word from s1
1867c478bd9Sstevel@tonic-gate	nop				! pad for optimal alignment of .cmp4
1877c478bd9Sstevel@tonic-gate	nop				! pad for optimal alignment of .cmp4
1887c478bd9Sstevel@tonic-gate	nop				! pad for optimal alignment of .cmp4
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate.cmp4:
1917c478bd9Sstevel@tonic-gate	lduw	[%o1], %g1		! load word from s2
1927c478bd9Sstevel@tonic-gate	cmp	%o3, %g1		! *scr1 == *src2 ?
1937c478bd9Sstevel@tonic-gate	bne	.wordsdiffer		! nope, find mismatching character
1947c478bd9Sstevel@tonic-gate	add	%o1, 4, %o1		! src1 += 4, src2 += 4
1957c478bd9Sstevel@tonic-gate	andn	%o4, %o3, %o0		! ~word & 0x80808080
1967c478bd9Sstevel@tonic-gate	sub	%o3, %o5, %o3		! word - 0x01010101
1977c478bd9Sstevel@tonic-gate	andcc	%o3, %o0, %g0		! (word - 0x01010101) & ~word & 0x80808080
1987c478bd9Sstevel@tonic-gate	bz,a	.cmp4			! no null-byte in s1 yet
1997c478bd9Sstevel@tonic-gate	lduw	[%o1 + %o2], %o3	! load word from s1
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate	! words are equal but the end of s1 has been reached
2027c478bd9Sstevel@tonic-gate	! this means the strings must be equal
2037c478bd9Sstevel@tonic-gate.stringsequal4:
2047c478bd9Sstevel@tonic-gate	retl				! return from leaf function
2057c478bd9Sstevel@tonic-gate	mov	%g0, %o0		! return 0, i.e. strings are equal
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate.wordsdiffer:
2087c478bd9Sstevel@tonic-gate	srl	%g1, 24, %o2		! first byte of mismatching word in s2
2097c478bd9Sstevel@tonic-gate	srl	%o3, 24, %o1		! first byte of mismatching word in s1
2107c478bd9Sstevel@tonic-gate	subcc	%o1, %o2, %o0		! *s1-*s2
2117c478bd9Sstevel@tonic-gate	bnz	.done			! bytes differ, return difference
2127c478bd9Sstevel@tonic-gate	srl	%g1, 16, %o2		! second byte of mismatching word in s2
2137c478bd9Sstevel@tonic-gate	andcc	%o1, 0xff, %o0		! *s1 == 0 ?
2147c478bd9Sstevel@tonic-gate	bz	.done			! yup
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate	! we know byte 1 is equal, so can compare bytes 1,2 as a group
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate	srl	%o3, 16, %o1		! second byte of mismatching word in s1
2197c478bd9Sstevel@tonic-gate	subcc	%o1, %o2, %o0		! *s1-*s2
2207c478bd9Sstevel@tonic-gate	bnz	.done			! bytes differ, return difference
2217c478bd9Sstevel@tonic-gate	srl	%g1, 8, %o2		! third byte of mismatching word in s2
2227c478bd9Sstevel@tonic-gate	andcc	%o1, 0xff, %o0		! *s1 == 0 ?
2237c478bd9Sstevel@tonic-gate	bz	.done			! yup
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate	! we know bytes 1, 2 are equal, so can compare bytes 1,2,3 as a group
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate	srl	%o3, 8, %o1		! third byte of mismatching word in s1
2287c478bd9Sstevel@tonic-gate	subcc	%o1, %o2, %o0		! *s1-*s2
2297c478bd9Sstevel@tonic-gate	bnz	.done			! bytes differ, return difference
2307c478bd9Sstevel@tonic-gate	andcc	%o1, 0xff, %g0		! *s1 == 0 ?
2317c478bd9Sstevel@tonic-gate	bz	.stringsequal1		! yup
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate	! we know bytes 1,2,3 are equal, so can compare bytes 1,2,3,4 as group
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate	subcc	%o3, %g1, %o0		! *s1-*s2
2367c478bd9Sstevel@tonic-gate	bz,a	.done			! bytes differ, return difference
2377c478bd9Sstevel@tonic-gate	andcc	%o3, 0xff, %o0		! *s1 == 0 ?
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate.done:
2407c478bd9Sstevel@tonic-gate	retl				! return from leaf routine
2417c478bd9Sstevel@tonic-gate	nop				! padding
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate	SET_SIZE(strcmp)
245