xref: /titanic_41/usr/src/lib/libc/sparcv9/gen/memcpy.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
58cd45542Sraf * Common Development and Distribution License (the "License").
68cd45542Sraf * 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 */
218cd45542Sraf
227c478bd9Sstevel@tonic-gate/*
238cd45542Sraf * 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	"memcpy.s"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate/*
307c478bd9Sstevel@tonic-gate * memcpy(s1, s2, len)
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate * Copy s2 to s1, always copy n bytes.
337c478bd9Sstevel@tonic-gate * Note: this does not work for overlapped copies, bcopy() does
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * Added entry __align_cpy_1 is generally for use of the compilers.
367c478bd9Sstevel@tonic-gate *
377c478bd9Sstevel@tonic-gate *
387c478bd9Sstevel@tonic-gate * Fast assembler language version of the following C-program for memcpy
397c478bd9Sstevel@tonic-gate * which represents the `standard' for the C-library.
407c478bd9Sstevel@tonic-gate *
417c478bd9Sstevel@tonic-gate *	void *
427c478bd9Sstevel@tonic-gate *	memcpy(void *s, const void *s0, size_t n)
437c478bd9Sstevel@tonic-gate *	{
447c478bd9Sstevel@tonic-gate *		if (n != 0) {
457c478bd9Sstevel@tonic-gate *			char *s1 = s;
467c478bd9Sstevel@tonic-gate *			const char *s2 = s0;
477c478bd9Sstevel@tonic-gate *			do {
487c478bd9Sstevel@tonic-gate *				*s1++ = *s2++;
497c478bd9Sstevel@tonic-gate *			} while (--n != 0);
507c478bd9Sstevel@tonic-gate *		}
517c478bd9Sstevel@tonic-gate *		return (s);
527c478bd9Sstevel@tonic-gate *	}
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(memcpy,function)
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate	ENTRY(memcpy)
607c478bd9Sstevel@tonic-gate	ENTRY(__align_cpy_1)
617c478bd9Sstevel@tonic-gate	mov	%o0, %g5		! save des address for return val
627c478bd9Sstevel@tonic-gate	cmp	%o2, 17			! for small counts copy bytes
637c478bd9Sstevel@tonic-gate	bleu,pn	%xcc, .dbytecp
647c478bd9Sstevel@tonic-gate	andcc	%o1, 3, %o5		! is src word aligned
657c478bd9Sstevel@tonic-gate	bz,pn	%icc, .aldst
667c478bd9Sstevel@tonic-gate	cmp	%o5, 2			! is src half-word aligned
677c478bd9Sstevel@tonic-gate	be,pt	%xcc, .s2algn
687c478bd9Sstevel@tonic-gate	cmp	%o5, 3			! src is byte aligned
697c478bd9Sstevel@tonic-gate.s1algn:ldub	[%o1], %o3		! move 1 or 3 bytes to align it
707c478bd9Sstevel@tonic-gate	inc	1, %o1
717c478bd9Sstevel@tonic-gate	stb	%o3, [%g5]		! move a byte to align src
727c478bd9Sstevel@tonic-gate	inc	1, %g5
737c478bd9Sstevel@tonic-gate	bne,pt	%icc, .s2algn
747c478bd9Sstevel@tonic-gate	dec	%o2
757c478bd9Sstevel@tonic-gate	b	.ald			! now go align dest
767c478bd9Sstevel@tonic-gate	andcc	%g5, 3, %o5
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate.s2algn:lduh	[%o1], %o3		! know src is 2 byte alinged
797c478bd9Sstevel@tonic-gate	inc	2, %o1
807c478bd9Sstevel@tonic-gate	srl	%o3, 8, %o4
817c478bd9Sstevel@tonic-gate	stb	%o4, [%g5]		! have to do bytes,
827c478bd9Sstevel@tonic-gate	stb	%o3, [%g5 + 1]		! don't know dst alingment
837c478bd9Sstevel@tonic-gate	inc	2, %g5
847c478bd9Sstevel@tonic-gate	dec	2, %o2
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate.aldst:	andcc	%g5, 3, %o5		! align the destination address
877c478bd9Sstevel@tonic-gate.ald:	bz,pn	%icc, .w4cp
887c478bd9Sstevel@tonic-gate	cmp	%o5, 2
897c478bd9Sstevel@tonic-gate	bz,pn	%icc, .w2cp
907c478bd9Sstevel@tonic-gate	cmp	%o5, 3
917c478bd9Sstevel@tonic-gate.w3cp:	lduw	[%o1], %o4
927c478bd9Sstevel@tonic-gate	inc	4, %o1
937c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o5
947c478bd9Sstevel@tonic-gate	stb	%o5, [%g5]
957c478bd9Sstevel@tonic-gate	bne,pt	%icc, .w1cp
967c478bd9Sstevel@tonic-gate	inc	%g5
977c478bd9Sstevel@tonic-gate	dec	1, %o2
987c478bd9Sstevel@tonic-gate	andn	%o2, 3, %o3		! o3 is aligned word count
997c478bd9Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond tail of src
1007c478bd9Sstevel@tonic-gate	sub	%o1, %g5, %o1		! o1 gets the difference
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate1:	sll	%o4, 8, %g1		! save residual bytes
1037c478bd9Sstevel@tonic-gate	lduw	[%o1+%g5], %o4
1047c478bd9Sstevel@tonic-gate	deccc	4, %o3
1057c478bd9Sstevel@tonic-gate	srl	%o4, 24, %o5		! merge with residual
1067c478bd9Sstevel@tonic-gate	or	%o5, %g1, %g1
1077c478bd9Sstevel@tonic-gate	st	%g1, [%g5]
1087c478bd9Sstevel@tonic-gate	bnz,pt	%xcc, 1b
1097c478bd9Sstevel@tonic-gate	inc	4, %g5
1107c478bd9Sstevel@tonic-gate	sub	%o1, 3, %o1		! used one byte of last word read
1117c478bd9Sstevel@tonic-gate	and	%o2, 3, %o2
1127c478bd9Sstevel@tonic-gate	b	7f
1137c478bd9Sstevel@tonic-gate	inc	4, %o2
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate.w1cp:	srl	%o4, 8, %o5
1167c478bd9Sstevel@tonic-gate	sth	%o5, [%g5]
1177c478bd9Sstevel@tonic-gate	inc	2, %g5
1187c478bd9Sstevel@tonic-gate	dec	3, %o2
1197c478bd9Sstevel@tonic-gate	andn	%o2, 3, %o3		! o3 is aligned word count
1207c478bd9Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond tail of src
1217c478bd9Sstevel@tonic-gate	sub	%o1, %g5, %o1		! o1 gets the difference
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate2:	sll	%o4, 24, %g1		! save residual bytes
1247c478bd9Sstevel@tonic-gate	lduw	[%o1+%g5], %o4
1257c478bd9Sstevel@tonic-gate	deccc	4, %o3
1267c478bd9Sstevel@tonic-gate	srl	%o4, 8, %o5		! merge with residual
1277c478bd9Sstevel@tonic-gate	or	%o5, %g1, %g1
1287c478bd9Sstevel@tonic-gate	st	%g1, [%g5]
1297c478bd9Sstevel@tonic-gate	bnz,pt	%xcc, 2b
1307c478bd9Sstevel@tonic-gate	inc	4, %g5
1317c478bd9Sstevel@tonic-gate	sub	%o1, 1, %o1		! used three bytes of last word read
1327c478bd9Sstevel@tonic-gate	and	%o2, 3, %o2
1337c478bd9Sstevel@tonic-gate	b	7f
1347c478bd9Sstevel@tonic-gate	inc	4, %o2
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate.w2cp:	lduw	[%o1], %o4
1377c478bd9Sstevel@tonic-gate	inc	4, %o1
1387c478bd9Sstevel@tonic-gate	srl	%o4, 16, %o5
1397c478bd9Sstevel@tonic-gate	sth	%o5, [%g5]
1407c478bd9Sstevel@tonic-gate	inc	2, %g5
1417c478bd9Sstevel@tonic-gate	dec	2, %o2
1427c478bd9Sstevel@tonic-gate	andn	%o2, 3, %o3		! o3 is aligned word count
1437c478bd9Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond tail of src
1447c478bd9Sstevel@tonic-gate	sub	%o1, %g5, %o1		! o1 gets the difference
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate3:	sll	%o4, 16, %g1		! save residual bytes
1477c478bd9Sstevel@tonic-gate	lduw	[%o1+%g5], %o4
1487c478bd9Sstevel@tonic-gate	deccc	4, %o3
1497c478bd9Sstevel@tonic-gate	srl	%o4, 16, %o5		! merge with residual
1507c478bd9Sstevel@tonic-gate	or	%o5, %g1, %g1
1517c478bd9Sstevel@tonic-gate	st	%g1, [%g5]
1527c478bd9Sstevel@tonic-gate	bnz,pt	%xcc, 3b
1537c478bd9Sstevel@tonic-gate	inc	4, %g5
1547c478bd9Sstevel@tonic-gate	sub	%o1, 2, %o1		! used two bytes of last word read
1557c478bd9Sstevel@tonic-gate	and	%o2, 3, %o2
1567c478bd9Sstevel@tonic-gate	b	7f
1577c478bd9Sstevel@tonic-gate	inc	4, %o2
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate.w4cp:	andn	%o2, 3, %o3		! o3 is aligned word count
1607c478bd9Sstevel@tonic-gate	sub	%o1, %g5, %o1		! o1 gets the difference
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate1:	lduw	[%o1+%g5], %o4		! read from address
1637c478bd9Sstevel@tonic-gate	deccc	4, %o3			! decrement count
1647c478bd9Sstevel@tonic-gate	st	%o4, [%g5]		! write at destination address
1657c478bd9Sstevel@tonic-gate	bgu,pt	%xcc, 1b
1667c478bd9Sstevel@tonic-gate	inc	4, %g5			! increment to address
1677c478bd9Sstevel@tonic-gate	b	7f
1687c478bd9Sstevel@tonic-gate	and	%o2, 3, %o2		! number of leftover bytes, if any
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate	!
1717c478bd9Sstevel@tonic-gate	! differenced byte copy, works with any alignment
1727c478bd9Sstevel@tonic-gate	!
1737c478bd9Sstevel@tonic-gate.dbytecp:
1747c478bd9Sstevel@tonic-gate	b	7f
1757c478bd9Sstevel@tonic-gate	sub	%o1, %g5, %o1		! o1 gets the difference
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate4:	stb	%o4, [%g5]		! write to address
1787c478bd9Sstevel@tonic-gate	inc	%g5			! inc to address
1797c478bd9Sstevel@tonic-gate7:	deccc	%o2			! decrement count
1807c478bd9Sstevel@tonic-gate	bgeu,a,pt %xcc,4b		! loop till done
1817c478bd9Sstevel@tonic-gate	ldub	[%o1+%g5], %o4		! read from address
1827c478bd9Sstevel@tonic-gate	retl
1837c478bd9Sstevel@tonic-gate	nop
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate	SET_SIZE(memcpy)
1867c478bd9Sstevel@tonic-gate	SET_SIZE(__align_cpy_1)
187