strlen.S 5.0 KB

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