strcpy.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef USE_AS_STPCPY
  19. # define STRCPY __strcpy
  20. .global strcpy
  21. .set strcpy,__strcpy
  22. .hidden __strcpy
  23. #endif
  24. .text
  25. ENTRY (BP_SYM (STRCPY))
  26. movq %rsi, %rcx /* Source register. */
  27. andl $7, %ecx /* mask alignment bits */
  28. movq %rdi, %rdx /* Duplicate destination pointer. */
  29. jz 5f /* aligned => start loop */
  30. neg %ecx /* We need to align to 8 bytes. */
  31. addl $8,%ecx
  32. /* Search the first bytes directly. */
  33. 0:
  34. movb (%rsi), %al /* Fetch a byte */
  35. testb %al, %al /* Is it NUL? */
  36. movb %al, (%rdx) /* Store it */
  37. jz 4f /* If it was NUL, done! */
  38. incq %rsi
  39. incq %rdx
  40. decl %ecx
  41. jnz 0b
  42. 5:
  43. movq $0xfefefefefefefeff,%r8
  44. /* Now the sources is aligned. Unfortunatly we cannot force
  45. to have both source and destination aligned, so ignore the
  46. alignment of the destination. */
  47. .p2align 4
  48. 1:
  49. /* 1st unroll. */
  50. movq (%rsi), %rax /* Read double word (8 bytes). */
  51. addq $8, %rsi /* Adjust pointer for next word. */
  52. movq %rax, %r9 /* Save a copy for NUL finding. */
  53. addq %r8, %r9 /* 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 %rax, %r9 /* (word+magic)^word */
  58. orq %r8, %r9 /* set all non-carry bits */
  59. incq %r9 /* 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. movq %rax, (%rdx) /* Write value to destination. */
  63. addq $8, %rdx /* Adjust pointer. */
  64. /* 2nd unroll. */
  65. movq (%rsi), %rax /* Read double word (8 bytes). */
  66. addq $8, %rsi /* Adjust pointer for next word. */
  67. movq %rax, %r9 /* Save a copy for NUL finding. */
  68. addq %r8, %r9 /* add the magic value to the word. We get
  69. carry bits reported for each byte which
  70. is *not* 0 */
  71. jnc 3f /* highest byte is NUL => return pointer */
  72. xorq %rax, %r9 /* (word+magic)^word */
  73. orq %r8, %r9 /* set all non-carry bits */
  74. incq %r9 /* add 1: if one carry bit was *not* set
  75. the addition will not result in 0. */
  76. jnz 3f /* found NUL => return pointer */
  77. movq %rax, (%rdx) /* Write value to destination. */
  78. addq $8, %rdx /* Adjust pointer. */
  79. /* 3rd unroll. */
  80. movq (%rsi), %rax /* Read double word (8 bytes). */
  81. addq $8, %rsi /* Adjust pointer for next word. */
  82. movq %rax, %r9 /* Save a copy for NUL finding. */
  83. addq %r8, %r9 /* add the magic value to the word. We get
  84. carry bits reported for each byte which
  85. is *not* 0 */
  86. jnc 3f /* highest byte is NUL => return pointer */
  87. xorq %rax, %r9 /* (word+magic)^word */
  88. orq %r8, %r9 /* set all non-carry bits */
  89. incq %r9 /* add 1: if one carry bit was *not* set
  90. the addition will not result in 0. */
  91. jnz 3f /* found NUL => return pointer */
  92. movq %rax, (%rdx) /* Write value to destination. */
  93. addq $8, %rdx /* Adjust pointer. */
  94. /* 4th unroll. */
  95. movq (%rsi), %rax /* Read double word (8 bytes). */
  96. addq $8, %rsi /* Adjust pointer for next word. */
  97. movq %rax, %r9 /* Save a copy for NUL finding. */
  98. addq %r8, %r9 /* add the magic value to the word. We get
  99. carry bits reported for each byte which
  100. is *not* 0 */
  101. jnc 3f /* highest byte is NUL => return pointer */
  102. xorq %rax, %r9 /* (word+magic)^word */
  103. orq %r8, %r9 /* set all non-carry bits */
  104. incq %r9 /* add 1: if one carry bit was *not* set
  105. the addition will not result in 0. */
  106. jnz 3f /* found NUL => return pointer */
  107. movq %rax, (%rdx) /* Write value to destination. */
  108. addq $8, %rdx /* Adjust pointer. */
  109. jmp 1b /* Next iteration. */
  110. /* Do the last few bytes. %rax contains the value to write.
  111. The loop is unrolled twice. */
  112. .p2align 4
  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))