strcmp.S 8.5 KB

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