xref: /onnv-gate/usr/src/lib/libc/sparc/gen/memcpy.s (revision 12719:bd9fb35d09c2)
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
56515Sraf * Common Development and Distribution License (the "License").
66515Sraf * 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 */
216515Sraf
220Sstevel@tonic-gate/*
23*12719SRod.Evans@Sun.COM * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
267298SMark.J.Nelson@Sun.COM	.file	"memcpy.s"
270Sstevel@tonic-gate
280Sstevel@tonic-gate/*
290Sstevel@tonic-gate * memcpy(s1, s2, len)
300Sstevel@tonic-gate *
310Sstevel@tonic-gate * Copy s2 to s1, always copy n bytes.
320Sstevel@tonic-gate * Note: this does not work for overlapped copies, bcopy() does
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * Fast assembler language version of the following C-program for memcpy
350Sstevel@tonic-gate * which represents the `standard' for the C-library.
360Sstevel@tonic-gate *
370Sstevel@tonic-gate *	void *
380Sstevel@tonic-gate *	memcpy(void *s, const void *s0, size_t n)
390Sstevel@tonic-gate *	{
400Sstevel@tonic-gate *		if (n != 0) {
410Sstevel@tonic-gate *	   	    char *s1 = s;
420Sstevel@tonic-gate *		    const char *s2 = s0;
430Sstevel@tonic-gate *		    do {
440Sstevel@tonic-gate *			*s1++ = *s2++;
450Sstevel@tonic-gate *		    } while (--n != 0);
460Sstevel@tonic-gate *		}
470Sstevel@tonic-gate *		return (s);
480Sstevel@tonic-gate *	}
490Sstevel@tonic-gate */
500Sstevel@tonic-gate
516812Sraf#include "SYS.h"
520Sstevel@tonic-gate
530Sstevel@tonic-gate	ANSI_PRAGMA_WEAK(memcpy,function)
540Sstevel@tonic-gate
550Sstevel@tonic-gate	ENTRY(memcpy)
56*12719SRod.Evans@Sun.COM        ENTRY(__align_cpy_1)
570Sstevel@tonic-gate	st	%o0, [%sp + 68]		! save des address for return val
580Sstevel@tonic-gate	cmp	%o2, 17			! for small counts copy bytes
590Sstevel@tonic-gate	bleu	.dbytecp
600Sstevel@tonic-gate	andcc	%o1, 3, %o5		! is src word aligned
610Sstevel@tonic-gate	bz	.aldst
620Sstevel@tonic-gate	cmp	%o5, 2			! is src half-word aligned
630Sstevel@tonic-gate	be	.s2algn
640Sstevel@tonic-gate	cmp	%o5, 3			! src is byte aligned
650Sstevel@tonic-gate.s1algn:ldub	[%o1], %o3		! move 1 or 3 bytes to align it
660Sstevel@tonic-gate	inc	1, %o1
670Sstevel@tonic-gate	stb	%o3, [%o0]		! move a byte to align src
680Sstevel@tonic-gate	inc	1, %o0
690Sstevel@tonic-gate	bne	.s2algn
700Sstevel@tonic-gate	dec	%o2
710Sstevel@tonic-gate	b	.ald			! now go align dest
720Sstevel@tonic-gate	andcc	%o0, 3, %o5
730Sstevel@tonic-gate
740Sstevel@tonic-gate.s2algn:lduh	[%o1], %o3		! know src is 2 byte alinged
750Sstevel@tonic-gate	inc	2, %o1
760Sstevel@tonic-gate	srl	%o3, 8, %o4
770Sstevel@tonic-gate	stb	%o4, [%o0]		! have to do bytes,
780Sstevel@tonic-gate	stb	%o3, [%o0 + 1]		! don't know dst alingment
790Sstevel@tonic-gate	inc	2, %o0
800Sstevel@tonic-gate	dec	2, %o2
810Sstevel@tonic-gate
820Sstevel@tonic-gate.aldst:	andcc	%o0, 3, %o5		! align the destination address
830Sstevel@tonic-gate.ald:	bz	.w4cp
840Sstevel@tonic-gate	cmp	%o5, 2
850Sstevel@tonic-gate	bz	.w2cp
860Sstevel@tonic-gate	cmp	%o5, 3
870Sstevel@tonic-gate.w3cp:	ld	[%o1], %o4
880Sstevel@tonic-gate	inc	4, %o1
890Sstevel@tonic-gate	srl	%o4, 24, %o5
900Sstevel@tonic-gate	stb	%o5, [%o0]
910Sstevel@tonic-gate	bne	.w1cp
920Sstevel@tonic-gate	inc	%o0
930Sstevel@tonic-gate	dec	1, %o2
940Sstevel@tonic-gate	andn	%o2, 3, %o3		! o3 is aligned word count
950Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond tail of src
960Sstevel@tonic-gate	sub	%o1, %o0, %o1		! o1 gets the difference
970Sstevel@tonic-gate
980Sstevel@tonic-gate1:	sll	%o4, 8, %g1		! save residual bytes
990Sstevel@tonic-gate	ld	[%o1+%o0], %o4
1000Sstevel@tonic-gate	deccc	4, %o3
1010Sstevel@tonic-gate	srl	%o4, 24, %o5		! merge with residual
1020Sstevel@tonic-gate	or	%o5, %g1, %g1
1030Sstevel@tonic-gate	st	%g1, [%o0]
1040Sstevel@tonic-gate	bnz	1b
1050Sstevel@tonic-gate	inc	4, %o0
1060Sstevel@tonic-gate	sub	%o1, 3, %o1		! used one byte of last word read
1070Sstevel@tonic-gate	and	%o2, 3, %o2
1080Sstevel@tonic-gate	b	7f
1090Sstevel@tonic-gate	inc	4, %o2
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate.w1cp:	srl	%o4, 8, %o5
1120Sstevel@tonic-gate	sth	%o5, [%o0]
1130Sstevel@tonic-gate	inc	2, %o0
1140Sstevel@tonic-gate	dec	3, %o2
1150Sstevel@tonic-gate	andn	%o2, 3, %o3		! o3 is aligned word count
1160Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond tail of src
1170Sstevel@tonic-gate	sub	%o1, %o0, %o1		! o1 gets the difference
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate2:	sll	%o4, 24, %g1		! save residual bytes
1200Sstevel@tonic-gate	ld	[%o1+%o0], %o4
1210Sstevel@tonic-gate	deccc	4, %o3
1220Sstevel@tonic-gate	srl	%o4, 8, %o5		! merge with residual
1230Sstevel@tonic-gate	or	%o5, %g1, %g1
1240Sstevel@tonic-gate	st	%g1, [%o0]
1250Sstevel@tonic-gate	bnz	2b
1260Sstevel@tonic-gate	inc	4, %o0
1270Sstevel@tonic-gate	sub	%o1, 1, %o1		! used three bytes of last word read
1280Sstevel@tonic-gate	and	%o2, 3, %o2
1290Sstevel@tonic-gate	b	7f
1300Sstevel@tonic-gate	inc	4, %o2
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate.w2cp:	ld	[%o1], %o4
1330Sstevel@tonic-gate	inc	4, %o1
1340Sstevel@tonic-gate	srl	%o4, 16, %o5
1350Sstevel@tonic-gate	sth	%o5, [%o0]
1360Sstevel@tonic-gate	inc	2, %o0
1370Sstevel@tonic-gate	dec	2, %o2
1380Sstevel@tonic-gate	andn	%o2, 3, %o3		! o3 is aligned word count
1390Sstevel@tonic-gate	dec	4, %o3			! avoid reading beyond tail of src
1400Sstevel@tonic-gate	sub	%o1, %o0, %o1		! o1 gets the difference
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate3:	sll	%o4, 16, %g1		! save residual bytes
1430Sstevel@tonic-gate	ld	[%o1+%o0], %o4
1440Sstevel@tonic-gate	deccc	4, %o3
1450Sstevel@tonic-gate	srl	%o4, 16, %o5		! merge with residual
1460Sstevel@tonic-gate	or	%o5, %g1, %g1
1470Sstevel@tonic-gate	st	%g1, [%o0]
1480Sstevel@tonic-gate	bnz	3b
1490Sstevel@tonic-gate	inc	4, %o0
1500Sstevel@tonic-gate	sub	%o1, 2, %o1		! used two bytes of last word read
1510Sstevel@tonic-gate	and	%o2, 3, %o2
1520Sstevel@tonic-gate	b	7f
1530Sstevel@tonic-gate	inc	4, %o2
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate.w4cp:	andn	%o2, 3, %o3		! o3 is aligned word count
1560Sstevel@tonic-gate	sub	%o1, %o0, %o1		! o1 gets the difference
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate1:	ld	[%o1+%o0], %o4		! read from address
1590Sstevel@tonic-gate	deccc	4, %o3			! decrement count
1600Sstevel@tonic-gate	st	%o4, [%o0]		! write at destination address
1610Sstevel@tonic-gate	bgu	1b
1620Sstevel@tonic-gate	inc	4, %o0			! increment to address
1630Sstevel@tonic-gate	b	7f
1640Sstevel@tonic-gate	and	%o2, 3, %o2		! number of leftover bytes, if any
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate	!
1670Sstevel@tonic-gate	! differenced byte copy, works with any alignment
1680Sstevel@tonic-gate	!
1690Sstevel@tonic-gate.dbytecp:
1700Sstevel@tonic-gate	b	7f
1710Sstevel@tonic-gate	sub	%o1, %o0, %o1		! o1 gets the difference
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate4:	stb	%o4, [%o0]		! write to address
1740Sstevel@tonic-gate	inc	%o0			! inc to address
1750Sstevel@tonic-gate7:	deccc	%o2			! decrement count
1760Sstevel@tonic-gate	bgeu,a	4b			! loop till done
1770Sstevel@tonic-gate	ldub	[%o1+%o0], %o4		! read from address
1780Sstevel@tonic-gate	retl
1790Sstevel@tonic-gate	ld	[%sp + 68], %o0		! return s1, destination address
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate	SET_SIZE(memcpy)
182