strlen.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* strlen(str) -- determine the length of the string STR.
  2. Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  3. Based on i486 version contributed by Ulrich Drepper <drepper@redhat.com>.
  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 (strlen)
  21. movq %rdi, %rcx /* Duplicate source pointer. */
  22. andl $7, %ecx /* mask alignment bits */
  23. movq %rdi, %rax /* duplicate destination. */
  24. jz 1f /* aligned => start loop */
  25. neg %ecx /* We need to align to 8 bytes. */
  26. addl $8,%ecx
  27. /* Search the first bytes directly. */
  28. 0: cmpb $0x0,(%rax) /* is byte NUL? */
  29. je 2f /* yes => return */
  30. incq %rax /* increment pointer */
  31. decl %ecx
  32. jnz 0b
  33. 1: movq $0xfefefefefefefeff,%r8 /* Save magic. */
  34. /* Align loop. */
  35. /* Next 3 insns are 10 bytes total, make sure we decode them in one go */
  36. .p2align 4,,10
  37. 4:
  38. /* Main Loop is unrolled 4 times. */
  39. /* First unroll. */
  40. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  41. addq $8,%rax /* adjust pointer for next word */
  42. movq %r8, %rdx /* magic value */
  43. addq %rcx, %rdx /* add the magic value to the word. We get
  44. carry bits reported for each byte which
  45. is *not* 0 */
  46. jnc 3f /* highest byte is NUL => return pointer */
  47. xorq %rcx, %rdx /* (word+magic)^word */
  48. orq %r8, %rdx /* set all non-carry bits */
  49. incq %rdx /* add 1: if one carry bit was *not* set
  50. the addition will not result in 0. */
  51. jnz 3f /* found NUL => return pointer */
  52. /* Second unroll. */
  53. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  54. addq $8,%rax /* adjust pointer for next word */
  55. movq %r8, %rdx /* magic value */
  56. addq %rcx, %rdx /* add the magic value to the word. We get
  57. carry bits reported for each byte which
  58. is *not* 0 */
  59. jnc 3f /* highest byte is NUL => return pointer */
  60. xorq %rcx, %rdx /* (word+magic)^word */
  61. orq %r8, %rdx /* set all non-carry bits */
  62. incq %rdx /* add 1: if one carry bit was *not* set
  63. the addition will not result in 0. */
  64. jnz 3f /* found NUL => return pointer */
  65. /* Third unroll. */
  66. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  67. addq $8,%rax /* adjust pointer for next word */
  68. movq %r8, %rdx /* magic value */
  69. addq %rcx, %rdx /* add the magic value to the word. We get
  70. carry bits reported for each byte which
  71. is *not* 0 */
  72. jnc 3f /* highest byte is NUL => return pointer */
  73. xorq %rcx, %rdx /* (word+magic)^word */
  74. orq %r8, %rdx /* set all non-carry bits */
  75. incq %rdx /* add 1: if one carry bit was *not* set
  76. the addition will not result in 0. */
  77. jnz 3f /* found NUL => return pointer */
  78. /* Fourth unroll. */
  79. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  80. addq $8,%rax /* adjust pointer for next word */
  81. movq %r8, %rdx /* magic value */
  82. addq %rcx, %rdx /* add the magic value to the word. We get
  83. carry bits reported for each byte which
  84. is *not* 0 */
  85. jnc 3f /* highest byte is NUL => return pointer */
  86. xorq %rcx, %rdx /* (word+magic)^word */
  87. orq %r8, %rdx /* set all non-carry bits */
  88. incq %rdx /* add 1: if one carry bit was *not* set
  89. the addition will not result in 0. */
  90. jz 4b /* no NUL found => continue loop */
  91. /* Align, it is a jump target. */
  92. /* Next 3 insns are 8 bytes total, make sure we decode them in one go */
  93. .p2align 3,,8
  94. 3:
  95. subq $8,%rax /* correct pointer increment. */
  96. testb %cl, %cl /* is first byte NUL? */
  97. jz 2f /* yes => return */
  98. incq %rax /* increment pointer */
  99. testb %ch, %ch /* is second byte NUL? */
  100. jz 2f /* yes => return */
  101. incq %rax /* increment pointer */
  102. testl $0x00ff0000, %ecx /* is third byte NUL? */
  103. jz 2f /* yes => return pointer */
  104. incq %rax /* increment pointer */
  105. testl $0xff000000, %ecx /* is fourth byte NUL? */
  106. jz 2f /* yes => return pointer */
  107. incq %rax /* increment pointer */
  108. shrq $32, %rcx /* look at other half. */
  109. testb %cl, %cl /* is first byte NUL? */
  110. jz 2f /* yes => return */
  111. incq %rax /* increment pointer */
  112. testb %ch, %ch /* is second byte NUL? */
  113. jz 2f /* yes => return */
  114. incq %rax /* increment pointer */
  115. testl $0xff0000, %ecx /* is third byte NUL? */
  116. jz 2f /* yes => return pointer */
  117. incq %rax /* increment pointer */
  118. 2:
  119. subq %rdi, %rax /* compute difference to string start */
  120. ret
  121. END (strlen)
  122. libc_hidden_def(strlen)