1 /* Optimized strlen implementation using SIMD.
2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library. If not, see
18 <https://www.gnu.org/licenses/>. */
24 * ARMv8-a, AArch64, Advanced SIMD, unaligned accesses.
53 /* For the first 32 bytes, NUL detection works on the principle that
54 (X - 1) & (~X) & 0x80 (=> (X - 1) & ~(X | 0x7f)) is non-zero if a
55 byte is zero, and can be done in parallel across the entire word. */
57 #define REP8_01 0x0101010101010101
58 #define REP8_7f 0x7f7f7f7f7f7f7f7f
60 /* To test the page crossing code path more thoroughly, compile with
61 -DTEST_PAGE_CROSS - this will force all calls through the slower
62 entry path. This option is not intended for production use. */
64 #ifdef TEST_PAGE_CROSS
65 # define MIN_PAGE_SIZE 32
67 # define MIN_PAGE_SIZE 4096
72 Since strings are short on average, we check the first 32 bytes of the
73 string for a NUL character without aligning the string. In order to use
74 unaligned loads safely we must do a page cross check first.
76 If there is a NUL byte we calculate the length from the 2 8-byte words
77 using conditional select to reduce branch mispredictions (it is unlikely
78 strlen will be repeatedly called on strings with the same length).
80 If the string is longer than 32 bytes, align src so we don't need further
81 page cross checks, and process 32 bytes per iteration using a fast SIMD
84 If the page cross check fails, we read 32 bytes from an aligned address,
85 and ignore any characters before the string. If it contains a NUL
86 character, return the length, if not, continue in the main loop. */
88 ENTRY (__strlen_asimd)
91 and tmp1, srcin, MIN_PAGE_SIZE - 1
92 cmp tmp1, MIN_PAGE_SIZE - 32
95 /* Look for a NUL byte in the first 16 bytes. */
96 ldp data1, data2, [srcin]
100 /* For big-endian, carry propagation (if the final byte in the
101 string is 0x01) means we cannot use has_nul1/2 directly.
102 Since we expect strings to be small and early-exit,
103 byte-swap the data now so has_null1/2 will be correct. */
107 sub tmp1, data1, zeroones
108 orr tmp2, data1, REP8_7f
109 sub tmp3, data2, zeroones
110 orr tmp4, data2, REP8_7f
111 bics has_nul1, tmp1, tmp2
112 bic has_nul2, tmp3, tmp4
113 ccmp has_nul2, 0, 0, eq
116 /* Find the exact offset of the first NUL byte in the first 16 bytes
117 from the string start. Enter with C = has_nul1 == 0. */
118 csel has_nul1, has_nul1, has_nul2, cc
120 rev has_nul1, has_nul1
121 csel len, xzr, len, cc
123 add len, len, tmp1, lsr 3
127 /* Look for a NUL byte at offset 16..31 in the string. */
129 ldp data1, data2, [srcin, 16]
134 sub tmp1, data1, zeroones
135 orr tmp2, data1, REP8_7f
136 sub tmp3, data2, zeroones
137 orr tmp4, data2, REP8_7f
138 bics has_nul1, tmp1, tmp2
139 bic has_nul2, tmp3, tmp4
140 ccmp has_nul2, 0, 0, eq
143 /* Find the exact offset of the first NUL byte at offset 16..31 from
144 the string start. Enter with C = has_nul1 == 0. */
145 csel has_nul1, has_nul1, has_nul2, cc
147 rev has_nul1, has_nul1
150 csel len, tmp3, len, cc
151 add len, len, tmp1, lsr 3
159 ldp dataq1, dataq2, [src, 32]!
160 uminp maskv.16b, datav1.16b, datav2.16b
161 uminp maskv.16b, maskv.16b, maskv.16b
162 cmeq maskv.8b, maskv.8b, 0
166 /* Low 32 bits of synd are non-zero if a NUL was found in datav1. */
167 cmeq maskv.16b, datav1.16b, 0
171 cmeq maskv.16b, datav2.16b, 0
174 /* Generate a bitmask and compute correct byte offset. */
178 bic maskv.8h, 0x0f, lsl 8
180 umaxp maskv.16b, maskv.16b, maskv.16b
182 #ifndef __AARCH64EB__
186 add len, len, tmp, lsr 2
194 movk tmpw, 0xc030, lsl 16
195 ld1 {datav1.16b, datav2.16b}, [src]
197 cmeq datav1.16b, datav1.16b, 0
198 cmeq datav2.16b, datav2.16b, 0
199 and datav1.16b, datav1.16b, maskv.16b
200 and datav2.16b, datav2.16b, maskv.16b
201 addp maskv.16b, datav1.16b, datav2.16b
202 addp maskv.16b, maskv.16b, maskv.16b
205 lsr synd, synd, shift
214 libc_hidden_builtin_def (__strlen_asimd)