memchr.S 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* memchr (str, ch, n) -- Return pointer to first occurrence of CH in STR less
  2. than N.
  3. For SPARC v9.
  4. Copyright (C) 1998, 1999, 2000, 2003 Free Software Foundation, Inc.
  5. This file is part of the GNU C Library.
  6. Contributed by Jan Vondrak <jvon4518@ss1000.ms.mff.cuni.cz> and
  7. Jakub Jelinek <jj@ultra.linux.cz>.
  8. This version is developed using the same algorithm as the fast C
  9. version which carries the following introduction:
  10. Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
  11. with help from Dan Sahlin (dan@sics.se) and
  12. commentary by Jim Blandy (jimb@ai.mit.edu);
  13. adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
  14. and implemented by Roland McGrath (roland@ai.mit.edu).
  15. The GNU C Library is free software; you can redistribute it and/or
  16. modify it under the terms of the GNU Lesser General Public
  17. License as published by the Free Software Foundation; either
  18. version 2.1 of the License, or (at your option) any later version.
  19. The GNU C Library is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. Lesser General Public License for more details.
  23. You should have received a copy of the GNU Lesser General Public
  24. License along with the GNU C Library; if not, write to the Free
  25. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  26. 02111-1307 USA. */
  27. #include <asm/asi.h>
  28. #ifndef XCC
  29. #define XCC xcc
  30. #define USE_BPR
  31. .register %g2, #scratch
  32. .register %g3, #scratch
  33. #endif
  34. /* Normally, this uses
  35. ((xword - 0x0101010101010101) & 0x8080808080808080) test
  36. to find out if any byte in xword could be zero. This is fast, but
  37. also gives false alarm for any byte in range 0x81-0xff. It does
  38. not matter for correctness, as if this test tells us there could
  39. be some zero byte, we check it byte by byte, but if bytes with
  40. high bits set are common in the strings, then this will give poor
  41. performance. You can #define EIGHTBIT_NOT_RARE and the algorithm
  42. will use one tick slower, but more precise test
  43. ((xword - 0x0101010101010101) & (~xword) & 0x8080808080808080),
  44. which does not give any false alarms (but if some bits are set,
  45. one cannot assume from it which bytes are zero and which are not).
  46. It is yet to be measured, what is the correct default for glibc
  47. in these days for an average user.
  48. */
  49. .text
  50. .align 32
  51. ENTRY(memchr)
  52. and %o1, 0xff, %o1 /* IEU0 Group */
  53. #ifdef USE_BPR
  54. brz,pn %o2, 12f /* CTI+IEU1 */
  55. #else
  56. tst %o2 /* IEU1 */
  57. be,pn %XCC, 12f /* CTI */
  58. #endif
  59. sll %o1, 8, %g3 /* IEU0 Group */
  60. add %o0, %o2, %o2 /* IEU1 */
  61. sethi %hi(0x01010101), %g1 /* IEU0 Group */
  62. or %g3, %o1, %g3 /* IEU1 */
  63. ldub [%o0], %o3 /* Load */
  64. sllx %g3, 16, %g5 /* IEU0 Group */
  65. or %g1, %lo(0x01010101), %g1 /* IEU1 */
  66. sllx %g1, 32, %g2 /* IEU0 Group */
  67. or %g3, %g5, %g3 /* IEU1 */
  68. sllx %g3, 32, %g5 /* IEU0 Group */
  69. cmp %o3, %o1 /* IEU1 */
  70. be,pn %xcc, 13f /* CTI */
  71. or %g1, %g2, %g1 /* IEU0 Group */
  72. andcc %o0, 7, %g0 /* IEU1 */
  73. bne,a,pn %icc, 21f /* CTI */
  74. add %o0, 1, %o0 /* IEU0 Group */
  75. ldx [%o0], %o3 /* Load Group */
  76. sllx %g1, 7, %g2 /* IEU0 */
  77. or %g3, %g5, %g3 /* IEU1 */
  78. 1: add %o0, 8, %o0 /* IEU0 Group */
  79. xor %o3, %g3, %o4 /* IEU1 */
  80. /* %g1 = 0101010101010101 *
  81. * %g2 = 8080088080808080 *
  82. * %g3 = c c c c c c c c *
  83. * %o3 = value *
  84. * %o4 = value XOR c */
  85. 2: cmp %o0, %o2 /* IEU1 Group */
  86. bg,pn %XCC, 11f /* CTI */
  87. ldxa [%o0] ASI_PNF, %o3 /* Load */
  88. sub %o4, %g1, %o5 /* IEU0 Group */
  89. add %o0, 8, %o0 /* IEU1 */
  90. #ifdef EIGHTBIT_NOT_RARE
  91. andn %o5, %o4, %o5 /* IEU0 Group */
  92. #endif
  93. andcc %o5, %g2, %g0 /* IEU1 Group */
  94. be,a,pt %xcc, 2b /* CTI */
  95. xor %o3, %g3, %o4 /* IEU0 */
  96. srlx %o4, 56, %g5 /* IEU0 */
  97. andcc %g5, 0xff, %g0 /* IEU1 Group */
  98. be,pn %icc, 3f /* CTI */
  99. srlx %o4, 48, %g5 /* IEU0 */
  100. andcc %g5, 0xff, %g0 /* IEU1 Group */
  101. be,pn %icc, 4f /* CTI */
  102. srlx %o4, 40, %g5 /* IEU0 */
  103. andcc %g5, 0xff, %g0 /* IEU1 Group */
  104. be,pn %icc, 5f /* CTI */
  105. srlx %o4, 32, %g5 /* IEU0 */
  106. andcc %g5, 0xff, %g0 /* IEU1 Group */
  107. be,pn %icc, 6f /* CTI */
  108. srlx %o4, 24, %g5 /* IEU0 */
  109. andcc %g5, 0xff, %g0 /* IEU1 Group */
  110. be,pn %icc, 7f /* CTI */
  111. srlx %o4, 16, %g5 /* IEU0 */
  112. andcc %g5, 0xff, %g0 /* IEU1 Group */
  113. be,pn %icc, 8f /* CTI */
  114. srlx %o4, 8, %g5 /* IEU0 */
  115. andcc %g5, 0xff, %g0 /* IEU1 Group */
  116. be,pn %icc, 9f /* CTI */
  117. andcc %o4, 0xff, %g0 /* IEU1 Group */
  118. bne,pt %icc, 2b /* CTI */
  119. xor %o3, %g3, %o4 /* IEU0 */
  120. retl /* CTI+IEU1 Group */
  121. add %o0, -9, %o0 /* IEU0 */
  122. .align 16
  123. 3: retl /* CTI+IEU1 Group */
  124. add %o0, -16, %o0 /* IEU0 */
  125. 4: retl /* CTI+IEU1 Group */
  126. add %o0, -15, %o0 /* IEU0 */
  127. 5: retl /* CTI+IEU1 Group */
  128. add %o0, -14, %o0 /* IEU0 */
  129. 6: retl /* CTI+IEU1 Group */
  130. add %o0, -13, %o0 /* IEU0 */
  131. 7: retl /* CTI+IEU1 Group */
  132. add %o0, -12, %o0 /* IEU0 */
  133. 8: retl /* CTI+IEU1 Group */
  134. add %o0, -11, %o0 /* IEU0 */
  135. 9: retl /* CTI+IEU1 Group */
  136. add %o0, -10, %o0 /* IEU0 */
  137. 11: sub %o4, %g1, %o5 /* IEU0 Group */
  138. sub %o0, 8, %o0 /* IEU1 */
  139. andcc %o5, %g2, %g0 /* IEU1 Group */
  140. be,pt %xcc, 12f /* CTI */
  141. sub %o2, %o0, %o2 /* IEU0 */
  142. tst %o2 /* IEU1 Group */
  143. be,pn %XCC, 12f /* CTI */
  144. srlx %o4, 56, %g5 /* IEU0 */
  145. andcc %g5, 0xff, %g0 /* IEU1 Group */
  146. be,pn %icc, 13f /* CTI */
  147. cmp %o2, 1 /* IEU0 */
  148. be,pn %XCC, 12f /* CTI Group */
  149. srlx %o4, 48, %g5 /* IEU0 */
  150. andcc %g5, 0xff, %g0 /* IEU1 Group */
  151. be,pn %icc, 14f /* CTI */
  152. cmp %o2, 2 /* IEU1 Group */
  153. be,pn %XCC, 12f /* CTI */
  154. srlx %o4, 40, %g5 /* IEU0 */
  155. andcc %g5, 0xff, %g0 /* IEU1 Group */
  156. be,pn %icc, 15f /* CTI */
  157. cmp %o2, 3 /* IEU1 Group */
  158. be,pn %XCC, 12f /* CTI */
  159. srlx %o4, 32, %g5 /* IEU0 */
  160. andcc %g5, 0xff, %g0 /* IEU1 Group */
  161. be,pn %icc, 16f /* CTI */
  162. cmp %o2, 4 /* IEU1 Group */
  163. be,pn %XCC, 12f /* CTI */
  164. srlx %o4, 24, %g5 /* IEU0 */
  165. andcc %g5, 0xff, %g0 /* IEU1 Group */
  166. be,pn %icc, 17f /* CTI */
  167. cmp %o2, 5 /* IEU1 Group */
  168. be,pn %XCC, 12f /* CTI */
  169. srlx %o4, 16, %g5 /* IEU0 */
  170. andcc %g5, 0xff, %g0 /* IEU1 Group */
  171. be,pn %icc, 18f /* CTI */
  172. cmp %o2, 6 /* IEU1 Group */
  173. be,pn %XCC, 12f /* CTI */
  174. srlx %o4, 8, %g5 /* IEU0 */
  175. andcc %g5, 0xff, %g0 /* IEU1 Group */
  176. be,pn %icc, 19f /* CTI */
  177. nop /* IEU0 */
  178. 12: retl /* CTI+IEU1 Group */
  179. clr %o0 /* IEU0 */
  180. nop /* Stub */
  181. 13: retl /* CTI+IEU1 Group */
  182. nop /* IEU0 */
  183. 14: retl /* CTI+IEU1 Group */
  184. add %o0, 1, %o0 /* IEU0 */
  185. 15: retl /* CTI+IEU1 Group */
  186. add %o0, 2, %o0 /* IEU0 */
  187. 16: retl /* CTI+IEU1 Group */
  188. add %o0, 3, %o0 /* IEU0 */
  189. 17: retl /* CTI+IEU1 Group */
  190. add %o0, 4, %o0 /* IEU0 */
  191. 18: retl /* CTI+IEU1 Group */
  192. add %o0, 5, %o0 /* IEU0 */
  193. 19: retl /* CTI+IEU1 Group */
  194. add %o0, 6, %o0 /* IEU0 */
  195. 21: cmp %o0, %o2 /* IEU1 */
  196. be,pn %XCC, 12b /* CTI */
  197. sllx %g1, 7, %g2 /* IEU0 Group */
  198. ldub [%o0], %o3 /* Load */
  199. or %g3, %g5, %g3 /* IEU1 */
  200. 22: andcc %o0, 7, %g0 /* IEU1 Group */
  201. be,a,pn %icc, 1b /* CTI */
  202. ldx [%o0], %o3 /* Load */
  203. cmp %o3, %o1 /* IEU1 Group */
  204. be,pn %xcc, 23f /* CTI */
  205. add %o0, 1, %o0 /* IEU0 */
  206. cmp %o0, %o2 /* IEU1 Group */
  207. bne,a,pt %XCC, 22b /* CTI */
  208. ldub [%o0], %o3 /* Load */
  209. retl /* CTI+IEU1 Group */
  210. clr %o0 /* IEU0 */
  211. 23: retl /* CTI+IEU1 Group */
  212. add %o0, -1, %o0 /* IEU0 */
  213. END(memchr)
  214. libc_hidden_def(memchr)
  215. #if !__BOUNDED_POINTERS__
  216. weak_alias(memchr,__ubp_memchr)
  217. #endif