2
* Copyright (C) 2008 The Android Open Source Project
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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
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
29
//#define HAVE_32_BYTE_CACHE_LINE
31
#if defined(__ARM_NEON__)
35
.type memcpy, %function
38
#ifdef HAVE_32_BYTE_CACHE_LINE
39
/* a prefetch distance of 2 cache-lines */
40
#define CACHE_LINE_SIZE 32
41
#define PREFETCH_DISTANCE (CACHE_LINE_SIZE*2)
43
/* a prefetch distance of 4 cache-lines works best experimentally */
44
#define CACHE_LINE_SIZE 64
45
#define PREFETCH_DISTANCE (CACHE_LINE_SIZE*4)
51
/* start preloading as early as possible */
52
pld [r1, #(CACHE_LINE_SIZE*0)]
54
pld [r1, #(CACHE_LINE_SIZE*1)]
56
/* do we have at least 16-bytes to copy (needed for alignment below) */
60
/* align destination to cache-line for the write-buffer */
65
/* copy up to 15-bytes (count in r3) */
76
// copies 4 bytes, destination 32-bits aligned
77
vld4.8 {d0[0], d1[0], d2[0], d3[0]}, [r1]!
78
vst4.8 {d0[0], d1[0], d2[0], d3[0]}, [r0, :32]!
80
// copies 8 bytes, destination 64-bits aligned
82
vst1.8 {d0}, [r0, :64]!
85
0: /* preload immediately the next cache line, which we may need */
86
pld [r1, #(CACHE_LINE_SIZE*0)]
87
pld [r1, #(CACHE_LINE_SIZE*1)]
89
#ifdef HAVE_32_BYTE_CACHE_LINE
90
/* make sure we have at least 32 bytes to copy */
94
/* preload all the cache lines we need.
95
* NOTE: the number of pld below depends on PREFETCH_DISTANCE,
96
* ideally would would increase the distance in the main loop to
97
* avoid the goofy code below. In practice this doesn't seem to make
100
pld [r1, #(PREFETCH_DISTANCE)]
102
1: /* The main loop copies 32 bytes at a time */
103
vld1.8 {d0 - d3}, [r1]!
104
pld [r1, #(PREFETCH_DISTANCE)]
106
vst1.8 {d0 - d3}, [r0, :128]!
109
/* make sure we have at least 64 bytes to copy */
113
/* preload all the cache lines we need.
114
* NOTE: the number of pld below depends on PREFETCH_DISTANCE,
115
* ideally would would increase the distance in the main loop to
116
* avoid the goofy code below. In practice this doesn't seem to make
119
pld [r1, #(CACHE_LINE_SIZE*2)]
120
pld [r1, #(CACHE_LINE_SIZE*3)]
121
pld [r1, #(PREFETCH_DISTANCE)]
123
1: /* The main loop copies 64 bytes at a time */
124
vld1.8 {d0 - d3}, [r1]!
125
vld1.8 {d4 - d7}, [r1]!
126
pld [r1, #(PREFETCH_DISTANCE)]
128
vst1.8 {d0 - d3}, [r0, :128]!
129
vst1.8 {d4 - d7}, [r0, :128]!
132
2: /* fix-up the remaining count and make sure we have >= 32 bytes left */
137
3: /* 32 bytes at a time. These cache lines were already preloaded */
138
vld1.8 {d0 - d3}, [r1]!
140
vst1.8 {d0 - d3}, [r0, :128]!
143
4: /* less than 32 left */
147
// copies 16 bytes, 128-bits aligned
148
vld1.8 {d0, d1}, [r1]!
149
vst1.8 {d0, d1}, [r0, :128]!
151
5: /* copy up to 15-bytes (count in r2) */
157
vld4.8 {d0[0], d1[0], d2[0], d3[0]}, [r1]!
158
vst4.8 {d0[0], d1[0], d2[0], d3[0]}, [r0]!
159
2: movs ip, r2, lsl #31
172
#else /* __ARM_ARCH__ < 7 */
176
* Optimized memcpy() for ARM.
178
* note that memcpy() always returns the destination pointer,
179
* so we have to preserve R0.
183
/* The stack must always be 64-bits aligned to be compliant with the
184
* ARM ABI. Since we have to save R0, we might as well save R4
185
* which we can use for better pipelining of the reads below
188
stmfd sp!, {r0, r4, lr}
189
/* Making room for r5-r11 which will be spilled later */
193
// preload the destination because we'll align it to a cache line
194
// with small writes. Also start the source "pump".
199
/* it simplifies things to take care of len<4 early */
201
blo copy_last_3_and_return
203
/* compute the offset to align the source
204
* offset = (4-(src&3))&3 = -src & 3
210
/* align source to 32 bits. We need to insert 2 instructions between
211
* a ldr[b|h] and str[b|h] because byte and half-word instructions
214
movs r12, r3, lsl #31
215
sub r2, r2, r3 /* we know that r3 <= r2 because r2 >= 4 */
225
/* see if src and dst are aligned together (congruent) */
230
/* Use post-incriment mode for stm to spill r5-r11 to reserved stack
231
* frame. Don't update sp.
235
/* align the destination to a cache-line */
238
beq congruent_aligned32
242
/* conditionnaly copies 0 to 7 words (length in r3) */
243
movs r12, r3, lsl #28
244
ldmcsia r1!, {r4, r5, r6, r7} /* 16 bytes */
245
ldmmiia r1!, {r8, r9} /* 8 bytes */
246
stmcsia r0!, {r4, r5, r6, r7}
247
stmmiia r0!, {r8, r9}
249
ldrne r10,[r1], #4 /* 4 bytes */
255
* here source is aligned to 32 bytes.
260
blo less_than_32_left
263
* We preload a cache-line up to 64 bytes ahead. On the 926, this will
264
* stall only until the requested world is fetched, but the linefill
265
* continues in the the background.
266
* While the linefill is going, we write our previous cache-line
267
* into the write-buffer (which should have some free space).
268
* When the linefill is done, the writebuffer will
269
* start dumping its content into memory
271
* While all this is going, we then load a full cache line into
272
* 8 registers, this cache line should be in the cache by now
273
* (or partly in the cache).
275
* This code should work well regardless of the source/dest alignment.
279
// Align the preload register to a cache-line because the cpu does
280
// "critical word first" (the first word requested is loaded first).
284
1: ldmia r1!, { r4-r11 }
288
// NOTE: if r12 is more than 64 ahead of r1, the following ldrhi
289
// for ARM9 preload will not be safely guarded by the preceding subs.
290
// When it is safely guarded the only possibility to have SIGSEGV here
291
// is because the caller overstates the length.
292
ldrhi r3, [r12], #32 /* cheap ARM9 preload */
293
stmia r0!, { r4-r11 }
303
* less than 32 bytes left at this point (length in r2)
306
/* skip all this if there is nothing to do, which should
307
* be a common case (if not executed the code below takes
313
/* conditionnaly copies 0 to 31 bytes */
314
movs r12, r2, lsl #28
315
ldmcsia r1!, {r4, r5, r6, r7} /* 16 bytes */
316
ldmmiia r1!, {r8, r9} /* 8 bytes */
317
stmcsia r0!, {r4, r5, r6, r7}
318
stmmiia r0!, {r8, r9}
319
movs r12, r2, lsl #30
320
ldrcs r3, [r1], #4 /* 4 bytes */
321
ldrmih r4, [r1], #2 /* 2 bytes */
325
ldrneb r3, [r1] /* last byte */
328
/* we're done! restore everything and return */
329
1: ldmfd sp!, {r5-r11}
330
ldmfd sp!, {r0, r4, lr}
333
/********************************************************************/
337
* here source is aligned to 4 bytes
338
* but destination is not.
340
* in the code below r2 is the number of bytes read
341
* (the number of bytes written is always smaller, because we have
342
* partial words in the shift queue)
345
blo copy_last_3_and_return
347
/* Use post-incriment mode for stm to spill r5-r11 to reserved stack
348
* frame. Don't update sp.
352
/* compute shifts needed to align src to dest */
354
and r5, r5, #3 /* r5 = # bytes in partial words */
355
mov r12, r5, lsl #3 /* r12 = right */
356
rsb lr, r12, #32 /* lr = left */
358
/* read the first word */
362
/* write a partial word (0 to 3 bytes), such that destination
363
* becomes aligned to 32 bits (r5 = nb of words to copy for alignment)
374
blo partial_word_tail
376
/* Align destination to 32 bytes (cache line boundary) */
381
orr r4, r3, r5, lsl lr
386
blo partial_word_tail
388
/* copy 32 bytes at a time */
390
blo less_than_thirtytwo
392
/* Use immediate mode for the shifts, because there is an extra cycle
393
* for register shifts, which could account for up to 50% of
405
ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
409
orr r3, r3, r4, lsl #16
411
orr r4, r4, r5, lsl #16
413
orr r5, r5, r6, lsl #16
415
orr r6, r6, r7, lsl #16
417
orr r7, r7, r8, lsl #16
419
orr r8, r8, r9, lsl #16
421
orr r9, r9, r10, lsl #16
422
mov r10, r10, lsr #16
423
orr r10, r10, r11, lsl #16
424
stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
427
b less_than_thirtytwo
432
ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
436
orr r3, r3, r4, lsl #24
438
orr r4, r4, r5, lsl #24
440
orr r5, r5, r6, lsl #24
442
orr r6, r6, r7, lsl #24
444
orr r7, r7, r8, lsl #24
446
orr r8, r8, r9, lsl #24
448
orr r9, r9, r10, lsl #24
450
orr r10, r10, r11, lsl #24
451
stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
454
b less_than_thirtytwo
459
ldmia r1!, { r5,r6,r7, r8,r9,r10,r11}
463
orr r3, r3, r4, lsl #8
465
orr r4, r4, r5, lsl #8
467
orr r5, r5, r6, lsl #8
469
orr r6, r6, r7, lsl #8
471
orr r7, r7, r8, lsl #8
473
orr r8, r8, r9, lsl #8
475
orr r9, r9, r10, lsl #8
476
mov r10, r10, lsr #24
477
orr r10, r10, r11, lsl #8
478
stmia r0!, {r3,r4,r5,r6, r7,r8,r9,r10}
484
/* copy the last 0 to 31 bytes of the source */
485
rsb r12, lr, #32 /* we corrupted r12, recompute it */
488
blo partial_word_tail
492
orr r4, r3, r5, lsl lr
499
/* we have a partial word in the input buffer */
500
movs r5, lr, lsl #(31-3)
507
/* Refill spilled registers from the stack. Don't update sp. */
510
copy_last_3_and_return:
511
movs r2, r2, lsl #31 /* copy remaining 0, 1, 2 or 3 bytes */
519
/* we're done! restore sp and spilled registers and return */
521
ldmfd sp!, {r0, r4, lr}
526
#endif /* __ARM_ARCH__ < 7 */