strcpy.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #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. .p2align 4
  45. 1:
  46. /* 1st unroll. */
  47. movq (%rsi), %rax /* Read double word (8 bytes). */
  48. addq $8, %rsi /* Adjust pointer for next word. */
  49. movq %rax, %r9 /* Save a copy for NUL finding. */
  50. addq %r8, %r9 /* add the magic value to the word. We get
  51. carry bits reported for each byte which
  52. is *not* 0 */
  53. jnc 3f /* highest byte is NUL => return pointer */
  54. xorq %rax, %r9 /* (word+magic)^word */
  55. orq %r8, %r9 /* set all non-carry bits */
  56. incq %r9 /* add 1: if one carry bit was *not* set
  57. the addition will not result in 0. */
  58. jnz 3f /* found NUL => return pointer */
  59. movq %rax, (%rdx) /* Write value to destination. */
  60. addq $8, %rdx /* Adjust pointer. */
  61. /* 2nd unroll. */
  62. movq (%rsi), %rax /* Read double word (8 bytes). */
  63. addq $8, %rsi /* Adjust pointer for next word. */
  64. movq %rax, %r9 /* Save a copy for NUL finding. */
  65. addq %r8, %r9 /* add the magic value to the word. We get
  66. carry bits reported for each byte which
  67. is *not* 0 */
  68. jnc 3f /* highest byte is NUL => return pointer */
  69. xorq %rax, %r9 /* (word+magic)^word */
  70. orq %r8, %r9 /* set all non-carry bits */
  71. incq %r9 /* add 1: if one carry bit was *not* set
  72. the addition will not result in 0. */
  73. jnz 3f /* found NUL => return pointer */
  74. movq %rax, (%rdx) /* Write value to destination. */
  75. addq $8, %rdx /* Adjust pointer. */
  76. /* 3rd unroll. */
  77. movq (%rsi), %rax /* Read double word (8 bytes). */
  78. addq $8, %rsi /* Adjust pointer for next word. */
  79. movq %rax, %r9 /* Save a copy for NUL finding. */
  80. addq %r8, %r9 /* add the magic value to the word. We get
  81. carry bits reported for each byte which
  82. is *not* 0 */
  83. jnc 3f /* highest byte is NUL => return pointer */
  84. xorq %rax, %r9 /* (word+magic)^word */
  85. orq %r8, %r9 /* set all non-carry bits */
  86. incq %r9 /* add 1: if one carry bit was *not* set
  87. the addition will not result in 0. */
  88. jnz 3f /* found NUL => return pointer */
  89. movq %rax, (%rdx) /* Write value to destination. */
  90. addq $8, %rdx /* Adjust pointer. */
  91. /* 4th unroll. */
  92. movq (%rsi), %rax /* Read double word (8 bytes). */
  93. addq $8, %rsi /* Adjust pointer for next word. */
  94. movq %rax, %r9 /* Save a copy for NUL finding. */
  95. addq %r8, %r9 /* add the magic value to the word. We get
  96. carry bits reported for each byte which
  97. is *not* 0 */
  98. jnc 3f /* highest byte is NUL => return pointer */
  99. xorq %rax, %r9 /* (word+magic)^word */
  100. orq %r8, %r9 /* set all non-carry bits */
  101. incq %r9 /* add 1: if one carry bit was *not* set
  102. the addition will not result in 0. */
  103. jnz 3f /* found NUL => return pointer */
  104. movq %rax, (%rdx) /* Write value to destination. */
  105. addq $8, %rdx /* Adjust pointer. */
  106. jmp 1b /* Next iteration. */
  107. /* Do the last few bytes. %rax contains the value to write.
  108. The loop is unrolled twice. */
  109. .p2align 4
  110. 3:
  111. /* Note that stpcpy needs to return with the value of the NUL
  112. byte. */
  113. movb %al, (%rdx) /* 1st byte. */
  114. testb %al, %al /* Is it NUL. */
  115. jz 4f /* yes, finish. */
  116. incq %rdx /* Increment destination. */
  117. movb %ah, (%rdx) /* 2nd byte. */
  118. testb %ah, %ah /* Is it NUL?. */
  119. jz 4f /* yes, finish. */
  120. incq %rdx /* Increment destination. */
  121. shrq $16, %rax /* Shift... */
  122. jmp 3b /* and look at next two bytes in %rax. */
  123. 4:
  124. #ifdef USE_AS_STPCPY
  125. movq %rdx, %rax /* Destination is return value. */
  126. #else
  127. movq %rdi, %rax /* Source is return value. */
  128. #endif
  129. retq
  130. END (BP_SYM (STRCPY))