xref: /netbsd/common/lib/libc/arch/m68k/string/bcopy.S (revision 6550d01e)
1/*	$NetBSD: bcopy.S,v 1.2 2008/04/28 20:22:52 martin Exp $	*/
2
3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by J.T. Conklin.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*-
33 * Copyright (c) 1990 The Regents of the University of California.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * the Systems Programming Group of the University of Utah Computer
38 * Science Department.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 *    notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 *    notice, this list of conditions and the following disclaimer in the
47 *    documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 *    may be used to endorse or promote products derived from this software
50 *    without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65#include <machine/asm.h>
66
67#if defined(LIBC_SCCS) && !defined(lint)
68#if 0
69	RCSID("from: @(#)bcopy.s	5.1 (Berkeley) 5/12/90")
70#else
71	RCSID("$NetBSD: bcopy.S,v 1.2 2008/04/28 20:22:52 martin Exp $")
72#endif
73#endif /* LIBC_SCCS and not lint */
74
75
76#ifdef MEMCOPY
77ENTRY(memcpy)
78#else
79#ifdef MEMMOVE
80ENTRY(memmove)
81#else
82ENTRY(bcopy)
83#endif
84#endif
85#if defined(MEMCOPY) || defined(MEMMOVE)
86	movl	%sp@(4),%a1		| dest address
87	movl	%sp@(8),%a0		| src address
88#else
89	movl	%sp@(4),%a0		| src address
90	movl	%sp@(8),%a1		| dest address
91#endif
92	movl	%sp@(12),%d1		| count
93
94	cmpl	%a1,%a0			| src after dest?
95	jlt	Lbcback			| yes, must copy backwards
96
97	/*
98	 * It isn't worth the overhead of aligning to {long}word boundries
99	 * if the string is too short.
100	 */
101	cmpl	#8,%d1
102	jlt	Lbcfbyte
103
104#ifdef	__mc68010__
105	/*
106	 * The 68010 cannot access a word or long on an odd boundary,
107	 * period.  If the source and the destination addresses aren't
108	 * of the same evenness, we're forced to do a bytewise copy.
109	 */
110	movl	%a0,%d0
111	addl	%a1,%d0
112	btst	#0,%d0
113	jne	Lbcfbyte
114#endif	/* __mc68010__ */
115
116	/* word align */
117	movl	%a1,%d0
118	btst	#0,%d0		| if (dst & 1)
119	jeq	Lbcfalgndw	|
120	movb	%a0@+,%a1@+	|	*(char *)dst++ = *(char *) src++
121	subql	#1,%d1		|	len--
122Lbcfalgndw:
123	/* long word align */
124	btst	#1,%d0		| if (dst & 2)
125	jeq	Lbcfalgndl
126	movw	%a0@+,%a1@+	|	*(short *)dst++ = *(short *) dst++
127	subql	#2,%d1		|	len -= 2
128Lbcfalgndl:
129	/* copy by 8 longwords */
130	movel	%d1,%d0
131	lsrl	#5,%d0		| cnt = len / 32
132	jeq	Lbcflong	| if (cnt)
133	andl	#31,%d1		|	len %= 32
134	subql	#1,%d0		|	set up for dbf
135Lbcf32loop:
136	movl	%a0@+,%a1@+	|	copy 8 long words
137	movl	%a0@+,%a1@+
138	movl	%a0@+,%a1@+
139	movl	%a0@+,%a1@+
140	movl	%a0@+,%a1@+
141	movl	%a0@+,%a1@+
142	movl	%a0@+,%a1@+
143	movl	%a0@+,%a1@+
144	dbf	%d0,Lbcf32loop	|	till done
145	clrw	%d0
146	subql	#1,%d0
147	jcc	Lbcf32loop
148
149Lbcflong:
150	/* copy by longwords */
151	movel	%d1,%d0
152	lsrl	#2,%d0		| cnt = len / 4
153	jeq	Lbcfbyte	| if (cnt)
154	subql	#1,%d0		|	set up for dbf
155Lbcflloop:
156	movl	%a0@+,%a1@+	|	copy longwords
157	dbf	%d0,Lbcflloop	|	til done
158	andl	#3,%d1		|	len %= 4
159	jeq	Lbcdone
160
161	subql	#1,%d1		| set up for dbf
162Lbcfbloop:
163	movb	%a0@+,%a1@+	| copy bytes
164Lbcfbyte:
165	dbf	%d1,Lbcfbloop	| till done
166Lbcdone:
167#if defined(MEMCOPY) || defined(MEMMOVE)
168	movl	%sp@(4),%d0	| dest address
169#if defined(__SVR4_ABI__)
170	moveal	%d0,%a0
171#endif
172#endif
173	rts
174
175
176Lbcback:
177	addl	%d1,%a0		| src pointer to end
178	addl	%d1,%a1		| dest pointer to end
179
180	/*
181	 * It isn't worth the overhead of aligning to {long}word boundries
182	 * if the string is too short.
183	 */
184	cmpl	#8,%d1
185	jlt	Lbcbbyte
186
187#ifdef	__mc68010__
188	/*
189	 * The 68010 cannot access a word or long on an odd boundary,
190	 * period.  If the source and the destination addresses aren't
191	 * of the same evenness, we're forced to do a bytewise copy.
192	 */
193	movl	%a0,%d0
194	addl	%a1,%d0
195	btst	#0,%d0
196	jne	Lbcbbyte
197#endif	/* __mc68010__ */
198
199	/* word align */
200	movl	%a1,%d0
201	btst	#0,%d0		| if (dst & 1)
202	jeq	Lbcbalgndw	|
203	movb	%a0@-,%a1@-	|	*(char *)dst-- = *(char *) src--
204	subql	#1,%d1		|	len--
205Lbcbalgndw:
206	/* long word align */
207	btst	#1,%d0		| if (dst & 2)
208	jeq	Lbcbalgndl
209	movw	%a0@-,%a1@-	|	*(short *)dst-- = *(short *) dst--
210	subql	#2,%d1		|	len -= 2
211Lbcbalgndl:
212	/* copy by 8 longwords */
213	movel	%d1,%d0
214	lsrl	#5,%d0		| cnt = len / 32
215	jeq	Lbcblong	| if (cnt)
216	andl	#31,%d1		|	len %= 32
217	subql	#1,%d0		|	set up for dbf
218Lbcb32loop:
219	movl	%a0@-,%a1@-	|	copy 8 long words
220	movl	%a0@-,%a1@-
221	movl	%a0@-,%a1@-
222	movl	%a0@-,%a1@-
223	movl	%a0@-,%a1@-
224	movl	%a0@-,%a1@-
225	movl	%a0@-,%a1@-
226	movl	%a0@-,%a1@-
227	dbf	%d0,Lbcb32loop	|	till done
228	clrw	%d0
229	subql	#1,%d0
230	jcc	Lbcb32loop
231
232Lbcblong:
233	/* copy by longwords */
234	movel	%d1,%d0
235	lsrl	#2,%d0		| cnt = len / 4
236	jeq	Lbcbbyte	| if (cnt)
237	subql	#1,%d0		|	set up for dbf
238Lbcblloop:
239	movl	%a0@-,%a1@-	|	copy longwords
240	dbf	%d0,Lbcblloop	|	til done
241	andl	#3,%d1		|	len %= 4
242	jeq	Lbcdone
243
244	subql	#1,%d1		| set up for dbf
245Lbcbbloop:
246	movb	%a0@-,%a1@-	| copy bytes
247Lbcbbyte:
248	dbf	%d1,Lbcbbloop	| till done
249
250#if defined(MEMCOPY) || defined(MEMMOVE)
251	movl	%sp@(4),%d0	| dest address
252#if defined(__SVR4_ABI__)
253	moveal	%d0,%a0
254#endif
255#endif
256	rts
257