strlen.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* strlen(str) -- determine the length of the string STR.
  2. Optimized for Intel 80x86, x>=4.
  3. Copyright (C) 1991-2026 Free Software Foundation, Inc.
  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. <https://www.gnu.org/licenses/>. */
  16. #include <sysdep.h>
  17. #include "asm-syntax.h"
  18. #define PARMS 4 /* no space for saved regs */
  19. #define STR PARMS
  20. .text
  21. ENTRY (strlen)
  22. movl STR(%esp), %ecx
  23. movl %ecx, %eax /* duplicate it */
  24. andl $3, %ecx /* mask alignment bits */
  25. jz L(1) /* aligned => start loop */
  26. cmpb %ch, (%eax) /* is byte NUL? */
  27. je L(2) /* yes => return */
  28. incl %eax /* increment pointer */
  29. xorl $3, %ecx /* was alignment = 3? */
  30. jz L(1) /* yes => now it is aligned and start loop */
  31. cmpb %ch, (%eax) /* is byte NUL? */
  32. je L(2) /* yes => return */
  33. addl $1, %eax /* increment pointer */
  34. subl $1, %ecx /* was alignment = 2? */
  35. jz L(1) /* yes => now it is aligned and start loop */
  36. cmpb %ch, (%eax) /* is byte NUL? */
  37. je L(2) /* yes => return */
  38. /* Don't change the above `addl $1,%eax' and `subl $1, %ecx' into `incl %eax'
  39. and `decl %ecx' resp. The additional two byte per instruction make the
  40. label 4 to be aligned on a 16 byte boundary with nops.
  41. The following `sub $15, %eax' is part of this trick, too. Together with
  42. the next instruction (`addl $16, %eax') it is in fact a `incl %eax', just
  43. as expected from the algorithm. But doing so has the advantage that
  44. no jump to label 1 is necessary and so the pipeline is not flushed. */
  45. subl $15, %eax /* effectively +1 */
  46. L(4): addl $16, %eax /* adjust pointer for full loop */
  47. L(1): movl (%eax), %ecx /* get word (= 4 bytes) in question */
  48. movl $0xfefefeff, %edx /* magic value */
  49. addl %ecx, %edx /* add the magic value to the word. We get
  50. carry bits reported for each byte which
  51. is *not* 0 */
  52. jnc L(3) /* highest byte is NUL => return pointer */
  53. xorl %ecx, %edx /* (word+magic)^word */
  54. orl $0xfefefeff, %edx /* set all non-carry bits */
  55. incl %edx /* add 1: if one carry bit was *not* set
  56. the addition will not result in 0. */
  57. jnz L(3) /* found NUL => return pointer */
  58. movl 4(%eax), %ecx /* get word (= 4 bytes) in question */
  59. movl $0xfefefeff, %edx /* magic value */
  60. addl %ecx, %edx /* add the magic value to the word. We get
  61. carry bits reported for each byte which
  62. is *not* 0 */
  63. jnc L(5) /* highest byte is NUL => return pointer */
  64. xorl %ecx, %edx /* (word+magic)^word */
  65. orl $0xfefefeff, %edx /* set all non-carry bits */
  66. incl %edx /* add 1: if one carry bit was *not* set
  67. the addition will not result in 0. */
  68. jnz L(5) /* found NUL => return pointer */
  69. movl 8(%eax), %ecx /* get word (= 4 bytes) in question */
  70. movl $0xfefefeff, %edx /* magic value */
  71. addl %ecx, %edx /* add the magic value to the word. We get
  72. carry bits reported for each byte which
  73. is *not* 0 */
  74. jnc L(6) /* highest byte is NUL => return pointer */
  75. xorl %ecx, %edx /* (word+magic)^word */
  76. orl $0xfefefeff, %edx /* set all non-carry bits */
  77. incl %edx /* add 1: if one carry bit was *not* set
  78. the addition will not result in 0. */
  79. jnz L(6) /* found NUL => return pointer */
  80. movl 12(%eax), %ecx /* get word (= 4 bytes) in question */
  81. movl $0xfefefeff, %edx /* magic value */
  82. addl %ecx, %edx /* add the magic value to the word. We get
  83. carry bits reported for each byte which
  84. is *not* 0 */
  85. jnc L(7) /* highest byte is NUL => return pointer */
  86. xorl %ecx, %edx /* (word+magic)^word */
  87. orl $0xfefefeff, %edx /* set all non-carry bits */
  88. incl %edx /* add 1: if one carry bit was *not* set
  89. the addition will not result in 0. */
  90. jz L(4) /* no NUL found => continue loop */
  91. L(7): addl $4, %eax /* adjust pointer */
  92. L(6): addl $4, %eax
  93. L(5): addl $4, %eax
  94. L(3): testb %cl, %cl /* is first byte NUL? */
  95. jz L(2) /* yes => return */
  96. incl %eax /* increment pointer */
  97. testb %ch, %ch /* is second byte NUL? */
  98. jz L(2) /* yes => return */
  99. incl %eax /* increment pointer */
  100. testl $0xff0000, %ecx /* is third byte NUL? */
  101. jz L(2) /* yes => return pointer */
  102. incl %eax /* increment pointer */
  103. L(2): subl STR(%esp), %eax /* compute difference to string start */
  104. ret
  105. END (strlen)
  106. libc_hidden_builtin_def (strlen)