xref: /onnv-gate/usr/src/lib/libc/sparcv9/gen/memcmp.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.
246812Sraf * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*7298SMark.J.Nelson@Sun.COM	.file	"memcmp.s"
280Sstevel@tonic-gate
290Sstevel@tonic-gate/*
300Sstevel@tonic-gate * memcmp(s1, s2, len)
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * Compare n bytes:  s1>s2: >0  s1==s2: 0  s1<s2: <0
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * Fast assembler language version of the following C-program for memcmp
350Sstevel@tonic-gate * which represents the `standard' for the C-library.
360Sstevel@tonic-gate *
370Sstevel@tonic-gate *	int
380Sstevel@tonic-gate *	memcmp(const void *s1, const void *s2, size_t n)
390Sstevel@tonic-gate *	{
400Sstevel@tonic-gate *		if (s1 != s2 && n != 0) {
410Sstevel@tonic-gate *			const char *ps1 = s1;
420Sstevel@tonic-gate *			const char *ps2 = s2;
430Sstevel@tonic-gate *			do {
440Sstevel@tonic-gate *				if (*ps1++ != *ps2++)
450Sstevel@tonic-gate *					return (ps1[-1] - ps2[-1]);
460Sstevel@tonic-gate *			} while (--n != 0);
470Sstevel@tonic-gate *		}
480Sstevel@tonic-gate *		return (0);
490Sstevel@tonic-gate *	}
500Sstevel@tonic-gate */
510Sstevel@tonic-gate
520Sstevel@tonic-gate#include <sys/asm_linkage.h>
530Sstevel@tonic-gate
540Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(memcmp,function)
550Sstevel@tonic-gate
560Sstevel@tonic-gate	ENTRY(memcmp)
570Sstevel@tonic-gate	cmp	%o0, %o1		! s1 == s2?
580Sstevel@tonic-gate	be,pn	%xcc, .cmpeq
590Sstevel@tonic-gate	cmp	%o2, 17
600Sstevel@tonic-gate	bleu,a,pn %xcc, .cmpbyt		! for small counts go do bytes
610Sstevel@tonic-gate	sub	%o1, %o0, %o1
620Sstevel@tonic-gate
630Sstevel@tonic-gate	andcc	%o0, 3, %o3		! is s1 aligned?
640Sstevel@tonic-gate	bz,a,pn	%icc, .iss2		! if so go check s2
650Sstevel@tonic-gate	andcc	%o1, 3, %o4		! is s2 aligned?
660Sstevel@tonic-gate	cmp	%o3, 2
670Sstevel@tonic-gate	be,pn	%icc, .algn2
680Sstevel@tonic-gate	cmp	%o3, 3
690Sstevel@tonic-gate
700Sstevel@tonic-gate.algn1:	ldub	[%o0], %o4		! cmp one byte
710Sstevel@tonic-gate	inc	%o0
720Sstevel@tonic-gate	ldub	[%o1], %o5
730Sstevel@tonic-gate	inc	%o1
740Sstevel@tonic-gate	dec	%o2
750Sstevel@tonic-gate	be,pn	%icc, .algn3
760Sstevel@tonic-gate	cmp	%o4, %o5
770Sstevel@tonic-gate	be,pt	%icc, .algn2
780Sstevel@tonic-gate	nop
790Sstevel@tonic-gate	b,a	.noteq
800Sstevel@tonic-gate
810Sstevel@tonic-gate.algn2:	lduh	[%o0], %o4
820Sstevel@tonic-gate	inc	2, %o0
830Sstevel@tonic-gate	ldub	[%o1], %o5
840Sstevel@tonic-gate	inc	1, %o1
850Sstevel@tonic-gate	srl	%o4, 8, %o3
860Sstevel@tonic-gate	cmp	%o3, %o5
870Sstevel@tonic-gate	be,a,pt	%icc, 1f
880Sstevel@tonic-gate	ldub	[%o1], %o5		! delay slot, get next byte from s2
890Sstevel@tonic-gate	b	.noteq
900Sstevel@tonic-gate	mov	%o3, %o4		! delay slot, move *s1 to %o4
910Sstevel@tonic-gate1:	inc	%o1
920Sstevel@tonic-gate	dec	2, %o2
930Sstevel@tonic-gate	and	%o4, 0xff, %o4
940Sstevel@tonic-gate	cmp	%o4, %o5
950Sstevel@tonic-gate.algn3:	be,a,pt	%icc, .iss2
960Sstevel@tonic-gate	andcc	%o1, 3, %o4		! delay slot, is s2 aligned?
970Sstevel@tonic-gate	b,a	.noteq
980Sstevel@tonic-gate
990Sstevel@tonic-gate.cmpbyt:b	.bytcmp
1000Sstevel@tonic-gate	deccc	%o2
1010Sstevel@tonic-gate1:	ldub	[%o0 + %o1], %o5	! byte compare loop
1020Sstevel@tonic-gate	inc	%o0
1030Sstevel@tonic-gate	cmp	%o4, %o5
1040Sstevel@tonic-gate	be,a,pt	%icc, .bytcmp
1050Sstevel@tonic-gate	deccc	%o2			! delay slot, compare count (len)
1060Sstevel@tonic-gate	b,a	.noteq
1070Sstevel@tonic-gate.bytcmp:bgeu,a,pt %xcc, 1b
1080Sstevel@tonic-gate	ldub	[%o0], %o4
1090Sstevel@tonic-gate.cmpeq:
1100Sstevel@tonic-gate	retl				! strings compare equal
1110Sstevel@tonic-gate	clr	%o0
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate.noteq_word:				! words aren't equal. find unequal byte
1140Sstevel@tonic-gate	srl	%o4, 24, %o1		! first byte
1150Sstevel@tonic-gate	srl	%o5, 24, %o2
1160Sstevel@tonic-gate	cmp	%o1, %o2
1170Sstevel@tonic-gate	bne,pn	%icc, 1f
1180Sstevel@tonic-gate	sll	%o4, 8, %o4
1190Sstevel@tonic-gate	sll	%o5, 8, %o5
1200Sstevel@tonic-gate	srl	%o4, 24, %o1
1210Sstevel@tonic-gate	srl	%o5, 24, %o2
1220Sstevel@tonic-gate	cmp	%o1, %o2
1230Sstevel@tonic-gate	bne,pn	%icc, 1f
1240Sstevel@tonic-gate	sll	%o4, 8, %o4
1250Sstevel@tonic-gate	sll	%o5, 8, %o5
1260Sstevel@tonic-gate	srl	%o4, 24, %o1
1270Sstevel@tonic-gate	srl	%o5, 24, %o2
1280Sstevel@tonic-gate	cmp	%o1, %o2
1290Sstevel@tonic-gate	bne,pn	%icc, 1f
1300Sstevel@tonic-gate	sll	%o4, 8, %o4
1310Sstevel@tonic-gate	sll	%o5, 8, %o5
1320Sstevel@tonic-gate	srl	%o4, 24, %o1
1330Sstevel@tonic-gate	srl	%o5, 24, %o2
1340Sstevel@tonic-gate1:
1350Sstevel@tonic-gate	retl
1360Sstevel@tonic-gate	sub	%o1, %o2, %o0		! delay slot
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate.noteq:
1390Sstevel@tonic-gate	retl				! strings aren't equal
1400Sstevel@tonic-gate	sub	%o4, %o5, %o0		! delay slot, return(*s1 - *s2)
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate.iss2:	andn	%o2, 3, %o3		! count of aligned bytes
1430Sstevel@tonic-gate	and	%o2, 3, %o2		! remaining bytes
1440Sstevel@tonic-gate	bz,pn	%icc, .w4cmp		! if s2 word aligned, compare words
1450Sstevel@tonic-gate	cmp	%o4, 2
1460Sstevel@tonic-gate	be,pn	%icc, .w2cmp		! s2 half aligned
1470Sstevel@tonic-gate	cmp	%o4, 1
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate.w3cmp:
1500Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond the last byte
1510Sstevel@tonic-gate	inc	4, %o2
1520Sstevel@tonic-gate	ldub	[%o1], %g1		! read a byte to align for word reads
1530Sstevel@tonic-gate	inc	1, %o1
1540Sstevel@tonic-gate	be,pt	%icc, .w1cmp		! aligned to 1 or 3 bytes
1550Sstevel@tonic-gate	sll	%g1, 24, %o5
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate	sub	%o1, %o0, %o1
1580Sstevel@tonic-gate2:	lduw	[%o0 + %o1], %g1
1590Sstevel@tonic-gate	lduw	[%o0], %o4
1600Sstevel@tonic-gate	inc	4, %o0
1610Sstevel@tonic-gate	srl	%g1, 8, %g5		! merge with the other half
1620Sstevel@tonic-gate	or	%g5, %o5, %o5
1630Sstevel@tonic-gate	cmp	%o4, %o5
1640Sstevel@tonic-gate	bne,pt	%icc, .noteq_word
1650Sstevel@tonic-gate	deccc	4, %o3
1660Sstevel@tonic-gate	bnz,pt	%xcc, 2b
1670Sstevel@tonic-gate	sll	%g1, 24, %o5
1680Sstevel@tonic-gate	sub	%o1, 1, %o1		! used 3 bytes of the last word read
1690Sstevel@tonic-gate	b	.bytcmp
1700Sstevel@tonic-gate	deccc	%o2
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate.w1cmp:
1730Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond the last byte
1740Sstevel@tonic-gate	inc	4, %o2
1750Sstevel@tonic-gate	lduh	[%o1], %g1		! read 3 bytes to word align
1760Sstevel@tonic-gate	inc	2, %o1
1770Sstevel@tonic-gate	sll	%g1, 8, %g5
1780Sstevel@tonic-gate	or	%o5, %g5, %o5
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate	sub	%o1, %o0, %o1
1810Sstevel@tonic-gate3:	lduw	[%o0 + %o1], %g1
1820Sstevel@tonic-gate	lduw	[%o0], %o4
1830Sstevel@tonic-gate	inc	4, %o0
1840Sstevel@tonic-gate	srl	%g1, 24, %g5		! merge with the other half
1850Sstevel@tonic-gate	or	%g5, %o5, %o5
1860Sstevel@tonic-gate	cmp	%o4, %o5
1870Sstevel@tonic-gate	bne,pt	%icc, .noteq_word
1880Sstevel@tonic-gate	deccc	4, %o3
1890Sstevel@tonic-gate	bnz,pt	%xcc, 3b
1900Sstevel@tonic-gate	sll	%g1, 8, %o5
1910Sstevel@tonic-gate	sub	%o1, 3, %o1		! used 1 byte of the last word read
1920Sstevel@tonic-gate	b	.bytcmp
1930Sstevel@tonic-gate	deccc	%o2
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate.w2cmp:
1960Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond the last byte
1970Sstevel@tonic-gate	inc	4, %o2
1980Sstevel@tonic-gate	lduh	[%o1], %g1		! read a halfword to align s2
1990Sstevel@tonic-gate	inc	2, %o1
2000Sstevel@tonic-gate	sll	%g1, 16, %o5
2010Sstevel@tonic-gate	sub	%o1, %o0, %o1
2020Sstevel@tonic-gate4:	lduw	[%o0 + %o1], %g1	! read a word from s2
2030Sstevel@tonic-gate	lduw	[%o0], %o4		! read a word from s1
2040Sstevel@tonic-gate	inc	4, %o0
2050Sstevel@tonic-gate	srl	%g1, 16, %g5		! merge with the other half
2060Sstevel@tonic-gate	or	%g5, %o5, %o5
2070Sstevel@tonic-gate	cmp	%o4, %o5
2080Sstevel@tonic-gate	bne,pn	%icc, .noteq_word
2090Sstevel@tonic-gate	deccc	4, %o3
2100Sstevel@tonic-gate	bnz,pt	%xcc, 4b
2110Sstevel@tonic-gate	sll	%g1, 16, %o5
2120Sstevel@tonic-gate	sub	%o1, 2, %o1		! only used half of the last read word
2130Sstevel@tonic-gate	b	.bytcmp
2140Sstevel@tonic-gate	deccc	%o2
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate.w4cmp:
2170Sstevel@tonic-gate	sub	%o1, %o0, %o1
2180Sstevel@tonic-gate	lduw	[%o0 + %o1], %o5
2190Sstevel@tonic-gate5:	lduw	[%o0], %o4
2200Sstevel@tonic-gate	inc	4, %o0
2210Sstevel@tonic-gate	cmp	%o4, %o5
2220Sstevel@tonic-gate	bne,pt	%icc, .noteq_word
2230Sstevel@tonic-gate	deccc	4, %o3
2240Sstevel@tonic-gate	bnz,a,pt %xcc, 5b
2250Sstevel@tonic-gate	lduw	[%o0 + %o1], %o5
2260Sstevel@tonic-gate	b	.bytcmp			! compare remaining bytes, if any
2270Sstevel@tonic-gate	deccc	%o2
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate	SET_SIZE(memcmp)
230