1 /* Copyright (c) 2012, Linaro Limited
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 * Neither the name of the Linaro nor the
12 names of its contributors may be used to endorse or promote products
13 derived from this software without specific prior written permission.
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 FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
28 * Copyright (c) 2015 ARM Ltd
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. The name of the company may not be used to endorse or promote
40 * products derived from this software without specific prior written
43 * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
44 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
45 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46 * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
48 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
49 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
50 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 * ARMv8-a, AArch64, unaligned accesses.
86 .macro def_fn f p2align=0
94 /* Copies are split into 3 main cases: small copies of up to 16 bytes,
95 medium copies of 17..96 bytes which are fully unrolled. Large copies
96 of more than 96 bytes align the destination and use an unrolled loop
97 processing 64 bytes per iteration.
98 Small and medium copies read all data before writing, allowing any
99 kind of overlap, and memmove tailcalls memcpy for these cases as
100 well as non-overlapping copies.
103 def_fn memcpy p2align=6
104 prfm PLDL1KEEP, [src]
105 add srcend, src, count
106 add dstend, dstin, count
112 /* Medium copies: 17..96 bytes. */
115 tbnz tmp1, 6, L(copy96)
116 ldp D_l, D_h, [srcend, -16]
118 ldp B_l, B_h, [src, 16]
119 ldp C_l, C_h, [srcend, -32]
120 stp B_l, B_h, [dstin, 16]
121 stp C_l, C_h, [dstend, -32]
123 stp A_l, A_h, [dstin]
124 stp D_l, D_h, [dstend, -16]
128 /* Small copies: 0..16 bytes. */
133 ldr A_h, [srcend, -8]
135 str A_h, [dstend, -8]
141 ldr A_hw, [srcend, -4]
143 str A_hw, [dstend, -4]
146 /* Copy 0..3 bytes. Use a branchless sequence that copies the same
147 byte 3 times if count==1, or the 2nd byte twice if count==2. */
152 ldrb A_hw, [srcend, -1]
153 ldrb B_lw, [src, tmp1]
155 strb B_lw, [dstin, tmp1]
156 strb A_hw, [dstend, -1]
160 /* Copy 64..96 bytes. Copy 64 bytes from the start and
161 32 bytes from the end. */
163 ldp B_l, B_h, [src, 16]
164 ldp C_l, C_h, [src, 32]
165 ldp D_l, D_h, [src, 48]
166 ldp E_l, E_h, [srcend, -32]
167 ldp F_l, F_h, [srcend, -16]
168 stp A_l, A_h, [dstin]
169 stp B_l, B_h, [dstin, 16]
170 stp C_l, C_h, [dstin, 32]
171 stp D_l, D_h, [dstin, 48]
172 stp E_l, E_h, [dstend, -32]
173 stp F_l, F_h, [dstend, -16]
176 /* Align DST to 16 byte alignment so that we don't cross cache line
177 boundaries on both loads and stores. There are at least 96 bytes
178 to copy, so copy 16 bytes unaligned and then align. The loop
179 copies 64 bytes per iteration and prefetches one iteration ahead. */
187 add count, count, tmp1 /* Count is now 16 too large. */
188 ldp A_l, A_h, [src, 16]
189 stp D_l, D_h, [dstin]
190 ldp B_l, B_h, [src, 32]
191 ldp C_l, C_h, [src, 48]
192 ldp D_l, D_h, [src, 64]!
193 subs count, count, 128 + 16 /* Test and readjust count. */
196 stp A_l, A_h, [dst, 16]
197 ldp A_l, A_h, [src, 16]
198 stp B_l, B_h, [dst, 32]
199 ldp B_l, B_h, [src, 32]
200 stp C_l, C_h, [dst, 48]
201 ldp C_l, C_h, [src, 48]
202 stp D_l, D_h, [dst, 64]!
203 ldp D_l, D_h, [src, 64]!
204 subs count, count, 64
207 /* Write the last full set of 64 bytes. The remainder is at most 64
208 bytes, so it is safe to always copy 64 bytes from the end even if
209 there is just 1 byte left. */
211 ldp E_l, E_h, [srcend, -64]
212 stp A_l, A_h, [dst, 16]
213 ldp A_l, A_h, [srcend, -48]
214 stp B_l, B_h, [dst, 32]
215 ldp B_l, B_h, [srcend, -32]
216 stp C_l, C_h, [dst, 48]
217 ldp C_l, C_h, [srcend, -16]
218 stp D_l, D_h, [dst, 64]
219 stp E_l, E_h, [dstend, -64]
220 stp A_l, A_h, [dstend, -48]
221 stp B_l, B_h, [dstend, -32]
222 stp C_l, C_h, [dstend, -16]
225 .size memcpy, . - memcpy