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