strchr.S 11 KB

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