1 /* $NetBSD: memcpy_arm.S,v 1.2 2008/04/28 20:22:52 martin 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>
35 * This is one fun bit of code ...
36 * Some easy listening music is suggested while trying to understand this
37 * code e.g. Iron Maiden
39 * For anyone attempting to understand it :
41 * The core code is implemented here with simple stubs for memcpy().
43 * All local labels are prefixed with Lmemcpy_
44 * Following the prefix a label starting f is used in the forward copy code
45 * while a label using b is used in the backwards copy code
46 * The source and destination addresses determine whether a forward or
47 * backward copy is performed.
48 * Separate bits of code are used to deal with the following situations
49 * for both the forward and backwards copy.
50 * unaligned source address
51 * unaligned destination address
52 * Separate copy routines are used to produce an optimised result for each
54 * The copy code will use LDM/STM instructions to copy up to 32 bytes at
55 * a time where possible.
57 * Note: r12 (aka ip) can be trashed during the function along with
58 * r0-r3 although r0-r2 have defined uses i.e. src, dest, len through out.
59 * Additional registers are preserved prior to use i.e. r4, r5 & lr
61 * Apologies for the state of the comments ;-)
63 /* LINTSTUB: Func: void *memcpy(void *dst, const void *src, size_t len) */
65 /* save leaf functions having to store this away */
66 stmdb sp!, {r0, lr} /* memcpy() returns dest addr */
69 blt .Lmemcpy_l4 /* less than 4 bytes */
71 bne .Lmemcpy_destul /* oh unaligned destination addr */
73 bne .Lmemcpy_srcul /* oh unaligned source addr */
76 /* We have aligned source and destination */
78 blt .Lmemcpy_l12 /* less than 12 bytes (4 from above) */
80 blt .Lmemcpy_l32 /* less than 32 bytes (12 from above) */
81 stmdb sp!, {r4} /* borrow r4 */
83 /* blat 32 bytes at a time */
84 /* XXX for really big copies perhaps we should use more registers */
86 ldmia r1!, {r3, r4, r12, lr}
87 stmia r0!, {r3, r4, r12, lr}
88 ldmia r1!, {r3, r4, r12, lr}
89 stmia r0!, {r3, r4, r12, lr}
94 ldmgeia r1!, {r3, r4, r12, lr} /* blat a remaining 16 bytes */
95 stmgeia r0!, {r3, r4, r12, lr}
97 ldmia sp!, {r4} /* return r4 */
102 /* blat 12 bytes at a time */
104 ldmgeia r1!, {r3, r12, lr}
105 stmgeia r0!, {r3, r12, lr}
116 ldmgeia r1!, {r3, r12}
117 stmgeia r0!, {r3, r12}
121 /* less than 4 bytes to go */
124 ldmeqia sp!, {r0, pc}^ /* done */
126 ldmeqia sp!, {r0, pc} /* done */
128 /* copy the crud byte at a time */
138 /* erg - unaligned destination */
143 /* align destination with byte copies */
151 blt .Lmemcpy_l4 /* less the 4 bytes */
154 beq .Lmemcpy_t8 /* we have an aligned source */
156 /* erg - unaligned source */
157 /* This is where it gets nasty ... */
165 blt .Lmemcpy_srcul1loop4
169 .Lmemcpy_srcul1loop16:
175 ldmia r1!, {r4, r5, r12, lr}
177 orr r3, r3, r4, lsr #24
179 orr r4, r4, r5, lsr #24
181 orr r5, r5, r12, lsr #24
183 orr r12, r12, lr, lsr #24
185 orr r3, r3, r4, lsl #24
187 orr r4, r4, r5, lsl #24
189 orr r5, r5, r12, lsl #24
191 orr r12, r12, lr, lsl #24
193 stmia r0!, {r3-r5, r12}
195 bge .Lmemcpy_srcul1loop16
198 blt .Lmemcpy_srcul1l4
200 .Lmemcpy_srcul1loop4:
208 orr r12, r12, lr, lsr #24
210 orr r12, r12, lr, lsl #24
214 bge .Lmemcpy_srcul1loop4
222 blt .Lmemcpy_srcul2loop4
226 .Lmemcpy_srcul2loop16:
232 ldmia r1!, {r4, r5, r12, lr}
234 orr r3, r3, r4, lsr #16
236 orr r4, r4, r5, lsr #16
238 orr r5, r5, r12, lsr #16
239 mov r12, r12, lsl #16
240 orr r12, r12, lr, lsr #16
242 orr r3, r3, r4, lsl #16
244 orr r4, r4, r5, lsl #16
246 orr r5, r5, r12, lsl #16
247 mov r12, r12, lsr #16
248 orr r12, r12, lr, lsl #16
250 stmia r0!, {r3-r5, r12}
252 bge .Lmemcpy_srcul2loop16
255 blt .Lmemcpy_srcul2l4
257 .Lmemcpy_srcul2loop4:
265 orr r12, r12, lr, lsr #16
267 orr r12, r12, lr, lsl #16
271 bge .Lmemcpy_srcul2loop4
279 blt .Lmemcpy_srcul3loop4
283 .Lmemcpy_srcul3loop16:
289 ldmia r1!, {r4, r5, r12, lr}
291 orr r3, r3, r4, lsr #8
293 orr r4, r4, r5, lsr #8
295 orr r5, r5, r12, lsr #8
296 mov r12, r12, lsl #24
297 orr r12, r12, lr, lsr #8
299 orr r3, r3, r4, lsl #8
301 orr r4, r4, r5, lsl #8
303 orr r5, r5, r12, lsl #8
304 mov r12, r12, lsr #24
305 orr r12, r12, lr, lsl #8
307 stmia r0!, {r3-r5, r12}
309 bge .Lmemcpy_srcul3loop16
312 blt .Lmemcpy_srcul3l4
314 .Lmemcpy_srcul3loop4:
322 orr r12, r12, lr, lsr #8
324 orr r12, r12, lr, lsl #8
328 bge .Lmemcpy_srcul3loop4