xref: /netbsd-src/common/lib/libc/arch/powerpc/string/memmove.S (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1/* $NetBSD: memmove.S,v 1.2 2008/03/06 21:17:17 phx Exp $ */
2
3/* stropt/memmove.S, pl_string_common, pl_linux 10/11/04 11:45:37
4 * ==========================================================================
5 * Optimized memmove implementation for IBM PowerPC 405/440.
6 *
7 *	Copyright (c) 2003, IBM Corporation
8 *	All rights reserved.
9 *
10 *	Redistribution and use in source and binary forms, with or
11 *	without modification, are permitted provided that the following
12 *	conditions are met:
13 *
14 *	* Redistributions of source code must retain the above
15 *	copyright notice, this list of conditions and the following
16 *	disclaimer.
17 *	* Redistributions in binary form must reproduce the above
18 *	copyright notice, this list of conditions and the following
19 *	disclaimer in the documentation and/or other materials
20 *	provided with the distribution.
21 *	* Neither the name of IBM nor the names of its contributors
22 *	may be used to endorse or promote products derived from this
23 *	software without specific prior written permission.
24 *
25 *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 *	CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 *	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 *	MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 *	DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
30 *	BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 *	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 *	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 *	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
34 *	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 *	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 *	USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * ==========================================================================
39 *
40 * Function: Move memory area (handles overlapping regions)
41 *
42 *		void *memmove(void * dest, const void * src, int n)
43 *
44 * Input:	r3 - destination address
45 *	 r4 - source address
46 *	 r5 - byte count
47 * Output: r3 - destination address
48 *
49 * ==========================================================================
50 */
51
52#define _NOREGNAMES
53#include <machine/asm.h>
54
55        .text
56        .align 4
57#ifdef _BCOPY
58/* bcopy = memcpy/memmove with arguments reversed. */
59/* LINTSTUB: Func: void bcopy(void *, void *, size_t) */
60ENTRY(bcopy)
61	mr	%r6, %r3		/* swap src/dst */
62	mr	%r3, %r4
63	mr	%r4, %r6
64#else
65/* LINTSTUB: Func: void *memmove(void *, const void *, size_t) */
66ENTRY(memmove)
67#endif
68
69	mr	%r8, %r3		/* Save dst (return value)	*/
70
71	cmpw	%r4, %r8		/* Branch to reverse if 	*/
72	blt	reverse			/* src < dest. Don't want to	*/
73					/* overwrite end of src with	*/
74					/* start of dest 		*/
75
76	addi	%r4, %r4, -4		/* Back up src and dst pointers */
77	addi	%r8, %r8, -4		/* due to auto-update of 'load' */
78
79	srwi.	%r9,%r5,2			/* How many words in total cnt	*/
80	beq-	last1			/* Handle byte by byte if < 4	*/
81					/* bytes total 			*/
82	mtctr	%r9			/* Count of words for loop		*/
83	lwzu	%r7, 4(%r4)		/* Preload first word		*/
84
85	b	g1
86
87g0:					/* Main loop			*/
88
89	lwzu	%r7, 4(%r4)		/* Load a new word		*/
90	stwu	%r6, 4(%r8)		/* Store previous word		*/
91
92g1:
93
94	bdz-	last			/* Dec cnt, and branch if just	*/
95					/* one word to store		*/
96	lwzu	%r6, 4(%r4)		/* Load another word		*/
97	stwu	%r7, 4(%r8)		/* Store previous word		*/
98	bdnz+	g0			/* Dec cnt, and loop again if	*/
99					/* more words			*/
100	mr	%r7, %r6			/* If word count -> 0, then...	*/
101
102last:
103
104	stwu	%r7, 4(%r8)		/* ... store last word		*/
105
106last1:					/* Byte-by-byte copy		*/
107
108	clrlwi.	%r5,%r5,30		/* If count -> 0, then ...	*/
109	beqlr				/* we're done			*/
110
111	mtctr	%r5			/* else load count for loop	*/
112
113	lbzu	%r6, 4(%r4)		/* 1st byte: update addr by 4	*/
114	stbu	%r6, 4(%r8)		/* since we pre-adjusted by 4	*/
115	bdzlr-				/* in anticipation of main loop */
116
117last2:
118
119	lbzu	%r6, 1(%r4)		/* But handle the rest by		*/
120	stbu	%r6, 1(%r8)		/* updating addr by 1		*/
121	bdnz+	last2
122
123	blr
124
125	/* We're here since src < dest. Don't want to overwrite end of	*/
126	/* src with start of dest						*/
127
128reverse:
129
130	add	%r4, %r4, %r5		/* Work from end to beginning	*/
131	add	%r8, %r8, %r5 		/* so add count to string ptrs	*/
132	srwi.	%r9,%r5,2			/* Words in total count		*/
133	beq-	rlast1			/* Handle byte by byte if < 4	*/
134					/* bytes total 			*/
135
136	mtctr	%r9			/* Count of words for loop 	*/
137
138	lwzu	%r7, -4(%r4)		/* Preload first word		*/
139	b	rg1
140
141rg0:					/* Main loop			*/
142
143	lwzu	%r7, -4(%r4)		/* Load a new word		*/
144	stwu	%r6, -4(%r8)		/* Store previous word		*/
145
146rg1:
147
148	bdz-	rlast			/* Dec cnt, and branch if just	*/
149					/* one word to store		*/
150
151	lwzu	%r6, -4(%r4)		/* Load another word		*/
152	stwu	%r7, -4(%r8)		/* Store previous word		*/
153
154	bdnz+	rg0			/* Dec cnt, and loop again if	*/
155					/* more words			*/
156
157	mr	%r7, %r6			/* If word count -> 0, then...	*/
158
159rlast:
160
161	stwu	%r7, -4(%r8)		/* ... store last word		*/
162
163rlast1:					/* Byte-by-byte copy		*/
164
165	clrlwi.	%r5,%r5,30		/* If count -> 0, then...	*/
166	beqlr				/* ... we're done 		*/
167
168	mtctr	%r5			/* else load count for loop 	*/
169
170rlast2:
171
172	lbzu	%r6, -1(%r4)		/* Handle the rest, byte by 	*/
173	stbu	%r6, -1(%r8)		/* byte				*/
174
175	bdnz+	rlast2		 	/* Dec ctr, and branch if more	*/
176					/* bytes left			*/
177	blr
178
179