xref: /titanic_44/usr/src/lib/libc/sparcv9/gen/memcpy.s (revision 7568150a58e78021968b6c22bc28e9787b33496a)
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	.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 * Added entry __align_cpy_1 is generally for use of the compilers.
38 *
39 *
40 * Fast assembler language version of the following C-program for memcpy
41 * which represents the `standard' for the C-library.
42 *
43 *	void *
44 *	memcpy(void *s, const void *s0, size_t n)
45 *	{
46 *		if (n != 0) {
47 *			char *s1 = s;
48 *			const char *s2 = s0;
49 *			do {
50 *				*s1++ = *s2++;
51 *			} while (--n != 0);
52 *		}
53 *		return (s);
54 *	}
55 */
56
57#include <sys/asm_linkage.h>
58
59	ANSI_PRAGMA_WEAK(memcpy,function)
60
61#include "synonyms.h"
62
63	ENTRY(memcpy)
64	ENTRY(__align_cpy_1)
65	mov	%o0, %g5		! save des address for return val
66	cmp	%o2, 17			! for small counts copy bytes
67	bleu,pn	%xcc, .dbytecp
68	andcc	%o1, 3, %o5		! is src word aligned
69	bz,pn	%icc, .aldst
70	cmp	%o5, 2			! is src half-word aligned
71	be,pt	%xcc, .s2algn
72	cmp	%o5, 3			! src is byte aligned
73.s1algn:ldub	[%o1], %o3		! move 1 or 3 bytes to align it
74	inc	1, %o1
75	stb	%o3, [%g5]		! move a byte to align src
76	inc	1, %g5
77	bne,pt	%icc, .s2algn
78	dec	%o2
79	b	.ald			! now go align dest
80	andcc	%g5, 3, %o5
81
82.s2algn:lduh	[%o1], %o3		! know src is 2 byte alinged
83	inc	2, %o1
84	srl	%o3, 8, %o4
85	stb	%o4, [%g5]		! have to do bytes,
86	stb	%o3, [%g5 + 1]		! don't know dst alingment
87	inc	2, %g5
88	dec	2, %o2
89
90.aldst:	andcc	%g5, 3, %o5		! align the destination address
91.ald:	bz,pn	%icc, .w4cp
92	cmp	%o5, 2
93	bz,pn	%icc, .w2cp
94	cmp	%o5, 3
95.w3cp:	lduw	[%o1], %o4
96	inc	4, %o1
97	srl	%o4, 24, %o5
98	stb	%o5, [%g5]
99	bne,pt	%icc, .w1cp
100	inc	%g5
101	dec	1, %o2
102	andn	%o2, 3, %o3		! o3 is aligned word count
103	dec	4, %o3			! avoid reading beyond tail of src
104	sub	%o1, %g5, %o1		! o1 gets the difference
105
1061:	sll	%o4, 8, %g1		! save residual bytes
107	lduw	[%o1+%g5], %o4
108	deccc	4, %o3
109	srl	%o4, 24, %o5		! merge with residual
110	or	%o5, %g1, %g1
111	st	%g1, [%g5]
112	bnz,pt	%xcc, 1b
113	inc	4, %g5
114	sub	%o1, 3, %o1		! used one byte of last word read
115	and	%o2, 3, %o2
116	b	7f
117	inc	4, %o2
118
119.w1cp:	srl	%o4, 8, %o5
120	sth	%o5, [%g5]
121	inc	2, %g5
122	dec	3, %o2
123	andn	%o2, 3, %o3		! o3 is aligned word count
124	dec	4, %o3			! avoid reading beyond tail of src
125	sub	%o1, %g5, %o1		! o1 gets the difference
126
1272:	sll	%o4, 24, %g1		! save residual bytes
128	lduw	[%o1+%g5], %o4
129	deccc	4, %o3
130	srl	%o4, 8, %o5		! merge with residual
131	or	%o5, %g1, %g1
132	st	%g1, [%g5]
133	bnz,pt	%xcc, 2b
134	inc	4, %g5
135	sub	%o1, 1, %o1		! used three bytes of last word read
136	and	%o2, 3, %o2
137	b	7f
138	inc	4, %o2
139
140.w2cp:	lduw	[%o1], %o4
141	inc	4, %o1
142	srl	%o4, 16, %o5
143	sth	%o5, [%g5]
144	inc	2, %g5
145	dec	2, %o2
146	andn	%o2, 3, %o3		! o3 is aligned word count
147	dec	4, %o3			! avoid reading beyond tail of src
148	sub	%o1, %g5, %o1		! o1 gets the difference
149
1503:	sll	%o4, 16, %g1		! save residual bytes
151	lduw	[%o1+%g5], %o4
152	deccc	4, %o3
153	srl	%o4, 16, %o5		! merge with residual
154	or	%o5, %g1, %g1
155	st	%g1, [%g5]
156	bnz,pt	%xcc, 3b
157	inc	4, %g5
158	sub	%o1, 2, %o1		! used two bytes of last word read
159	and	%o2, 3, %o2
160	b	7f
161	inc	4, %o2
162
163.w4cp:	andn	%o2, 3, %o3		! o3 is aligned word count
164	sub	%o1, %g5, %o1		! o1 gets the difference
165
1661:	lduw	[%o1+%g5], %o4		! read from address
167	deccc	4, %o3			! decrement count
168	st	%o4, [%g5]		! write at destination address
169	bgu,pt	%xcc, 1b
170	inc	4, %g5			! increment to address
171	b	7f
172	and	%o2, 3, %o2		! number of leftover bytes, if any
173
174	!
175	! differenced byte copy, works with any alignment
176	!
177.dbytecp:
178	b	7f
179	sub	%o1, %g5, %o1		! o1 gets the difference
180
1814:	stb	%o4, [%g5]		! write to address
182	inc	%g5			! inc to address
1837:	deccc	%o2			! decrement count
184	bgeu,a,pt %xcc,4b		! loop till done
185	ldub	[%o1+%g5], %o4		! read from address
186	retl
187	nop
188
189	SET_SIZE(memcpy)
190	SET_SIZE(__align_cpy_1)
191