xref: /titanic_52/usr/src/lib/libc/sparc/gen/memcmp.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.
247257d1b4Sraf * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*9a70fc3bSMark 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 (NULL);
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	st	%g2, [%sp + 68]		! g2 must be restored before retl
587c478bd9Sstevel@tonic-gate	cmp	%o0, %o1		! s1 == s2?
597c478bd9Sstevel@tonic-gate	be	.cmpeq
607c478bd9Sstevel@tonic-gate	cmp	%o2, 17
617c478bd9Sstevel@tonic-gate	bleu,a	.cmpbyt			! for small counts go do bytes
627c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o1
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate	andcc	%o0, 3, %o3		! is s1 aligned?
657c478bd9Sstevel@tonic-gate	bz,a	.iss2			! if so go check s2
667c478bd9Sstevel@tonic-gate	andcc	%o1, 3, %o4		! is s2 aligned?
677c478bd9Sstevel@tonic-gate	cmp	%o3, 2
687c478bd9Sstevel@tonic-gate	be	.algn2
697c478bd9Sstevel@tonic-gate	cmp	%o3, 3
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate.algn1:	ldub	[%o0], %o4		! cmp one byte
727c478bd9Sstevel@tonic-gate	inc	%o0
737c478bd9Sstevel@tonic-gate	ldub	[%o1], %o5
747c478bd9Sstevel@tonic-gate	inc	%o1
757c478bd9Sstevel@tonic-gate	dec	%o2
767c478bd9Sstevel@tonic-gate	be	.algn3
777c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
787c478bd9Sstevel@tonic-gate	be	.algn2
797c478bd9Sstevel@tonic-gate	nop
807c478bd9Sstevel@tonic-gate	b,a	.noteq
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate.algn2:	lduh	[%o0], %o4
837c478bd9Sstevel@tonic-gate	inc	2, %o0
847c478bd9Sstevel@tonic-gate	ldub	[%o1], %o5
857c478bd9Sstevel@tonic-gate	inc	1, %o1
867c478bd9Sstevel@tonic-gate	srl	%o4, 8, %o3
877c478bd9Sstevel@tonic-gate	cmp	%o3, %o5
887c478bd9Sstevel@tonic-gate	be,a	1f
897c478bd9Sstevel@tonic-gate	ldub	[%o1], %o5		! delay slot, get next byte from s2
907c478bd9Sstevel@tonic-gate	b	.noteq
917c478bd9Sstevel@tonic-gate	mov	%o3, %o4		! delay slot, move *s1 to %o4
927c478bd9Sstevel@tonic-gate1:	inc	%o1
937c478bd9Sstevel@tonic-gate	dec	2, %o2
947c478bd9Sstevel@tonic-gate	and	%o4, 0xff, %o4
957c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
967c478bd9Sstevel@tonic-gate.algn3:	be,a	.iss2
977c478bd9Sstevel@tonic-gate	andcc	%o1, 3, %o4		! delay slot, is s2 aligned?
987c478bd9Sstevel@tonic-gate	b,a	.noteq
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate.cmpbyt:b	.bytcmp
1017c478bd9Sstevel@tonic-gate	deccc	%o2
1027c478bd9Sstevel@tonic-gate1:	ldub	[%o0 + %o1], %o5	! byte compare loop
1037c478bd9Sstevel@tonic-gate	inc	%o0
1047c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
1057c478bd9Sstevel@tonic-gate	be,a	.bytcmp
1067c478bd9Sstevel@tonic-gate	deccc	%o2			! delay slot, compare count (len)
1077c478bd9Sstevel@tonic-gate	b,a	.noteq
1087c478bd9Sstevel@tonic-gate.bytcmp:bgeu,a	1b
1097c478bd9Sstevel@tonic-gate	ldub	[%o0], %o4
1107c478bd9Sstevel@tonic-gate.cmpeq:	ld	[%sp + 68], %g2
1117c478bd9Sstevel@tonic-gate	retl				! strings compare equal
1127c478bd9Sstevel@tonic-gate	clr	%o0
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate.noteq_word:				! words aren't equal. find unequal byte
1157c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o1		! first byte
1167c478bd9Sstevel@tonic-gate	srl	%o5, 24, %o2
1177c478bd9Sstevel@tonic-gate	cmp	%o1, %o2
1187c478bd9Sstevel@tonic-gate	bne	1f
1197c478bd9Sstevel@tonic-gate	sll	%o4, 8, %o4
1207c478bd9Sstevel@tonic-gate	sll	%o5, 8, %o5
1217c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o1
1227c478bd9Sstevel@tonic-gate	srl	%o5, 24, %o2
1237c478bd9Sstevel@tonic-gate	cmp	%o1, %o2
1247c478bd9Sstevel@tonic-gate	bne	1f
1257c478bd9Sstevel@tonic-gate	sll	%o4, 8, %o4
1267c478bd9Sstevel@tonic-gate	sll	%o5, 8, %o5
1277c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o1
1287c478bd9Sstevel@tonic-gate	srl	%o5, 24, %o2
1297c478bd9Sstevel@tonic-gate	cmp	%o1, %o2
1307c478bd9Sstevel@tonic-gate	bne	1f
1317c478bd9Sstevel@tonic-gate	sll	%o4, 8, %o4
1327c478bd9Sstevel@tonic-gate	sll	%o5, 8, %o5
1337c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o1
1347c478bd9Sstevel@tonic-gate	srl	%o5, 24, %o2
1357c478bd9Sstevel@tonic-gate1:
1367c478bd9Sstevel@tonic-gate	ld	[%sp + 68], %g2
1377c478bd9Sstevel@tonic-gate	retl
1387c478bd9Sstevel@tonic-gate	sub	%o1, %o2, %o0		! delay slot
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate.noteq:
1417c478bd9Sstevel@tonic-gate	ld	[%sp + 68], %g2
1427c478bd9Sstevel@tonic-gate	retl				! strings aren't equal
1437c478bd9Sstevel@tonic-gate	sub	%o4, %o5, %o0		! delay slot, return(*s1 - *s2)
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate.iss2:	andn	%o2, 3, %o3		! count of aligned bytes
1467c478bd9Sstevel@tonic-gate	and	%o2, 3, %o2		! remaining bytes
1477c478bd9Sstevel@tonic-gate	bz	.w4cmp			! if s2 word aligned, compare words
1487c478bd9Sstevel@tonic-gate	cmp	%o4, 2
1497c478bd9Sstevel@tonic-gate	be	.w2cmp			! s2 half aligned
1507c478bd9Sstevel@tonic-gate	cmp	%o4, 1
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate.w3cmp:
1537c478bd9Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond the last byte
1547c478bd9Sstevel@tonic-gate	inc	4, %o2
1557c478bd9Sstevel@tonic-gate	ldub	[%o1], %g1		! read a byte to align for word reads
1567c478bd9Sstevel@tonic-gate	inc	1, %o1
1577c478bd9Sstevel@tonic-gate	be	.w1cmp			! aligned to 1 or 3 bytes
1587c478bd9Sstevel@tonic-gate	sll	%g1, 24, %o5
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o1
1617c478bd9Sstevel@tonic-gate2:	ld	[%o0 + %o1], %g1
1627c478bd9Sstevel@tonic-gate	ld	[%o0], %o4
1637c478bd9Sstevel@tonic-gate	inc	4, %o0
1647c478bd9Sstevel@tonic-gate	srl	%g1, 8, %g2		! merge with the other half
1657c478bd9Sstevel@tonic-gate	or	%g2, %o5, %o5
1667c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
1677c478bd9Sstevel@tonic-gate	bne	.noteq_word
1687c478bd9Sstevel@tonic-gate	deccc	4, %o3
1697c478bd9Sstevel@tonic-gate	bnz	2b
1707c478bd9Sstevel@tonic-gate	sll	%g1, 24, %o5
1717c478bd9Sstevel@tonic-gate	sub	%o1, 1, %o1		! used 3 bytes of the last word read
1727c478bd9Sstevel@tonic-gate	b	.bytcmp
1737c478bd9Sstevel@tonic-gate	deccc	%o2
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate.w1cmp:
1767c478bd9Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond the last byte
1777c478bd9Sstevel@tonic-gate	inc	4, %o2
1787c478bd9Sstevel@tonic-gate	lduh	[%o1], %g1		! read 3 bytes to word align
1797c478bd9Sstevel@tonic-gate	inc	2, %o1
1807c478bd9Sstevel@tonic-gate	sll	%g1, 8, %g2
1817c478bd9Sstevel@tonic-gate	or	%o5, %g2, %o5
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o1
1847c478bd9Sstevel@tonic-gate3:	ld	[%o0 + %o1], %g1
1857c478bd9Sstevel@tonic-gate	ld	[%o0], %o4
1867c478bd9Sstevel@tonic-gate	inc	4, %o0
1877c478bd9Sstevel@tonic-gate	srl	%g1, 24, %g2		! merge with the other half
1887c478bd9Sstevel@tonic-gate	or	%g2, %o5, %o5
1897c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
1907c478bd9Sstevel@tonic-gate	bne	.noteq_word
1917c478bd9Sstevel@tonic-gate	deccc	4, %o3
1927c478bd9Sstevel@tonic-gate	bnz	3b
1937c478bd9Sstevel@tonic-gate	sll	%g1, 8, %o5
1947c478bd9Sstevel@tonic-gate	sub	%o1, 3, %o1		! used 1 byte of the last word read
1957c478bd9Sstevel@tonic-gate	b	.bytcmp
1967c478bd9Sstevel@tonic-gate	deccc	%o2
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate.w2cmp:
1997c478bd9Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond the last byte
2007c478bd9Sstevel@tonic-gate	inc	4, %o2
2017c478bd9Sstevel@tonic-gate	lduh	[%o1], %g1		! read a halfword to align s2
2027c478bd9Sstevel@tonic-gate	inc	2, %o1
2037c478bd9Sstevel@tonic-gate	sll	%g1, 16, %o5
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o1
2067c478bd9Sstevel@tonic-gate4:	ld	[%o0 + %o1], %g1	! read a word from s2
2077c478bd9Sstevel@tonic-gate	ld	[%o0], %o4		! read a word from s1
2087c478bd9Sstevel@tonic-gate	inc	4, %o0
2097c478bd9Sstevel@tonic-gate	srl	%g1, 16, %g2		! merge with the other half
2107c478bd9Sstevel@tonic-gate	or	%g2, %o5, %o5
2117c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
2127c478bd9Sstevel@tonic-gate	bne	.noteq_word
2137c478bd9Sstevel@tonic-gate	deccc	4, %o3
2147c478bd9Sstevel@tonic-gate	bnz	4b
2157c478bd9Sstevel@tonic-gate	sll	%g1, 16, %o5
2167c478bd9Sstevel@tonic-gate	sub	%o1, 2, %o1		! only used half of the last read word
2177c478bd9Sstevel@tonic-gate	b	.bytcmp
2187c478bd9Sstevel@tonic-gate	deccc	%o2
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate.w4cmp:
2217c478bd9Sstevel@tonic-gate	sub	%o1, %o0, %o1
2227c478bd9Sstevel@tonic-gate	ld	[%o0 + %o1], %o5
2237c478bd9Sstevel@tonic-gate5:	ld	[%o0], %o4
2247c478bd9Sstevel@tonic-gate	inc	4, %o0
2257c478bd9Sstevel@tonic-gate	cmp	%o4, %o5
2267c478bd9Sstevel@tonic-gate	bne	.noteq_word
2277c478bd9Sstevel@tonic-gate	deccc	4, %o3
2287c478bd9Sstevel@tonic-gate	bnz,a	5b
2297c478bd9Sstevel@tonic-gate	ld	[%o0 + %o1], %o5
2307c478bd9Sstevel@tonic-gate	b	.bytcmp			! compare remaining bytes, if any
2317c478bd9Sstevel@tonic-gate	deccc	%o2
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate	SET_SIZE(memcmp)
234