blob: d1e4372e11e0f82785f0a8742ec1d5a6f59f5075 [file] [log] [blame]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughes851e68a2014-02-19 16:53:20 -080029#include <private/bionic_asm.h>
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080030
31 /*
32 * Optimized memcpy() for ARM.
33 *
34 * note that memcpy() always returns the destination pointer,
35 * so we have to preserve R0.
36 */
37
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -070038 .syntax unified
39
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080040ENTRY(memcpy)
41 /* The stack must always be 64-bits aligned to be compliant with the
42 * ARM ABI. Since we have to save R0, we might as well save R4
43 * which we can use for better pipelining of the reads below
44 */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080045 stmfd sp!, {r0, r4, lr}
Christopher Ferris7123d432014-10-17 14:08:54 -070046 .cfi_def_cfa_offset 12
47 .cfi_rel_offset r0, 0
48 .cfi_rel_offset r4, 4
49 .cfi_rel_offset lr, 8
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080050 /* Making room for r5-r11 which will be spilled later */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080051 sub sp, sp, #28
Christopher Ferris7123d432014-10-17 14:08:54 -070052 .cfi_adjust_cfa_offset 28
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080053
54 // preload the destination because we'll align it to a cache line
55 // with small writes. Also start the source "pump".
Elliott Hughesc54ca402013-12-13 12:17:13 -080056 pld [r0, #0]
57 pld [r1, #0]
58 pld [r1, #32]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080059
60 /* it simplifies things to take care of len<4 early */
61 cmp r2, #4
Christopher Ferris7123d432014-10-17 14:08:54 -070062 blo .Lcopy_last_3_and_return
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080063
64 /* compute the offset to align the source
65 * offset = (4-(src&3))&3 = -src & 3
66 */
67 rsb r3, r1, #0
68 ands r3, r3, #3
Christopher Ferris7123d432014-10-17 14:08:54 -070069 beq .Lsrc_aligned
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080070
71 /* align source to 32 bits. We need to insert 2 instructions between
72 * a ldr[b|h] and str[b|h] because byte and half-word instructions
73 * stall 2 cycles.
74 */
75 movs r12, r3, lsl #31
76 sub r2, r2, r3 /* we know that r3 <= r2 because r2 >= 4 */
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -070077 ldrbmi r3, [r1], #1
78 ldrbcs r4, [r1], #1
79 ldrbcs r12,[r1], #1
80 strbmi r3, [r0], #1
81 strbcs r4, [r0], #1
82 strbcs r12,[r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080083
Christopher Ferris7123d432014-10-17 14:08:54 -070084.Lsrc_aligned:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080085
86 /* see if src and dst are aligned together (congruent) */
87 eor r12, r0, r1
88 tst r12, #3
Christopher Ferris7123d432014-10-17 14:08:54 -070089 bne .Lnon_congruent
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080090
91 /* Use post-incriment mode for stm to spill r5-r11 to reserved stack
92 * frame. Don't update sp.
93 */
94 stmea sp, {r5-r11}
95
96 /* align the destination to a cache-line */
97 rsb r3, r0, #0
98 ands r3, r3, #0x1C
Christopher Ferris7123d432014-10-17 14:08:54 -070099 beq .Lcongruent_aligned32
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800100 cmp r3, r2
101 andhi r3, r2, #0x1C
102
103 /* conditionally copies 0 to 7 words (length in r3) */
104 movs r12, r3, lsl #28
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700105 ldmcs r1!, {r4, r5, r6, r7} /* 16 bytes */
106 ldmmi r1!, {r8, r9} /* 8 bytes */
107 stmcs r0!, {r4, r5, r6, r7}
108 stmmi r0!, {r8, r9}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800109 tst r3, #0x4
110 ldrne r10,[r1], #4 /* 4 bytes */
111 strne r10,[r0], #4
112 sub r2, r2, r3
113
Christopher Ferris7123d432014-10-17 14:08:54 -0700114.Lcongruent_aligned32:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800115 /*
116 * here source is aligned to 32 bytes.
117 */
118
Christopher Ferris7123d432014-10-17 14:08:54 -0700119.Lcached_aligned32:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800120 subs r2, r2, #32
Christopher Ferris7123d432014-10-17 14:08:54 -0700121 blo .Lless_than_32_left
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800122
123 /*
124 * We preload a cache-line up to 64 bytes ahead. On the 926, this will
125 * stall only until the requested world is fetched, but the linefill
126 * continues in the the background.
127 * While the linefill is going, we write our previous cache-line
128 * into the write-buffer (which should have some free space).
129 * When the linefill is done, the writebuffer will
130 * start dumping its content into memory
131 *
132 * While all this is going, we then load a full cache line into
133 * 8 registers, this cache line should be in the cache by now
134 * (or partly in the cache).
135 *
136 * This code should work well regardless of the source/dest alignment.
137 *
138 */
139
140 // Align the preload register to a cache-line because the cpu does
141 // "critical word first" (the first word requested is loaded first).
142 bic r12, r1, #0x1F
143 add r12, r12, #64
144
1451: ldmia r1!, { r4-r11 }
Elliott Hughesc54ca402013-12-13 12:17:13 -0800146 pld [r12, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800147 subs r2, r2, #32
148
149 // NOTE: if r12 is more than 64 ahead of r1, the following ldrhi
150 // for ARM9 preload will not be safely guarded by the preceding subs.
151 // When it is safely guarded the only possibility to have SIGSEGV here
152 // is because the caller overstates the length.
153 ldrhi r3, [r12], #32 /* cheap ARM9 preload */
154 stmia r0!, { r4-r11 }
155 bhs 1b
156
157 add r2, r2, #32
158
Christopher Ferris7123d432014-10-17 14:08:54 -0700159.Lless_than_32_left:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800160 /*
161 * less than 32 bytes left at this point (length in r2)
162 */
163
164 /* skip all this if there is nothing to do, which should
165 * be a common case (if not executed the code below takes
166 * about 16 cycles)
167 */
168 tst r2, #0x1F
169 beq 1f
170
171 /* conditionnaly copies 0 to 31 bytes */
172 movs r12, r2, lsl #28
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700173 ldmcs r1!, {r4, r5, r6, r7} /* 16 bytes */
174 ldmmi r1!, {r8, r9} /* 8 bytes */
175 stmcs r0!, {r4, r5, r6, r7}
176 stmmi r0!, {r8, r9}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800177 movs r12, r2, lsl #30
178 ldrcs r3, [r1], #4 /* 4 bytes */
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700179 ldrhmi r4, [r1], #2 /* 2 bytes */
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800180 strcs r3, [r0], #4
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700181 strhmi r4, [r0], #2
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800182 tst r2, #0x1
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700183 ldrbne r3, [r1] /* last byte */
184 strbne r3, [r0]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800185
186 /* we're done! restore everything and return */
1871: ldmfd sp!, {r5-r11}
Christopher Ferrise1e434a2015-07-06 12:03:40 -0700188 ldmfd sp!, {r0, r4, pc}
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800189
190 /********************************************************************/
191
Christopher Ferris7123d432014-10-17 14:08:54 -0700192.Lnon_congruent:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800193 /*
194 * here source is aligned to 4 bytes
195 * but destination is not.
196 *
197 * in the code below r2 is the number of bytes read
198 * (the number of bytes written is always smaller, because we have
199 * partial words in the shift queue)
200 */
201 cmp r2, #4
Christopher Ferris7123d432014-10-17 14:08:54 -0700202 blo .Lcopy_last_3_and_return
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800203
Christopher Ferris7123d432014-10-17 14:08:54 -0700204 /* Use post-increment mode for stm to spill r5-r11 to reserved stack
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800205 * frame. Don't update sp.
206 */
207 stmea sp, {r5-r11}
208
209 /* compute shifts needed to align src to dest */
210 rsb r5, r0, #0
211 and r5, r5, #3 /* r5 = # bytes in partial words */
212 mov r12, r5, lsl #3 /* r12 = right */
213 rsb lr, r12, #32 /* lr = left */
214
215 /* read the first word */
216 ldr r3, [r1], #4
217 sub r2, r2, #4
218
219 /* write a partial word (0 to 3 bytes), such that destination
220 * becomes aligned to 32 bits (r5 = nb of words to copy for alignment)
221 */
222 movs r5, r5, lsl #31
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700223 strbmi r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800224 movmi r3, r3, lsr #8
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700225 strbcs r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800226 movcs r3, r3, lsr #8
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700227 strbcs r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800228 movcs r3, r3, lsr #8
229
230 cmp r2, #4
Christopher Ferris7123d432014-10-17 14:08:54 -0700231 blo .Lpartial_word_tail
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800232
233 /* Align destination to 32 bytes (cache line boundary) */
2341: tst r0, #0x1c
235 beq 2f
236 ldr r5, [r1], #4
237 sub r2, r2, #4
238 orr r4, r3, r5, lsl lr
239 mov r3, r5, lsr r12
240 str r4, [r0], #4
241 cmp r2, #4
242 bhs 1b
Christopher Ferris7123d432014-10-17 14:08:54 -0700243 blo .Lpartial_word_tail
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800244
245 /* copy 32 bytes at a time */
2462: subs r2, r2, #32
Christopher Ferris7123d432014-10-17 14:08:54 -0700247 blo .Lless_than_thirtytwo
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800248
249 /* Use immediate mode for the shifts, because there is an extra cycle
250 * for register shifts, which could account for up to 50% of
251 * performance hit.
252 */
253
254 cmp r12, #24
Christopher Ferris7123d432014-10-17 14:08:54 -0700255 beq .Lloop24
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800256 cmp r12, #8
Christopher Ferris7123d432014-10-17 14:08:54 -0700257 beq .Lloop8
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800258
Christopher Ferris7123d432014-10-17 14:08:54 -0700259.Lloop16:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800260 ldr r12, [r1], #4
2611: mov r4, r12
262 ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
Elliott Hughesc54ca402013-12-13 12:17:13 -0800263 pld [r1, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800264 subs r2, r2, #32
265 ldrhs r12, [r1], #4
266 orr r3, r3, r4, lsl #16
267 mov r4, r4, lsr #16
268 orr r4, r4, r5, lsl #16
269 mov r5, r5, lsr #16
270 orr r5, r5, r6, lsl #16
271 mov r6, r6, lsr #16
272 orr r6, r6, r7, lsl #16
273 mov r7, r7, lsr #16
274 orr r7, r7, r8, lsl #16
275 mov r8, r8, lsr #16
276 orr r8, r8, r9, lsl #16
277 mov r9, r9, lsr #16
278 orr r9, r9, r10, lsl #16
279 mov r10, r10, lsr #16
280 orr r10, r10, r11, lsl #16
281 stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
282 mov r3, r11, lsr #16
283 bhs 1b
Christopher Ferris7123d432014-10-17 14:08:54 -0700284 b .Lless_than_thirtytwo
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800285
Christopher Ferris7123d432014-10-17 14:08:54 -0700286.Lloop8:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800287 ldr r12, [r1], #4
2881: mov r4, r12
289 ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
Elliott Hughesc54ca402013-12-13 12:17:13 -0800290 pld [r1, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800291 subs r2, r2, #32
292 ldrhs r12, [r1], #4
293 orr r3, r3, r4, lsl #24
294 mov r4, r4, lsr #8
295 orr r4, r4, r5, lsl #24
296 mov r5, r5, lsr #8
297 orr r5, r5, r6, lsl #24
298 mov r6, r6, lsr #8
299 orr r6, r6, r7, lsl #24
300 mov r7, r7, lsr #8
301 orr r7, r7, r8, lsl #24
302 mov r8, r8, lsr #8
303 orr r8, r8, r9, lsl #24
304 mov r9, r9, lsr #8
305 orr r9, r9, r10, lsl #24
306 mov r10, r10, lsr #8
307 orr r10, r10, r11, lsl #24
308 stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
309 mov r3, r11, lsr #8
310 bhs 1b
Christopher Ferris7123d432014-10-17 14:08:54 -0700311 b .Lless_than_thirtytwo
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800312
Christopher Ferris7123d432014-10-17 14:08:54 -0700313.Lloop24:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800314 ldr r12, [r1], #4
3151: mov r4, r12
316 ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
Elliott Hughesc54ca402013-12-13 12:17:13 -0800317 pld [r1, #64]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800318 subs r2, r2, #32
319 ldrhs r12, [r1], #4
320 orr r3, r3, r4, lsl #8
321 mov r4, r4, lsr #24
322 orr r4, r4, r5, lsl #8
323 mov r5, r5, lsr #24
324 orr r5, r5, r6, lsl #8
325 mov r6, r6, lsr #24
326 orr r6, r6, r7, lsl #8
327 mov r7, r7, lsr #24
328 orr r7, r7, r8, lsl #8
329 mov r8, r8, lsr #24
330 orr r8, r8, r9, lsl #8
331 mov r9, r9, lsr #24
332 orr r9, r9, r10, lsl #8
333 mov r10, r10, lsr #24
334 orr r10, r10, r11, lsl #8
335 stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
336 mov r3, r11, lsr #24
337 bhs 1b
338
339
Christopher Ferris7123d432014-10-17 14:08:54 -0700340.Lless_than_thirtytwo:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800341 /* copy the last 0 to 31 bytes of the source */
342 rsb r12, lr, #32 /* we corrupted r12, recompute it */
343 add r2, r2, #32
344 cmp r2, #4
Christopher Ferris7123d432014-10-17 14:08:54 -0700345 blo .Lpartial_word_tail
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800346
3471: ldr r5, [r1], #4
348 sub r2, r2, #4
349 orr r4, r3, r5, lsl lr
350 mov r3, r5, lsr r12
351 str r4, [r0], #4
352 cmp r2, #4
353 bhs 1b
354
Christopher Ferris7123d432014-10-17 14:08:54 -0700355.Lpartial_word_tail:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800356 /* we have a partial word in the input buffer */
357 movs r5, lr, lsl #(31-3)
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700358 strbmi r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800359 movmi r3, r3, lsr #8
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700360 strbcs r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800361 movcs r3, r3, lsr #8
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700362 strbcs r3, [r0], #1
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800363
364 /* Refill spilled registers from the stack. Don't update sp. */
365 ldmfd sp, {r5-r11}
366
Christopher Ferris7123d432014-10-17 14:08:54 -0700367.Lcopy_last_3_and_return:
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800368 movs r2, r2, lsl #31 /* copy remaining 0, 1, 2 or 3 bytes */
Chih-Hung Hsieh33f33512015-05-11 11:21:19 -0700369 ldrbmi r2, [r1], #1
370 ldrbcs r3, [r1], #1
371 ldrbcs r12,[r1]
372 strbmi r2, [r0], #1
373 strbcs r3, [r0], #1
374 strbcs r12,[r0]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800375
376 /* we're done! restore sp and spilled registers and return */
377 add sp, sp, #28
Christopher Ferrise1e434a2015-07-06 12:03:40 -0700378 ldmfd sp!, {r0, r4, pc}
Christopher Ferris7123d432014-10-17 14:08:54 -0700379END(memcpy)