strcmp.S 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #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. .literal .Lmask0, MASK0
  31. .literal .Lmask1, MASK1
  32. .literal .Lmask2, MASK2
  33. .literal .Lmask3, MASK3
  34. .literal .Lmask4, MASK4
  35. .text
  36. ENTRY (strcmp)
  37. /* a2 = s1, a3 = s2 */
  38. l8ui a8, a2, 0 // byte 0 from s1
  39. l8ui a9, a3, 0 // byte 0 from s2
  40. movi a10, 3 // mask
  41. bne a8, a9, .Lretdiff
  42. or a11, a2, a3
  43. bnone a11, a10, .Laligned
  44. xor a11, a2, a3 // compare low two bits of s1 and s2
  45. bany a11, a10, .Lunaligned // if they have different alignment
  46. /* s1/s2 are not word-aligned. */
  47. addi a2, a2, 1 // advance s1
  48. beqz a8, .Leq // bytes equal, if zero, strings are equal
  49. addi a3, a3, 1 // advance s2
  50. bnone a2, a10, .Laligned // if s1/s2 now aligned
  51. l8ui a8, a2, 0 // byte 1 from s1
  52. l8ui a9, a3, 0 // byte 1 from s2
  53. addi a2, a2, 1 // advance s1
  54. bne a8, a9, .Lretdiff // if different, return difference
  55. beqz a8, .Leq // bytes equal, if zero, strings are equal
  56. addi a3, a3, 1 // advance s2
  57. bnone a2, a10, .Laligned // if s1/s2 now aligned
  58. l8ui a8, a2, 0 // byte 2 from s1
  59. l8ui a9, a3, 0 // byte 2 from s2
  60. addi a2, a2, 1 // advance s1
  61. bne a8, a9, .Lretdiff // if different, return difference
  62. beqz a8, .Leq // bytes equal, if zero, strings are equal
  63. addi a3, a3, 1 // advance s2
  64. j .Laligned
  65. /* s1 and s2 have different alignment.
  66. If the zero-overhead loop option is available, use an (almost)
  67. infinite zero-overhead loop with conditional exits so we only pay
  68. for taken branches when exiting the loop.
  69. Note: It is important for this unaligned case to come before the
  70. code for aligned strings, because otherwise some of the branches
  71. above cannot reach and have to be transformed to branches around
  72. jumps. The unaligned code is smaller and the branches can reach
  73. over it. */
  74. .align 4
  75. /* (2 mod 4) alignment for loop instruction */
  76. .Lunaligned:
  77. #if XCHAL_HAVE_LOOPS
  78. _movi.n a8, 0 // set up for the maximum loop count
  79. loop a8, .Lretdiff // loop forever (almost anyway)
  80. #endif
  81. .Lnextbyte:
  82. l8ui a8, a2, 0
  83. l8ui a9, a3, 0
  84. addi a2, a2, 1
  85. bne a8, a9, .Lretdiff
  86. addi a3, a3, 1
  87. #if XCHAL_HAVE_LOOPS
  88. beqz a8, .Lretdiff
  89. #else
  90. bnez a8, .Lnextbyte
  91. #endif
  92. .Lretdiff:
  93. sub a2, a8, a9
  94. retw
  95. /* s1 is word-aligned; s2 is word-aligned.
  96. If the zero-overhead loop option is available, use an (almost)
  97. infinite zero-overhead loop with conditional exits so we only pay
  98. for taken branches when exiting the loop. */
  99. /* New algorithm, relying on the fact that all normal ASCII is between
  100. 32 and 127.
  101. Rather than check all bytes for zero:
  102. Take one word (4 bytes). Call it w1.
  103. Shift w1 left by one into w1'.
  104. Or w1 and w1'. For all normal ASCII bit 6 will be 1; for zero it won't.
  105. Check that all 4 bit 6's (one for each byte) are one:
  106. If they are, we are definitely not done.
  107. If they are not, we are probably done, but need to check for zero. */
  108. .align 4
  109. #if XCHAL_HAVE_LOOPS
  110. .Laligned:
  111. .begin no-transform
  112. l32r a4, .Lmask0 // mask for byte 0
  113. l32r a7, .Lmask4
  114. /* Loop forever. (a4 is more than than the maximum number
  115. of iterations) */
  116. loop a4, .Laligned_done
  117. /* First unrolled loop body. */
  118. l32i a8, a2, 0 // get word from s1
  119. l32i a9, a3, 0 // get word from s2
  120. slli a5, a8, 1
  121. bne a8, a9, .Lwne2
  122. or a9, a8, a5
  123. bnall a9, a7, .Lprobeq
  124. /* Second unrolled loop body. */
  125. l32i a8, a2, 4 // get word from s1+4
  126. l32i a9, a3, 4 // get word from s2+4
  127. slli a5, a8, 1
  128. bne a8, a9, .Lwne2
  129. or a9, a8, a5
  130. bnall a9, a7, .Lprobeq2
  131. addi a2, a2, 8 // advance s1 pointer
  132. addi a3, a3, 8 // advance s2 pointer
  133. .Laligned_done:
  134. or a1, a1, a1 // nop
  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 a4, .Leq // loop forever (a4 is bigger than max iters)
  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. retw
  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. retw
  207. .Lposreturn:
  208. movi a2, 1
  209. retw
  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. retw
  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. retw
  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. retw
  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. retw
  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. retw
  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. retw
  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