memchr.S 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, see
  25. <http://www.gnu.org/licenses/>. */
  26. #include <asm/asi.h>
  27. #ifndef XCC
  28. #define XCC xcc
  29. #define USE_BPR
  30. .register %g2, #scratch
  31. .register %g3, #scratch
  32. #endif
  33. /* Normally, this uses
  34. ((xword - 0x0101010101010101) & 0x8080808080808080) test
  35. to find out if any byte in xword could be zero. This is fast, but
  36. also gives false alarm for any byte in range 0x81-0xff. It does
  37. not matter for correctness, as if this test tells us there could
  38. be some zero byte, we check it byte by byte, but if bytes with
  39. high bits set are common in the strings, then this will give poor
  40. performance. You can #define EIGHTBIT_NOT_RARE and the algorithm
  41. will use one tick slower, but more precise test
  42. ((xword - 0x0101010101010101) & (~xword) & 0x8080808080808080),
  43. which does not give any false alarms (but if some bits are set,
  44. one cannot assume from it which bytes are zero and which are not).
  45. It is yet to be measured, what is the correct default for glibc
  46. in these days for an average user.
  47. */
  48. .text
  49. .align 32
  50. ENTRY(memchr)
  51. and %o1, 0xff, %o1 /* IEU0 Group */
  52. #ifdef USE_BPR
  53. brz,pn %o2, 12f /* CTI+IEU1 */
  54. #else
  55. tst %o2 /* IEU1 */
  56. be,pn %XCC, 12f /* CTI */
  57. #endif
  58. sll %o1, 8, %g3 /* IEU0 Group */
  59. add %o0, %o2, %o2 /* IEU1 */
  60. sethi %hi(0x01010101), %g1 /* IEU0 Group */
  61. or %g3, %o1, %g3 /* IEU1 */
  62. ldub [%o0], %o3 /* Load */
  63. sllx %g3, 16, %g5 /* IEU0 Group */
  64. or %g1, %lo(0x01010101), %g1 /* IEU1 */
  65. sllx %g1, 32, %g2 /* IEU0 Group */
  66. or %g3, %g5, %g3 /* IEU1 */
  67. sllx %g3, 32, %g5 /* IEU0 Group */
  68. cmp %o3, %o1 /* IEU1 */
  69. be,pn %xcc, 13f /* CTI */
  70. or %g1, %g2, %g1 /* IEU0 Group */
  71. andcc %o0, 7, %g0 /* IEU1 */
  72. bne,a,pn %icc, 21f /* CTI */
  73. add %o0, 1, %o0 /* IEU0 Group */
  74. ldx [%o0], %o3 /* Load Group */
  75. sllx %g1, 7, %g2 /* IEU0 */
  76. or %g3, %g5, %g3 /* IEU1 */
  77. 1: add %o0, 8, %o0 /* IEU0 Group */
  78. xor %o3, %g3, %o4 /* IEU1 */
  79. /* %g1 = 0101010101010101 *
  80. * %g2 = 8080088080808080 *
  81. * %g3 = c c c c c c c c *
  82. * %o3 = value *
  83. * %o4 = value XOR c */
  84. 2: cmp %o0, %o2 /* IEU1 Group */
  85. bg,pn %XCC, 11f /* CTI */
  86. ldxa [%o0] ASI_PNF, %o3 /* Load */
  87. sub %o4, %g1, %o5 /* IEU0 Group */
  88. add %o0, 8, %o0 /* IEU1 */
  89. #ifdef EIGHTBIT_NOT_RARE
  90. andn %o5, %o4, %o5 /* IEU0 Group */
  91. #endif
  92. andcc %o5, %g2, %g0 /* IEU1 Group */
  93. be,a,pt %xcc, 2b /* CTI */
  94. xor %o3, %g3, %o4 /* IEU0 */
  95. srlx %o4, 56, %g5 /* IEU0 */
  96. andcc %g5, 0xff, %g0 /* IEU1 Group */
  97. be,pn %icc, 3f /* CTI */
  98. srlx %o4, 48, %g5 /* IEU0 */
  99. andcc %g5, 0xff, %g0 /* IEU1 Group */
  100. be,pn %icc, 4f /* CTI */
  101. srlx %o4, 40, %g5 /* IEU0 */
  102. andcc %g5, 0xff, %g0 /* IEU1 Group */
  103. be,pn %icc, 5f /* CTI */
  104. srlx %o4, 32, %g5 /* IEU0 */
  105. andcc %g5, 0xff, %g0 /* IEU1 Group */
  106. be,pn %icc, 6f /* CTI */
  107. srlx %o4, 24, %g5 /* IEU0 */
  108. andcc %g5, 0xff, %g0 /* IEU1 Group */
  109. be,pn %icc, 7f /* CTI */
  110. srlx %o4, 16, %g5 /* IEU0 */
  111. andcc %g5, 0xff, %g0 /* IEU1 Group */
  112. be,pn %icc, 8f /* CTI */
  113. srlx %o4, 8, %g5 /* IEU0 */
  114. andcc %g5, 0xff, %g0 /* IEU1 Group */
  115. be,pn %icc, 9f /* CTI */
  116. andcc %o4, 0xff, %g0 /* IEU1 Group */
  117. bne,pt %icc, 2b /* CTI */
  118. xor %o3, %g3, %o4 /* IEU0 */
  119. retl /* CTI+IEU1 Group */
  120. add %o0, -9, %o0 /* IEU0 */
  121. .align 16
  122. 3: retl /* CTI+IEU1 Group */
  123. add %o0, -16, %o0 /* IEU0 */
  124. 4: retl /* CTI+IEU1 Group */
  125. add %o0, -15, %o0 /* IEU0 */
  126. 5: retl /* CTI+IEU1 Group */
  127. add %o0, -14, %o0 /* IEU0 */
  128. 6: retl /* CTI+IEU1 Group */
  129. add %o0, -13, %o0 /* IEU0 */
  130. 7: retl /* CTI+IEU1 Group */
  131. add %o0, -12, %o0 /* IEU0 */
  132. 8: retl /* CTI+IEU1 Group */
  133. add %o0, -11, %o0 /* IEU0 */
  134. 9: retl /* CTI+IEU1 Group */
  135. add %o0, -10, %o0 /* IEU0 */
  136. 11: sub %o4, %g1, %o5 /* IEU0 Group */
  137. sub %o0, 8, %o0 /* IEU1 */
  138. andcc %o5, %g2, %g0 /* IEU1 Group */
  139. be,pt %xcc, 12f /* CTI */
  140. sub %o2, %o0, %o2 /* IEU0 */
  141. tst %o2 /* IEU1 Group */
  142. be,pn %XCC, 12f /* CTI */
  143. srlx %o4, 56, %g5 /* IEU0 */
  144. andcc %g5, 0xff, %g0 /* IEU1 Group */
  145. be,pn %icc, 13f /* CTI */
  146. cmp %o2, 1 /* IEU0 */
  147. be,pn %XCC, 12f /* CTI Group */
  148. srlx %o4, 48, %g5 /* IEU0 */
  149. andcc %g5, 0xff, %g0 /* IEU1 Group */
  150. be,pn %icc, 14f /* CTI */
  151. cmp %o2, 2 /* IEU1 Group */
  152. be,pn %XCC, 12f /* CTI */
  153. srlx %o4, 40, %g5 /* IEU0 */
  154. andcc %g5, 0xff, %g0 /* IEU1 Group */
  155. be,pn %icc, 15f /* CTI */
  156. cmp %o2, 3 /* IEU1 Group */
  157. be,pn %XCC, 12f /* CTI */
  158. srlx %o4, 32, %g5 /* IEU0 */
  159. andcc %g5, 0xff, %g0 /* IEU1 Group */
  160. be,pn %icc, 16f /* CTI */
  161. cmp %o2, 4 /* IEU1 Group */
  162. be,pn %XCC, 12f /* CTI */
  163. srlx %o4, 24, %g5 /* IEU0 */
  164. andcc %g5, 0xff, %g0 /* IEU1 Group */
  165. be,pn %icc, 17f /* CTI */
  166. cmp %o2, 5 /* IEU1 Group */
  167. be,pn %XCC, 12f /* CTI */
  168. srlx %o4, 16, %g5 /* IEU0 */
  169. andcc %g5, 0xff, %g0 /* IEU1 Group */
  170. be,pn %icc, 18f /* CTI */
  171. cmp %o2, 6 /* IEU1 Group */
  172. be,pn %XCC, 12f /* CTI */
  173. srlx %o4, 8, %g5 /* IEU0 */
  174. andcc %g5, 0xff, %g0 /* IEU1 Group */
  175. be,pn %icc, 19f /* CTI */
  176. nop /* IEU0 */
  177. 12: retl /* CTI+IEU1 Group */
  178. clr %o0 /* IEU0 */
  179. nop /* Stub */
  180. 13: retl /* CTI+IEU1 Group */
  181. nop /* IEU0 */
  182. 14: retl /* CTI+IEU1 Group */
  183. add %o0, 1, %o0 /* IEU0 */
  184. 15: retl /* CTI+IEU1 Group */
  185. add %o0, 2, %o0 /* IEU0 */
  186. 16: retl /* CTI+IEU1 Group */
  187. add %o0, 3, %o0 /* IEU0 */
  188. 17: retl /* CTI+IEU1 Group */
  189. add %o0, 4, %o0 /* IEU0 */
  190. 18: retl /* CTI+IEU1 Group */
  191. add %o0, 5, %o0 /* IEU0 */
  192. 19: retl /* CTI+IEU1 Group */
  193. add %o0, 6, %o0 /* IEU0 */
  194. 21: cmp %o0, %o2 /* IEU1 */
  195. be,pn %XCC, 12b /* CTI */
  196. sllx %g1, 7, %g2 /* IEU0 Group */
  197. ldub [%o0], %o3 /* Load */
  198. or %g3, %g5, %g3 /* IEU1 */
  199. 22: andcc %o0, 7, %g0 /* IEU1 Group */
  200. be,a,pn %icc, 1b /* CTI */
  201. ldx [%o0], %o3 /* Load */
  202. cmp %o3, %o1 /* IEU1 Group */
  203. be,pn %xcc, 23f /* CTI */
  204. add %o0, 1, %o0 /* IEU0 */
  205. cmp %o0, %o2 /* IEU1 Group */
  206. bne,a,pt %XCC, 22b /* CTI */
  207. ldub [%o0], %o3 /* Load */
  208. retl /* CTI+IEU1 Group */
  209. clr %o0 /* IEU0 */
  210. 23: retl /* CTI+IEU1 Group */
  211. add %o0, -1, %o0 /* IEU0 */
  212. END(memchr)
  213. libc_hidden_def(memchr)
  214. weak_alias(memchr,__ubp_memchr)