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>
25 #define MASK0 0xff000000
26 #define MASK1 0x00ff0000
27 #define MASK2 0x0000ff00
28 #define MASK3 0x000000ff
30 #define MASK0 0x000000ff
31 #define MASK1 0x0000ff00
32 #define MASK2 0x00ff0000
33 #define MASK3 0xff000000
36 #define MASK4 0x40404040
38 .literal .Lmask0, MASK0
39 .literal .Lmask1, MASK1
40 .literal .Lmask2, MASK2
41 .literal .Lmask3, MASK3
42 .literal .Lmask4, MASK4
46 /* a2 = s1, a3 = s2 */
48 l8ui a8, a2, 0 // byte 0 from s1
49 l8ui a9, a3, 0 // byte 0 from s2
54 bnone a11, a10, .Laligned
56 xor a11, a2, a3 // compare low two bits of s1 and s2
57 bany a11, a10, .Lunaligned // if they have different alignment
59 /* s1/s2 are not word-aligned. */
60 addi a2, a2, 1 // advance s1
61 beqz a8, .Leq // bytes equal, if zero, strings are equal
62 addi a3, a3, 1 // advance s2
63 bnone a2, a10, .Laligned // if s1/s2 now aligned
64 l8ui a8, a2, 0 // byte 1 from s1
65 l8ui a9, a3, 0 // byte 1 from s2
66 addi a2, a2, 1 // advance s1
67 bne a8, a9, .Lretdiff // if different, return difference
68 beqz a8, .Leq // bytes equal, if zero, strings are equal
69 addi a3, a3, 1 // advance s2
70 bnone a2, a10, .Laligned // if s1/s2 now aligned
71 l8ui a8, a2, 0 // byte 2 from s1
72 l8ui a9, a3, 0 // byte 2 from s2
73 addi a2, a2, 1 // advance s1
74 bne a8, a9, .Lretdiff // if different, return difference
75 beqz a8, .Leq // bytes equal, if zero, strings are equal
76 addi a3, a3, 1 // advance s2
79 /* s1 and s2 have different alignment.
81 If the zero-overhead loop option is available, use an (almost)
82 infinite zero-overhead loop with conditional exits so we only pay
83 for taken branches when exiting the loop.
85 Note: It is important for this unaligned case to come before the
86 code for aligned strings, because otherwise some of the branches
87 above cannot reach and have to be transformed to branches around
88 jumps. The unaligned code is smaller and the branches can reach
92 /* (2 mod 4) alignment for loop instruction */
95 _movi.n a8, 0 // set up for the maximum loop count
96 loop a8, .Lretdiff // loop forever (almost anyway)
102 bne a8, a9, .Lretdiff
113 /* s1 is word-aligned; s2 is word-aligned.
115 If the zero-overhead loop option is available, use an (almost)
116 infinite zero-overhead loop with conditional exits so we only pay
117 for taken branches when exiting the loop. */
119 /* New algorithm, relying on the fact that all normal ASCII is between
122 Rather than check all bytes for zero:
123 Take one word (4 bytes). Call it w1.
124 Shift w1 left by one into w1'.
125 Or w1 and w1'. For all normal ASCII bit 6 will be 1; for zero it won't.
126 Check that all 4 bit 6's (one for each byte) are one:
127 If they are, we are definitely not done.
128 If they are not, we are probably done, but need to check for zero. */
134 l32r a4, .Lmask0 // mask for byte 0
136 /* Loop forever. (a4 is more than than the maximum number
138 loop a4, .Laligned_done
140 /* First unrolled loop body. */
141 l32i a8, a2, 0 // get word from s1
142 l32i a9, a3, 0 // get word from s2
146 bnall a9, a7, .Lprobeq
148 /* Second unrolled loop body. */
149 l32i a8, a2, 4 // get word from s1+4
150 l32i a9, a3, 4 // get word from s2+4
154 bnall a9, a7, .Lprobeq2
156 addi a2, a2, 8 // advance s1 pointer
157 addi a3, a3, 8 // advance s2 pointer
162 /* Adjust pointers to account for the loop unrolling. */
166 #else /* !XCHAL_HAVE_LOOPS */
169 movi a4, MASK0 // mask for byte 0
173 addi a2, a2, 4 // advance s1 pointer
174 addi a3, a3, 4 // advance s2 pointer
176 l32i a8, a2, 0 // get word from s1
177 l32i a9, a3, 0 // get word from s2
181 ball a9, a7, .Lnextword
182 #endif /* !XCHAL_HAVE_LOOPS */
184 /* align (0 mod 4) */
186 /* Words are probably equal, but check for sure.
187 If not, loop over the rest of string using normal algorithm. */
189 bnone a8, a4, .Leq // if byte 0 is zero
190 l32r a5, .Lmask1 // mask for byte 1
191 l32r a6, .Lmask2 // mask for byte 2
192 bnone a8, a5, .Leq // if byte 1 is zero
193 l32r a7, .Lmask3 // mask for byte 3
194 bnone a8, a6, .Leq // if byte 2 is zero
195 bnone a8, a7, .Leq // if byte 3 is zero
196 addi.n a2, a2, 4 // advance s1 pointer
197 addi.n a3, a3, 4 // advance s2 pointer
200 /* align (1 mod 4) */
201 loop a4, .Leq // loop forever (a4 is bigger than max iters)
204 l32i a8, a2, 0 // get word from s1
205 l32i a9, a3, 0 // get word from s2
206 addi a2, a2, 4 // advance s1 pointer
208 bnone a8, a4, .Leq // if byte 0 is zero
209 bnone a8, a5, .Leq // if byte 1 is zero
210 bnone a8, a6, .Leq // if byte 2 is zero
211 bnone a8, a7, .Leq // if byte 3 is zero
212 addi a3, a3, 4 // advance s2 pointer
214 #else /* !XCHAL_HAVE_LOOPS */
218 addi a3, a3, 4 // advance s2 pointer
220 l32i a8, a2, 0 // get word from s1
221 l32i a9, a3, 0 // get word from s2
222 addi a2, a2, 4 // advance s1 pointer
224 bnone a8, a4, .Leq // if byte 0 is zero
225 bnone a8, a5, .Leq // if byte 1 is zero
226 bnone a8, a6, .Leq // if byte 2 is zero
227 bany a8, a7, .Lnextword2 // if byte 3 is zero
228 #endif /* !XCHAL_HAVE_LOOPS */
230 /* Words are equal; some byte is zero. */
231 .Leq: movi a2, 0 // return equal
234 .Lwne2: /* Words are not equal. On big-endian processors, if none of the
235 bytes are zero, the return value can be determined by a simple
239 bnall a10, a7, .Lsomezero
240 bgeu a8, a9, .Lposreturn
246 .Lsomezero: // There is probably some zero byte.
247 #endif /* __XTENSA_EB__ */
248 .Lwne: /* Words are not equal. */
249 xor a2, a8, a9 // get word with nonzero in byte that differs
250 bany a2, a4, .Ldiff0 // if byte 0 differs
251 movi a5, MASK1 // mask for byte 1
252 bnone a8, a4, .Leq // if byte 0 is zero
253 bany a2, a5, .Ldiff1 // if byte 1 differs
254 movi a6, MASK2 // mask for byte 2
255 bnone a8, a5, .Leq // if byte 1 is zero
256 bany a2, a6, .Ldiff2 // if byte 2 differs
257 bnone a8, a6, .Leq // if byte 2 is zero
262 /* Byte 0 is equal (at least) and there is a difference before a zero
263 byte. Just subtract words to get the return value.
264 The high order equal bytes cancel, leaving room for the sign. */
269 /* Need to make room for the sign, so can't subtract whole words. */
275 #else /* !__XTENSA_EB__ */
276 /* Little-endian is a little more difficult because can't subtract
279 /* Bytes 0-2 are equal; byte 3 is different.
280 For little-endian need to have a sign bit for the difference. */
287 /* Byte 0 is different. */
294 /* Byte 0 is equal; byte 1 is different. */
301 /* Bytes 0-1 are equal; byte 2 is different. */
307 #endif /* !__XTENSA_EB */
309 libc_hidden_def (strcmp)
311 #ifndef __UCLIBC_HAS_LOCALE__
312 strong_alias (strcmp, strcoll)
313 libc_hidden_def (strcoll)