xref: /illumos-gate/usr/src/lib/libc/sparcv9/gen/memcmp.S (revision 55fea89d)
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.
247257d1b4Sraf * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
279a70fc3bSMark J. Nelson	.file	"memcmp.s"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate/*
307c478bd9Sstevel@tonic-gate * memcmp(s1, s2, len)
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate * Compare n bytes:  s1>s2: >0  s1==s2: 0  s1<s2: <0
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate * Fast assembler language version of the following C-program for memcmp
357c478bd9Sstevel@tonic-gate * which represents the `standard' for the C-library.
367c478bd9Sstevel@tonic-gate *
377c478bd9Sstevel@tonic-gate *	int
387c478bd9Sstevel@tonic-gate *	memcmp(const void *s1, const void *s2, size_t n)
397c478bd9Sstevel@tonic-gate *	{
407c478bd9Sstevel@tonic-gate *		if (s1 != s2 && n != 0) {
417c478bd9Sstevel@tonic-gate *			const char *ps1 = s1;
427c478bd9Sstevel@tonic-gate *			const char *ps2 = s2;
437c478bd9Sstevel@tonic-gate *			do {
447c478bd9Sstevel@tonic-gate *				if (*ps1++ != *ps2++)
457c478bd9Sstevel@tonic-gate *					return (ps1[-1] - ps2[-1]);
467c478bd9Sstevel@tonic-gate *			} while (--n != 0);
477c478bd9Sstevel@tonic-gate *		}
487c478bd9Sstevel@tonic-gate *		return (0);
497c478bd9Sstevel@tonic-gate *	}
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(memcmp,function)
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate	ENTRY(memcmp)
577c478bd9Sstevel@tonic-gate	cmp	%o0, %o1		! s1 == s2?
587c478bd9Sstevel@tonic-gate	be,pn	%xcc, .cmpeq
597c478bd9Sstevel@tonic-gate	cmp	%o2, 17
607c478bd9Sstevel@tonic-gate	bleu,a,pn %xcc, .cmpbyt		! for small counts go do bytes
61*55fea89dSDan Cross	sub	%o1, %o0, %o1
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate	andcc	%o0, 3, %o3		! is s1 aligned?
647c478bd9Sstevel@tonic-gate	bz,a,pn	%icc, .iss2		! if so go check s2
657c478bd9Sstevel@tonic-gate	andcc	%o1, 3, %o4		! is s2 aligned?
667c478bd9Sstevel@tonic-gate	cmp	%o3, 2
677c478bd9Sstevel@tonic-gate	be,pn	%icc, .algn2
687c478bd9Sstevel@tonic-gate	cmp	%o3, 3
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate.algn1:	ldub	[%o0], %o4		! cmp one byte
717c478bd9Sstevel@tonic-gate	inc	%o0
727c478bd9Sstevel@tonic-gate	ldub	[%o1], %o5
737c478bd9Sstevel@tonic-gate	inc	%o1
747c478bd9Sstevel@tonic-gate	dec	%o2
757c478bd9Sstevel@tonic-gate	be,pn	%icc, .algn3
767c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
777c478bd9Sstevel@tonic-gate	be,pt	%icc, .algn2
787c478bd9Sstevel@tonic-gate	nop
797c478bd9Sstevel@tonic-gate	b,a	.noteq
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate.algn2:	lduh	[%o0], %o4
827c478bd9Sstevel@tonic-gate	inc	2, %o0
837c478bd9Sstevel@tonic-gate	ldub	[%o1], %o5
847c478bd9Sstevel@tonic-gate	inc	1, %o1
857c478bd9Sstevel@tonic-gate	srl	%o4, 8, %o3
867c478bd9Sstevel@tonic-gate	cmp	%o3, %o5
877c478bd9Sstevel@tonic-gate	be,a,pt	%icc, 1f
887c478bd9Sstevel@tonic-gate	ldub	[%o1], %o5		! delay slot, get next byte from s2
897c478bd9Sstevel@tonic-gate	b	.noteq
907c478bd9Sstevel@tonic-gate	mov	%o3, %o4		! delay slot, move *s1 to %o4
917c478bd9Sstevel@tonic-gate1:	inc	%o1
927c478bd9Sstevel@tonic-gate	dec	2, %o2
937c478bd9Sstevel@tonic-gate	and	%o4, 0xff, %o4
947c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
957c478bd9Sstevel@tonic-gate.algn3:	be,a,pt	%icc, .iss2
967c478bd9Sstevel@tonic-gate	andcc	%o1, 3, %o4		! delay slot, is s2 aligned?
977c478bd9Sstevel@tonic-gate	b,a	.noteq
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate.cmpbyt:b	.bytcmp
1007c478bd9Sstevel@tonic-gate	deccc	%o2
1017c478bd9Sstevel@tonic-gate1:	ldub	[%o0 + %o1], %o5	! byte compare loop
1027c478bd9Sstevel@tonic-gate	inc	%o0
1037c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
1047c478bd9Sstevel@tonic-gate	be,a,pt	%icc, .bytcmp
1057c478bd9Sstevel@tonic-gate	deccc	%o2			! delay slot, compare count (len)
1067c478bd9Sstevel@tonic-gate	b,a	.noteq
1077c478bd9Sstevel@tonic-gate.bytcmp:bgeu,a,pt %xcc, 1b
1087c478bd9Sstevel@tonic-gate	ldub	[%o0], %o4
109*55fea89dSDan Cross.cmpeq:
1107c478bd9Sstevel@tonic-gate	retl				! strings compare equal
1117c478bd9Sstevel@tonic-gate	clr	%o0
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate.noteq_word:				! words aren't equal. find unequal byte
1147c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o1		! first byte
1157c478bd9Sstevel@tonic-gate	srl	%o5, 24, %o2
1167c478bd9Sstevel@tonic-gate	cmp	%o1, %o2
1177c478bd9Sstevel@tonic-gate	bne,pn	%icc, 1f
1187c478bd9Sstevel@tonic-gate	sll	%o4, 8, %o4
1197c478bd9Sstevel@tonic-gate	sll	%o5, 8, %o5
1207c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o1
1217c478bd9Sstevel@tonic-gate	srl	%o5, 24, %o2
1227c478bd9Sstevel@tonic-gate	cmp	%o1, %o2
1237c478bd9Sstevel@tonic-gate	bne,pn	%icc, 1f
1247c478bd9Sstevel@tonic-gate	sll	%o4, 8, %o4
1257c478bd9Sstevel@tonic-gate	sll	%o5, 8, %o5
1267c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o1
1277c478bd9Sstevel@tonic-gate	srl	%o5, 24, %o2
1287c478bd9Sstevel@tonic-gate	cmp	%o1, %o2
1297c478bd9Sstevel@tonic-gate	bne,pn	%icc, 1f
1307c478bd9Sstevel@tonic-gate	sll	%o4, 8, %o4
1317c478bd9Sstevel@tonic-gate	sll	%o5, 8, %o5
1327c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o1
1337c478bd9Sstevel@tonic-gate	srl	%o5, 24, %o2
1347c478bd9Sstevel@tonic-gate1:
1357c478bd9Sstevel@tonic-gate	retl
1367c478bd9Sstevel@tonic-gate	sub	%o1, %o2, %o0		! delay slot
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate.noteq:
1397c478bd9Sstevel@tonic-gate	retl				! strings aren't equal
1407c478bd9Sstevel@tonic-gate	sub	%o4, %o5, %o0		! delay slot, return(*s1 - *s2)
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate.iss2:	andn	%o2, 3, %o3		! count of aligned bytes
1437c478bd9Sstevel@tonic-gate	and	%o2, 3, %o2		! remaining bytes
1447c478bd9Sstevel@tonic-gate	bz,pn	%icc, .w4cmp		! if s2 word aligned, compare words
1457c478bd9Sstevel@tonic-gate	cmp	%o4, 2
1467c478bd9Sstevel@tonic-gate	be,pn	%icc, .w2cmp		! s2 half aligned
147*55fea89dSDan Cross	cmp	%o4, 1
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate.w3cmp:
1507c478bd9Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond the last byte
1517c478bd9Sstevel@tonic-gate	inc	4, %o2
1527c478bd9Sstevel@tonic-gate	ldub	[%o1], %g1		! read a byte to align for word reads
153*55fea89dSDan Cross	inc	1, %o1
1547c478bd9Sstevel@tonic-gate	be,pt	%icc, .w1cmp		! aligned to 1 or 3 bytes
1557c478bd9Sstevel@tonic-gate	sll	%g1, 24, %o5
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o1
1587c478bd9Sstevel@tonic-gate2:	lduw	[%o0 + %o1], %g1
1597c478bd9Sstevel@tonic-gate	lduw	[%o0], %o4
1607c478bd9Sstevel@tonic-gate	inc	4, %o0
1617c478bd9Sstevel@tonic-gate	srl	%g1, 8, %g5		! merge with the other half
1627c478bd9Sstevel@tonic-gate	or	%g5, %o5, %o5
1637c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
1647c478bd9Sstevel@tonic-gate	bne,pt	%icc, .noteq_word
1657c478bd9Sstevel@tonic-gate	deccc	4, %o3
1667c478bd9Sstevel@tonic-gate	bnz,pt	%xcc, 2b
1677c478bd9Sstevel@tonic-gate	sll	%g1, 24, %o5
1687c478bd9Sstevel@tonic-gate	sub	%o1, 1, %o1		! used 3 bytes of the last word read
1697c478bd9Sstevel@tonic-gate	b	.bytcmp
1707c478bd9Sstevel@tonic-gate	deccc	%o2
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate.w1cmp:
1737c478bd9Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond the last byte
1747c478bd9Sstevel@tonic-gate	inc	4, %o2
1757c478bd9Sstevel@tonic-gate	lduh	[%o1], %g1		! read 3 bytes to word align
1767c478bd9Sstevel@tonic-gate	inc	2, %o1
1777c478bd9Sstevel@tonic-gate	sll	%g1, 8, %g5
1787c478bd9Sstevel@tonic-gate	or	%o5, %g5, %o5
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o1
1817c478bd9Sstevel@tonic-gate3:	lduw	[%o0 + %o1], %g1
1827c478bd9Sstevel@tonic-gate	lduw	[%o0], %o4
1837c478bd9Sstevel@tonic-gate	inc	4, %o0
1847c478bd9Sstevel@tonic-gate	srl	%g1, 24, %g5		! merge with the other half
1857c478bd9Sstevel@tonic-gate	or	%g5, %o5, %o5
1867c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
1877c478bd9Sstevel@tonic-gate	bne,pt	%icc, .noteq_word
1887c478bd9Sstevel@tonic-gate	deccc	4, %o3
1897c478bd9Sstevel@tonic-gate	bnz,pt	%xcc, 3b
1907c478bd9Sstevel@tonic-gate	sll	%g1, 8, %o5
1917c478bd9Sstevel@tonic-gate	sub	%o1, 3, %o1		! used 1 byte of the last word read
1927c478bd9Sstevel@tonic-gate	b	.bytcmp
1937c478bd9Sstevel@tonic-gate	deccc	%o2
194*55fea89dSDan Cross
1957c478bd9Sstevel@tonic-gate.w2cmp:
1967c478bd9Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond the last byte
1977c478bd9Sstevel@tonic-gate	inc	4, %o2
1987c478bd9Sstevel@tonic-gate	lduh	[%o1], %g1		! read a halfword to align s2
199*55fea89dSDan Cross	inc	2, %o1
2007c478bd9Sstevel@tonic-gate	sll	%g1, 16, %o5
2017c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o1
2027c478bd9Sstevel@tonic-gate4:	lduw	[%o0 + %o1], %g1	! read a word from s2
2037c478bd9Sstevel@tonic-gate	lduw	[%o0], %o4		! read a word from s1
2047c478bd9Sstevel@tonic-gate	inc	4, %o0
2057c478bd9Sstevel@tonic-gate	srl	%g1, 16, %g5		! merge with the other half
2067c478bd9Sstevel@tonic-gate	or	%g5, %o5, %o5
2077c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
2087c478bd9Sstevel@tonic-gate	bne,pn	%icc, .noteq_word
2097c478bd9Sstevel@tonic-gate	deccc	4, %o3
2107c478bd9Sstevel@tonic-gate	bnz,pt	%xcc, 4b
2117c478bd9Sstevel@tonic-gate	sll	%g1, 16, %o5
2127c478bd9Sstevel@tonic-gate	sub	%o1, 2, %o1		! only used half of the last read word
2137c478bd9Sstevel@tonic-gate	b	.bytcmp
2147c478bd9Sstevel@tonic-gate	deccc	%o2
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate.w4cmp:
2177c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o1
2187c478bd9Sstevel@tonic-gate	lduw	[%o0 + %o1], %o5
2197c478bd9Sstevel@tonic-gate5:	lduw	[%o0], %o4
2207c478bd9Sstevel@tonic-gate	inc	4, %o0
2217c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
2227c478bd9Sstevel@tonic-gate	bne,pt	%icc, .noteq_word
2237c478bd9Sstevel@tonic-gate	deccc	4, %o3
2247c478bd9Sstevel@tonic-gate	bnz,a,pt %xcc, 5b
2257c478bd9Sstevel@tonic-gate	lduw	[%o0 + %o1], %o5
2267c478bd9Sstevel@tonic-gate	b	.bytcmp			! compare remaining bytes, if any
2277c478bd9Sstevel@tonic-gate	deccc	%o2
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate	SET_SIZE(memcmp)
230