strcpy.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. .p2align 4
  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. .p2align 4
  111. 3:
  112. /* Note that stpcpy needs to return with the value of the NUL
  113. byte. */
  114. movb %al, (%rdx) /* 1st byte. */
  115. testb %al, %al /* Is it NUL. */
  116. jz 4f /* yes, finish. */
  117. incq %rdx /* Increment destination. */
  118. movb %ah, (%rdx) /* 2nd byte. */
  119. testb %ah, %ah /* Is it NUL?. */
  120. jz 4f /* yes, finish. */
  121. incq %rdx /* Increment destination. */
  122. shrq $16, %rax /* Shift... */
  123. jmp 3b /* and look at next two bytes in %rax. */
  124. 4:
  125. #ifdef USE_AS_STPCPY
  126. movq %rdx, %rax /* Destination is return value. */
  127. #else
  128. movq %rdi, %rax /* Source is return value. */
  129. #endif
  130. retq
  131. END (BP_SYM (STRCPY))
  132. #ifndef USE_AS_STPCPY
  133. libc_hidden_def(strcpy)
  134. #endif