blob: e78bc9cfd2fc291b1bd1884ce8c77fcc14233eb8 [file] [log] [blame]
Christopher Ferris31dea252013-03-08 16:50:31 -08001/*
2 * Copyright (c) 2013 ARM Ltd
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the company may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <machine/cpu-features.h>
30#include <machine/asm.h>
31
32#ifdef __ARMEB__
33#define S2LOMEM lsl
34#define S2LOMEMEQ lsleq
35#define S2HIMEM lsr
36#define MSB 0x000000ff
37#define LSB 0xff000000
38#define BYTE0_OFFSET 24
39#define BYTE1_OFFSET 16
40#define BYTE2_OFFSET 8
41#define BYTE3_OFFSET 0
42#else /* not __ARMEB__ */
43#define S2LOMEM lsr
44#define S2LOMEMEQ lsreq
45#define S2HIMEM lsl
46#define BYTE0_OFFSET 0
47#define BYTE1_OFFSET 8
48#define BYTE2_OFFSET 16
49#define BYTE3_OFFSET 24
50#define MSB 0xff000000
51#define LSB 0x000000ff
52#endif /* not __ARMEB__ */
53
54.syntax unified
55
56#if defined (__thumb__)
57 .thumb
58 .thumb_func
59#endif
60
61ENTRY(strcmp)
62 /* Use LDRD whenever possible. */
63
64/* The main thing to look out for when comparing large blocks is that
65 the loads do not cross a page boundary when loading past the index
66 of the byte with the first difference or the first string-terminator.
67
68 For example, if the strings are identical and the string-terminator
69 is at index k, byte by byte comparison will not load beyond address
70 s1+k and s2+k; word by word comparison may load up to 3 bytes beyond
71 k; double word - up to 7 bytes. If the load of these bytes crosses
72 a page boundary, it might cause a memory fault (if the page is not mapped)
73 that would not have happened in byte by byte comparison.
74
75 If an address is (double) word aligned, then a load of a (double) word
76 from that address will not cross a page boundary.
77 Therefore, the algorithm below considers word and double-word alignment
78 of strings separately. */
79
80/* High-level description of the algorithm.
81
82 * The fast path: if both strings are double-word aligned,
83 use LDRD to load two words from each string in every loop iteration.
84 * If the strings have the same offset from a word boundary,
85 use LDRB to load and compare byte by byte until
86 the first string is aligned to a word boundary (at most 3 bytes).
87 This is optimized for quick return on short unaligned strings.
88 * If the strings have the same offset from a double-word boundary,
89 use LDRD to load two words from each string in every loop iteration, as in the fast path.
90 * If the strings do not have the same offset from a double-word boundary,
91 load a word from the second string before the loop to initialize the queue.
92 Use LDRD to load two words from every string in every loop iteration.
93 Inside the loop, load the second word from the second string only after comparing
94 the first word, using the queued value, to guarantee safety across page boundaries.
95 * If the strings do not have the same offset from a word boundary,
96 use LDR and a shift queue. Order of loads and comparisons matters,
97 similarly to the previous case.
98
99 * Use UADD8 and SEL to compare words, and use REV and CLZ to compute the return value.
100 * The only difference between ARM and Thumb modes is the use of CBZ instruction.
101 * The only difference between big and little endian is the use of REV in little endian
102 to compute the return value, instead of MOV.
103*/
104
105 .macro m_cbz reg label
106#ifdef __thumb2__
107 cbz \reg, \label
108#else /* not defined __thumb2__ */
109 cmp \reg, #0
110 beq \label
111#endif /* not defined __thumb2__ */
112 .endm /* m_cbz */
113
114 .macro m_cbnz reg label
115#ifdef __thumb2__
116 cbnz \reg, \label
117#else /* not defined __thumb2__ */
118 cmp \reg, #0
119 bne \label
120#endif /* not defined __thumb2__ */
121 .endm /* m_cbnz */
122
123 .macro init
124 /* Macro to save temporary registers and prepare magic values. */
125 subs sp, sp, #16
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -0700126 .cfi_def_cfa_offset 16
Christopher Ferris31dea252013-03-08 16:50:31 -0800127 strd r4, r5, [sp, #8]
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -0700128 .cfi_rel_offset r4, 0
129 .cfi_rel_offset r5, 4
Christopher Ferris31dea252013-03-08 16:50:31 -0800130 strd r6, r7, [sp]
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -0700131 .cfi_rel_offset r6, 8
132 .cfi_rel_offset r7, 12
Christopher Ferris31dea252013-03-08 16:50:31 -0800133 mvn r6, #0 /* all F */
134 mov r7, #0 /* all 0 */
135 .endm /* init */
136
137 .macro magic_compare_and_branch w1 w2 label
138 /* Macro to compare registers w1 and w2 and conditionally branch to label. */
139 cmp \w1, \w2 /* Are w1 and w2 the same? */
140 magic_find_zero_bytes \w1
141 it eq
142 cmpeq ip, #0 /* Is there a zero byte in w1? */
143 bne \label
144 .endm /* magic_compare_and_branch */
145
146 .macro magic_find_zero_bytes w1
147 /* Macro to find all-zero bytes in w1, result is in ip. */
Christopher Ferris31dea252013-03-08 16:50:31 -0800148 uadd8 ip, \w1, r6
149 sel ip, r7, r6
Christopher Ferris31dea252013-03-08 16:50:31 -0800150 .endm /* magic_find_zero_bytes */
151
152 .macro setup_return w1 w2
153#ifdef __ARMEB__
154 mov r1, \w1
155 mov r2, \w2
156#else /* not __ARMEB__ */
157 rev r1, \w1
158 rev r2, \w2
159#endif /* not __ARMEB__ */
160 .endm /* setup_return */
161
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -0700162 .cfi_startproc
Christopher Ferris31dea252013-03-08 16:50:31 -0800163 pld [r0, #0]
164 pld [r1, #0]
165
166 /* Are both strings double-word aligned? */
167 orr ip, r0, r1
168 tst ip, #7
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700169 bne .L_do_align
Christopher Ferris31dea252013-03-08 16:50:31 -0800170
171 /* Fast path. */
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700172 .save {r4-r7}
Christopher Ferris31dea252013-03-08 16:50:31 -0800173 init
174
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700175.L_doubleword_aligned:
Christopher Ferris31dea252013-03-08 16:50:31 -0800176
177 /* Get here when the strings to compare are double-word aligned. */
178 /* Compare two words in every iteration. */
179 .p2align 2
1802:
181 pld [r0, #16]
182 pld [r1, #16]
183
184 /* Load the next double-word from each string. */
185 ldrd r2, r3, [r0], #8
186 ldrd r4, r5, [r1], #8
187
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700188 magic_compare_and_branch w1=r2, w2=r4, label=.L_return_24
189 magic_compare_and_branch w1=r3, w2=r5, label=.L_return_35
Christopher Ferris31dea252013-03-08 16:50:31 -0800190 b 2b
191
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700192.L_do_align:
Christopher Ferris31dea252013-03-08 16:50:31 -0800193 /* Is the first string word-aligned? */
194 ands ip, r0, #3
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700195 beq .L_word_aligned_r0
Christopher Ferris31dea252013-03-08 16:50:31 -0800196
197 /* Fast compare byte by byte until the first string is word-aligned. */
198 /* The offset of r0 from a word boundary is in ip. Thus, the number of bytes
199 to read until the next word boundary is 4-ip. */
200 bic r0, r0, #3
201 ldr r2, [r0], #4
202 lsls ip, ip, #31
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700203 beq .L_byte2
204 bcs .L_byte3
Christopher Ferris31dea252013-03-08 16:50:31 -0800205
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700206.L_byte1:
Christopher Ferris31dea252013-03-08 16:50:31 -0800207 ldrb ip, [r1], #1
208 uxtb r3, r2, ror #BYTE1_OFFSET
209 subs ip, r3, ip
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700210 bne .L_fast_return
211 m_cbz reg=r3, label=.L_fast_return
Christopher Ferris31dea252013-03-08 16:50:31 -0800212
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700213.L_byte2:
Christopher Ferris31dea252013-03-08 16:50:31 -0800214 ldrb ip, [r1], #1
215 uxtb r3, r2, ror #BYTE2_OFFSET
216 subs ip, r3, ip
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700217 bne .L_fast_return
218 m_cbz reg=r3, label=.L_fast_return
Christopher Ferris31dea252013-03-08 16:50:31 -0800219
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700220.L_byte3:
Christopher Ferris31dea252013-03-08 16:50:31 -0800221 ldrb ip, [r1], #1
222 uxtb r3, r2, ror #BYTE3_OFFSET
223 subs ip, r3, ip
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700224 bne .L_fast_return
225 m_cbnz reg=r3, label=.L_word_aligned_r0
Christopher Ferris31dea252013-03-08 16:50:31 -0800226
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700227.L_fast_return:
Christopher Ferris31dea252013-03-08 16:50:31 -0800228 mov r0, ip
229 bx lr
230
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700231.L_word_aligned_r0:
Christopher Ferris31dea252013-03-08 16:50:31 -0800232 init
233 /* The first string is word-aligned. */
234 /* Is the second string word-aligned? */
235 ands ip, r1, #3
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700236 bne .L_strcmp_unaligned
Christopher Ferris31dea252013-03-08 16:50:31 -0800237
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700238.L_word_aligned:
Christopher Ferris31dea252013-03-08 16:50:31 -0800239 /* The strings are word-aligned. */
240 /* Is the first string double-word aligned? */
241 tst r0, #4
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700242 beq .L_doubleword_aligned_r0
Christopher Ferris31dea252013-03-08 16:50:31 -0800243
244 /* If r0 is not double-word aligned yet, align it by loading
245 and comparing the next word from each string. */
246 ldr r2, [r0], #4
247 ldr r4, [r1], #4
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700248 magic_compare_and_branch w1=r2 w2=r4 label=.L_return_24
Christopher Ferris31dea252013-03-08 16:50:31 -0800249
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700250.L_doubleword_aligned_r0:
Christopher Ferris31dea252013-03-08 16:50:31 -0800251 /* Get here when r0 is double-word aligned. */
252 /* Is r1 doubleword_aligned? */
253 tst r1, #4
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700254 beq .L_doubleword_aligned
Christopher Ferris31dea252013-03-08 16:50:31 -0800255
256 /* Get here when the strings to compare are word-aligned,
257 r0 is double-word aligned, but r1 is not double-word aligned. */
258
259 /* Initialize the queue. */
260 ldr r5, [r1], #4
261
262 /* Compare two words in every iteration. */
263 .p2align 2
2643:
265 pld [r0, #16]
266 pld [r1, #16]
267
268 /* Load the next double-word from each string and compare. */
269 ldrd r2, r3, [r0], #8
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700270 magic_compare_and_branch w1=r2 w2=r5 label=.L_return_25
Christopher Ferris31dea252013-03-08 16:50:31 -0800271 ldrd r4, r5, [r1], #8
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700272 magic_compare_and_branch w1=r3 w2=r4 label=.L_return_34
Christopher Ferris31dea252013-03-08 16:50:31 -0800273 b 3b
274
275 .macro miscmp_word offsetlo offsethi
276 /* Macro to compare misaligned strings. */
277 /* r0, r1 are word-aligned, and at least one of the strings
278 is not double-word aligned. */
279 /* Compare one word in every loop iteration. */
280 /* OFFSETLO is the original bit-offset of r1 from a word-boundary,
281 OFFSETHI is 32 - OFFSETLO (i.e., offset from the next word). */
282
283 /* Initialize the shift queue. */
284 ldr r5, [r1], #4
285
286 /* Compare one word from each string in every loop iteration. */
287 .p2align 2
2887:
289 ldr r3, [r0], #4
290 S2LOMEM r5, r5, #\offsetlo
291 magic_find_zero_bytes w1=r3
292 cmp r7, ip, S2HIMEM #\offsetlo
293 and r2, r3, r6, S2LOMEM #\offsetlo
294 it eq
295 cmpeq r2, r5
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700296 bne .L_return_25
Christopher Ferris31dea252013-03-08 16:50:31 -0800297 ldr r5, [r1], #4
298 cmp ip, #0
299 eor r3, r2, r3
300 S2HIMEM r2, r5, #\offsethi
301 it eq
302 cmpeq r3, r2
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700303 bne .L_return_32
Christopher Ferris31dea252013-03-08 16:50:31 -0800304 b 7b
305 .endm /* miscmp_word */
306
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700307.L_strcmp_unaligned:
Christopher Ferris31dea252013-03-08 16:50:31 -0800308 /* r0 is word-aligned, r1 is at offset ip from a word. */
309 /* Align r1 to the (previous) word-boundary. */
310 bic r1, r1, #3
311
312 /* Unaligned comparison word by word using LDRs. */
313 cmp ip, #2
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700314 beq .L_miscmp_word_16 /* If ip == 2. */
315 bge .L_miscmp_word_24 /* If ip == 3. */
Christopher Ferris31dea252013-03-08 16:50:31 -0800316 miscmp_word offsetlo=8 offsethi=24 /* If ip == 1. */
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700317.L_miscmp_word_24: miscmp_word offsetlo=24 offsethi=8
Christopher Ferris31dea252013-03-08 16:50:31 -0800318
319
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700320.L_return_32:
Christopher Ferris31dea252013-03-08 16:50:31 -0800321 setup_return w1=r3, w2=r2
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700322 b .L_do_return
323.L_return_34:
Christopher Ferris31dea252013-03-08 16:50:31 -0800324 setup_return w1=r3, w2=r4
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700325 b .L_do_return
326.L_return_25:
Christopher Ferris31dea252013-03-08 16:50:31 -0800327 setup_return w1=r2, w2=r5
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700328 b .L_do_return
329.L_return_35:
Christopher Ferris31dea252013-03-08 16:50:31 -0800330 setup_return w1=r3, w2=r5
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700331 b .L_do_return
332.L_return_24:
Christopher Ferris31dea252013-03-08 16:50:31 -0800333 setup_return w1=r2, w2=r4
334
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700335.L_do_return:
Christopher Ferris31dea252013-03-08 16:50:31 -0800336
337#ifdef __ARMEB__
338 mov r0, ip
339#else /* not __ARMEB__ */
340 rev r0, ip
341#endif /* not __ARMEB__ */
342
343 /* Restore temporaries early, before computing the return value. */
344 ldrd r6, r7, [sp]
345 ldrd r4, r5, [sp, #8]
346 adds sp, sp, #16
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -0700347 .cfi_def_cfa_offset 0
348 .cfi_restore r4
349 .cfi_restore r5
350 .cfi_restore r6
351 .cfi_restore r7
Christopher Ferris31dea252013-03-08 16:50:31 -0800352
353 /* There is a zero or a different byte between r1 and r2. */
354 /* r0 contains a mask of all-zero bytes in r1. */
355 /* Using r0 and not ip here because cbz requires low register. */
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700356 m_cbz reg=r0, label=.L_compute_return_value
Christopher Ferris31dea252013-03-08 16:50:31 -0800357 clz r0, r0
358 /* r0 contains the number of bits on the left of the first all-zero byte in r1. */
359 rsb r0, r0, #24
360 /* Here, r0 contains the number of bits on the right of the first all-zero byte in r1. */
361 lsr r1, r1, r0
362 lsr r2, r2, r0
363
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700364.L_compute_return_value:
Christopher Ferris31dea252013-03-08 16:50:31 -0800365 movs r0, #1
366 cmp r1, r2
367 /* The return value is computed as follows.
368 If r1>r2 then (C==1 and Z==0) and LS doesn't hold and r0 is #1 at return.
369 If r1<r2 then (C==0 and Z==0) and we execute SBC with carry_in=0,
370 which means r0:=r0-r0-1 and r0 is #-1 at return.
371 If r1=r2 then (C==1 and Z==1) and we execute SBC with carry_in=1,
372 which means r0:=r0-r0 and r0 is #0 at return.
373 (C==0 and Z==1) cannot happen because the carry bit is "not borrow". */
374 it ls
375 sbcls r0, r0, r0
376 bx lr
377
378 /* The code from the previous version of strcmp.S handles this
379 * particular case (the second string is 2 bytes off a word alignment)
380 * faster than any current version. In this very specific case, use the
381 * previous version. See bionic/libc/arch-arm/cortex-a15/bionic/strcmp.S
382 * for the unedited version of this code.
383 */
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700384.L_miscmp_word_16:
Christopher Ferris31dea252013-03-08 16:50:31 -0800385 wp1 .req r0
386 wp2 .req r1
387 b1 .req r2
388 w1 .req r4
389 w2 .req r5
390 t1 .req ip
391 @ r3 is scratch
392
393 /* At this point, wp1 (r0) has already been word-aligned. */
3942:
395 mov b1, #1
396 orr b1, b1, b1, lsl #8
397 orr b1, b1, b1, lsl #16
398
399 and t1, wp2, #3
400 bic wp2, wp2, #3
401 ldr w1, [wp1], #4
402 ldr w2, [wp2], #4
403
404 /* Critical inner Loop: Block with 2 bytes initial overlap */
405 .p2align 2
4062:
407 S2HIMEM t1, w1, #16
408 sub r3, w1, b1
409 S2LOMEM t1, t1, #16
410 bic r3, r3, w1
411 cmp t1, w2, S2LOMEM #16
412 bne 4f
413 ands r3, r3, b1, lsl #7
414 it eq
415 ldreq w2, [wp2], #4
416 bne 5f
417 eor t1, t1, w1
418 cmp t1, w2, S2HIMEM #16
419 bne 6f
420 ldr w1, [wp1], #4
421 b 2b
422
4235:
424#ifdef __ARMEB__
425 /* The syndrome value may contain false ones if the string ends
426 * with the bytes 0x01 0x00
427 */
428 tst w1, #0xff000000
429 it ne
430 tstne w1, #0x00ff0000
431 beq 7f
432#else
433 lsls r3, r3, #16
434 bne 7f
435#endif
436 ldrh w2, [wp2]
437 S2LOMEM t1, w1, #16
438#ifdef __ARMEB__
439 lsl w2, w2, #16
440#endif
441 b 8f
442
4436:
444 S2HIMEM w2, w2, #16
445 S2LOMEM t1, w1, #16
4464:
447 S2LOMEM w2, w2, #16
448 b 8f
449
4507:
451 mov r0, #0
452
453 /* Restore registers and stack. */
454 ldrd r6, r7, [sp]
455 ldrd r4, r5, [sp, #8]
456 adds sp, sp, #16
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -0700457 .cfi_def_cfa_offset 0
458 .cfi_restore r4
459 .cfi_restore r5
460 .cfi_restore r6
461 .cfi_restore r7
Christopher Ferris31dea252013-03-08 16:50:31 -0800462
463 bx lr
464
4658:
466 and r2, t1, #LSB
467 and r0, w2, #LSB
468 cmp r0, #1
469 it cs
470 cmpcs r0, r2
471 itt eq
472 S2LOMEMEQ t1, t1, #8
473 S2LOMEMEQ w2, w2, #8
474 beq 8b
475 sub r0, r2, r0
476
477 /* Restore registers and stack. */
478 ldrd r6, r7, [sp]
479 ldrd r4, r5, [sp, #8]
480 adds sp, sp, #16
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -0700481 .cfi_def_cfa_offset 0
482 .cfi_restore r4
483 .cfi_restore r5
484 .cfi_restore r6
485 .cfi_restore r7
Christopher Ferris31dea252013-03-08 16:50:31 -0800486
487 bx lr
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -0700488 .cfi_endproc
Christopher Ferris31dea252013-03-08 16:50:31 -0800489END(strcmp)