xref: /onnv-gate/usr/src/lib/libc/sparcv9/gen/strcmp.s (revision 7298:b69e27387f74)
10Sstevel@tonic-gate/*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56812Sraf * Common Development and Distribution License (the "License").
66812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216812Sraf
220Sstevel@tonic-gate/*
236812Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*7298SMark.J.Nelson@Sun.COM	.file	"strcmp.s"
280Sstevel@tonic-gate
290Sstevel@tonic-gate/* strcmp(s1, s2)
300Sstevel@tonic-gate *
310Sstevel@tonic-gate * Compare strings:  s1>s2: >0  s1==s2: 0  s1<s2: <0
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * Fast assembler language version of the following C-program for strcmp
340Sstevel@tonic-gate * which represents the `standard' for the C-library.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate *	int
370Sstevel@tonic-gate *	strcmp(s1, s2)
380Sstevel@tonic-gate *	register const char *s1;
390Sstevel@tonic-gate *	register const char *s2;
400Sstevel@tonic-gate *	{
410Sstevel@tonic-gate *
420Sstevel@tonic-gate *		if(s1 == s2)
430Sstevel@tonic-gate *			return(0);
440Sstevel@tonic-gate *		while(*s1 == *s2++)
450Sstevel@tonic-gate *			if(*s1++ == '\0')
460Sstevel@tonic-gate *				return(0);
470Sstevel@tonic-gate *		return(*s1 - s2[-1]);
480Sstevel@tonic-gate *	}
490Sstevel@tonic-gate */
500Sstevel@tonic-gate
510Sstevel@tonic-gate#include <sys/asm_linkage.h>
520Sstevel@tonic-gate
530Sstevel@tonic-gate	! This strcmp implementation first determines whether s1 is aligned.
540Sstevel@tonic-gate	! If it is not, it attempts to align it and then checks the
550Sstevel@tonic-gate	! alignment of the destination string.  If it is possible to
560Sstevel@tonic-gate	! align s2, this also happens and then the compare begins.  Otherwise,
570Sstevel@tonic-gate	! a different compare for non-aligned strings is used.
580Sstevel@tonic-gate
590Sstevel@tonic-gate	ENTRY(strcmp)
600Sstevel@tonic-gate
610Sstevel@tonic-gate	.align 32
620Sstevel@tonic-gate
630Sstevel@tonic-gate	subcc	%o0, %o1, %o2		! s1 == s2 ?
640Sstevel@tonic-gate	bz,pn	%xcc, .stringsequal	! yup, same string, done
650Sstevel@tonic-gate 	sethi	%hi(0x01010101), %o5	! magic2<31:13>
660Sstevel@tonic-gate	andcc	%o0, 7, %o3		! s1 sword-aligned ?
670Sstevel@tonic-gate	or	%o5, %lo(0x01010101),%o5! magic2<31:0>
680Sstevel@tonic-gate	bz,pn	%xcc, .s1aligned	! yup
690Sstevel@tonic-gate	sllx	%o5, 32, %o4		! magic2<63:32>
700Sstevel@tonic-gate	sub 	%o3, 8, %o3		! number of bytes till s1 aligned
710Sstevel@tonic-gate
720Sstevel@tonic-gate.aligns1:
730Sstevel@tonic-gate	ldub	[%o1 + %o2], %o0	! s1[]
740Sstevel@tonic-gate	ldub	[%o1], %g1		! s2[]
750Sstevel@tonic-gate	subcc	%o0, %g1, %o0		! s1[] != s2[] ?
760Sstevel@tonic-gate	bne,pn	%xcc, .done		! yup, done
770Sstevel@tonic-gate	addcc	%o0, %g1, %g0		! s1[] == 0 ?
780Sstevel@tonic-gate	bz,pn	%xcc, .done		! yup, done
790Sstevel@tonic-gate	inccc	%o3			! s1 aligned yet?
800Sstevel@tonic-gate	bnz,pt	%xcc, .aligns1		! nope, compare another pair of bytes
810Sstevel@tonic-gate	inc	%o1			! s1++, s2++
820Sstevel@tonic-gate
830Sstevel@tonic-gate.s1aligned:
840Sstevel@tonic-gate	andcc	%o1, 7, %o3		! s2 dword aligned ?
850Sstevel@tonic-gate	or	%o5, %o4, %o5		! magic2<63:0>
860Sstevel@tonic-gate	bz,pn	%xcc, .s2aligned	! yup
870Sstevel@tonic-gate	sllx	%o5, 7, %o4		! magic1
880Sstevel@tonic-gate	sllx	%o3, 3, %g5		! leftshift = 8*ofs
890Sstevel@tonic-gate	orn	%g0, %g0, %g1		! all ones
900Sstevel@tonic-gate	and	%o1, -8, %o1		! round s1 down to next aligned dword
910Sstevel@tonic-gate	srlx	%g1, %g5, %g1		! mask for fixing up bytes
920Sstevel@tonic-gate	ldx	[%o1], %o0		! new lower dword in s2
930Sstevel@tonic-gate	orn	%o0, %g1, %o0 		! force start bytes to non-zero
940Sstevel@tonic-gate	sub	%g0, %g5, %g4		! rightshift = -(8*ofs) mod 64
950Sstevel@tonic-gate	sllx	%o0, %g5, %g1		! partial unaligned word from s2
960Sstevel@tonic-gate	add	%o2, %o3, %o2		! adjust pointers
970Sstevel@tonic-gate	nop				! align loop to 16-byte boundary
980Sstevel@tonic-gate	nop				! align loop to 16-byte boundary
990Sstevel@tonic-gate
1000Sstevel@tonic-gate.cmp:
1010Sstevel@tonic-gate	andn	%o4, %o0, %o3		! ~word & 0x80808080
1020Sstevel@tonic-gate	sub	%o0, %o5, %o0		! word - 0x01010101
1030Sstevel@tonic-gate	andcc	%o0, %o3, %g0		! (word - 0x01010101) & ~word & 0x80808080
1040Sstevel@tonic-gate	bz,a,pt	%xcc, .doload		! no null byte in previous word from s2
1050Sstevel@tonic-gate 	ldx	[%o1+8], %o0		! next aligned word in s2
1060Sstevel@tonic-gate.doload:
1070Sstevel@tonic-gate	srlx	%o0, %g4, %o3		! bytes from aligned word from s2
1080Sstevel@tonic-gate	or	%g1, %o3, %g1		! merge to get unaligned word from s2
1090Sstevel@tonic-gate	ldx	[%o1 + %o2], %o3	! word from s1
1100Sstevel@tonic-gate	cmp	%o3, %g1		! *s1 != *s2 ?
1110Sstevel@tonic-gate	bne,pn	%xcc, .wordsdiffer	! yup, find the byte that is different
1120Sstevel@tonic-gate	add	%o1, 8, %o1		! s1+=8, s2+=8
1130Sstevel@tonic-gate	andn	%o4, %o3, %g1		! ~word & 0x80808080
1140Sstevel@tonic-gate	sub	%o3, %o5, %o3		! word - 0x01010101
1150Sstevel@tonic-gate	andcc	%o3, %g1, %g0		! (word - 0x01010101) & ~word & 0x80808080
1160Sstevel@tonic-gate	bz,pt	%xcc, .cmp		! no null-byte in s1 yet
1170Sstevel@tonic-gate	sllx	%o0, %g5, %g1		! partial unaligned word from s2
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate	! words are equal but the end of s1 has been reached
1200Sstevel@tonic-gate	! this means the strings must be equal
1210Sstevel@tonic-gate.strequal:
1220Sstevel@tonic-gate	retl				! return from leaf function
1230Sstevel@tonic-gate	mov	%g0, %o0		! return 0, i.e. strings are equal
1240Sstevel@tonic-gate	nop
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate.s2aligned:
1270Sstevel@tonic-gate	ldx	[%o1 + %o2], %o3	! load word from s1
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate.cmpaligned:
1300Sstevel@tonic-gate	ldx	[%o1], %g1		! load word from s2
1310Sstevel@tonic-gate	cmp	%o3, %g1		! *scr1 == *src2 ?
1320Sstevel@tonic-gate	bne,pn	%xcc, .wordsdiffer	! nope, find mismatching character
1330Sstevel@tonic-gate	add	%o1, 8, %o1		! src1 += 8, src2 += 8
1340Sstevel@tonic-gate	andn	%o4, %o3, %o0		! ~word & 0x80808080
1350Sstevel@tonic-gate	sub	%o3, %o5, %o3		! word - 0x01010101
1360Sstevel@tonic-gate	andcc	%o3, %o0, %g0		! (word - 0x01010101) & ~word & 0x80808080
1370Sstevel@tonic-gate	bz,a,pt	%xcc, .cmpaligned	! no null-byte in s1 yet
1380Sstevel@tonic-gate	ldx	[%o1 + %o2], %o3	! load word from s1
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate	! words are equal but the end of s1 has been reached
1410Sstevel@tonic-gate	! this means the strings must be equal
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate.stringsequal:
1440Sstevel@tonic-gate	retl				! return from leaf function
1450Sstevel@tonic-gate	mov	%g0, %o0		! return 0, i.e. strings are equal
1460Sstevel@tonic-gate	nop				! align loop on 16-byte boundary
1470Sstevel@tonic-gate	nop				! align loop on 16-byte boundary
1480Sstevel@tonic-gate	nop				! align loop on 16-byte boundary
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate.wordsdiffer:
1510Sstevel@tonic-gate	mov	56, %o4			! initial shift count
1520Sstevel@tonic-gate	srlx	%g1, %o4, %o2		! first byte of mismatching word in s2
1530Sstevel@tonic-gate.cmpbytes:
1540Sstevel@tonic-gate	srlx	%o3, %o4, %o1		! first byte of mismatching word in s1
1550Sstevel@tonic-gate	subcc	%o1, %o2, %o0		! *s1-*s2
1560Sstevel@tonic-gate	bnz,pn	%xcc, .done		! bytes differ, return difference
1570Sstevel@tonic-gate	nop
1580Sstevel@tonic-gate	andcc	%o1, 0xff, %o0		! *s1 == 0 ?
1590Sstevel@tonic-gate	bz,pn	%xcc, .done		! yup, strings match
1600Sstevel@tonic-gate	subcc	%o4, 8, %o4		! shift_count -= 8
1610Sstevel@tonic-gate	bpos,pt	%xcc, .cmpbytes		! until all bytes processed
1620Sstevel@tonic-gate	srlx	%g1, %o4, %o2		! first byte of mismatching word in s2
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate.done:
1650Sstevel@tonic-gate	retl				! return from leaf routine
1660Sstevel@tonic-gate	nop				! padding
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate	SET_SIZE(strcmp)
169