blob: b81f43aa2a9752b000d2e49898d19744cce63dcf [file] [log] [blame]
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +01001/* Copyright (c) 2014, Linaro Limited
2 All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 * Neither the name of the Linaro nor the
12 names of its contributors may be used to endorse or promote products
13 derived from this software without specific prior written permission.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28/* Assumptions:
29 *
30 * ARMv8-a, AArch64
31 */
32
33#include <private/bionic_asm.h>
34
35#define REP8_01 0x0101010101010101
36#define REP8_7f 0x7f7f7f7f7f7f7f7f
37#define REP8_80 0x8080808080808080
38
39/* Parameters and result. */
40#define src1 x0
41#define src2 x1
42#define limit x2
43#define result x0
44
45/* Internal variables. */
46#define data1 x3
47#define data1w w3
48#define data2 x4
49#define data2w w4
50#define has_nul x5
51#define diff x6
52#define syndrome x7
53#define tmp1 x8
54#define tmp2 x9
55#define tmp3 x10
56#define zeroones x11
57#define pos x12
58#define limit_wd x13
59#define mask x14
60#define endloop x15
Adhemerval Zanella65a62112018-06-22 15:30:03 -030061#define count mask
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +010062
63 .text
64 .p2align 6
65 .rep 7
66 nop /* Pad so that the loop below fits a cache line. */
67 .endr
68ENTRY(strncmp)
69 cbz limit, .Lret0
70 eor tmp1, src1, src2
71 mov zeroones, #REP8_01
72 tst tmp1, #7
Adhemerval Zanella65a62112018-06-22 15:30:03 -030073 and count, src1, #7
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +010074 b.ne .Lmisaligned8
Adhemerval Zanella65a62112018-06-22 15:30:03 -030075 cbnz count, .Lmutual_align
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +010076 /* Calculate the number of full and partial words -1. */
77 sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
78 lsr limit_wd, limit_wd, #3 /* Convert to Dwords. */
79
80 /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
81 (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
82 can be done in parallel across the entire word. */
83 /* Start of performance-critical section -- one 64B cache line. */
84.Lloop_aligned:
85 ldr data1, [src1], #8
86 ldr data2, [src2], #8
87.Lstart_realigned:
88 subs limit_wd, limit_wd, #1
89 sub tmp1, data1, zeroones
90 orr tmp2, data1, #REP8_7f
91 eor diff, data1, data2 /* Non-zero if differences found. */
92 csinv endloop, diff, xzr, pl /* Last Dword or differences. */
93 bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
94 ccmp endloop, #0, #0, eq
95 b.eq .Lloop_aligned
96 /* End of performance-critical section -- one 64B cache line. */
97
98 /* Not reached the limit, must have found the end or a diff. */
99 tbz limit_wd, #63, .Lnot_limit
100
101 /* Limit % 8 == 0 => all bytes significant. */
102 ands limit, limit, #7
103 b.eq .Lnot_limit
104
105 lsl limit, limit, #3 /* Bits -> bytes. */
106 mov mask, #~0
107#ifdef __AARCH64EB__
108 lsr mask, mask, limit
109#else
110 lsl mask, mask, limit
111#endif
112 bic data1, data1, mask
113 bic data2, data2, mask
114
115 /* Make sure that the NUL byte is marked in the syndrome. */
116 orr has_nul, has_nul, mask
117
118.Lnot_limit:
119 orr syndrome, diff, has_nul
120
121#ifndef __AARCH64EB__
122 rev syndrome, syndrome
123 rev data1, data1
124 /* The MS-non-zero bit of the syndrome marks either the first bit
125 that is different, or the top bit of the first zero byte.
126 Shifting left now will bring the critical information into the
127 top bits. */
128 clz pos, syndrome
129 rev data2, data2
130 lsl data1, data1, pos
131 lsl data2, data2, pos
132 /* But we need to zero-extend (char is unsigned) the value and then
133 perform a signed 32-bit subtraction. */
134 lsr data1, data1, #56
135 sub result, data1, data2, lsr #56
136 ret
137#else
138 /* For big-endian we cannot use the trick with the syndrome value
139 as carry-propagation can corrupt the upper bits if the trailing
140 bytes in the string contain 0x01. */
141 /* However, if there is no NUL byte in the dword, we can generate
142 the result directly. We can't just subtract the bytes as the
143 MSB might be significant. */
144 cbnz has_nul, 1f
145 cmp data1, data2
146 cset result, ne
147 cneg result, result, lo
148 ret
1491:
150 /* Re-compute the NUL-byte detection, using a byte-reversed value. */
151 rev tmp3, data1
152 sub tmp1, tmp3, zeroones
153 orr tmp2, tmp3, #REP8_7f
154 bic has_nul, tmp1, tmp2
155 rev has_nul, has_nul
156 orr syndrome, diff, has_nul
157 clz pos, syndrome
158 /* The MS-non-zero bit of the syndrome marks either the first bit
159 that is different, or the top bit of the first zero byte.
160 Shifting left now will bring the critical information into the
161 top bits. */
162 lsl data1, data1, pos
163 lsl data2, data2, pos
164 /* But we need to zero-extend (char is unsigned) the value and then
165 perform a signed 32-bit subtraction. */
166 lsr data1, data1, #56
167 sub result, data1, data2, lsr #56
168 ret
169#endif
170
171.Lmutual_align:
172 /* Sources are mutually aligned, but are not currently at an
173 alignment boundary. Round down the addresses and then mask off
174 the bytes that precede the start point.
175 We also need to adjust the limit calculations, but without
176 overflowing if the limit is near ULONG_MAX. */
177 bic src1, src1, #7
178 bic src2, src2, #7
179 ldr data1, [src1], #8
Adhemerval Zanella65a62112018-06-22 15:30:03 -0300180 neg tmp3, count, lsl #3 /* 64 - bits(bytes beyond align). */
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100181 ldr data2, [src2], #8
182 mov tmp2, #~0
183 sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
184#ifdef __AARCH64EB__
185 /* Big-endian. Early bytes are at MSB. */
Adhemerval Zanella65a62112018-06-22 15:30:03 -0300186 lsl tmp2, tmp2, tmp3 /* Shift (count & 63). */
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100187#else
188 /* Little-endian. Early bytes are at LSB. */
Adhemerval Zanella65a62112018-06-22 15:30:03 -0300189 lsr tmp2, tmp2, tmp3 /* Shift (count & 63). */
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100190#endif
191 and tmp3, limit_wd, #7
192 lsr limit_wd, limit_wd, #3
193 /* Adjust the limit. Only low 3 bits used, so overflow irrelevant. */
Adhemerval Zanella65a62112018-06-22 15:30:03 -0300194 add limit, limit, count
195 add tmp3, tmp3, count
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100196 orr data1, data1, tmp2
197 orr data2, data2, tmp2
198 add limit_wd, limit_wd, tmp3, lsr #3
199 b .Lstart_realigned
200
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100201 .p2align 6
Adhemerval Zanella65a62112018-06-22 15:30:03 -0300202 /* Don't bother with dwords for up to 16 bytes. */
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100203.Lmisaligned8:
Adhemerval Zanella65a62112018-06-22 15:30:03 -0300204 cmp limit, #16
205 b.hs .Ltry_misaligned_words
206
207.Lbyte_loop:
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100208 /* Perhaps we can do better than this. */
209 ldrb data1w, [src1], #1
210 ldrb data2w, [src2], #1
211 subs limit, limit, #1
Adhemerval Zanella65a62112018-06-22 15:30:03 -0300212 ccmp data1w, #1, #0, hi /* NZCV = 0b0000. */
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100213 ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
Adhemerval Zanella65a62112018-06-22 15:30:03 -0300214 b.eq .Lbyte_loop
215.Ldone:
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100216 sub result, data1, data2
217 ret
Adhemerval Zanella65a62112018-06-22 15:30:03 -0300218 /* Align the SRC1 to a dword by doing a bytewise compare and then do
219 the dword loop. */
220.Ltry_misaligned_words:
221 lsr limit_wd, limit, #3
222 cbz count, .Ldo_misaligned
223
224 neg count, count
225 and count, count, #7
226 sub limit, limit, count
227 lsr limit_wd, limit, #3
228
229.Lpage_end_loop:
230 ldrb data1w, [src1], #1
231 ldrb data2w, [src2], #1
232 cmp data1w, #1
233 ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
234 b.ne .Ldone
235 subs count, count, #1
236 b.hi .Lpage_end_loop
237
238.Ldo_misaligned:
239 /* Prepare ourselves for the next page crossing. Unlike the aligned
240 loop, we fetch 1 less dword because we risk crossing bounds on
241 SRC2. */
242 mov count, #8
243 subs limit_wd, limit_wd, #1
244 b.lo .Ldone_loop
245.Lloop_misaligned:
246 and tmp2, src2, #0xff8
247 eor tmp2, tmp2, #0xff8
248 cbz tmp2, .Lpage_end_loop
249
250 ldr data1, [src1], #8
251 ldr data2, [src2], #8
252 sub tmp1, data1, zeroones
253 orr tmp2, data1, #REP8_7f
254 eor diff, data1, data2 /* Non-zero if differences found. */
255 bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
256 ccmp diff, #0, #0, eq
257 b.ne .Lnot_limit
258 subs limit_wd, limit_wd, #1
259 b.pl .Lloop_misaligned
260
261.Ldone_loop:
262 /* We found a difference or a NULL before the limit was reached. */
263 and limit, limit, #7
264 cbz limit, .Lnot_limit
265 /* Read the last word. */
266 sub src1, src1, 8
267 sub src2, src2, 8
268 ldr data1, [src1, limit]
269 ldr data2, [src2, limit]
270 sub tmp1, data1, zeroones
271 orr tmp2, data1, #REP8_7f
272 eor diff, data1, data2 /* Non-zero if differences found. */
273 bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
274 ccmp diff, #0, #0, eq
275 b.ne .Lnot_limit
276
277.Lret0:
278 mov result, #0
279 ret
Bernhard Rosenkraenzer7e4fa562014-03-05 11:40:57 +0100280END(strncmp)