strcpy.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* strcpy/stpcpy implementation for x86-64.
  2. Copyright (C) 2002 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Andreas Jaeger <aj@suse.de>, 2002.
  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. #ifndef USE_AS_STPCPY
  19. # define STRCPY strcpy
  20. #endif
  21. .text
  22. ENTRY (BP_SYM (STRCPY))
  23. movq %rsi, %rcx /* Source register. */
  24. andl $7, %ecx /* mask alignment bits */
  25. movq %rdi, %rdx /* Duplicate destination pointer. */
  26. jz 5f /* aligned => start loop */
  27. neg %ecx /* We need to align to 8 bytes. */
  28. addl $8,%ecx
  29. /* Search the first bytes directly. */
  30. 0:
  31. movb (%rsi), %al /* Fetch a byte */
  32. testb %al, %al /* Is it NUL? */
  33. movb %al, (%rdx) /* Store it */
  34. jz 4f /* If it was NUL, done! */
  35. incq %rsi
  36. incq %rdx
  37. decl %ecx
  38. jnz 0b
  39. 5:
  40. movq $0xfefefefefefefeff,%r8
  41. /* Now the sources is aligned. Unfortunatly we cannot force
  42. to have both source and destination aligned, so ignore the
  43. alignment of the destination. */
  44. /* Next 3 insns are 10 bytes total, make sure we decode them in one go */
  45. .p2align 4,,10
  46. 1:
  47. /* 1st unroll. */
  48. movq (%rsi), %rax /* Read double word (8 bytes). */
  49. addq $8, %rsi /* Adjust pointer for next word. */
  50. movq %rax, %r9 /* Save a copy for NUL finding. */
  51. addq %r8, %r9 /* add the magic value to the word. We get
  52. carry bits reported for each byte which
  53. is *not* 0 */
  54. jnc 3f /* highest byte is NUL => return pointer */
  55. xorq %rax, %r9 /* (word+magic)^word */
  56. orq %r8, %r9 /* set all non-carry bits */
  57. incq %r9 /* add 1: if one carry bit was *not* set
  58. the addition will not result in 0. */
  59. jnz 3f /* found NUL => return pointer */
  60. movq %rax, (%rdx) /* Write value to destination. */
  61. addq $8, %rdx /* Adjust pointer. */
  62. /* 2nd unroll. */
  63. movq (%rsi), %rax /* Read double word (8 bytes). */
  64. addq $8, %rsi /* Adjust pointer for next word. */
  65. movq %rax, %r9 /* Save a copy for NUL finding. */
  66. addq %r8, %r9 /* 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 %rax, %r9 /* (word+magic)^word */
  71. orq %r8, %r9 /* set all non-carry bits */
  72. incq %r9 /* 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. movq %rax, (%rdx) /* Write value to destination. */
  76. addq $8, %rdx /* Adjust pointer. */
  77. /* 3rd unroll. */
  78. movq (%rsi), %rax /* Read double word (8 bytes). */
  79. addq $8, %rsi /* Adjust pointer for next word. */
  80. movq %rax, %r9 /* Save a copy for NUL finding. */
  81. addq %r8, %r9 /* 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 %rax, %r9 /* (word+magic)^word */
  86. orq %r8, %r9 /* set all non-carry bits */
  87. incq %r9 /* add 1: if one carry bit was *not* set
  88. the addition will not result in 0. */
  89. jnz 3f /* found NUL => return pointer */
  90. movq %rax, (%rdx) /* Write value to destination. */
  91. addq $8, %rdx /* Adjust pointer. */
  92. /* 4th unroll. */
  93. movq (%rsi), %rax /* Read double word (8 bytes). */
  94. addq $8, %rsi /* Adjust pointer for next word. */
  95. movq %rax, %r9 /* Save a copy for NUL finding. */
  96. addq %r8, %r9 /* add the magic value to the word. We get
  97. carry bits reported for each byte which
  98. is *not* 0 */
  99. jnc 3f /* highest byte is NUL => return pointer */
  100. xorq %rax, %r9 /* (word+magic)^word */
  101. orq %r8, %r9 /* set all non-carry bits */
  102. incq %r9 /* add 1: if one carry bit was *not* set
  103. the addition will not result in 0. */
  104. jnz 3f /* found NUL => return pointer */
  105. movq %rax, (%rdx) /* Write value to destination. */
  106. addq $8, %rdx /* Adjust pointer. */
  107. jmp 1b /* Next iteration. */
  108. /* Do the last few bytes. %rax contains the value to write.
  109. The loop is unrolled twice. */
  110. /* Next 3 insns are 6 bytes total, make sure we decode them in one go */
  111. .p2align 3,,6
  112. 3:
  113. /* Note that stpcpy needs to return with the value of the NUL
  114. byte. */
  115. movb %al, (%rdx) /* 1st byte. */
  116. testb %al, %al /* Is it NUL. */
  117. jz 4f /* yes, finish. */
  118. incq %rdx /* Increment destination. */
  119. movb %ah, (%rdx) /* 2nd byte. */
  120. testb %ah, %ah /* Is it NUL?. */
  121. jz 4f /* yes, finish. */
  122. incq %rdx /* Increment destination. */
  123. shrq $16, %rax /* Shift... */
  124. jmp 3b /* and look at next two bytes in %rax. */
  125. 4:
  126. #ifdef USE_AS_STPCPY
  127. movq %rdx, %rax /* Destination is return value. */
  128. #else
  129. movq %rdi, %rax /* Source is return value. */
  130. #endif
  131. retq
  132. END (BP_SYM (STRCPY))
  133. #ifndef USE_AS_STPCPY
  134. libc_hidden_def(strcpy)
  135. #endif