2 * Copyright (c) 2006 Apple Computer, 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@
25 #include <mach/machine/asm.h>
28 * This file contains the following functions:
30 * void memset_pattern4(void *b, const void *c4, size_t len)
31 * void memset_pattern8(void *b, const void *c8, size_t len)
32 * void memset_pattern16(void *b, const void *c16, size_t len)
34 * The memset() is implemented in the bzero.s file.
36 * This is a reasonably well optimized version of memset_pattern* routines
37 * implemented for ARM processors using the ARMv4 and later instruction sets.
38 * These routines use the ARM's core registers.
40 * The algorithm is to align the destination pointer on a 16 byte boundary
41 * and then blast data 64 bytes at a time, in two stores of 32 bytes per loop.
48 /*----------------------------------------------------------------------------*/
49 /* void memset_pattern4(void *ptr, const void *pattern4, size_t len); */
51 /* r0 << destination pointer */
52 /* r1 << pointer to 4-byte pattern */
53 /* r2 << 'len' (length of destination buffer in bytes) */
54 /*----------------------------------------------------------------------------*/
55 .globl _memset_pattern4
57 cmp r2, #0 /* check if len is zero */
58 bxeq lr /* return if length is zero */
60 /* We need some registers, so save volatiles on stack */
61 /* Avoid r7 (frame pointer) and r9 (thread register) */
62 stmfd sp!, {r4-r7, lr}
63 add r7, sp, #12 /* establish frame */
64 stmfd sp!, {r8, r10-r11}
66 /* copy destination base pointer r0 to r12 and leave r0 alone */
67 /* so that we return original pointer back to the caller */
70 /* Check if 'len' is long enough to bother alignment of destination */
72 cmp r2, #32 /* long enough to bother aligning? */
73 movlt r3, #4 /* move pattern length into r3 */
74 movlt r10, #4 /* pattern index */
75 movlt r11, r1 /* move pattern pointer into r11 */
78 /* move 'len' into r1, get 4-byte pattern in r2 */
79 mov r6, r2 /* temporarily move 'len' in to r6 */
80 bl L_GetPatternWord /* get unaligned pattern word in r5 */
81 mov r2, r5 /* move pattern word into r2 */
82 mov r0, r12 /* r0 was clobbered - restore it */
83 mov r1, r6 /* move 'len' from r6 to r1 */
85 mov r3, r2 /* copy 4-byte pattern into r3, r4 and r5 registers */
91 /* Check for 16 or 32 byte aligned destination pointer */
92 tst r12, #0x1F /* check for 32 byte aligned */
94 tst r12, #0xF /* check for 16 byte aligned */
96 b L_Unaligned /* yes */
107 cmp r2, #0 /* more bytes left? */
109 ldm sp!, {r8, r10-r11} /* restores registers from stack */
110 ldm sp!, {r4-r7, lr} /* restore & return from subroutine */
113 /* 'len' is long enough to justify aligning the destination pointer */
115 /* By the time we reach here, data is stored in registers as follows: */
116 /* r1 << 'len' (length of destination buffer in bytes) */
117 /* r2-r5 << pattern; either 4x4byte OR 2x8byte OR 1x16-byte */
118 /* r12 << destination pointer copy (scratch register) */
119 /* r0 << destination pointer original */
121 /* Use r11 as scratch register to store the #bytes offset to 16-byte align */
123 /* Unaligned on 32-byte boundary, store 1-15 bytes until 16-byte aligned */
124 /* As we store these bytes, we rotate the pattern stored in r2-r5 to reflect */
128 mov r11, r12, lsl #28
130 msr cpsr_f, r11 /* Bits[31:28] of cpsr now contain #bytes to align*/
132 L_Store15BytesAndRotatePattern:
133 strbvs r2, [r12], #1 /* v is set, unaligned in the 1s column */
134 andvs r6, r2, #0xFF /* Rotate pattern right in r2-r5 by 1-byte */
135 andvs r8, r3, #0xFF /* Consider register r2-r5 and a contiguous */
136 andvs r10, r4, #0xFF /* 16-byte register with r2 containing LSB */
137 andvs r11, r5, #0xFF /* and r5 containing MSB */
142 orrvs r2, r2, r8, lsl #24
143 orrvs r3, r3, r10, lsl #24
144 orrvs r4, r4, r11, lsl #24
145 orrvs r5, r5, r6, lsl #24
147 strhcs r2, [r12], #2 /* c is set, unaligned in the 2s column */
148 movcs r6, r2, lsl #16 /* Rotate pattern right in r2-r5 by 2-bytes */
149 movcs r8, r3, lsl #16
150 movcs r10, r4, lsl #16
151 movcs r11, r5, lsl #16
161 streq r2, [r12], #4 /* z is set, unaligned in the 4s column */
162 moveq r6, r2 /* Rotate pattern right in r2-r5 by 4-bytes */
168 stmmi r12!, {r2-r3} /* n is set, unaligned in the 8s column */
169 movmi r6, r2 /* Rotate pattern right in r2-r5 by 4-bytes */
176 mrs r11, cpsr /*copy cpsr in to r11 */
177 subs r1, r1, r11, lsr #28
178 ldmeq sp!, {r8, r10-r11} /* restores registers from stack */
179 ldmeq sp!, {r4-r7, lr} /* restore & return from subroutine */
182 /* By the time we reach here, we are 16-byte aligned and r2-r5 contains */
183 /* rotated pattern. Now lets make sure we are 32-byte aligned. */
189 /* By the time we reach here, data is stored in registers as follows: */
190 /* r1 << 'len' (remaining length of destination buffer in bytes) */
191 /* r2-r5 << rotated pattern; either 4x4byte OR 2x8byte OR 1x16-byte */
192 /* r12 << aligned destination pointer copy (scratch register) */
195 blt L_AlignedLessThan64
197 /* Copy pattern in four more registers so that we can do 64 byte transfers */
203 /* At this point, we are 16-byte aligned and 'len' is greater than 64 bytes */
204 /* Lets transfer 64 bytes at a time until len becomes less than 64 bytes */
205 sub r1, r1, #64 /* pre-subtract to avoid extra compare in loop */
207 stm r12!, {r2-r6, r8, r10-r11}
209 stm r12!, {r2-r6, r8, r10-r11}
212 /* return if 'len' is zero */
213 adds r1, r1, #64 /* readjust length; previously subtracted extra 64*/
214 ldmeq sp!, {r8, r10-r11} /* restores registers from stack */
215 ldmeq sp!, {r4-r7, lr} /* restore & return from subroutine */
219 /* do we have 16 or more bytes left */
223 bgt L_AlignedLessThan64
224 ldmeq sp!, {r8, r10-r11} /* restores registers from stack */
225 ldmeq sp!, {r4-r7, lr} /* restore & return from subroutine */
229 /* store last up-to 15 bytes */
230 /* move the remaining len bits [3:0] to the flags area of cpsr */
234 stmmi r12!, {r2-r3} /* n is set, store 8 bytes */
235 movmi r2, r4 /* shift vector down 8 bytes */
238 streq r2, [r12], #4 /* z is set, store 4 bytes */
239 moveq r2, r3 /* shift vector down 4 bytes */
241 strhcs r2, [r12], #2 /* c is set, store 2 bytes */
242 lsrcs r2, #16 /* shift register right 2 bytes */
244 strbvs r2, [r12], #1 /* v is set, store 1 byte */
245 ldm sp!, {r8, r10-r11} /* restores registers from stack */
246 ldm sp!, {r4-r7, lr} /* restore & return from subroutine */
249 /*----------------------------------------------------------------------------*/
250 /* void memset_pattern8(void *ptr, const void *pattern8, size_t len); */
252 /* r0 << destination pointer */
253 /* r1 << pointer to 8-byte pattern */
254 /* r2 << 'len' (length of destination buffer in bytes) */
255 /*----------------------------------------------------------------------------*/
256 .globl _memset_pattern8
258 cmp r2, #0 /* check if len is zero */
259 bxeq lr /* return if length is zero */
261 /* We need some registers, so save volatiles on stack */
262 /* Avoid r7 (frame pointer) and r9 (thread register) */
263 stmfd sp!, {r4-r7, lr}
264 add r7, sp, #12 /* establish frame */
265 stmfd sp!, {r8, r10-r11}
267 /* copy destination base pointer r0 to r12 and leave r0 alone */
268 /* so that we return original pointer back to the caller */
271 /* Check if 'len' is long enough to bother alignment of destination */
273 cmp r2, #32 /* long enough to bother aligning? */
274 movlt r3, #8 /* move pattern length into r3 */
275 movlt r10, #8 /* pattern index */
276 movlt r11, r1 /* move pattern pointer into r11 */
279 /* move 'len' into r1, get 8-byte pattern in r2-r3 */
280 mov r6, r2 /* temporarily move 'len' in to r6 */
281 bl L_GetPatternWord /* get unaligned pattern word in r5 */
282 mov r2, r5 /* move pattern word into r2 */
285 mov r0, r12 /* r0 was clobbered - restore it */
286 mov r1, r6 /* move 'len' from r6 to r1 */
288 mov r4, r2 /* copy 8-byte pattern into r4-r5 registers */
290 b L_NotShort /* yes */
293 /*----------------------------------------------------------------------------*/
294 /* void memset_pattern16(void *ptr, const void *pattern16, size_t len); */
296 /* r0 << destination pointer */
297 /* r1 << pointer to 16-byte pattern */
298 /* r2 << 'len' (length of destination buffer in bytes) */
299 /*----------------------------------------------------------------------------*/
300 .globl _memset_pattern16
302 cmp r2, #0 /* check if len is zero */
303 bxeq lr /* return if length is zero */
305 /* We need some registers, so save volatiles on stack */
306 /* Avoid r7 (frame pointer) and r9 (thread register) */
307 stmfd sp!, {r4-r7, lr}
308 add r7, sp, #12 /* establish frame */
309 stmfd sp!, {r8, r10-r11}
311 /* copy destination base pointer r0 to r12 and leave r0 alone */
312 /* so that we return original pointer back to the caller */
315 /* Check if 'len' is long enough to bother alignment of destination */
317 cmp r2, #32 /* long enough to bother aligning? */
318 movlt r3, #16 /* move pattern length into r3 */
319 movlt r10, #16 /* pattern index */
320 movlt r11, r1 /* move pattern pointer into r11 */
323 /* move 'len' into r1, get 16-byte pattern in r2-r5 */
324 mov r6, r2 /* temporarily move 'len' in to r6 */
325 bl L_GetPatternWord /* get unaligned pattern word in r5 */
326 mov r2, r5 /* move pattern word into r2 */
332 mov r0, r12 /* r0 was clobbered - restore it */
333 mov r1, r6 /* move 'len' from r6 to r1 */
335 b L_NotShort /* yes */
338 /*----------------------------------------------------------------------------*/
339 /* Get an unaligned word at r1, returning it in r5. */
340 /* Increments r1 by 4, clobbers r0. */
341 /* This is tailored to fit the register usage by the call sites. */
342 /*----------------------------------------------------------------------------*/
344 ldrb r5, [r1], #1 /* get the 1st byte at r1 */
345 ldrb r0, [r1], #1 /* get the 2nd byte at r1 */
346 orr r5, r5, r0, lsl #8 /* move into bits 15:8 */
347 ldrb r0, [r1], #1 /* get the 3rd byte */
348 orr r5, r5, r0, lsl #16 /* bits 23:16 */
349 ldrb r0, [r1], #1 /* get the 4th byte */
350 orr r5, r5, r0, lsl #24 /* bits 31:24 */