strcmp.S 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Optimized strcmp for Xtensa.
  2. Copyright (C) 2001, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "../../sysdeps/linux/xtensa/sysdep.h"
  17. #include <bits/xtensa-config.h>
  18. #include <features.h>
  19. #ifdef __XTENSA_EB__
  20. #define MASK0 0xff000000
  21. #define MASK1 0x00ff0000
  22. #define MASK2 0x0000ff00
  23. #define MASK3 0x000000ff
  24. #else
  25. #define MASK0 0x000000ff
  26. #define MASK1 0x0000ff00
  27. #define MASK2 0x00ff0000
  28. #define MASK3 0xff000000
  29. #endif
  30. #define MASK4 0x40404040
  31. .literal .Lmask0, MASK0
  32. .literal .Lmask1, MASK1
  33. .literal .Lmask2, MASK2
  34. .literal .Lmask3, MASK3
  35. .literal .Lmask4, MASK4
  36. .text
  37. ENTRY (strcmp)
  38. /* a2 = s1, a3 = s2 */
  39. l8ui a8, a2, 0 /* byte 0 from s1 */
  40. l8ui a9, a3, 0 /* byte 0 from s2 */
  41. movi a10, 3 /* mask */
  42. bne a8, a9, .Lretdiff
  43. or a11, a2, a3
  44. bnone a11, a10, .Laligned
  45. xor a11, a2, a3 /* compare low two bits of s1 and s2 */
  46. bany a11, a10, .Lunaligned /* if they have different alignment */
  47. /* s1/s2 are not word-aligned. */
  48. addi a2, a2, 1 /* advance s1 */
  49. beqz a8, .Leq /* bytes equal, if zero, strings are equal */
  50. addi a3, a3, 1 /* advance s2 */
  51. bnone a2, a10, .Laligned /* if s1/s2 now aligned */
  52. l8ui a8, a2, 0 /* byte 1 from s1 */
  53. l8ui a9, a3, 0 /* byte 1 from s2 */
  54. addi a2, a2, 1 /* advance s1 */
  55. bne a8, a9, .Lretdiff /* if different, return difference */
  56. beqz a8, .Leq /* bytes equal, if zero, strings are equal */
  57. addi a3, a3, 1 /* advance s2 */
  58. bnone a2, a10, .Laligned /* if s1/s2 now aligned */
  59. l8ui a8, a2, 0 /* byte 2 from s1 */
  60. l8ui a9, a3, 0 /* byte 2 from s2 */
  61. addi a2, a2, 1 /* advance s1 */
  62. bne a8, a9, .Lretdiff /* if different, return difference */
  63. beqz a8, .Leq /* bytes equal, if zero, strings are equal */
  64. addi a3, a3, 1 /* advance s2 */
  65. j .Laligned
  66. /* s1 and s2 have different alignment.
  67. If the zero-overhead loop option is available, use an (almost)
  68. infinite zero-overhead loop with conditional exits so we only pay
  69. for taken branches when exiting the loop.
  70. Note: It is important for this unaligned case to come before the
  71. code for aligned strings, because otherwise some of the branches
  72. above cannot reach and have to be transformed to branches around
  73. jumps. The unaligned code is smaller and the branches can reach
  74. over it. */
  75. .align 4
  76. /* (2 mod 4) alignment for loop instruction */
  77. .Lunaligned:
  78. #if XCHAL_HAVE_LOOPS
  79. _movi.n a8, 0 /* set up for the maximum loop count */
  80. loop a8, .Lretdiff /* loop forever (almost anyway) */
  81. #endif
  82. .Lnextbyte:
  83. l8ui a8, a2, 0
  84. l8ui a9, a3, 0
  85. addi a2, a2, 1
  86. bne a8, a9, .Lretdiff
  87. addi a3, a3, 1
  88. #if XCHAL_HAVE_LOOPS
  89. beqz a8, .Lretdiff
  90. #else
  91. bnez a8, .Lnextbyte
  92. #endif
  93. .Lretdiff:
  94. sub a2, a8, a9
  95. retw
  96. /* s1 is word-aligned; s2 is word-aligned.
  97. If the zero-overhead loop option is available, use an (almost)
  98. infinite zero-overhead loop with conditional exits so we only pay
  99. for taken branches when exiting the loop. */
  100. /* New algorithm, relying on the fact that all normal ASCII is between
  101. 32 and 127.
  102. Rather than check all bytes for zero:
  103. Take one word (4 bytes). Call it w1.
  104. Shift w1 left by one into w1'.
  105. Or w1 and w1'. For all normal ASCII bit 6 will be 1; for zero it won't.
  106. Check that all 4 bit 6's (one for each byte) are one:
  107. If they are, we are definitely not done.
  108. If they are not, we are probably done, but need to check for zero. */
  109. .align 4
  110. #if XCHAL_HAVE_LOOPS
  111. .Laligned:
  112. .begin no-transform
  113. l32r a4, .Lmask0 /* mask for byte 0 */
  114. l32r a7, .Lmask4
  115. /* Loop forever. (a4 is more than than the maximum number
  116. of iterations) */
  117. loop a4, .Laligned_done
  118. /* First unrolled loop body. */
  119. l32i a8, a2, 0 /* get word from s1 */
  120. l32i a9, a3, 0 /* get word from s2 */
  121. slli a5, a8, 1
  122. bne a8, a9, .Lwne2
  123. or a9, a8, a5
  124. bnall a9, a7, .Lprobeq
  125. /* Second unrolled loop body. */
  126. l32i a8, a2, 4 /* get word from s1+4 */
  127. l32i a9, a3, 4 /* get word from s2+4 */
  128. slli a5, a8, 1
  129. bne a8, a9, .Lwne2
  130. or a9, a8, a5
  131. bnall a9, a7, .Lprobeq2
  132. addi a2, a2, 8 /* advance s1 pointer */
  133. addi a3, a3, 8 /* advance s2 pointer */
  134. .Laligned_done:
  135. or a1, a1, a1 /* nop */
  136. .Lprobeq2:
  137. /* Adjust pointers to account for the loop unrolling. */
  138. addi a2, a2, 4
  139. addi a3, a3, 4
  140. #else /* !XCHAL_HAVE_LOOPS */
  141. .Laligned:
  142. movi a4, MASK0 /* mask for byte 0 */
  143. movi a7, MASK4
  144. j .Lfirstword
  145. .Lnextword:
  146. addi a2, a2, 4 /* advance s1 pointer */
  147. addi a3, a3, 4 /* advance s2 pointer */
  148. .Lfirstword:
  149. l32i a8, a2, 0 /* get word from s1 */
  150. l32i a9, a3, 0 /* get word from s2 */
  151. slli a5, a8, 1
  152. bne a8, a9, .Lwne2
  153. or a9, a8, a5
  154. ball a9, a7, .Lnextword
  155. #endif /* !XCHAL_HAVE_LOOPS */
  156. /* align (0 mod 4) */
  157. .Lprobeq:
  158. /* Words are probably equal, but check for sure.
  159. If not, loop over the rest of string using normal algorithm. */
  160. bnone a8, a4, .Leq /* if byte 0 is zero */
  161. l32r a5, .Lmask1 /* mask for byte 1 */
  162. l32r a6, .Lmask2 /* mask for byte 2 */
  163. bnone a8, a5, .Leq /* if byte 1 is zero */
  164. l32r a7, .Lmask3 /* mask for byte 3 */
  165. bnone a8, a6, .Leq /* if byte 2 is zero */
  166. bnone a8, a7, .Leq /* if byte 3 is zero */
  167. addi.n a2, a2, 4 /* advance s1 pointer */
  168. addi.n a3, a3, 4 /* advance s2 pointer */
  169. #if XCHAL_HAVE_LOOPS
  170. /* align (1 mod 4) */
  171. loop a4, .Leq /* loop forever (a4 is bigger than max iters) */
  172. .end no-transform
  173. l32i a8, a2, 0 /* get word from s1 */
  174. l32i a9, a3, 0 /* get word from s2 */
  175. addi a2, a2, 4 /* advance s1 pointer */
  176. bne a8, a9, .Lwne
  177. bnone a8, a4, .Leq /* if byte 0 is zero */
  178. bnone a8, a5, .Leq /* if byte 1 is zero */
  179. bnone a8, a6, .Leq /* if byte 2 is zero */
  180. bnone a8, a7, .Leq /* if byte 3 is zero */
  181. addi a3, a3, 4 /* advance s2 pointer */
  182. #else /* !XCHAL_HAVE_LOOPS */
  183. j .Lfirstword2
  184. .Lnextword2:
  185. addi a3, a3, 4 /* advance s2 pointer */
  186. .Lfirstword2:
  187. l32i a8, a2, 0 /* get word from s1 */
  188. l32i a9, a3, 0 /* get word from s2 */
  189. addi a2, a2, 4 /* advance s1 pointer */
  190. bne a8, a9, .Lwne
  191. bnone a8, a4, .Leq /* if byte 0 is zero */
  192. bnone a8, a5, .Leq /* if byte 1 is zero */
  193. bnone a8, a6, .Leq /* if byte 2 is zero */
  194. bany a8, a7, .Lnextword2 /* if byte 3 is zero */
  195. #endif /* !XCHAL_HAVE_LOOPS */
  196. /* Words are equal; some byte is zero. */
  197. .Leq: movi a2, 0 /* return equal */
  198. retw
  199. .Lwne2: /* Words are not equal. On big-endian processors, if none of the
  200. bytes are zero, the return value can be determined by a simple
  201. comparison. */
  202. #ifdef __XTENSA_EB__
  203. or a10, a8, a5
  204. bnall a10, a7, .Lsomezero
  205. bgeu a8, a9, .Lposreturn
  206. movi a2, -1
  207. retw
  208. .Lposreturn:
  209. movi a2, 1
  210. retw
  211. .Lsomezero: /* There is probably some zero byte. */
  212. #endif /* __XTENSA_EB__ */
  213. .Lwne: /* Words are not equal. */
  214. xor a2, a8, a9 /* get word with nonzero in byte that differs */
  215. bany a2, a4, .Ldiff0 /* if byte 0 differs */
  216. movi a5, MASK1 /* mask for byte 1 */
  217. bnone a8, a4, .Leq /* if byte 0 is zero */
  218. bany a2, a5, .Ldiff1 /* if byte 1 differs */
  219. movi a6, MASK2 /* mask for byte 2 */
  220. bnone a8, a5, .Leq /* if byte 1 is zero */
  221. bany a2, a6, .Ldiff2 /* if byte 2 differs */
  222. bnone a8, a6, .Leq /* if byte 2 is zero */
  223. #ifdef __XTENSA_EB__
  224. .Ldiff3:
  225. .Ldiff2:
  226. .Ldiff1:
  227. /* Byte 0 is equal (at least) and there is a difference before a zero
  228. byte. Just subtract words to get the return value.
  229. The high order equal bytes cancel, leaving room for the sign. */
  230. sub a2, a8, a9
  231. retw
  232. .Ldiff0:
  233. /* Need to make room for the sign, so can't subtract whole words. */
  234. extui a10, a8, 24, 8
  235. extui a11, a9, 24, 8
  236. sub a2, a10, a11
  237. retw
  238. #else /* !__XTENSA_EB__ */
  239. /* Little-endian is a little more difficult because can't subtract
  240. whole words. */
  241. .Ldiff3:
  242. /* Bytes 0-2 are equal; byte 3 is different.
  243. For little-endian need to have a sign bit for the difference. */
  244. extui a10, a8, 24, 8
  245. extui a11, a9, 24, 8
  246. sub a2, a10, a11
  247. retw
  248. .Ldiff0:
  249. /* Byte 0 is different. */
  250. extui a10, a8, 0, 8
  251. extui a11, a9, 0, 8
  252. sub a2, a10, a11
  253. retw
  254. .Ldiff1:
  255. /* Byte 0 is equal; byte 1 is different. */
  256. extui a10, a8, 8, 8
  257. extui a11, a9, 8, 8
  258. sub a2, a10, a11
  259. retw
  260. .Ldiff2:
  261. /* Bytes 0-1 are equal; byte 2 is different. */
  262. extui a10, a8, 16, 8
  263. extui a11, a9, 16, 8
  264. sub a2, a10, a11
  265. retw
  266. #endif /* !__XTENSA_EB */
  267. libc_hidden_def (strcmp)
  268. #ifndef __UCLIBC_HAS_LOCALE__
  269. strong_alias (strcmp, strcoll)
  270. libc_hidden_def (strcoll)
  271. #endif