strchr.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* strchr (str, ch) -- Return pointer to first occurrence of CH in STR.
  2. For AMD x86-64.
  3. Copyright (C) 2002, 2005 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include "_glibc_inc.h"
  18. .text
  19. .globl strchr
  20. .set strchr,__strchr
  21. .hidden __strchr
  22. ENTRY (BP_SYM (__strchr))
  23. /* Before we start with the main loop we process single bytes
  24. until the source pointer is aligned. This has two reasons:
  25. 1. aligned 64-bit memory access is faster
  26. and (more important)
  27. 2. we process in the main loop 64 bit in one step although
  28. we don't know the end of the string. But accessing at
  29. 8-byte alignment guarantees that we never access illegal
  30. memory if this would not also be done by the trivial
  31. implementation (this is because all processor inherent
  32. boundaries are multiples of 8). */
  33. movq %rdi, %rdx
  34. andl $7, %edx /* Mask alignment bits */
  35. movq %rdi, %rax /* duplicate destination. */
  36. jz 1f /* aligned => start loop */
  37. neg %edx
  38. addl $8, %edx /* Align to 8 bytes. */
  39. /* Search the first bytes directly. */
  40. 0: movb (%rax), %cl /* load byte */
  41. cmpb %cl,%sil /* compare byte. */
  42. je 6f /* target found */
  43. testb %cl,%cl /* is byte NUL? */
  44. je 7f /* yes => return NULL */
  45. incq %rax /* increment pointer */
  46. decl %edx
  47. jnz 0b
  48. 1:
  49. /* At the moment %rsi contains C. What we need for the
  50. algorithm is C in all bytes of the register. Avoid
  51. operations on 16 bit words because these require an
  52. prefix byte (and one more cycle). */
  53. /* Populate 8 bit data to full 64-bit. */
  54. movabs $0x0101010101010101,%r9
  55. movzbl %sil,%edx
  56. imul %rdx,%r9
  57. movq $0xfefefefefefefeff, %r8 /* Save magic. */
  58. /* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
  59. change any of the hole bits of LONGWORD.
  60. 1) Is this safe? Will it catch all the zero bytes?
  61. Suppose there is a byte with all zeros. Any carry bits
  62. propagating from its left will fall into the hole at its
  63. least significant bit and stop. Since there will be no
  64. carry from its most significant bit, the LSB of the
  65. byte to the left will be unchanged, and the zero will be
  66. detected.
  67. 2) Is this worthwhile? Will it ignore everything except
  68. zero bytes? Suppose every byte of QUARDWORD has a bit set
  69. somewhere. There will be a carry into bit 8. If bit 8
  70. is set, this will carry into bit 16. If bit 8 is clear,
  71. one of bits 9-15 must be set, so there will be a carry
  72. into bit 16. Similarly, there will be a carry into bit
  73. 24 tec.. If one of bits 54-63 is set, there will be a carry
  74. into bit 64 (=carry flag), so all of the hole bits will
  75. be changed.
  76. 3) But wait! Aren't we looking for C, not zero?
  77. Good point. So what we do is XOR LONGWORD with a longword,
  78. each of whose bytes is C. This turns each byte that is C
  79. into a zero. */
  80. .p2align 4
  81. 4:
  82. /* Main Loop is unrolled 4 times. */
  83. /* First unroll. */
  84. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  85. addq $8,%rax /* adjust pointer for next word */
  86. movq %r8, %rdx /* magic value */
  87. xorq %r9, %rcx /* XOR with qword c|...|c => bytes of str == c
  88. are now 0 */
  89. addq %rcx, %rdx /* add the magic value to the word. We get
  90. carry bits reported for each byte which
  91. is *not* 0 */
  92. jnc 3f /* highest byte is NUL => return pointer */
  93. xorq %rcx, %rdx /* (word+magic)^word */
  94. orq %r8, %rdx /* set all non-carry bits */
  95. incq %rdx /* add 1: if one carry bit was *not* set
  96. the addition will not result in 0. */
  97. jnz 3f /* found c => return pointer */
  98. /* The quadword we looked at does not contain the value we're looking
  99. for. Let's search now whether we have reached the end of the
  100. string. */
  101. xorq %r9, %rcx /* restore original dword without reload */
  102. movq %r8, %rdx /* magic value */
  103. addq %rcx, %rdx /* add the magic value to the word. We get
  104. carry bits reported for each byte which
  105. is *not* 0 */
  106. jnc 7f /* highest byte is NUL => return NULL */
  107. xorq %rcx, %rdx /* (word+magic)^word */
  108. orq %r8, %rdx /* set all non-carry bits */
  109. incq %rdx /* add 1: if one carry bit was *not* set
  110. the addition will not result in 0. */
  111. jnz 7f /* found NUL => return NULL */
  112. /* Second unroll. */
  113. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  114. addq $8,%rax /* adjust pointer for next word */
  115. movq %r8, %rdx /* magic value */
  116. xorq %r9, %rcx /* XOR with qword c|...|c => bytes of str == c
  117. are now 0 */
  118. addq %rcx, %rdx /* add the magic value to the word. We get
  119. carry bits reported for each byte which
  120. is *not* 0 */
  121. jnc 3f /* highest byte is NUL => return pointer */
  122. xorq %rcx, %rdx /* (word+magic)^word */
  123. orq %r8, %rdx /* set all non-carry bits */
  124. incq %rdx /* add 1: if one carry bit was *not* set
  125. the addition will not result in 0. */
  126. jnz 3f /* found c => return pointer */
  127. /* The quadword we looked at does not contain the value we're looking
  128. for. Let's search now whether we have reached the end of the
  129. string. */
  130. xorq %r9, %rcx /* restore original dword without reload */
  131. movq %r8, %rdx /* magic value */
  132. addq %rcx, %rdx /* add the magic value to the word. We get
  133. carry bits reported for each byte which
  134. is *not* 0 */
  135. jnc 7f /* highest byte is NUL => return NULL */
  136. xorq %rcx, %rdx /* (word+magic)^word */
  137. orq %r8, %rdx /* set all non-carry bits */
  138. incq %rdx /* add 1: if one carry bit was *not* set
  139. the addition will not result in 0. */
  140. jnz 7f /* found NUL => return NULL */
  141. /* Third unroll. */
  142. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  143. addq $8,%rax /* adjust pointer for next word */
  144. movq %r8, %rdx /* magic value */
  145. xorq %r9, %rcx /* XOR with qword c|...|c => bytes of str == c
  146. are now 0 */
  147. addq %rcx, %rdx /* add the magic value to the word. We get
  148. carry bits reported for each byte which
  149. is *not* 0 */
  150. jnc 3f /* highest byte is NUL => return pointer */
  151. xorq %rcx, %rdx /* (word+magic)^word */
  152. orq %r8, %rdx /* set all non-carry bits */
  153. incq %rdx /* add 1: if one carry bit was *not* set
  154. the addition will not result in 0. */
  155. jnz 3f /* found c => return pointer */
  156. /* The quadword we looked at does not contain the value we're looking
  157. for. Let's search now whether we have reached the end of the
  158. string. */
  159. xorq %r9, %rcx /* restore original dword without reload */
  160. movq %r8, %rdx /* magic value */
  161. addq %rcx, %rdx /* add the magic value to the word. We get
  162. carry bits reported for each byte which
  163. is *not* 0 */
  164. jnc 7f /* highest byte is NUL => return NULL */
  165. xorq %rcx, %rdx /* (word+magic)^word */
  166. orq %r8, %rdx /* set all non-carry bits */
  167. incq %rdx /* add 1: if one carry bit was *not* set
  168. the addition will not result in 0. */
  169. jnz 7f /* found NUL => return NULL */
  170. /* Fourth unroll. */
  171. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  172. addq $8,%rax /* adjust pointer for next word */
  173. movq %r8, %rdx /* magic value */
  174. xorq %r9, %rcx /* XOR with qword c|...|c => bytes of str == c
  175. are now 0 */
  176. addq %rcx, %rdx /* add the magic value to the word. We get
  177. carry bits reported for each byte which
  178. is *not* 0 */
  179. jnc 3f /* highest byte is NUL => return pointer */
  180. xorq %rcx, %rdx /* (word+magic)^word */
  181. orq %r8, %rdx /* set all non-carry bits */
  182. incq %rdx /* add 1: if one carry bit was *not* set
  183. the addition will not result in 0. */
  184. jnz 3f /* found c => return pointer */
  185. /* The quadword we looked at does not contain the value we're looking
  186. for. Let's search now whether we have reached the end of the
  187. string. */
  188. xorq %r9, %rcx /* restore original dword without reload */
  189. movq %r8, %rdx /* magic value */
  190. addq %rcx, %rdx /* add the magic value to the word. We get
  191. carry bits reported for each byte which
  192. is *not* 0 */
  193. jnc 7f /* highest byte is NUL => return NULL */
  194. xorq %rcx, %rdx /* (word+magic)^word */
  195. orq %r8, %rdx /* set all non-carry bits */
  196. incq %rdx /* add 1: if one carry bit was *not* set
  197. the addition will not result in 0. */
  198. jz 4b /* no NUL found => restart loop */
  199. 7: /* Return NULL. */
  200. xorl %eax, %eax
  201. retq
  202. /* We now scan for the byte in which the character was matched.
  203. But we have to take care of the case that a NUL char is
  204. found before this in the dword. Note that we XORed %rcx
  205. with the byte we're looking for, therefore the tests below look
  206. reversed. */
  207. .p2align 4 /* Align, it's a jump target. */
  208. 3: movq %r9,%rdx /* move to %rdx so that we can access bytes */
  209. subq $8,%rax /* correct pointer increment. */
  210. testb %cl, %cl /* is first byte C? */
  211. jz 6f /* yes => return pointer */
  212. cmpb %dl, %cl /* is first byte NUL? */
  213. je 7b /* yes => return NULL */
  214. incq %rax /* increment pointer */
  215. testb %ch, %ch /* is second byte C? */
  216. jz 6f /* yes => return pointer */
  217. cmpb %dl, %ch /* is second byte NUL? */
  218. je 7b /* yes => return NULL? */
  219. incq %rax /* increment pointer */
  220. shrq $16, %rcx /* make upper bytes accessible */
  221. testb %cl, %cl /* is third byte C? */
  222. jz 6f /* yes => return pointer */
  223. cmpb %dl, %cl /* is third byte NUL? */
  224. je 7b /* yes => return NULL */
  225. incq %rax /* increment pointer */
  226. testb %ch, %ch /* is fourth byte C? */
  227. jz 6f /* yes => return pointer */
  228. cmpb %dl, %ch /* is fourth byte NUL? */
  229. je 7b /* yes => return NULL? */
  230. incq %rax /* increment pointer */
  231. shrq $16, %rcx /* make upper bytes accessible */
  232. testb %cl, %cl /* is fifth byte C? */
  233. jz 6f /* yes => return pointer */
  234. cmpb %dl, %cl /* is fifth byte NUL? */
  235. je 7b /* yes => return NULL */
  236. incq %rax /* increment pointer */
  237. testb %ch, %ch /* is sixth byte C? */
  238. jz 6f /* yes => return pointer */
  239. cmpb %dl, %ch /* is sixth byte NUL? */
  240. je 7b /* yes => return NULL? */
  241. incq %rax /* increment pointer */
  242. shrq $16, %rcx /* make upper bytes accessible */
  243. testb %cl, %cl /* is seventh byte C? */
  244. jz 6f /* yes => return pointer */
  245. cmpb %dl, %cl /* is seventh byte NUL? */
  246. je 7b /* yes => return NULL */
  247. /* It must be in the eigth byte and it cannot be NUL. */
  248. incq %rax
  249. 6:
  250. nop
  251. retq
  252. END (BP_SYM (__strchr))
  253. weak_alias (BP_SYM (strchr), BP_SYM (index))