strchr.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "_glibc_inc.h"
  17. /* Seems to be unrolled too much */
  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. /* Next 3 insns are 10 bytes total, make sure we decode them in one go */
  78. .p2align 4,,10
  79. 4:
  80. /* Main Loop is unrolled 4 times. */
  81. /* First unroll. */
  82. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  83. addq $8,%rax /* adjust pointer for next word */
  84. movq %r8, %rdx /* magic value */
  85. xorq %r9, %rcx /* XOR with qword c|...|c => bytes of str == c
  86. are now 0 */
  87. addq %rcx, %rdx /* add the magic value to the word. We get
  88. carry bits reported for each byte which
  89. is *not* 0 */
  90. jnc 3f /* highest byte is NUL => return pointer */
  91. xorq %rcx, %rdx /* (word+magic)^word */
  92. orq %r8, %rdx /* set all non-carry bits */
  93. incq %rdx /* add 1: if one carry bit was *not* set
  94. the addition will not result in 0. */
  95. jnz 3f /* found c => return pointer */
  96. /* The quadword we looked at does not contain the value we're looking
  97. for. Let's search now whether we have reached the end of the
  98. string. */
  99. xorq %r9, %rcx /* restore original dword without reload */
  100. movq %r8, %rdx /* magic value */
  101. addq %rcx, %rdx /* add the magic value to the word. We get
  102. carry bits reported for each byte which
  103. is *not* 0 */
  104. jnc 7f /* highest byte is NUL => return NULL */
  105. xorq %rcx, %rdx /* (word+magic)^word */
  106. orq %r8, %rdx /* set all non-carry bits */
  107. incq %rdx /* add 1: if one carry bit was *not* set
  108. the addition will not result in 0. */
  109. jnz 7f /* found NUL => return NULL */
  110. /* Second unroll. */
  111. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  112. addq $8,%rax /* adjust pointer for next word */
  113. movq %r8, %rdx /* magic value */
  114. xorq %r9, %rcx /* XOR with qword c|...|c => bytes of str == c
  115. are now 0 */
  116. addq %rcx, %rdx /* add the magic value to the word. We get
  117. carry bits reported for each byte which
  118. is *not* 0 */
  119. jnc 3f /* highest byte is NUL => return pointer */
  120. xorq %rcx, %rdx /* (word+magic)^word */
  121. orq %r8, %rdx /* set all non-carry bits */
  122. incq %rdx /* add 1: if one carry bit was *not* set
  123. the addition will not result in 0. */
  124. jnz 3f /* found c => return pointer */
  125. /* The quadword we looked at does not contain the value we're looking
  126. for. Let's search now whether we have reached the end of the
  127. string. */
  128. xorq %r9, %rcx /* restore original dword without reload */
  129. movq %r8, %rdx /* magic value */
  130. addq %rcx, %rdx /* add the magic value to the word. We get
  131. carry bits reported for each byte which
  132. is *not* 0 */
  133. jnc 7f /* highest byte is NUL => return NULL */
  134. xorq %rcx, %rdx /* (word+magic)^word */
  135. orq %r8, %rdx /* set all non-carry bits */
  136. incq %rdx /* add 1: if one carry bit was *not* set
  137. the addition will not result in 0. */
  138. jnz 7f /* found NUL => return NULL */
  139. /* Third unroll. */
  140. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  141. addq $8,%rax /* adjust pointer for next word */
  142. movq %r8, %rdx /* magic value */
  143. xorq %r9, %rcx /* XOR with qword c|...|c => bytes of str == c
  144. are now 0 */
  145. addq %rcx, %rdx /* add the magic value to the word. We get
  146. carry bits reported for each byte which
  147. is *not* 0 */
  148. jnc 3f /* highest byte is NUL => return pointer */
  149. xorq %rcx, %rdx /* (word+magic)^word */
  150. orq %r8, %rdx /* set all non-carry bits */
  151. incq %rdx /* add 1: if one carry bit was *not* set
  152. the addition will not result in 0. */
  153. jnz 3f /* found c => return pointer */
  154. /* The quadword we looked at does not contain the value we're looking
  155. for. Let's search now whether we have reached the end of the
  156. string. */
  157. xorq %r9, %rcx /* restore original dword without reload */
  158. movq %r8, %rdx /* magic value */
  159. addq %rcx, %rdx /* add the magic value to the word. We get
  160. carry bits reported for each byte which
  161. is *not* 0 */
  162. jnc 7f /* highest byte is NUL => return NULL */
  163. xorq %rcx, %rdx /* (word+magic)^word */
  164. orq %r8, %rdx /* set all non-carry bits */
  165. incq %rdx /* add 1: if one carry bit was *not* set
  166. the addition will not result in 0. */
  167. jnz 7f /* found NUL => return NULL */
  168. /* Fourth unroll. */
  169. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  170. addq $8,%rax /* adjust pointer for next word */
  171. movq %r8, %rdx /* magic value */
  172. xorq %r9, %rcx /* XOR with qword c|...|c => bytes of str == c
  173. are now 0 */
  174. addq %rcx, %rdx /* add the magic value to the word. We get
  175. carry bits reported for each byte which
  176. is *not* 0 */
  177. jnc 3f /* highest byte is NUL => return pointer */
  178. xorq %rcx, %rdx /* (word+magic)^word */
  179. orq %r8, %rdx /* set all non-carry bits */
  180. incq %rdx /* add 1: if one carry bit was *not* set
  181. the addition will not result in 0. */
  182. jnz 3f /* found c => return pointer */
  183. /* The quadword we looked at does not contain the value we're looking
  184. for. Let's search now whether we have reached the end of the
  185. string. */
  186. xorq %r9, %rcx /* restore original dword without reload */
  187. movq %r8, %rdx /* magic value */
  188. addq %rcx, %rdx /* add the magic value to the word. We get
  189. carry bits reported for each byte which
  190. is *not* 0 */
  191. jnc 7f /* highest byte is NUL => return NULL */
  192. xorq %rcx, %rdx /* (word+magic)^word */
  193. orq %r8, %rdx /* set all non-carry bits */
  194. incq %rdx /* add 1: if one carry bit was *not* set
  195. the addition will not result in 0. */
  196. jz 4b /* no NUL found => restart loop */
  197. 7: /* Return NULL. */
  198. xorl %eax, %eax
  199. retq
  200. /* We now scan for the byte in which the character was matched.
  201. But we have to take care of the case that a NUL char is
  202. found before this in the dword. Note that we XORed %rcx
  203. with the byte we're looking for, therefore the tests below look
  204. reversed. */
  205. /* Align, it's a jump target. */
  206. /* Next 3 insns are 9 bytes total, make sure we decode them in one go */
  207. .p2align 4,,9
  208. 3:
  209. movq %r9,%rdx /* move to %rdx so that we can access bytes */
  210. subq $8,%rax /* correct pointer increment. */
  211. testb %cl, %cl /* is first byte C? */
  212. jz 6f /* yes => return pointer */
  213. cmpb %dl, %cl /* is first byte NUL? */
  214. je 7b /* yes => return NULL */
  215. incq %rax /* increment pointer */
  216. testb %ch, %ch /* is second byte C? */
  217. jz 6f /* yes => return pointer */
  218. cmpb %dl, %ch /* is second byte NUL? */
  219. je 7b /* yes => return NULL? */
  220. incq %rax /* increment pointer */
  221. shrq $16, %rcx /* make upper bytes accessible */
  222. testb %cl, %cl /* is third byte C? */
  223. jz 6f /* yes => return pointer */
  224. cmpb %dl, %cl /* is third byte NUL? */
  225. je 7b /* yes => return NULL */
  226. incq %rax /* increment pointer */
  227. testb %ch, %ch /* is fourth byte C? */
  228. jz 6f /* yes => return pointer */
  229. cmpb %dl, %ch /* is fourth byte NUL? */
  230. je 7b /* yes => return NULL? */
  231. incq %rax /* increment pointer */
  232. shrq $16, %rcx /* make upper bytes accessible */
  233. testb %cl, %cl /* is fifth byte C? */
  234. jz 6f /* yes => return pointer */
  235. cmpb %dl, %cl /* is fifth byte NUL? */
  236. je 7b /* yes => return NULL */
  237. incq %rax /* increment pointer */
  238. testb %ch, %ch /* is sixth byte C? */
  239. jz 6f /* yes => return pointer */
  240. cmpb %dl, %ch /* is sixth byte NUL? */
  241. je 7b /* yes => return NULL? */
  242. incq %rax /* increment pointer */
  243. shrq $16, %rcx /* make upper bytes accessible */
  244. testb %cl, %cl /* is seventh byte C? */
  245. jz 6f /* yes => return pointer */
  246. cmpb %dl, %cl /* is seventh byte NUL? */
  247. je 7b /* yes => return NULL */
  248. /* It must be in the eigth byte and it cannot be NUL. */
  249. incq %rax
  250. 6:
  251. /* nop - huh?? */
  252. retq
  253. END (BP_SYM (strchr))
  254. libc_hidden_def(strchr)
  255. #ifdef __UCLIBC_SUSV3_LEGACY__
  256. strong_alias (BP_SYM (strchr), BP_SYM (index))
  257. #endif