1 /* Copyright (C) 2013-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <https://www.gnu.org/licenses/>. */
26 #define REP8_01 0x0101010101010101
27 #define REP8_7f 0x7f7f7f7f7f7f7f7f
28 #define REP8_80 0x8080808080808080
30 /* Parameters and result. */
36 /* Internal variables. */
54 ENTRY_ALIGN_AND_PAD (strncmp, 6, 7)
60 mov zeroones, #REP8_01
64 cbnz count, L(mutual_align)
65 /* Calculate the number of full and partial words -1. */
66 sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
67 lsr limit_wd, limit_wd, #3 /* Convert to Dwords. */
69 /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
70 (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
71 can be done in parallel across the entire word. */
72 /* Start of performance-critical section -- one 64B cache line. */
77 subs limit_wd, limit_wd, #1
78 sub tmp1, data1, zeroones
79 orr tmp2, data1, #REP8_7f
80 eor diff, data1, data2 /* Non-zero if differences found. */
81 csinv endloop, diff, xzr, pl /* Last Dword or differences. */
82 bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
83 ccmp endloop, #0, #0, eq
85 /* End of performance-critical section -- one 64B cache line. */
87 /* Not reached the limit, must have found the end or a diff. */
88 tbz limit_wd, #63, L(not_limit)
90 /* Limit % 8 == 0 => all bytes significant. */
94 lsl limit, limit, #3 /* Bits -> bytes. */
101 bic data1, data1, mask
102 bic data2, data2, mask
104 /* Make sure that the NUL byte is marked in the syndrome. */
105 orr has_nul, has_nul, mask
108 orr syndrome, diff, has_nul
110 #ifndef __AARCH64EB__
111 rev syndrome, syndrome
113 /* The MS-non-zero bit of the syndrome marks either the first bit
114 that is different, or the top bit of the first zero byte.
115 Shifting left now will bring the critical information into the
119 lsl data1, data1, pos
120 lsl data2, data2, pos
121 /* But we need to zero-extend (char is unsigned) the value and then
122 perform a signed 32-bit subtraction. */
123 lsr data1, data1, #56
124 sub result, data1, data2, lsr #56
127 /* For big-endian we cannot use the trick with the syndrome value
128 as carry-propagation can corrupt the upper bits if the trailing
129 bytes in the string contain 0x01. */
130 /* However, if there is no NUL byte in the dword, we can generate
131 the result directly. We can't just subtract the bytes as the
132 MSB might be significant. */
136 cneg result, result, lo
139 /* Re-compute the NUL-byte detection, using a byte-reversed value. */
141 sub tmp1, tmp3, zeroones
142 orr tmp2, tmp3, #REP8_7f
143 bic has_nul, tmp1, tmp2
145 orr syndrome, diff, has_nul
147 /* The MS-non-zero bit of the syndrome marks either the first bit
148 that is different, or the top bit of the first zero byte.
149 Shifting left now will bring the critical information into the
151 lsl data1, data1, pos
152 lsl data2, data2, pos
153 /* But we need to zero-extend (char is unsigned) the value and then
154 perform a signed 32-bit subtraction. */
155 lsr data1, data1, #56
156 sub result, data1, data2, lsr #56
161 /* Sources are mutually aligned, but are not currently at an
162 alignment boundary. Round down the addresses and then mask off
163 the bytes that precede the start point.
164 We also need to adjust the limit calculations, but without
165 overflowing if the limit is near ULONG_MAX. */
168 ldr data1, [src1], #8
169 neg tmp3, count, lsl #3 /* 64 - bits(bytes beyond align). */
170 ldr data2, [src2], #8
172 sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
174 /* Big-endian. Early bytes are at MSB. */
175 lsl tmp2, tmp2, tmp3 /* Shift (count & 63). */
177 /* Little-endian. Early bytes are at LSB. */
178 lsr tmp2, tmp2, tmp3 /* Shift (count & 63). */
180 and tmp3, limit_wd, #7
181 lsr limit_wd, limit_wd, #3
182 /* Adjust the limit. Only low 3 bits used, so overflow irrelevant. */
183 add limit, limit, count
184 add tmp3, tmp3, count
185 orr data1, data1, tmp2
186 orr data2, data2, tmp2
187 add limit_wd, limit_wd, tmp3, lsr #3
191 /* Don't bother with dwords for up to 16 bytes. */
194 b.hs L(try_misaligned_words)
197 /* Perhaps we can do better than this. */
198 ldrb data1w, [src1], #1
199 ldrb data2w, [src2], #1
200 subs limit, limit, #1
201 ccmp data1w, #1, #0, hi /* NZCV = 0b0000. */
202 ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
205 sub result, data1, data2
208 /* Align the SRC1 to a dword by doing a bytewise compare and then do
210 L(try_misaligned_words):
211 lsr limit_wd, limit, #3
212 cbz count, L(do_misaligned)
216 sub limit, limit, count
217 lsr limit_wd, limit, #3
220 ldrb data1w, [src1], #1
221 ldrb data2w, [src2], #1
223 ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
225 subs count, count, #1
226 b.hi L(page_end_loop)
229 /* Prepare ourselves for the next page crossing. Unlike the aligned
230 loop, we fetch 1 less dword because we risk crossing bounds on
233 subs limit_wd, limit_wd, #1
236 and tmp2, src2, #0xff8
237 eor tmp2, tmp2, #0xff8
238 cbz tmp2, L(page_end_loop)
240 ldr data1, [src1], #8
241 ldr data2, [src2], #8
242 sub tmp1, data1, zeroones
243 orr tmp2, data1, #REP8_7f
244 eor diff, data1, data2 /* Non-zero if differences found. */
245 bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
246 ccmp diff, #0, #0, eq
248 subs limit_wd, limit_wd, #1
249 b.pl L(loop_misaligned)
252 /* We found a difference or a NULL before the limit was reached. */
254 cbz limit, L(not_limit)
255 /* Read the last word. */
258 ldr data1, [src1, limit]
259 ldr data2, [src2, limit]
260 sub tmp1, data1, zeroones
261 orr tmp2, data1, #REP8_7f
262 eor diff, data1, data2 /* Non-zero if differences found. */
263 bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
264 ccmp diff, #0, #0, eq
272 libc_hidden_builtin_def (strncmp)