strlen.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* $OpenBSD: strlen.S,v 1.4 2014/12/09 15:13:57 reyk Exp $ */
  2. /* $NetBSD: strlen.S,v 1.6 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. /*
  32. * Inspired by a version written by J.T. Conklin <jtc@acorntoolworks.com>
  33. * (Only the long comment really remains his work!)
  34. */
  35. #include <machine/asm.h>
  36. /*
  37. * There are many well known branch-free sequences which are used
  38. * for determining whether a zero-byte is contained within a word.
  39. * These sequences are generally much more efficent than loading
  40. * and comparing each byte individually.
  41. *
  42. * The expression [1,2]:
  43. *
  44. * (1) ~(((x & 0x7f....7f) + 0x7f....7f) | (x | 0x7f....7f))
  45. *
  46. * evaluates to a non-zero value if any of the bytes in the
  47. * original word is zero.
  48. *
  49. * It also has the useful property that bytes in the result word
  50. * that correspond to non-zero bytes in the original word have
  51. * the value 0x00, while bytes corresponding to zero bytes have
  52. * the value 0x80. This allows calculation of the first (and
  53. * last) occurrence of a zero byte within the word (useful for C's
  54. * str* primitives) by counting the number of leading (or
  55. * trailing) zeros and dividing the result by 8. On machines
  56. * without (or with slow) clz() / ctz() instructions, testing
  57. * each byte in the result word for zero is necessary.
  58. *
  59. * This typically takes 4 instructions (5 on machines without
  60. * "not-or") not including those needed to load the constant.
  61. *
  62. *
  63. * The expression:
  64. *
  65. * (2) ((x - 0x01....01) & 0x80....80 & ~x)
  66. *
  67. * evaluates to a non-zero value if any of the bytes in the
  68. * original word is zero.
  69. *
  70. * On little endian machines, the first byte in the result word
  71. * that corresponds to a zero byte in the original byte is 0x80,
  72. * so clz() can be used as above. On big endian machines, and
  73. * little endian machines without (or with a slow) clz() insn,
  74. * testing each byte in the original for zero is necessary.
  75. *
  76. * This typically takes 3 instructions (4 on machines without
  77. * "and with complement") not including those needed to load
  78. * constants.
  79. *
  80. *
  81. * The expression:
  82. *
  83. * (3) ((x - 0x01....01) & 0x80....80)
  84. *
  85. * always evaluates to a non-zero value if any of the bytes in
  86. * the original word is zero or has the top bit set.
  87. * For strings that are likely to only contain 7-bit ascii these
  88. * false positives will be rare.
  89. *
  90. * To account for possible false positives, each byte of the
  91. * original word must be checked when the expression evaluates to
  92. * a non-zero value. However, because it is simpler than those
  93. * presented above, code that uses it will be faster as long as
  94. * the rate of false positives is low.
  95. *
  96. * This is likely, because the the false positive can only occur
  97. * if the most siginificant bit of a byte within the word is set.
  98. * The expression will never fail for typical 7-bit ASCII strings.
  99. *
  100. * This typically takes 2 instructions not including those needed
  101. * to load constants.
  102. *
  103. *
  104. * [1] Henry S. Warren Jr., "Hacker's Delight", Addison-Westley 2003
  105. *
  106. * [2] International Business Machines, "The PowerPC Compiler Writer's
  107. * Guide", Warthman Associates, 1996
  108. */
  109. ENTRY(strlen)
  110. movabsq $0x0101010101010101,%r8
  111. test $7,%dil
  112. movq %rdi,%rax /* Buffer, %rdi unchanged */
  113. movabsq $0x8080808080808080,%r9
  114. jnz 10f /* Jump if misaligned */
  115. _ALIGN_TEXT
  116. 1:
  117. movq (%rax),%rdx /* get bytes to check */
  118. 2:
  119. addq $8,%rax
  120. mov %rdx,%rcx /* save for later check */
  121. subq %r8,%rdx /* alg (3) above first */
  122. not %rcx /* Invert of data */
  123. andq %r9,%rdx
  124. je 1b /* jump if all 0x01-0x80 */
  125. /* Do check from alg (2) above - loops for 0x81..0xff bytes */
  126. andq %rcx,%rdx
  127. je 1b
  128. /* Since we are LE, use bit scan for first 0x80 byte */
  129. sub %rdi,%rax /* length to next word */
  130. bsf %rdx,%rdx /* 7, 15, 23 ... 63 */
  131. shr $3,%rdx /* 0, 1, 2 ... 7 */
  132. lea -8(%rax,%rdx),%rax
  133. ret
  134. /* Misaligned, read aligned word and make low bytes non-zero */
  135. _ALIGN_TEXT
  136. 10:
  137. mov %al,%cl
  138. mov $1,%rsi
  139. and $7,%cl /* offset into word 1..7 */
  140. and $~7,%al /* start of word with buffer */
  141. shl $3,%cl /* bit count 8, 16 .. 56 */
  142. movq (%rax),%rdx /* first data in high bytes */
  143. shl %cl,%rsi
  144. dec %rsi
  145. or %rsi,%rdx /* low bytes now non-zero */
  146. jmp 2b