strchr.S 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* $OpenBSD: strchr.S,v 1.3 2014/12/09 15:13:57 reyk Exp $ */
  2. /* $NetBSD: strchr.S,v 1.7 2014/03/22 19:16:34 jakllsch Exp $ */
  3. /*-
  4. * Copyright (c) 2009 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by David Laight.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /* See comments in strlen.S about checking words for byte values */
  32. #include <machine/asm.h>
  33. STRONG_ALIAS(index, strchr)
  34. /*
  35. * On entry %rdi is the buffer and the low byte of %rsi (%sil) the
  36. * character to search for.
  37. *
  38. * Registers %rdx, %rcx, %r8-%r11 and %rax are also usable
  39. */
  40. ENTRY(strchr)
  41. movabsq $0x0101010101010101,%r8
  42. movzbq %sil,%rdx /* value to search for (c) */
  43. /* These imul are 'directpath' on athlons, so are fast */
  44. imul $0x80,%r8,%r9 /* 0x8080808080808080 */
  45. imul %r8,%rdx /* (c) copied to all bytes */
  46. test $7,%dil
  47. jnz 20f /* jump if misaligned */
  48. _ALIGN_TEXT /* one byte nop */
  49. 1:
  50. movq (%rdi),%rax /* bytes to check (x) */
  51. 2:
  52. addq $8,%rdi
  53. mov %rax,%r10
  54. mov %rax,%r11 /* for 'char' check */
  55. not %r10 /* invert of data (~x) */
  56. xorq %rdx,%r11 /* convert 'char' test to one for NUL */
  57. subq %r8,%rax /* x - 0x10 */
  58. movq %r10,%rsi /* ~x */
  59. subq %r8,%r11 /* (x ^ c) - 0x10 */
  60. /*
  61. * Here we could check ((x - 0x10) | ((x ^ c) - 0x10)) & 0x80
  62. * and short-circuit the case where no top bits are set, and
  63. * we continue the loop.
  64. * However it needs 3 more clocks that are difficult to interleave
  65. * in the existing dependency chain ...
  66. */
  67. andq %r9,%rax /* (x - 0x10) & 0x80 */
  68. xorq %rdx,%rsi /* c ^ ~x == ~(c ^ x) */
  69. andq %r9,%r11 /* ((x ^ c) - 0x10) & 0x80 */
  70. andq %r10,%rax /* (x - 0x10) & 0x80 & ~x */
  71. jne 10f /* jump if string ends */
  72. andq %rsi,%r11 /* ((x ^ c) - 0x10) & 0x80 & ~(x ^ c) */
  73. je 1b /* jump if no match */
  74. /* Found char, since LE can use bit scan */
  75. bsf %r11,%r11 /* 7, 15, 23 ... 63 */
  76. 8: shr $3,%r11 /* 0, 1, 2 .. 7 */
  77. lea -8(%r11,%rdi),%rax
  78. ret
  79. /* End of string, check whether char is before NUL */
  80. _ALIGN_TEXT /* adds three byte nop */
  81. 10:
  82. bsf %rax,%rax /* count to NUL */
  83. andq %rsi,%r11 /* check for char in last 8 bytes */
  84. je 11f
  85. bsf %r11,%r11 /* NUL and char - see which was first */
  86. cmp %r11,%rax
  87. jae 8b /* return 'found' if same - searching for NUL */
  88. 11: xor %eax,%eax /* char not found */
  89. ret
  90. /* Source misaligned: read aligned word and make low bytes invalid */
  91. /* I (dsl) think a _ALIGN_TEXT here will slow things down! */
  92. 20:
  93. xor %rcx,%rcx
  94. sub %dil,%cl /* Convert low address values 1..7 ... */
  95. sbb %rsi,%rsi /* carry was set, so %rsi now ~0u! */
  96. and $7,%cl /* ... to 7..1 */
  97. and $~7,%dil /* move address to start of word */
  98. shl $3,%cl /* now 56, 48 ... 16, 8 */
  99. movq (%rdi),%rax /* aligned word containing first data */
  100. xor %rdx,%rsi /* invert of search pattern (~c) */
  101. je 22f /* searching for 0xff */
  102. 21: shr %cl,%rsi /* ~c in low bytes */
  103. or %rsi,%rax /* set some bits making low bytes invalid */
  104. jmp 2b
  105. /* We are searching for 0xff, so can't use ~pattern for invalid value */
  106. 22:
  107. mov %r8,%r10 /* 0x01 pattern */
  108. lea (%r8,%r8),%rsi /* 0x02 - bits gets set (above) */
  109. not %r10 /* now 0xfe */
  110. sar %cl,%r10 /* top bytes 0xff */
  111. and %r10,%rax /* clear lsb from unwanted low bytes */
  112. jmp 21b