strchr.S 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* strchr (str, ch) -- Return pointer to first occurrence of CH in STR.
  2. For SPARC v7.
  3. Copyright (C) 1996, 1999, 2003 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Jakub Jelinek <jj@ultra.linux.cz> and
  6. David S. Miller <davem@caip.rutgers.edu>.
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; if not, write to the Free
  17. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA. */
  19. /* Normally, this uses ((xword - 0x01010101) & 0x80808080) test
  20. to find out if any byte in xword could be zero. This is fast, but
  21. also gives false alarm for any byte in range 0x81-0xff. It does
  22. not matter for correctness, as if this test tells us there could
  23. be some zero byte, we check it byte by byte, but if bytes with
  24. high bits set are common in the strings, then this will give poor
  25. performance. You can #define EIGHTBIT_NOT_RARE and the algorithm
  26. will use one tick slower, but more precise test
  27. ((xword - 0x01010101) & (~xword) & 0x80808080),
  28. which does not give any false alarms (but if some bits are set,
  29. one cannot assume from it which bytes are zero and which are not).
  30. It is yet to be measured, what is the correct default for glibc
  31. in these days for an average user.
  32. */
  33. .text
  34. .align 4
  35. 10: ldub [%o0], %g4
  36. cmp %g4, %o1
  37. be 1f
  38. add %o0, 1, %o0
  39. cmp %g4, 0
  40. be 9f
  41. andcc %o0, 3, %g0
  42. be 4f
  43. or %o4, %lo(0x80808080), %o3
  44. ldub [%o0], %g4
  45. cmp %g4, %o1
  46. be 1f
  47. add %o0, 1, %o0
  48. cmp %g4, 0
  49. be 9f
  50. andcc %o0, 3, %g0
  51. be 5f
  52. sethi %hi(0x01010101), %o5
  53. ldub [%o0], %g4
  54. cmp %g4, %o1
  55. be 1f
  56. add %o0, 1, %o0
  57. cmp %g4, 0
  58. be 9f
  59. or %o5, %lo(0x01010101), %o2
  60. b 6f
  61. ld [%o0], %g4
  62. 1: retl
  63. sub %o0, 1, %o0
  64. .globl strchr
  65. .set strchr,__strchr
  66. .hidden __strchr
  67. ENTRY(__strchr)
  68. andcc %o1, 0xff, %o1
  69. be 12f
  70. sll %o1, 8, %o2
  71. andcc %o0, 3, %g0
  72. or %o1, %o2, %o2
  73. sethi %hi(0x80808080), %o4
  74. sll %o2, 16, %o3
  75. bne 10b
  76. or %o3, %o2, %g2
  77. or %o4, %lo(0x80808080), %o3
  78. 4: sethi %hi(0x01010101), %o5
  79. 5: or %o5, %lo(0x01010101), %o2
  80. 7: ld [%o0], %g4
  81. 6: xor %g4, %g2, %g5
  82. sub %g4, %o2, %o4
  83. #ifdef EIGHTBIT_NOT_RARE
  84. sub %g5, %o2, %g6
  85. andn %o4, %g4, %o4
  86. andn %g6, %g5, %g5
  87. #else
  88. sub %g5, %o2, %g5
  89. #endif
  90. or %g5, %o4, %o4
  91. andcc %o4, %o3, %g0
  92. be 7b
  93. add %o0, 4, %o0
  94. /* Check every byte. */
  95. 8: srl %g4, 24, %g5
  96. 7: andcc %g5, 0xff, %g5
  97. be 9f
  98. cmp %g5, %o1
  99. be 4f
  100. srl %g4, 16, %g5
  101. andcc %g5, 0xff, %g5
  102. be 9f
  103. cmp %g5, %o1
  104. be 3f
  105. srl %g4, 8, %g5
  106. andcc %g5, 0xff, %g5
  107. be 9f
  108. cmp %g5, %o1
  109. be 2f
  110. andcc %g4, 0xff, %g5
  111. be 9f
  112. cmp %g5, %o1
  113. bne,a 6b
  114. ld [%o0], %g4
  115. retl
  116. sub %o0, 1, %o0
  117. 2: retl
  118. sub %o0, 2, %o0
  119. 3: retl
  120. sub %o0, 3, %o0
  121. 4: retl
  122. sub %o0, 4, %o0
  123. 9: retl
  124. clr %o0
  125. 11: ldub [%o0], %o5
  126. cmp %o5, 0
  127. be 1f
  128. add %o0, 1, %o0
  129. andcc %o0, 3, %g0
  130. be 4f
  131. or %o4, %lo(0x80808080), %o3
  132. ldub [%o0], %o5
  133. cmp %o5, 0
  134. be 1f
  135. add %o0, 1, %o0
  136. andcc %o0, 3, %g0
  137. be 5f
  138. sethi %hi(0x01010101), %o4
  139. ldub [%o0], %o5
  140. cmp %o5, 0
  141. be 1f
  142. add %o0, 1, %o0
  143. b 6f
  144. or %o4, %lo(0x01010101), %o2
  145. 1: retl
  146. sub %o0, 1, %o0
  147. 12: andcc %o0, 3, %g0
  148. bne 11b
  149. sethi %hi(0x80808080), %o4
  150. or %o4, %lo(0x80808080), %o3
  151. 4: sethi %hi(0x01010101), %o4
  152. 5: or %o4, %lo(0x01010101), %o2
  153. 6: ld [%o0], %o5
  154. 7: sub %o5, %o2, %o4
  155. #ifdef EIGHTBIT_NOT_RARE
  156. andn %o4, %o5, %o4
  157. #endif
  158. andcc %o4, %o3, %g0
  159. be 6b
  160. add %o0, 4, %o0
  161. /* Check every byte. */
  162. srl %o5, 24, %g5
  163. andcc %g5, 0xff, %g0
  164. be 8f
  165. add %o0, -4, %o4
  166. srl %o5, 16, %g5
  167. andcc %g5, 0xff, %g0
  168. be 8f
  169. add %o4, 1, %o4
  170. srl %o5, 8, %g5
  171. andcc %g5, 0xff, %g0
  172. be 8f
  173. add %o4, 1, %o4
  174. andcc %o5, 0xff, %g0
  175. bne,a 7b
  176. ld [%o0], %o5
  177. add %o4, 1, %o4
  178. 8: retl
  179. mov %o4, %o0
  180. 13: ldub [%o0], %g4
  181. cmp %g4, %o1
  182. add %o0, 1, %o0
  183. be,a 1f
  184. sub %o0, 1, %o5
  185. cmp %g4, 0
  186. be 9f
  187. 1: andcc %o0, 3, %g0
  188. be 4f
  189. or %o4, %lo(0x80808080), %o3
  190. ldub [%o0], %g4
  191. cmp %g4, %o1
  192. add %o0, 1, %o0
  193. be,a 1f
  194. sub %o0, 1, %o5
  195. cmp %g4, 0
  196. be 9f
  197. 1: andcc %o0, 3, %g0
  198. be 5f
  199. sethi %hi(0x01010101), %o4
  200. ldub [%o0], %g4
  201. cmp %g4, %o1
  202. add %o0, 1, %o0
  203. be,a 1f
  204. sub %o0, 1, %o5
  205. cmp %g4, 0
  206. be 9f
  207. 1: or %o4, %lo(0x01010101), %o2
  208. b 7f
  209. ld [%o0], %g4
  210. END(__strchr)
  211. .globl strrchr
  212. .set strrchr,__strrchr
  213. .hidden __strrchr
  214. ENTRY(__strrchr)
  215. andcc %o1, 0xff, %o1
  216. clr %o5
  217. be 12b
  218. sll %o1, 8, %o2
  219. andcc %o0, 3, %g0
  220. or %o1, %o2, %o2
  221. sethi %hi(0x80808080), %o4
  222. sll %o2, 16, %o3
  223. bne 13b
  224. or %o3, %o2, %g2
  225. or %o4, %lo(0x80808080), %o3
  226. 4: sethi %hi(0x01010101), %o4
  227. 5: or %o4, %lo(0x01010101), %o2
  228. 6: ld [%o0], %g4
  229. 7: xor %g4, %g2, %g5
  230. sub %g4, %o2, %o4
  231. #ifdef EIGHTBIT_NOT_RARE
  232. sub %g5, %o2, %g6
  233. andn %o4, %g4, %o4
  234. andn %g6, %g5, %g5
  235. #else
  236. sub %g5, %o2, %g5
  237. #endif
  238. or %g5, %o4, %o4
  239. andcc %o4, %o3, %g0
  240. be 6b
  241. add %o0, 4, %o0
  242. /* Check every byte. */
  243. 3: srl %g4, 24, %g5
  244. 8: andcc %g5, 0xff, %g5
  245. be 9f
  246. cmp %g5, %o1
  247. be,a 1f
  248. sub %o0, 4, %o5
  249. 1: srl %g4, 16, %g5
  250. andcc %g5, 0xff, %g5
  251. be 9f
  252. cmp %g5, %o1
  253. be,a 1f
  254. sub %o0, 3, %o5
  255. 1: srl %g4, 8, %g5
  256. andcc %g5, 0xff, %g5
  257. be 9f
  258. cmp %g5, %o1
  259. be,a 1f
  260. sub %o0, 2, %o5
  261. 1: andcc %g4, 0xff, %g5
  262. be 9f
  263. cmp %g5, %o1
  264. be,a 1f
  265. sub %o0, 1, %o5
  266. 1: b 7b
  267. ld [%o0], %g4
  268. 9: retl
  269. mov %o5, %o0
  270. END(__strrchr)
  271. weak_alias (strchr, index)
  272. weak_alias (strrchr, rindex)