xref: /titanic_51/usr/src/lib/libc/sparc/gen/memcpy.s (revision b9bd317cda1afb3a01f4812de73e8cec888cbbd7)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31/*
32 * memcpy(s1, s2, len)
33 *
34 * Copy s2 to s1, always copy n bytes.
35 * Note: this does not work for overlapped copies, bcopy() does
36 *
37 * Fast assembler language version of the following C-program for memcpy
38 * which represents the `standard' for the C-library.
39 *
40 *	void *
41 *	memcpy(void *s, const void *s0, size_t n)
42 *	{
43 *		if (n != 0) {
44 *	   	    char *s1 = s;
45 *		    const char *s2 = s0;
46 *		    do {
47 *			*s1++ = *s2++;
48 *		    } while (--n != 0);
49 *		}
50 *		return (s);
51 *	}
52 */
53
54#include "SYS.h"
55
56	ANSI_PRAGMA_WEAK(memcpy,function)
57
58	ENTRY(memcpy)
59	st	%o0, [%sp + 68]		! save des address for return val
60	cmp	%o2, 17			! for small counts copy bytes
61	bleu	.dbytecp
62	andcc	%o1, 3, %o5		! is src word aligned
63	bz	.aldst
64	cmp	%o5, 2			! is src half-word aligned
65	be	.s2algn
66	cmp	%o5, 3			! src is byte aligned
67.s1algn:ldub	[%o1], %o3		! move 1 or 3 bytes to align it
68	inc	1, %o1
69	stb	%o3, [%o0]		! move a byte to align src
70	inc	1, %o0
71	bne	.s2algn
72	dec	%o2
73	b	.ald			! now go align dest
74	andcc	%o0, 3, %o5
75
76.s2algn:lduh	[%o1], %o3		! know src is 2 byte alinged
77	inc	2, %o1
78	srl	%o3, 8, %o4
79	stb	%o4, [%o0]		! have to do bytes,
80	stb	%o3, [%o0 + 1]		! don't know dst alingment
81	inc	2, %o0
82	dec	2, %o2
83
84.aldst:	andcc	%o0, 3, %o5		! align the destination address
85.ald:	bz	.w4cp
86	cmp	%o5, 2
87	bz	.w2cp
88	cmp	%o5, 3
89.w3cp:	ld	[%o1], %o4
90	inc	4, %o1
91	srl	%o4, 24, %o5
92	stb	%o5, [%o0]
93	bne	.w1cp
94	inc	%o0
95	dec	1, %o2
96	andn	%o2, 3, %o3		! o3 is aligned word count
97	dec	4, %o3			! avoid reading beyond tail of src
98	sub	%o1, %o0, %o1		! o1 gets the difference
99
1001:	sll	%o4, 8, %g1		! save residual bytes
101	ld	[%o1+%o0], %o4
102	deccc	4, %o3
103	srl	%o4, 24, %o5		! merge with residual
104	or	%o5, %g1, %g1
105	st	%g1, [%o0]
106	bnz	1b
107	inc	4, %o0
108	sub	%o1, 3, %o1		! used one byte of last word read
109	and	%o2, 3, %o2
110	b	7f
111	inc	4, %o2
112
113.w1cp:	srl	%o4, 8, %o5
114	sth	%o5, [%o0]
115	inc	2, %o0
116	dec	3, %o2
117	andn	%o2, 3, %o3		! o3 is aligned word count
118	dec	4, %o3			! avoid reading beyond tail of src
119	sub	%o1, %o0, %o1		! o1 gets the difference
120
1212:	sll	%o4, 24, %g1		! save residual bytes
122	ld	[%o1+%o0], %o4
123	deccc	4, %o3
124	srl	%o4, 8, %o5		! merge with residual
125	or	%o5, %g1, %g1
126	st	%g1, [%o0]
127	bnz	2b
128	inc	4, %o0
129	sub	%o1, 1, %o1		! used three bytes of last word read
130	and	%o2, 3, %o2
131	b	7f
132	inc	4, %o2
133
134.w2cp:	ld	[%o1], %o4
135	inc	4, %o1
136	srl	%o4, 16, %o5
137	sth	%o5, [%o0]
138	inc	2, %o0
139	dec	2, %o2
140	andn	%o2, 3, %o3		! o3 is aligned word count
141	dec	4, %o3			! avoid reading beyond tail of src
142	sub	%o1, %o0, %o1		! o1 gets the difference
143
1443:	sll	%o4, 16, %g1		! save residual bytes
145	ld	[%o1+%o0], %o4
146	deccc	4, %o3
147	srl	%o4, 16, %o5		! merge with residual
148	or	%o5, %g1, %g1
149	st	%g1, [%o0]
150	bnz	3b
151	inc	4, %o0
152	sub	%o1, 2, %o1		! used two bytes of last word read
153	and	%o2, 3, %o2
154	b	7f
155	inc	4, %o2
156
157.w4cp:	andn	%o2, 3, %o3		! o3 is aligned word count
158	sub	%o1, %o0, %o1		! o1 gets the difference
159
1601:	ld	[%o1+%o0], %o4		! read from address
161	deccc	4, %o3			! decrement count
162	st	%o4, [%o0]		! write at destination address
163	bgu	1b
164	inc	4, %o0			! increment to address
165	b	7f
166	and	%o2, 3, %o2		! number of leftover bytes, if any
167
168	!
169	! differenced byte copy, works with any alignment
170	!
171.dbytecp:
172	b	7f
173	sub	%o1, %o0, %o1		! o1 gets the difference
174
1754:	stb	%o4, [%o0]		! write to address
176	inc	%o0			! inc to address
1777:	deccc	%o2			! decrement count
178	bgeu,a	4b			! loop till done
179	ldub	[%o1+%o0], %o4		! read from address
180	retl
181	ld	[%sp + 68], %o0		! return s1, destination address
182
183	SET_SIZE(memcpy)
184