2 strcpy/stpcpy - copy a string returning pointer to start/end.
4 Copyright (c) 2013, 2014, 2015 ARM Ltd.
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 * Neither the name of the company nor the names of its contributors
15 may be used to endorse or promote products derived from this
16 software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32 * ARMv8-a, AArch64, unaligned accesses, min page size 4k.
35 /* To build as stpcpy, define BUILD_STPCPY before compiling this file.
37 To test the page crossing code path more thoroughly, compile with
38 -DSTRCPY_TEST_PAGE_CROSS - this will force all copies through the slower
39 entry path. This option is not intended for production use. */
41 /* Arguments and results. */
45 /* Locals and temporaries. */
71 .macro def_fn f p2align=0
79 /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
80 (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
81 can be done in parallel across the entire word. */
83 #define REP8_01 0x0101010101010101
84 #define REP8_7f 0x7f7f7f7f7f7f7f7f
85 #define REP8_80 0x8080808080808080
87 /* AArch64 systems have a minimum page size of 4k. We can do a quick
88 page size check for crossing this boundary on entry and if we
89 do not, then we can short-circuit much of the entry code. We
90 expect early page-crossing strings to be rare (probability of
91 16/MIN_PAGE_SIZE ~= 0.4%), so the branch should be quite
92 predictable, even with random strings.
94 We don't bother checking for larger page sizes, the cost of setting
95 up the correct page size is just not worth the extra gain from
96 a small reduction in the cases taking the slow path. Note that
97 we only care about whether the first fetch, which may be
98 misaligned, crosses a page boundary - after that we move to aligned
99 fetches for the remainder of the string. */
101 #ifdef STRCPY_TEST_PAGE_CROSS
102 /* Make everything that isn't Qword aligned look like a page cross. */
103 #define MIN_PAGE_P2 4
105 #define MIN_PAGE_P2 12
108 #define MIN_PAGE_SIZE (1 << MIN_PAGE_P2)
110 def_fn STRCPY p2align=6
111 /* For moderately short strings, the fastest way to do the copy is to
112 calculate the length of the string in the same way as strlen, then
113 essentially do a memcpy of the result. This avoids the need for
114 multiple byte copies and further means that by the time we
115 reach the bulk copy loop we know we can always use DWord
116 accesses. We expect strcpy to rarely be called repeatedly
117 with the same source string, so branch prediction is likely to
118 always be difficult - we mitigate against this by preferring
119 conditional select operations over branches whenever this is
121 and tmp2, srcin, #(MIN_PAGE_SIZE - 1)
122 mov zeroones, #REP8_01
123 and to_align, srcin, #15
124 cmp tmp2, #(MIN_PAGE_SIZE - 16)
126 /* The first fetch will straddle a (possible) page boundary iff
127 srcin + 15 causes bit[MIN_PAGE_P2] to change value. A 16-byte
128 aligned string will never fail the page align check, so will
129 always take the fast path. */
133 ldp data1, data2, [srcin]
135 /* Because we expect the end to be found within 16 characters
136 (profiling shows this is the most common case), it's worth
137 swapping the bytes now to save having to recalculate the
138 termination syndrome later. We preserve data1 and data2
139 so that we can re-use the values later on. */
141 sub tmp1, tmp2, zeroones
142 orr tmp2, tmp2, #REP8_7f
143 bics has_nul1, tmp1, tmp2
146 sub tmp3, tmp4, zeroones
147 orr tmp4, tmp4, #REP8_7f
149 sub tmp1, data1, zeroones
150 orr tmp2, data1, #REP8_7f
151 bics has_nul1, tmp1, tmp2
153 sub tmp3, data2, zeroones
154 orr tmp4, data2, #REP8_7f
156 bics has_nul2, tmp3, tmp4
159 /* The string is short (<=16 bytes). We don't know exactly how
160 short though, yet. Work out the exact length so that we can
161 quickly select the optimal copy strategy. */
163 rev has_nul2, has_nul2
166 add dst, dstin, pos, lsr #3 /* Bits to bytes. */
169 lsr data2, data2, pos
171 lsl data2, data2, pos
181 rev has_nul1, has_nul1
183 add dst, dstin, pos, lsr #3 /* Bits to bytes. */
184 subs tmp2, pos, #24 /* Pos in bits. */
189 lsr data2, data1, pos
190 lsr data1, data1, #32
192 lsr data2, data1, tmp2
194 /* 4->7 bytes to copy. */
195 str data2w, [dst, #-3]
203 /* 2->3 bytes to copy. */
205 lsr data1, data1, #48
208 /* Fall-through, one byte (max) to go. */
210 /* Null-terminated string. Last character must be zero! */
218 /* Aligning here ensures that the entry code and main loop all lies
219 within one 64-byte cache line. */
221 sub to_align, to_align, #16
222 stp data1, data2, [dstin]
223 sub src, srcin, to_align
224 sub dst, dstin, to_align
225 b .Lentry_no_page_cross
227 /* The inner loop deals with two Dwords at a time. This has a
228 slightly higher start-up cost, but we should win quite quickly,
229 especially on cores with a high number of issue slots per
230 cycle, as we get much better parallelism out of the operations. */
232 stp data1, data2, [dst], #16
233 .Lentry_no_page_cross:
234 ldp data1, data2, [src], #16
235 sub tmp1, data1, zeroones
236 orr tmp2, data1, #REP8_7f
237 sub tmp3, data2, zeroones
238 orr tmp4, data2, #REP8_7f
239 bic has_nul1, tmp1, tmp2
240 bics has_nul2, tmp3, tmp4
241 ccmp has_nul1, #0, #0, eq /* NZCV = 0000 */
244 /* Since we know we are copying at least 16 bytes, the fastest way
245 to deal with the tail is to determine the location of the
246 trailing NUL, then (re)copy the 16 bytes leading up to that. */
249 /* For big-endian, carry propagation (if the final byte in the
250 string is 0x01) means we cannot use has_nul directly. The
251 easiest way to get the correct byte is to byte-swap the data
252 and calculate the syndrome a second time. */
253 csel data1, data1, data2, ne
255 sub tmp1, data1, zeroones
256 orr tmp2, data1, #REP8_7f
257 bic has_nul1, tmp1, tmp2
259 csel has_nul1, has_nul1, has_nul2, ne
261 rev has_nul1, has_nul1
265 csel pos, pos, tmp1, ne
266 add src, src, pos, lsr #3
267 add dst, dst, pos, lsr #3
268 ldp data1, data2, [src, #-32]
269 stp data1, data2, [dst, #-16]
277 /* Start by loading two words at [srcin & ~15], then forcing the
278 bytes that precede srcin to 0xff. This means they never look
279 like termination bytes. */
280 ldp data1, data2, [src]
281 lsl tmp1, tmp1, #3 /* Bytes beyond alignment -> bits. */
285 lsl tmp2, tmp2, tmp1 /* Shift (tmp1 & 63). */
287 lsr tmp2, tmp2, tmp1 /* Shift (tmp1 & 63). */
289 orr data1, data1, tmp2
290 orr data2a, data2, tmp2
292 csinv data1, data1, xzr, lt
293 csel data2, data2, data2a, lt
294 sub tmp1, data1, zeroones
295 orr tmp2, data1, #REP8_7f
296 sub tmp3, data2, zeroones
297 orr tmp4, data2, #REP8_7f
298 bic has_nul1, tmp1, tmp2
299 bics has_nul2, tmp3, tmp4
300 ccmp has_nul1, #0, #0, eq /* NZCV = 0000 */
302 /* We now need to make data1 and data2 look like they've been
303 loaded directly from srcin. Do a rotate on the 128-bit value. */
304 lsl tmp1, to_align, #3 /* Bytes->bits. */
305 neg tmp2, to_align, lsl #3
307 lsl data1a, data1, tmp1
308 lsr tmp4, data2, tmp2
309 lsl data2, data2, tmp1
310 orr tmp4, tmp4, data1a
312 csel data1, tmp4, data2, lt
315 sub tmp1, tmp2, zeroones
316 orr tmp2, tmp2, #REP8_7f
317 sub tmp3, tmp4, zeroones
318 orr tmp4, tmp4, #REP8_7f
320 lsr data1a, data1, tmp1
321 lsl tmp4, data2, tmp2
322 lsr data2, data2, tmp1
323 orr tmp4, tmp4, data1a
325 csel data1, tmp4, data2, lt
326 sub tmp1, data1, zeroones
327 orr tmp2, data1, #REP8_7f
328 sub tmp3, data2, zeroones
329 orr tmp4, data2, #REP8_7f
331 bic has_nul1, tmp1, tmp2
332 cbnz has_nul1, .Lfp_le8
333 bic has_nul2, tmp3, tmp4
336 .size STRCPY, . - STRCPY