strcmp.S 8.7 KB

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