1 /* Optimized strcmp for Xtensa.
2 Copyright (C) 2001, 2007 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, write to the Free
17 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
20 #include "../../sysdeps/linux/xtensa/sysdep.h"
21 #include <bits/xtensa-config.h>
24 #define MASK0 0xff000000
25 #define MASK1 0x00ff0000
26 #define MASK2 0x0000ff00
27 #define MASK3 0x000000ff
29 #define MASK0 0x000000ff
30 #define MASK1 0x0000ff00
31 #define MASK2 0x00ff0000
32 #define MASK3 0xff000000
35 #define MASK4 0x40404040
37 .literal .Lmask0, MASK0
38 .literal .Lmask1, MASK1
39 .literal .Lmask2, MASK2
40 .literal .Lmask3, MASK3
41 .literal .Lmask4, MASK4
45 /* a2 = s1, a3 = s2 */
47 l8ui a8, a2, 0 // byte 0 from s1
48 l8ui a9, a3, 0 // byte 0 from s2
53 bnone a11, a10, .Laligned
55 xor a11, a2, a3 // compare low two bits of s1 and s2
56 bany a11, a10, .Lunaligned // if they have different alignment
58 /* s1/s2 are not word-aligned. */
59 addi a2, a2, 1 // advance s1
60 beqz a8, .Leq // bytes equal, if zero, strings are equal
61 addi a3, a3, 1 // advance s2
62 bnone a2, a10, .Laligned // if s1/s2 now aligned
63 l8ui a8, a2, 0 // byte 1 from s1
64 l8ui a9, a3, 0 // byte 1 from s2
65 addi a2, a2, 1 // advance s1
66 bne a8, a9, .Lretdiff // if different, return difference
67 beqz a8, .Leq // bytes equal, if zero, strings are equal
68 addi a3, a3, 1 // advance s2
69 bnone a2, a10, .Laligned // if s1/s2 now aligned
70 l8ui a8, a2, 0 // byte 2 from s1
71 l8ui a9, a3, 0 // byte 2 from s2
72 addi a2, a2, 1 // advance s1
73 bne a8, a9, .Lretdiff // if different, return difference
74 beqz a8, .Leq // bytes equal, if zero, strings are equal
75 addi a3, a3, 1 // advance s2
78 /* s1 and s2 have different alignment.
80 If the zero-overhead loop option is available, use an (almost)
81 infinite zero-overhead loop with conditional exits so we only pay
82 for taken branches when exiting the loop.
84 Note: It is important for this unaligned case to come before the
85 code for aligned strings, because otherwise some of the branches
86 above cannot reach and have to be transformed to branches around
87 jumps. The unaligned code is smaller and the branches can reach
91 /* (2 mod 4) alignment for loop instruction */
94 _movi.n a8, 0 // set up for the maximum loop count
95 loop a8, .Lretdiff // loop forever (almost anyway)
101 bne a8, a9, .Lretdiff
112 /* s1 is word-aligned; s2 is word-aligned.
114 If the zero-overhead loop option is available, use an (almost)
115 infinite zero-overhead loop with conditional exits so we only pay
116 for taken branches when exiting the loop. */
118 /* New algorithm, relying on the fact that all normal ASCII is between
121 Rather than check all bytes for zero:
122 Take one word (4 bytes). Call it w1.
123 Shift w1 left by one into w1'.
124 Or w1 and w1'. For all normal ASCII bit 6 will be 1; for zero it won't.
125 Check that all 4 bit 6's (one for each byte) are one:
126 If they are, we are definitely not done.
127 If they are not, we are probably done, but need to check for zero. */
133 l32r a4, .Lmask0 // mask for byte 0
135 /* Loop forever. (a4 is more than than the maximum number
137 loop a4, .Laligned_done
139 /* First unrolled loop body. */
140 l32i a8, a2, 0 // get word from s1
141 l32i a9, a3, 0 // get word from s2
145 bnall a9, a7, .Lprobeq
147 /* Second unrolled loop body. */
148 l32i a8, a2, 4 // get word from s1+4
149 l32i a9, a3, 4 // get word from s2+4
153 bnall a9, a7, .Lprobeq2
155 addi a2, a2, 8 // advance s1 pointer
156 addi a3, a3, 8 // advance s2 pointer
161 /* Adjust pointers to account for the loop unrolling. */
165 #else /* !XCHAL_HAVE_LOOPS */
168 movi a4, MASK0 // mask for byte 0
172 addi a2, a2, 4 // advance s1 pointer
173 addi a3, a3, 4 // advance s2 pointer
175 l32i a8, a2, 0 // get word from s1
176 l32i a9, a3, 0 // get word from s2
180 ball a9, a7, .Lnextword
181 #endif /* !XCHAL_HAVE_LOOPS */
183 /* align (0 mod 4) */
185 /* Words are probably equal, but check for sure.
186 If not, loop over the rest of string using normal algorithm. */
188 bnone a8, a4, .Leq // if byte 0 is zero
189 l32r a5, .Lmask1 // mask for byte 1
190 l32r a6, .Lmask2 // mask for byte 2
191 bnone a8, a5, .Leq // if byte 1 is zero
192 l32r a7, .Lmask3 // mask for byte 3
193 bnone a8, a6, .Leq // if byte 2 is zero
194 bnone a8, a7, .Leq // if byte 3 is zero
195 addi.n a2, a2, 4 // advance s1 pointer
196 addi.n a3, a3, 4 // advance s2 pointer
199 /* align (1 mod 4) */
200 loop a4, .Leq // loop forever (a4 is bigger than max iters)
203 l32i a8, a2, 0 // get word from s1
204 l32i a9, a3, 0 // get word from s2
205 addi a2, a2, 4 // advance s1 pointer
207 bnone a8, a4, .Leq // if byte 0 is zero
208 bnone a8, a5, .Leq // if byte 1 is zero
209 bnone a8, a6, .Leq // if byte 2 is zero
210 bnone a8, a7, .Leq // if byte 3 is zero
211 addi a3, a3, 4 // advance s2 pointer
213 #else /* !XCHAL_HAVE_LOOPS */
217 addi a3, a3, 4 // advance s2 pointer
219 l32i a8, a2, 0 // get word from s1
220 l32i a9, a3, 0 // get word from s2
221 addi a2, a2, 4 // advance s1 pointer
223 bnone a8, a4, .Leq // if byte 0 is zero
224 bnone a8, a5, .Leq // if byte 1 is zero
225 bnone a8, a6, .Leq // if byte 2 is zero
226 bany a8, a7, .Lnextword2 // if byte 3 is zero
227 #endif /* !XCHAL_HAVE_LOOPS */
229 /* Words are equal; some byte is zero. */
230 .Leq: movi a2, 0 // return equal
233 .Lwne2: /* Words are not equal. On big-endian processors, if none of the
234 bytes are zero, the return value can be determined by a simple
238 bnall a10, a7, .Lsomezero
239 bgeu a8, a9, .Lposreturn
245 .Lsomezero: // There is probably some zero byte.
246 #endif /* __XTENSA_EB__ */
247 .Lwne: /* Words are not equal. */
248 xor a2, a8, a9 // get word with nonzero in byte that differs
249 bany a2, a4, .Ldiff0 // if byte 0 differs
250 movi a5, MASK1 // mask for byte 1
251 bnone a8, a4, .Leq // if byte 0 is zero
252 bany a2, a5, .Ldiff1 // if byte 1 differs
253 movi a6, MASK2 // mask for byte 2
254 bnone a8, a5, .Leq // if byte 1 is zero
255 bany a2, a6, .Ldiff2 // if byte 2 differs
256 bnone a8, a6, .Leq // if byte 2 is zero
261 /* Byte 0 is equal (at least) and there is a difference before a zero
262 byte. Just subtract words to get the return value.
263 The high order equal bytes cancel, leaving room for the sign. */
268 /* Need to make room for the sign, so can't subtract whole words. */
274 #else /* !__XTENSA_EB__ */
275 /* Little-endian is a little more difficult because can't subtract
278 /* Bytes 0-2 are equal; byte 3 is different.
279 For little-endian need to have a sign bit for the difference. */
286 /* Byte 0 is different. */
293 /* Byte 0 is equal; byte 1 is different. */
300 /* Bytes 0-1 are equal; byte 2 is different. */
306 #endif /* !__XTENSA_EB */
308 libc_hidden_def (strcmp)
310 #ifndef __UCLIBC_HAS_LOCALE__
311 strong_alias (strcmp, strcoll)
312 libc_hidden_def (strcoll)