memset_arm.S revision 1.2.2.2 1 1.2.2.2 yamt /* $NetBSD: memset_arm.S,v 1.2.2.2 2013/01/23 00:04:06 yamt Exp $ */
2 1.2.2.2 yamt
3 1.2.2.2 yamt /*-
4 1.2.2.2 yamt * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.2.2.2 yamt * All rights reserved.
6 1.2.2.2 yamt *
7 1.2.2.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 yamt * by Matt Thomas of 3am Software Foundry.
9 1.2.2.2 yamt *
10 1.2.2.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 yamt * modification, are permitted provided that the following conditions
12 1.2.2.2 yamt * are met:
13 1.2.2.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 yamt * documentation and/or other materials provided with the distribution.
18 1.2.2.2 yamt *
19 1.2.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 yamt */
31 1.2.2.2 yamt #include <machine/asm.h>
32 1.2.2.2 yamt
33 1.2.2.2 yamt #if defined(NEON)
34 1.2.2.2 yamt #define STORE8 vst1.32 {d0}, [ip:64]!
35 1.2.2.2 yamt #define STORE16 vst1.32 {d0-d1}, [ip:64]!
36 1.2.2.2 yamt #define STORE32 vst1.32 {d0-d3}, [ip:64]!
37 1.2.2.2 yamt #elif defined(VFP)
38 1.2.2.2 yamt #define STORE8 vstmia ip!, {d0}
39 1.2.2.2 yamt #define STORE16 vstmia ip!, {d0-d1}
40 1.2.2.2 yamt #define STORE32 vstmia ip!, {d0-d3}
41 1.2.2.2 yamt #elif defined(_ARM_ARCH_DWORD_OK)
42 1.2.2.2 yamt #define STORE8 strd r2, [ip], #8
43 1.2.2.2 yamt #define STORE16 STORE8; STORE8
44 1.2.2.2 yamt #define STORE32 STORE16; STORE16
45 1.2.2.2 yamt #else
46 1.2.2.2 yamt #define STORE8 stmia ip!, {r2,r3}
47 1.2.2.2 yamt #define STORE16 STORE8; STORE8
48 1.2.2.2 yamt #define STORE32 STORE16; STORE16
49 1.2.2.2 yamt #endif
50 1.2.2.2 yamt /*
51 1.2.2.2 yamt * memset: Sets a block of memory to the specified value
52 1.2.2.2 yamt * Using NEON instructions
53 1.2.2.2 yamt *
54 1.2.2.2 yamt * On entry:
55 1.2.2.2 yamt * r0 - dest address
56 1.2.2.2 yamt * r1 - byte to write
57 1.2.2.2 yamt * r2 - number of bytes to write
58 1.2.2.2 yamt *
59 1.2.2.2 yamt * On exit:
60 1.2.2.2 yamt * r0 - dest address
61 1.2.2.2 yamt */
62 1.2.2.2 yamt /* LINTSTUB: Func: void *memset(void *, int, size_t) */
63 1.2.2.2 yamt ENTRY(memset)
64 1.2.2.2 yamt ands r3, r1, #0xff /* We deal with bytes */
65 1.2.2.2 yamt orrne r3, r3, r3, lsl #8 /* replicate to all bytes */
66 1.2.2.2 yamt orrne r3, r3, r3, lsl #16 /* replicate to all bytes */
67 1.2.2.2 yamt movs r1, r2 /* we need r2 & r3 */
68 1.2.2.2 yamt RETc(eq) /* return if length is 0 */
69 1.2.2.2 yamt mov ip, r0 /* r0 needs to stay the same */
70 1.2.2.2 yamt
71 1.2.2.2 yamt cmp r1, #12 /* is this a small memset? *?
72 1.2.2.2 yamt blt .Lbyte_by_byte /* then do it byte by byte */
73 1.2.2.2 yamt
74 1.2.2.2 yamt /* Ok first we will dword align the address */
75 1.2.2.2 yamt ands r2, ip, #7 /* grab the bottom three bits */
76 1.2.2.2 yamt beq .Lmemset_dwordaligned /* The addr is dword aligned */
77 1.2.2.2 yamt
78 1.2.2.2 yamt rsb r2, r2, #8 /* how far until dword aligned? */
79 1.2.2.2 yamt sub r1, r1, r2 /* subtract it from remaining length */
80 1.2.2.2 yamt mov r2, r3 /* duplicate fill value */
81 1.2.2.2 yamt
82 1.2.2.2 yamt tst ip, #1 /* halfword aligned? */
83 1.2.2.2 yamt strneb r3, [ip], #1 /* no, write a byte */
84 1.2.2.2 yamt tst ip, #2 /* word aligned? */
85 1.2.2.2 yamt strneh r3, [ip], #2 /* no, write a halfword */
86 1.2.2.2 yamt tst ip, #4 /* dword aligned? */
87 1.2.2.2 yamt strne r3, [ip], #4 /* no, write a word */
88 1.2.2.2 yamt
89 1.2.2.2 yamt /* We are now doubleword aligned */
90 1.2.2.2 yamt .Lmemset_dwordaligned:
91 1.2.2.2 yamt #if defined(NEON)
92 1.2.2.2 yamt vdup.8 q0, r3 /* move fill to SIMD */
93 1.2.2.2 yamt vmov q1, q0 /* put fill in q1 (d2-d3) */
94 1.2.2.2 yamt #elif defined(VFP)
95 1.2.2.2 yamt mov r2, r3 /* duplicate fill value */
96 1.2.2.2 yamt vmov d0, r2, r3 /* move to VFP */
97 1.2.2.2 yamt vmov d1, r2, r3
98 1.2.2.2 yamt vmov d2, r2, r3
99 1.2.2.2 yamt vmov d3, r2, r3
100 1.2.2.2 yamt #endif
101 1.2.2.2 yamt
102 1.2.2.2 yamt #if 1
103 1.2.2.2 yamt cmp r1, #128
104 1.2.2.2 yamt blt .Lmemset_mainloop
105 1.2.2.2 yamt ands r2, ip, #63 /* check for 64-byte alignment */
106 1.2.2.2 yamt beq .Lmemset_mainloop
107 1.2.2.2 yamt /*
108 1.2.2.2 yamt * Let's align to a 64-byte boundary so that stores don't cross
109 1.2.2.2 yamt * cacheline boundaries. We also know we have at least 128-bytes to
110 1.2.2.2 yamt * copy so we don't have to worry about the length at the moment.
111 1.2.2.2 yamt */
112 1.2.2.2 yamt rsb r2, r2, #64 /* how many bytes until 64 bytes */
113 1.2.2.2 yamt sub r1, r1, r2 /* subtract from remaining length */
114 1.2.2.2 yamt #if !defined(NEON) && !defined(VFP)
115 1.2.2.2 yamt mov r2, r3 /* put fill back in r2 */
116 1.2.2.2 yamt #endif
117 1.2.2.2 yamt
118 1.2.2.2 yamt tst ip, #8 /* quadword aligned? */
119 1.2.2.2 yamt beq 1f /* yes */
120 1.2.2.2 yamt STORE8 /* no, store a dword */
121 1.2.2.2 yamt 1: tst ip, #16 /* octaword aligned? *?
122 1.2.2.2 yamt beq 2f /* yes */
123 1.2.2.2 yamt STORE16 /* no, store a quadword */
124 1.2.2.2 yamt 2: tst ip, #32 /* 32 word aligned? */
125 1.2.2.2 yamt beq .Lmemset_mainloop /* yes */
126 1.2.2.2 yamt STORE32 /* no, make 64-byte aligned */
127 1.2.2.2 yamt #endif
128 1.2.2.2 yamt
129 1.2.2.2 yamt .Lmemset_mainloop:
130 1.2.2.2 yamt #if !defined(NEON) && !defined(VFP)
131 1.2.2.2 yamt mov r2, r3 /* put fill back in r2 */
132 1.2.2.2 yamt #endif
133 1.2.2.2 yamt subs r1, r1, #64 /* subtract an initial 64 */
134 1.2.2.2 yamt blt .Lmemset_lessthan_64bytes
135 1.2.2.2 yamt
136 1.2.2.2 yamt 3: STORE32 /* store first octaword */
137 1.2.2.2 yamt STORE32 /* store second octaword */
138 1.2.2.2 yamt RETc(eq) /* return if done */
139 1.2.2.2 yamt subs r1, r1, #64 /* subtract another 64 */
140 1.2.2.2 yamt bge 3b /* and do other if still >= 0 */
141 1.2.2.2 yamt .Lmemset_lessthan_64bytes:
142 1.2.2.2 yamt tst r1, #32 /* do we have 16 bytes left? */
143 1.2.2.2 yamt beq .Lmemset_lessthan_32bytes
144 1.2.2.2 yamt STORE32 /* yes, store an octaword */
145 1.2.2.2 yamt bics r1, r1, #32 /* subtract 16 */
146 1.2.2.2 yamt RETc(eq) /* return if length is 0 */
147 1.2.2.2 yamt .Lmemset_lessthan_32bytes:
148 1.2.2.2 yamt tst r1, #16 /* do we have 16 bytes left? */
149 1.2.2.2 yamt beq .Lmemset_lessthan_16bytes
150 1.2.2.2 yamt STORE16 /* yes, store a quadword */
151 1.2.2.2 yamt bics r1, r1, #16 /* subtract 16 */
152 1.2.2.2 yamt RETc(eq) /* return if length is 0 */
153 1.2.2.2 yamt .Lmemset_lessthan_16bytes:
154 1.2.2.2 yamt tst r1, #8 /* do we have 8 bytes left? */
155 1.2.2.2 yamt beq .Lmemset_lessthan_8bytes/* no */
156 1.2.2.2 yamt STORE8 /* yes, store a dword */
157 1.2.2.2 yamt bics r1, r1, #8 /* subtract 8 */
158 1.2.2.2 yamt RETc(eq) /* return if length is 0 */
159 1.2.2.2 yamt .Lmemset_lessthan_8bytes:
160 1.2.2.2 yamt tst r1, #4 /* do we have a word left? */
161 1.2.2.2 yamt strne r2, [ip], #4 /* yes, so write one */
162 1.2.2.2 yamt tst r1, #2 /* do we have a halfword left? */
163 1.2.2.2 yamt strneh r2, [ip], #2 /* yes, so write one */
164 1.2.2.2 yamt tst r1, #1 /* do we have a byte left? */
165 1.2.2.2 yamt strneb r2, [ip], #1 /* yes, so write one */
166 1.2.2.2 yamt RET /* return */
167 1.2.2.2 yamt
168 1.2.2.2 yamt .Lbyte_by_byte:
169 1.2.2.2 yamt subs r1, r1, #1 /* can we write a byte? */
170 1.2.2.2 yamt RETc(lt) /* no, we're done */
171 1.2.2.2 yamt strb r3, [ip], #1 /* yes, so do it */
172 1.2.2.2 yamt b .Lbyte_by_byte /* try next byte */
173 1.2.2.2 yamt END(memset)
174