1 /* $NetBSD: memcpy_arm.S,v 1.1 2003/10/14 07:51:45 scw Exp $ */
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Neil A. Carson and Mark Brinicombe
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <machine/asm.h>
40 __FBSDID("$FreeBSD$");
42 * This is one fun bit of code ...
43 * Some easy listening music is suggested while trying to understand this
44 * code e.g. Iron Maiden
46 * For anyone attempting to understand it :
48 * The core code is implemented here with simple stubs for memcpy().
50 * All local labels are prefixed with Lmemcpy_
51 * Following the prefix a label starting f is used in the forward copy code
52 * while a label using b is used in the backwards copy code
53 * The source and destination addresses determine whether a forward or
54 * backward copy is performed.
55 * Separate bits of code are used to deal with the following situations
56 * for both the forward and backwards copy.
57 * unaligned source address
58 * unaligned destination address
59 * Separate copy routines are used to produce an optimised result for each
61 * The copy code will use LDM/STM instructions to copy up to 32 bytes at
62 * a time where possible.
64 * Note: r12 (aka ip) can be trashed during the function along with
65 * r0-r3 although r0-r2 have defined uses i.e. src, dest, len through out.
66 * Additional registers are preserved prior to use i.e. r4, r5 & lr
68 * Apologies for the state of the comments ;-)
70 /* LINTSTUB: Func: void *memcpy(void *dst, const void *src, size_t len) */
72 /* save leaf functions having to store this away */
73 stmdb sp!, {r0, lr} /* memcpy() returns dest addr */
76 blt .Lmemcpy_l4 /* less than 4 bytes */
78 bne .Lmemcpy_destul /* oh unaligned destination addr */
80 bne .Lmemcpy_srcul /* oh unaligned source addr */
83 /* We have aligned source and destination */
85 blt .Lmemcpy_l12 /* less than 12 bytes (4 from above) */
87 blt .Lmemcpy_l32 /* less than 32 bytes (12 from above) */
88 stmdb sp!, {r4} /* borrow r4 */
90 /* blat 32 bytes at a time */
91 /* XXX for really big copies perhaps we should use more registers */
93 ldmia r1!, {r3, r4, r12, lr}
94 stmia r0!, {r3, r4, r12, lr}
95 ldmia r1!, {r3, r4, r12, lr}
96 stmia r0!, {r3, r4, r12, lr}
101 ldmgeia r1!, {r3, r4, r12, lr} /* blat a remaining 16 bytes */
102 stmgeia r0!, {r3, r4, r12, lr}
104 ldmia sp!, {r4} /* return r4 */
109 /* blat 12 bytes at a time */
111 ldmgeia r1!, {r3, r12, lr}
112 stmgeia r0!, {r3, r12, lr}
123 ldmgeia r1!, {r3, r12}
124 stmgeia r0!, {r3, r12}
128 /* less than 4 bytes to go */
131 ldmeqia sp!, {r0, pc}^ /* done */
133 ldmeqia sp!, {r0, pc} /* done */
135 /* copy the crud byte at a time */
145 /* erg - unaligned destination */
150 /* align destination with byte copies */
158 blt .Lmemcpy_l4 /* less the 4 bytes */
161 beq .Lmemcpy_t8 /* we have an aligned source */
163 /* erg - unaligned source */
164 /* This is where it gets nasty ... */
172 blt .Lmemcpy_srcul1loop4
176 .Lmemcpy_srcul1loop16:
182 ldmia r1!, {r4, r5, r12, lr}
184 orr r3, r3, r4, lsr #24
186 orr r4, r4, r5, lsr #24
188 orr r5, r5, r12, lsr #24
190 orr r12, r12, lr, lsr #24
192 orr r3, r3, r4, lsl #24
194 orr r4, r4, r5, lsl #24
196 orr r5, r5, r12, lsl #24
198 orr r12, r12, lr, lsl #24
200 stmia r0!, {r3-r5, r12}
202 bge .Lmemcpy_srcul1loop16
205 blt .Lmemcpy_srcul1l4
207 .Lmemcpy_srcul1loop4:
215 orr r12, r12, lr, lsr #24
217 orr r12, r12, lr, lsl #24
221 bge .Lmemcpy_srcul1loop4
229 blt .Lmemcpy_srcul2loop4
233 .Lmemcpy_srcul2loop16:
239 ldmia r1!, {r4, r5, r12, lr}
241 orr r3, r3, r4, lsr #16
243 orr r4, r4, r5, lsr #16
245 orr r5, r5, r12, lsr #16
246 mov r12, r12, lsl #16
247 orr r12, r12, lr, lsr #16
249 orr r3, r3, r4, lsl #16
251 orr r4, r4, r5, lsl #16
253 orr r5, r5, r12, lsl #16
254 mov r12, r12, lsr #16
255 orr r12, r12, lr, lsl #16
257 stmia r0!, {r3-r5, r12}
259 bge .Lmemcpy_srcul2loop16
262 blt .Lmemcpy_srcul2l4
264 .Lmemcpy_srcul2loop4:
272 orr r12, r12, lr, lsr #16
274 orr r12, r12, lr, lsl #16
278 bge .Lmemcpy_srcul2loop4
286 blt .Lmemcpy_srcul3loop4
290 .Lmemcpy_srcul3loop16:
296 ldmia r1!, {r4, r5, r12, lr}
298 orr r3, r3, r4, lsr #8
300 orr r4, r4, r5, lsr #8
302 orr r5, r5, r12, lsr #8
303 mov r12, r12, lsl #24
304 orr r12, r12, lr, lsr #8
306 orr r3, r3, r4, lsl #8
308 orr r4, r4, r5, lsl #8
310 orr r5, r5, r12, lsl #8
311 mov r12, r12, lsr #24
312 orr r12, r12, lr, lsl #8
314 stmia r0!, {r3-r5, r12}
316 bge .Lmemcpy_srcul3loop16
319 blt .Lmemcpy_srcul3l4
321 .Lmemcpy_srcul3loop4:
329 orr r12, r12, lr, lsr #8
331 orr r12, r12, lr, lsl #8
335 bge .Lmemcpy_srcul3loop4