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.
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.
32 #include <machine/asm.h>
33 __FBSDID("$FreeBSD$");
38 * This is one fun bit of code ...
39 * Some easy listening music is suggested while trying to understand this
40 * code e.g. Iron Maiden
42 * For anyone attempting to understand it :
44 * The core code is implemented here with simple stubs for memcpy().
46 * All local labels are prefixed with Lmemcpy_
47 * Following the prefix a label starting f is used in the forward copy code
48 * while a label using b is used in the backwards copy code
49 * The source and destination addresses determine whether a forward or
50 * backward copy is performed.
51 * Separate bits of code are used to deal with the following situations
52 * for both the forward and backwards copy.
53 * unaligned source address
54 * unaligned destination address
55 * Separate copy routines are used to produce an optimised result for each
57 * The copy code will use LDM/STM instructions to copy up to 32 bytes at
58 * a time where possible.
60 * Note: r12 (aka ip) can be trashed during the function along with
61 * r0-r3 although r0-r2 have defined uses i.e. src, dest, len through out.
62 * Additional registers are preserved prior to use i.e. r4, r5 & lr
64 * Apologies for the state of the comments ;-)
66 /* LINTSTUB: Func: void *memcpy(void *dst, const void *src, size_t len) */
68 /* save leaf functions having to store this away */
69 stmdb sp!, {r0, lr} /* memcpy() returns dest addr */
72 blt .Lmemcpy_l4 /* less than 4 bytes */
74 bne .Lmemcpy_destul /* oh unaligned destination addr */
76 bne .Lmemcpy_srcul /* oh unaligned source addr */
79 /* We have aligned source and destination */
81 blt .Lmemcpy_l12 /* less than 12 bytes (4 from above) */
83 blt .Lmemcpy_l32 /* less than 32 bytes (12 from above) */
84 stmdb sp!, {r4} /* borrow r4 */
86 /* blat 32 bytes at a time */
87 /* XXX for really big copies perhaps we should use more registers */
89 ldmia r1!, {r3, r4, r12, lr}
90 stmia r0!, {r3, r4, r12, lr}
91 ldmia r1!, {r3, r4, r12, lr}
92 stmia r0!, {r3, r4, r12, lr}
97 ldmiage r1!, {r3, r4, r12, lr} /* blat a remaining 16 bytes */
98 stmiage r0!, {r3, r4, r12, lr}
100 ldmia sp!, {r4} /* return r4 */
105 /* blat 12 bytes at a time */
107 ldmiage r1!, {r3, r12, lr}
108 stmiage r0!, {r3, r12, lr}
119 ldmiage r1!, {r3, r12}
120 stmiage r0!, {r3, r12}
124 /* less than 4 bytes to go */
127 ldmiaeq sp!, {r0, pc}^ /* done */
129 ldmiaeq sp!, {r0, pc} /* done */
131 /* copy the crud byte at a time */
141 /* erg - unaligned destination */
146 /* align destination with byte copies */
154 blt .Lmemcpy_l4 /* less the 4 bytes */
157 beq .Lmemcpy_t8 /* we have an aligned source */
159 /* erg - unaligned source */
160 /* This is where it gets nasty ... */
168 blt .Lmemcpy_srcul1loop4
172 .Lmemcpy_srcul1loop16:
178 ldmia r1!, {r4, r5, r12, lr}
180 orr r3, r3, r4, lsr #24
182 orr r4, r4, r5, lsr #24
184 orr r5, r5, r12, lsr #24
186 orr r12, r12, lr, lsr #24
188 orr r3, r3, r4, lsl #24
190 orr r4, r4, r5, lsl #24
192 orr r5, r5, r12, lsl #24
194 orr r12, r12, lr, lsl #24
196 stmia r0!, {r3-r5, r12}
198 bge .Lmemcpy_srcul1loop16
201 blt .Lmemcpy_srcul1l4
203 .Lmemcpy_srcul1loop4:
211 orr r12, r12, lr, lsr #24
213 orr r12, r12, lr, lsl #24
217 bge .Lmemcpy_srcul1loop4
225 blt .Lmemcpy_srcul2loop4
229 .Lmemcpy_srcul2loop16:
235 ldmia r1!, {r4, r5, r12, lr}
237 orr r3, r3, r4, lsr #16
239 orr r4, r4, r5, lsr #16
241 orr r5, r5, r12, lsr #16
242 mov r12, r12, lsl #16
243 orr r12, r12, lr, lsr #16
245 orr r3, r3, r4, lsl #16
247 orr r4, r4, r5, lsl #16
249 orr r5, r5, r12, lsl #16
250 mov r12, r12, lsr #16
251 orr r12, r12, lr, lsl #16
253 stmia r0!, {r3-r5, r12}
255 bge .Lmemcpy_srcul2loop16
258 blt .Lmemcpy_srcul2l4
260 .Lmemcpy_srcul2loop4:
268 orr r12, r12, lr, lsr #16
270 orr r12, r12, lr, lsl #16
274 bge .Lmemcpy_srcul2loop4
282 blt .Lmemcpy_srcul3loop4
286 .Lmemcpy_srcul3loop16:
292 ldmia r1!, {r4, r5, r12, lr}
294 orr r3, r3, r4, lsr #8
296 orr r4, r4, r5, lsr #8
298 orr r5, r5, r12, lsr #8
299 mov r12, r12, lsl #24
300 orr r12, r12, lr, lsr #8
302 orr r3, r3, r4, lsl #8
304 orr r4, r4, r5, lsl #8
306 orr r5, r5, r12, lsl #8
307 mov r12, r12, lsr #24
308 orr r12, r12, lr, lsl #8
310 stmia r0!, {r3-r5, r12}
312 bge .Lmemcpy_srcul3loop16
315 blt .Lmemcpy_srcul3l4
317 .Lmemcpy_srcul3loop4:
325 orr r12, r12, lr, lsr #8
327 orr r12, r12, lr, lsl #8
331 bge .Lmemcpy_srcul3loop4