strcmp.S 8.7 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, 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.n a8, 0 /* set up for the maximum loop count */
  81. loop a8, .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. retw
  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. l32r a4, .Lmask0 /* mask for byte 0 */
  115. l32r a7, .Lmask4
  116. /* Loop forever. (a4 is more than than the maximum number
  117. of iterations) */
  118. loop a4, .Laligned_done
  119. /* First unrolled loop body. */
  120. l32i a8, a2, 0 /* get word from s1 */
  121. l32i a9, a3, 0 /* get word from s2 */
  122. slli a5, a8, 1
  123. bne a8, a9, .Lwne2
  124. or a9, a8, a5
  125. bnall a9, a7, .Lprobeq
  126. /* Second unrolled loop body. */
  127. l32i a8, a2, 4 /* get word from s1+4 */
  128. l32i a9, a3, 4 /* get word from s2+4 */
  129. slli a5, a8, 1
  130. bne a8, a9, .Lwne2
  131. or a9, a8, a5
  132. bnall a9, a7, .Lprobeq2
  133. addi a2, a2, 8 /* advance s1 pointer */
  134. addi a3, a3, 8 /* advance s2 pointer */
  135. .Laligned_done:
  136. or a1, a1, a1 /* nop */
  137. .Lprobeq2:
  138. /* Adjust pointers to account for the loop unrolling. */
  139. addi a2, a2, 4
  140. addi a3, a3, 4
  141. #else /* !XCHAL_HAVE_LOOPS */
  142. .Laligned:
  143. movi a4, MASK0 /* mask for byte 0 */
  144. movi a7, MASK4
  145. j .Lfirstword
  146. .Lnextword:
  147. addi a2, a2, 4 /* advance s1 pointer */
  148. addi a3, a3, 4 /* advance s2 pointer */
  149. .Lfirstword:
  150. l32i a8, a2, 0 /* get word from s1 */
  151. l32i a9, a3, 0 /* get word from s2 */
  152. slli a5, a8, 1
  153. bne a8, a9, .Lwne2
  154. or a9, a8, a5
  155. ball a9, a7, .Lnextword
  156. #endif /* !XCHAL_HAVE_LOOPS */
  157. /* align (0 mod 4) */
  158. .Lprobeq:
  159. /* Words are probably equal, but check for sure.
  160. If not, loop over the rest of string using normal algorithm. */
  161. bnone a8, a4, .Leq /* if byte 0 is zero */
  162. l32r a5, .Lmask1 /* mask for byte 1 */
  163. l32r a6, .Lmask2 /* mask for byte 2 */
  164. bnone a8, a5, .Leq /* if byte 1 is zero */
  165. l32r a7, .Lmask3 /* mask for byte 3 */
  166. bnone a8, a6, .Leq /* if byte 2 is zero */
  167. bnone a8, a7, .Leq /* if byte 3 is zero */
  168. addi.n a2, a2, 4 /* advance s1 pointer */
  169. addi.n a3, a3, 4 /* advance s2 pointer */
  170. #if XCHAL_HAVE_LOOPS
  171. /* align (1 mod 4) */
  172. loop a4, .Leq /* loop forever (a4 is bigger than max iters) */
  173. .end no-transform
  174. l32i a8, a2, 0 /* get word from s1 */
  175. l32i a9, a3, 0 /* get word from s2 */
  176. addi a2, a2, 4 /* advance s1 pointer */
  177. bne a8, a9, .Lwne
  178. bnone a8, a4, .Leq /* if byte 0 is zero */
  179. bnone a8, a5, .Leq /* if byte 1 is zero */
  180. bnone a8, a6, .Leq /* if byte 2 is zero */
  181. bnone a8, a7, .Leq /* if byte 3 is zero */
  182. addi a3, a3, 4 /* advance s2 pointer */
  183. #else /* !XCHAL_HAVE_LOOPS */
  184. j .Lfirstword2
  185. .Lnextword2:
  186. addi a3, a3, 4 /* advance s2 pointer */
  187. .Lfirstword2:
  188. l32i a8, a2, 0 /* get word from s1 */
  189. l32i a9, a3, 0 /* get word from s2 */
  190. addi a2, a2, 4 /* advance s1 pointer */
  191. bne a8, a9, .Lwne
  192. bnone a8, a4, .Leq /* if byte 0 is zero */
  193. bnone a8, a5, .Leq /* if byte 1 is zero */
  194. bnone a8, a6, .Leq /* if byte 2 is zero */
  195. bany a8, a7, .Lnextword2 /* if byte 3 is zero */
  196. #endif /* !XCHAL_HAVE_LOOPS */
  197. /* Words are equal; some byte is zero. */
  198. .Leq: movi a2, 0 /* return equal */
  199. retw
  200. .Lwne2: /* Words are not equal. On big-endian processors, if none of the
  201. bytes are zero, the return value can be determined by a simple
  202. comparison. */
  203. #ifdef __XTENSA_EB__
  204. or a10, a8, a5
  205. bnall a10, a7, .Lsomezero
  206. bgeu a8, a9, .Lposreturn
  207. movi a2, -1
  208. retw
  209. .Lposreturn:
  210. movi a2, 1
  211. retw
  212. .Lsomezero: /* There is probably some zero byte. */
  213. #endif /* __XTENSA_EB__ */
  214. .Lwne: /* Words are not equal. */
  215. xor a2, a8, a9 /* get word with nonzero in byte that differs */
  216. bany a2, a4, .Ldiff0 /* if byte 0 differs */
  217. movi a5, MASK1 /* mask for byte 1 */
  218. bnone a8, a4, .Leq /* if byte 0 is zero */
  219. bany a2, a5, .Ldiff1 /* if byte 1 differs */
  220. movi a6, MASK2 /* mask for byte 2 */
  221. bnone a8, a5, .Leq /* if byte 1 is zero */
  222. bany a2, a6, .Ldiff2 /* if byte 2 differs */
  223. bnone a8, a6, .Leq /* if byte 2 is zero */
  224. #ifdef __XTENSA_EB__
  225. .Ldiff3:
  226. .Ldiff2:
  227. .Ldiff1:
  228. /* Byte 0 is equal (at least) and there is a difference before a zero
  229. byte. Just subtract words to get the return value.
  230. The high order equal bytes cancel, leaving room for the sign. */
  231. sub a2, a8, a9
  232. retw
  233. .Ldiff0:
  234. /* Need to make room for the sign, so can't subtract whole words. */
  235. extui a10, a8, 24, 8
  236. extui a11, a9, 24, 8
  237. sub a2, a10, a11
  238. retw
  239. #else /* !__XTENSA_EB__ */
  240. /* Little-endian is a little more difficult because can't subtract
  241. whole words. */
  242. .Ldiff3:
  243. /* Bytes 0-2 are equal; byte 3 is different.
  244. For little-endian need to have a sign bit for the difference. */
  245. extui a10, a8, 24, 8
  246. extui a11, a9, 24, 8
  247. sub a2, a10, a11
  248. retw
  249. .Ldiff0:
  250. /* Byte 0 is different. */
  251. extui a10, a8, 0, 8
  252. extui a11, a9, 0, 8
  253. sub a2, a10, a11
  254. retw
  255. .Ldiff1:
  256. /* Byte 0 is equal; byte 1 is different. */
  257. extui a10, a8, 8, 8
  258. extui a11, a9, 8, 8
  259. sub a2, a10, a11
  260. retw
  261. .Ldiff2:
  262. /* Bytes 0-1 are equal; byte 2 is different. */
  263. extui a10, a8, 16, 8
  264. extui a11, a9, 16, 8
  265. sub a2, a10, a11
  266. retw
  267. #endif /* !__XTENSA_EB */
  268. libc_hidden_def (strcmp)
  269. #ifndef __UCLIBC_HAS_LOCALE__
  270. strong_alias (strcmp, strcoll)
  271. libc_hidden_def (strcoll)
  272. #endif