strlen.S 4.9 KB

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