2 * strcmp - compare two strings
4 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 * See https://llvm.org/LICENSE.txt for license information.
6 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 #include "../asmdefs.h"
16 #define REP8_01 0x0101010101010101
17 #define REP8_7f 0x7f7f7f7f7f7f7f7f
18 #define REP8_80 0x8080808080808080
20 /* Parameters and result. */
25 /* Internal variables. */
39 /* Start of performance-critical section -- one 64B cache line. */
40 ENTRY (__strcmp_aarch64)
42 mov zeroones, #REP8_01
47 /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
48 (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
49 can be done in parallel across the entire word. */
54 sub tmp1, data1, zeroones
55 orr tmp2, data1, #REP8_7f
56 eor diff, data1, data2 /* Non-zero if differences found. */
57 bic has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
58 orr syndrome, diff, has_nul
59 cbz syndrome, L(loop_aligned)
60 /* End of performance-critical section -- one 64B cache line. */
64 rev syndrome, syndrome
66 /* The MS-non-zero bit of the syndrome marks either the first bit
67 that is different, or the top bit of the first zero byte.
68 Shifting left now will bring the critical information into the
74 /* But we need to zero-extend (char is unsigned) the value and then
75 perform a signed 32-bit subtraction. */
77 sub result, data1, data2, lsr #56
80 /* For big-endian we cannot use the trick with the syndrome value
81 as carry-propagation can corrupt the upper bits if the trailing
82 bytes in the string contain 0x01. */
83 /* However, if there is no NUL byte in the dword, we can generate
84 the result directly. We can't just subtract the bytes as the
85 MSB might be significant. */
89 cneg result, result, lo
92 /* Re-compute the NUL-byte detection, using a byte-reversed value. */
94 sub tmp1, tmp3, zeroones
95 orr tmp2, tmp3, #REP8_7f
96 bic has_nul, tmp1, tmp2
98 orr syndrome, diff, has_nul
100 /* The MS-non-zero bit of the syndrome marks either the first bit
101 that is different, or the top bit of the first zero byte.
102 Shifting left now will bring the critical information into the
104 lsl data1, data1, pos
105 lsl data2, data2, pos
106 /* But we need to zero-extend (char is unsigned) the value and then
107 perform a signed 32-bit subtraction. */
108 lsr data1, data1, #56
109 sub result, data1, data2, lsr #56
114 /* Sources are mutually aligned, but are not currently at an
115 alignment boundary. Round down the addresses and then mask off
116 the bytes that preceed the start point. */
119 lsl tmp1, tmp1, #3 /* Bytes beyond alignment -> bits. */
120 ldr data1, [src1], #8
121 neg tmp1, tmp1 /* Bits to alignment -64. */
122 ldr data2, [src2], #8
125 /* Big-endian. Early bytes are at MSB. */
126 lsl tmp2, tmp2, tmp1 /* Shift (tmp1 & 63). */
128 /* Little-endian. Early bytes are at LSB. */
129 lsr tmp2, tmp2, tmp1 /* Shift (tmp1 & 63). */
131 orr data1, data1, tmp2
132 orr data2, data2, tmp2
136 /* Align SRC1 to 8 bytes and then compare 8 bytes at a time, always
137 checking to make sure that we don't access beyond page boundary in
140 b.eq L(loop_misaligned)
142 ldrb data1w, [src1], #1
143 ldrb data2w, [src2], #1
145 ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
148 b.ne L(do_misaligned)
151 /* Test if we are within the last dword of the end of a 4K page. If
152 yes then jump back to the misaligned loop to copy a byte at a time. */
153 and tmp1, src2, #0xff8
154 eor tmp1, tmp1, #0xff8
155 cbz tmp1, L(do_misaligned)
156 ldr data1, [src1], #8
157 ldr data2, [src2], #8
159 sub tmp1, data1, zeroones
160 orr tmp2, data1, #REP8_7f
161 eor diff, data1, data2 /* Non-zero if differences found. */
162 bic has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
163 orr syndrome, diff, has_nul
164 cbz syndrome, L(loop_misaligned)
168 sub result, data1, data2
171 END (__strcmp_aarch64)