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