1 /* Copyright (C) 2013-2014 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 <http://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. */
53 ENTRY_ALIGN_AND_PAD (strncmp, 6, 7)
56 mov zeroones, #REP8_01
61 /* Calculate the number of full and partial words -1. */
62 sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
63 lsr limit_wd, limit_wd, #3 /* Convert to Dwords. */
65 /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
66 (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
67 can be done in parallel across the entire word. */
68 /* Start of performance-critical section -- one 64B cache line. */
73 subs limit_wd, limit_wd, #1
74 sub tmp1, data1, zeroones
75 orr tmp2, data1, #REP8_7f
76 eor diff, data1, data2 /* Non-zero if differences found. */
77 csinv endloop, diff, xzr, pl /* Last Dword or differences. */
78 bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
79 ccmp endloop, #0, #0, eq
81 /* End of performance-critical section -- one 64B cache line. */
83 /* Not reached the limit, must have found the end or a diff. */
84 tbz limit_wd, #63, L(not_limit)
86 /* Limit % 8 == 0 => all bytes significant. */
90 lsl limit, limit, #3 /* Bits -> bytes. */
97 bic data1, data1, mask
98 bic data2, data2, mask
100 /* Make sure that the NUL byte is marked in the syndrome. */
101 orr has_nul, has_nul, mask
104 orr syndrome, diff, has_nul
106 #ifndef __AARCH64EB__
107 rev syndrome, syndrome
109 /* The MS-non-zero bit of the syndrome marks either the first bit
110 that is different, or the top bit of the first zero byte.
111 Shifting left now will bring the critical information into the
115 lsl data1, data1, pos
116 lsl data2, data2, pos
117 /* But we need to zero-extend (char is unsigned) the value and then
118 perform a signed 32-bit subtraction. */
119 lsr data1, data1, #56
120 sub result, data1, data2, lsr #56
123 /* For big-endian we cannot use the trick with the syndrome value
124 as carry-propagation can corrupt the upper bits if the trailing
125 bytes in the string contain 0x01. */
126 /* However, if there is no NUL byte in the dword, we can generate
127 the result directly. We can't just subtract the bytes as the
128 MSB might be significant. */
132 cneg result, result, lo
135 /* Re-compute the NUL-byte detection, using a byte-reversed value. */
137 sub tmp1, tmp3, zeroones
138 orr tmp2, tmp3, #REP8_7f
139 bic has_nul, tmp1, tmp2
141 orr syndrome, diff, has_nul
143 /* The MS-non-zero bit of the syndrome marks either the first bit
144 that is different, or the top bit of the first zero byte.
145 Shifting left now will bring the critical information into the
147 lsl data1, data1, pos
148 lsl data2, data2, pos
149 /* But we need to zero-extend (char is unsigned) the value and then
150 perform a signed 32-bit subtraction. */
151 lsr data1, data1, #56
152 sub result, data1, data2, lsr #56
157 /* Sources are mutually aligned, but are not currently at an
158 alignment boundary. Round down the addresses and then mask off
159 the bytes that precede the start point.
160 We also need to adjust the limit calculations, but without
161 overflowing if the limit is near ULONG_MAX. */
164 ldr data1, [src1], #8
165 neg tmp3, tmp1, lsl #3 /* 64 - bits(bytes beyond align). */
166 ldr data2, [src2], #8
168 sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
170 /* Big-endian. Early bytes are at MSB. */
171 lsl tmp2, tmp2, tmp3 /* Shift (tmp1 & 63). */
173 /* Little-endian. Early bytes are at LSB. */
174 lsr tmp2, tmp2, tmp3 /* Shift (tmp1 & 63). */
176 and tmp3, limit_wd, #7
177 lsr limit_wd, limit_wd, #3
178 /* Adjust the limit. Only low 3 bits used, so overflow irrelevant. */
179 add limit, limit, tmp1
181 orr data1, data1, tmp2
182 orr data2, data2, tmp2
183 add limit_wd, limit_wd, tmp3, lsr #3
194 /* Perhaps we can do better than this. */
195 ldrb data1w, [src1], #1
196 ldrb data2w, [src2], #1
197 subs limit, limit, #1
198 ccmp data1w, #1, #0, cs /* NZCV = 0b0000. */
199 ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
201 sub result, data1, data2
204 libc_hidden_builtin_def (strncmp)