strchr.S 11 KB

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