strlen.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. .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. .p2align 4 /* Align loop. */
  34. 4: /* Main Loop is unrolled 4 times. */
  35. /* First unroll. */
  36. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  37. addq $8,%rax /* adjust pointer for next word */
  38. movq %r8, %rdx /* magic value */
  39. addq %rcx, %rdx /* add the magic value to the word. We get
  40. carry bits reported for each byte which
  41. is *not* 0 */
  42. jnc 3f /* highest byte is NUL => return pointer */
  43. xorq %rcx, %rdx /* (word+magic)^word */
  44. orq %r8, %rdx /* set all non-carry bits */
  45. incq %rdx /* add 1: if one carry bit was *not* set
  46. the addition will not result in 0. */
  47. jnz 3f /* found NUL => return pointer */
  48. /* Second unroll. */
  49. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  50. addq $8,%rax /* adjust pointer for next word */
  51. movq %r8, %rdx /* magic value */
  52. addq %rcx, %rdx /* add the magic value to the word. We get
  53. carry bits reported for each byte which
  54. is *not* 0 */
  55. jnc 3f /* highest byte is NUL => return pointer */
  56. xorq %rcx, %rdx /* (word+magic)^word */
  57. orq %r8, %rdx /* set all non-carry bits */
  58. incq %rdx /* add 1: if one carry bit was *not* set
  59. the addition will not result in 0. */
  60. jnz 3f /* found NUL => return pointer */
  61. /* Third unroll. */
  62. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  63. addq $8,%rax /* adjust pointer for next word */
  64. movq %r8, %rdx /* magic value */
  65. addq %rcx, %rdx /* add the magic value to the word. We get
  66. carry bits reported for each byte which
  67. is *not* 0 */
  68. jnc 3f /* highest byte is NUL => return pointer */
  69. xorq %rcx, %rdx /* (word+magic)^word */
  70. orq %r8, %rdx /* set all non-carry bits */
  71. incq %rdx /* add 1: if one carry bit was *not* set
  72. the addition will not result in 0. */
  73. jnz 3f /* found NUL => return pointer */
  74. /* Fourth unroll. */
  75. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  76. addq $8,%rax /* adjust pointer for next word */
  77. movq %r8, %rdx /* magic value */
  78. addq %rcx, %rdx /* add the magic value to the word. We get
  79. carry bits reported for each byte which
  80. is *not* 0 */
  81. jnc 3f /* highest byte is NUL => return pointer */
  82. xorq %rcx, %rdx /* (word+magic)^word */
  83. orq %r8, %rdx /* set all non-carry bits */
  84. incq %rdx /* add 1: if one carry bit was *not* set
  85. the addition will not result in 0. */
  86. jz 4b /* no NUL found => continue loop */
  87. .p2align 4 /* Align, it's a jump target. */
  88. 3: subq $8,%rax /* correct pointer increment. */
  89. testb %cl, %cl /* is first byte NUL? */
  90. jz 2f /* yes => return */
  91. incq %rax /* increment pointer */
  92. testb %ch, %ch /* is second byte NUL? */
  93. jz 2f /* yes => return */
  94. incq %rax /* increment pointer */
  95. testl $0x00ff0000, %ecx /* is third byte NUL? */
  96. jz 2f /* yes => return pointer */
  97. incq %rax /* increment pointer */
  98. testl $0xff000000, %ecx /* is fourth byte NUL? */
  99. jz 2f /* yes => return pointer */
  100. incq %rax /* increment pointer */
  101. shrq $32, %rcx /* look at other half. */
  102. testb %cl, %cl /* is first byte NUL? */
  103. jz 2f /* yes => return */
  104. incq %rax /* increment pointer */
  105. testb %ch, %ch /* is second byte NUL? */
  106. jz 2f /* yes => return */
  107. incq %rax /* increment pointer */
  108. testl $0xff0000, %ecx /* is third byte NUL? */
  109. jz 2f /* yes => return pointer */
  110. incq %rax /* increment pointer */
  111. 2:
  112. subq %rdi, %rax /* compute difference to string start */
  113. ret
  114. END (strlen)
  115. libc_hidden_def(strlen)