strcpy.S 5.3 KB

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