2 * Copyright (c) 2010 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 * This file implements the following functions for the Cortex-A9 processor:
25 * void bcopy(const void * source,
29 * void *memmove(void * destination,
30 * const void * source,
33 * void *memcpy(void * restrict destination,
34 * const void * restrict source,
37 * All copy n successive bytes from source to destination. Memmove and memcpy
38 * return destination, whereas bcopy has no return value. Copying takes place
39 * as if it were through a temporary buffer -- after return destination
40 * contains exactly the bytes from source, even if the buffers overlap (this is
41 * not required of memcpy by the C standard; its behavior is undefined if the
42 * buffers overlap, but we are holding ourselves to the historical behavior of
43 * this function on OS X and iOS).
47 #if defined _ARM_ARCH_7 && !defined VARIANT_DYLD
49 /*****************************************************************************
51 *****************************************************************************/
53 #define A9_ENTRY(name) \
55 .globl _ ## name ## $VARIANT$CortexA9;\
56 _ ## name ## $VARIANT$CortexA9:
58 #define ESTABLISH_FRAME \
62 #define CLEAR_FRAME_AND_RETURN \
65 #define ADDITIONAL_CALLEE_SAVE_REGISTERS {r5,r6,r8,r10}
67 #define COPY_REGISTERS {r3,r4,r5,r6,r8,r9,r10,r12}
69 /*****************************************************************************
71 *****************************************************************************/
78 // Translate bcopy calls into memcpy calls by swapping the first and second
86 // Our preference is to copy the data in ascending address order, but if the
87 // buffers overlap such that the beginning of the destination buffer aliases
88 // the end of the source buffer, we need to copy in descending address order
89 // instead to preserve the memmove semantics. We detect this case with the
92 // destination - source < length (unsigned compare)
94 // If the address of the source buffer is higher than the address of the
95 // destination buffer, this arithmetic can overflow, but the overflowed value
96 // can only be smaller than length if the buffers do not overlap, so we don't
97 // need to worry about false positives due to the overflow (they happen, but
98 // only in cases where copying in either order is correct).
105 /*****************************************************************************
107 *****************************************************************************/
109 // The layout of the two buffers is such that we can use our preferred
110 // (ascending address order) copy implementation. Throughout this copy,
111 // registers are used as follows:
113 // r0 lowest unwritten address in the destination buffer.
114 // r1 lowest unread address in the source buffer.
115 // r2 number of bytes remaining to copy less an offset that varies
116 // with the size of the copies that are being made.
117 // r3, r4, r5, r6, r8, r9, r10, r12
118 // temporary registers used to hold the data during copies.
119 // r12 also used as a scratch register for alignment / length calculations
122 // We begin by checking if less than four bytes are to be copied; if so, we
123 // branch directly to a small-buffer copy and return. Otherwise, we copy up
124 // to three bytes if needed to make the destination pointer have word (four
127 blo L_ascendingLengthLessThanFour
129 beq L_ascendingDestinationWordAligned
139 bhs L_ascendingDestinationWordAligned
141 L_ascendingLengthLessThanFour:
142 // Conditionally copies up to three bytes, assuming no alignment. This is
143 // only used if the original length of the buffer is smaller than four.
151 CLEAR_FRAME_AND_RETURN
153 L_ascendingDestinationWordAligned:
154 // We know that the destination has word alignment. If the source is not
155 // similarly aligned, jump to an unaligned copy loop.
157 bne L_ascendingUnalignedCopy
159 /*****************************************************************************
160 * ascending copy, both buffers have word alignment *
161 *****************************************************************************/
163 // If less than sixty-four bytes remain to be copied, jump directly to the
164 // word-aligned cleanup path. Otherwise, we copy up to 28 bytes as needed
165 // to make the destination pointer have cacheline alignment.
167 blo L_ascendingLengthLessThanSixtyFour
169 beq L_ascendingDestinationCachelineAligned
174 b L_ascendingLengthLessThanSixtyFour
176 L_ascendingDestinationCachelineAligned:
177 // Unrolled main copy loop; copies two cachelines (64 bytes) per iteration.
178 // Empirical testing suggests that 0x60 is the optimal lookahead for preload,
179 // though anything between 0x40 and 0x100 seems to be "acceptable".
180 push ADDITIONAL_CALLEE_SAVE_REGISTERS
181 0: ldm r1!, COPY_REGISTERS
183 stm r0!, COPY_REGISTERS
185 ldm r1!, COPY_REGISTERS
187 stm r0!, COPY_REGISTERS
189 pop ADDITIONAL_CALLEE_SAVE_REGISTERS
191 L_ascendingLengthLessThanSixtyFour:
192 // Cleanup copy of up to 63 bytes. We can assume that both the source and
193 // destination addresses have word alignment here.
196 0: ldm r1!, {r3,r4,r9,ip}
198 stm r0!, {r3,r4,r9,ip}
213 2: CLEAR_FRAME_AND_RETURN
215 /*****************************************************************************
216 * ascending copy, source buffer is not word aligned *
217 *****************************************************************************/
219 L_ascendingUnalignedCopy:
220 // Destination buffer is word aligned, but source buffer is not. Copy
221 // byte-by-byte until the destination buffer has eightbyte alignment.
223 blo L_ascendingUnalignedByteCleanup
225 beq L_ascendingUnalignedVectorCopy
230 L_ascendingUnalignedByteCleanup:
237 1: CLEAR_FRAME_AND_RETURN
239 L_ascendingUnalignedVectorCopy:
240 // Destination buffer is eightbyte aligned. Source buffer has unknown
241 // alignment. Use NEON to handle the misaligned copies. We begin by copying
242 // up to 24 bytes to get cacheline alignment of the destination buffer.
244 blo L_ascendingUnalignedVectorCleanup
246 beq L_ascendingUnalignedCachelineCopy
249 vst1.8 {d0}, [r0,:64]!
251 L_ascendingUnalignedVectorCleanup:
253 blo L_ascendingUnalignedByteCleanup
254 0: vld1.8 {d0}, [r1]!
256 vst1.8 {d0}, [r0,:64]!
258 b L_ascendingUnalignedByteCleanup
260 L_ascendingUnalignedCachelineCopy:
261 // Main copy loop; moves 32 bytes per iteration. Requires only byte alignment
262 // of the source address.
265 vst1.8 {q0,q1},[r0,:256]!
267 bhs L_ascendingUnalignedCachelineCopy
268 b L_ascendingUnalignedVectorCleanup
270 /*****************************************************************************
272 *****************************************************************************/
274 // The layout of the two buffers is such that we must copy in descending-
275 // address order. Throughout this copy, registers are used as follows:
277 // r0 lowest address in the destination buffer that has been written to.
278 // r1 lowest address in the source buffer that has been read from.
279 // r2 number of bytes remaining to copy less an offset that varies
280 // with the size of the copies that are being made.
281 // r3, r4, r5, r6, r8, r9, r10, r12
282 // temporary registers used to hold the data during copies.
283 // r12 also used as a scratch register for alignment / length calculations
286 // We begin by checking if less than four bytes are to be copied; if so, we
287 // branch directly to a small-buffer copy and return. Otherwise, we copy up
288 // to three bytes if needed to make the destination pointer have word (four
293 blo L_descendingLengthLessThanFour
295 beq L_descendingDestinationWordAligned
298 ldrbhs r4, [r1, #-1]!
300 ldrbhi r3, [r1, #-1]!
301 strbhs r4, [r0, #-1]!
302 strbhi r3, [r0, #-1]!
304 bhs L_descendingDestinationWordAligned
306 L_descendingLengthLessThanFour:
307 // Conditionally copies up to three bytes, assuming no alignment. This is
308 // only used if the original length of the buffer is smaller than four.
310 ldrbcs r3, [r1, #-1]!
311 ldrbcs ip, [r1, #-1]!
313 strbcs r3, [r0, #-1]!
314 strbcs ip, [r0, #-1]!
316 CLEAR_FRAME_AND_RETURN
318 L_descendingDestinationWordAligned:
319 // We know that the destination has word alignment. If the source is not
320 // similarly aligned, jump to an unaligned copy loop.
322 bne L_descendingUnalignedCopy
324 /*****************************************************************************
325 * descending copy, both buffers have word alignment *
326 *****************************************************************************/
328 // If less than sixty-four bytes remain to be copied, jump directly to the
329 // word-aligned cleanup path. Otherwise, we copy up to 28 bytes as needed
330 // to make the destination pointer have cacheline alignment.
332 blo L_descendingLengthLessThanSixtyFour
334 beq L_descendingDestinationCachelineAligned
339 b L_descendingLengthLessThanSixtyFour
341 L_descendingDestinationCachelineAligned:
342 // Unrolled main copy loop; copies two cachelines (64 bytes) per iteration.
343 // Empirical testing suggests that -0x80 is the optimal lookahead for preload,
344 // though anything between -0x40 and -0x100 seems to be "acceptable".
345 push ADDITIONAL_CALLEE_SAVE_REGISTERS
346 0: ldmdb r1!, COPY_REGISTERS
348 stmdb r0!, COPY_REGISTERS
350 ldmdb r1!, COPY_REGISTERS
352 stmdb r0!, COPY_REGISTERS
354 pop ADDITIONAL_CALLEE_SAVE_REGISTERS
356 L_descendingLengthLessThanSixtyFour:
357 // Cleanup copy of up to 63 bytes. We can assume that both the source and
358 // destination addresses have word alignment here.
361 0: ldmdb r1!, {r3,r4,r9,ip}
363 stmdb r0!, {r3,r4,r9,ip}
374 ldrhcs r3, [r1, #-2]!
375 strhcs r3, [r0, #-2]!
378 2: CLEAR_FRAME_AND_RETURN
380 /*****************************************************************************
381 * descending copy, source buffer is not word aligned *
382 *****************************************************************************/
384 L_descendingUnalignedCopy:
385 // Destination buffer is word aligned, but source buffer is not. Copy
386 // byte-by-byte until the destination buffer has eightbyte alignment.
388 blo L_descendingUnalignedByteCleanup
390 beq L_descendingUnalignedVectorCopy
395 L_descendingUnalignedByteCleanup:
398 0: ldrb r3, [r1, #-1]!
402 1: CLEAR_FRAME_AND_RETURN
404 L_descendingUnalignedVectorCopy:
405 // Destination buffer is eightbyte aligned. Source buffer has unknown
406 // alignment. Use NEON to handle the misaligned copies. We begin by copying
407 // up to 24 bytes to get cacheline alignment of the destination buffer.
409 blo L_descendingUnalignedVectorCleanup
411 beq L_descendingUnalignedCachelineCopy
415 vst1.8 {d0}, [r0,:64]
418 L_descendingUnalignedVectorCleanup:
420 blo L_descendingUnalignedByteCleanup
424 vst1.8 {d0}, [r0,:64]
427 b L_descendingUnalignedByteCleanup
429 L_descendingUnalignedCachelineCopy:
430 // Main copy loop; moves 32 bytes per iteration. Requires only byte alignment
431 // of the source address.
435 0: vld1.8 {q0,q1},[r1], r4
437 vst1.8 {q0,q1},[r0,:256], r4
442 b L_descendingUnalignedVectorCleanup
444 #endif // defined _ARM_ARCH_7 && !defined VARIANT_DYLD