2 * strcmp - compare two strings
4 * Copyright (c) 2012-2022, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
8 #if (defined (__OPTIMIZE_SIZE__) || defined (PREFER_SIZE_OVER_SPEED))
9 /* See strcmp-stub.c */
20 #define REP8_01 0x0101010101010101
21 #define REP8_7f 0x7f7f7f7f7f7f7f7f
41 /* On big-endian early bytes are at MSB and on little-endian LSB.
42 LS_FW means shifting towards early bytes. */
49 /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
50 (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
51 can be done in parallel across the entire word.
52 Since carry propagation makes 0x1 bytes before a NUL byte appear
53 NUL too in big-endian, byte-reverse the data before the NUL check. */
64 cbnz tmp, L(mutual_align)
69 ldr data2, [src1, off2]
74 sub has_nul, tmp, zeroones
77 sub has_nul, data1, zeroones
78 orr tmp, data1, REP8_7f
80 bics has_nul, has_nul, tmp /* Non-zero if NUL terminator. */
81 ccmp data1, data2, 0, eq
86 eor diff, data1, data2
87 orr syndrome, diff, has_nul
90 rev syndrome, syndrome
95 /* The most-significant-non-zero bit of the syndrome marks either the
96 first bit that is different, or the top bit of the first zero byte.
97 Shifting left now will bring the critical information into the
99 lsl data1, data1, shift
100 lsl data2, data2, shift
101 /* But we need to zero-extend (char is unsigned) the value and then
102 perform a signed 32-bit subtraction. */
104 sub result, data1, data2, lsr 56
110 /* Sources are mutually aligned, but are not currently at an
111 alignment boundary. Round down the addresses and then mask off
112 the bytes that precede the start point. */
114 ldr data2, [src1, off2]
116 neg shift, src2, lsl 3 /* Bits to alignment -64. */
118 LS_FW tmp, tmp, shift
119 orr data1, data1, tmp
120 orr data2, data2, tmp
124 /* Align SRC1 to 8 bytes and then compare 8 bytes at a time, always
125 checking to make sure that we don't access beyond the end of SRC2. */
126 cbz tmp, L(src1_aligned)
128 ldrb data1w, [src1], 1
129 ldrb data2w, [src2], 1
131 ccmp data1w, data2w, 0, ne /* NZCV = 0b0000. */
134 b.ne L(do_misaligned)
137 neg shift, src2, lsl 3
143 lsr tmp, zeroones, shift
144 orr data3, data3, tmp
145 sub has_nul, data3, zeroones
146 orr tmp, data3, REP8_7f
147 bics has_nul, has_nul, tmp
155 ldr data3, [src1, off1]
156 ldr data2, [src1, off2]
160 sub has_nul, data3, zeroones
161 orr tmp, data3, REP8_7f
163 bics has_nul, has_nul, tmp
164 ccmp data1, data2, 0, eq
165 b.eq L(loop_unaligned)
167 lsl tmp, has_nul, shift
171 eor diff, data1, data2
172 orr syndrome, diff, tmp
173 cbnz syndrome, L(end)
177 lsr data2, data3, shift
178 lsr has_nul, has_nul, shift
183 eor diff, data1, data2
184 orr syndrome, diff, has_nul
188 sub result, data1, data2