strcat.S 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* strcat(dest, src) -- Append SRC on the end of DEST.
  2. Optimized for x86-64.
  3. Copyright (C) 2002 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Andreas Jaeger <aj@suse.de>, 2002.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. #include "_glibc_inc.h"
  19. /* Seems to be unrolled too much */
  20. .text
  21. ENTRY (BP_SYM (strcat))
  22. movq %rdi, %rcx /* Dest. register. */
  23. andl $7, %ecx /* mask alignment bits */
  24. movq %rdi, %rax /* Duplicate destination pointer. */
  25. movq $0xfefefefefefefeff,%r8
  26. /* First step: Find end of destination. */
  27. jz 4f /* 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: cmpb $0x0,(%rax) /* is byte NUL? */
  32. je 2f /* yes => start copy */
  33. incq %rax /* increment pointer */
  34. decl %ecx
  35. jnz 0b
  36. /* Now the source is aligned. Scan for NUL byte. */
  37. .p2align 4
  38. 4:
  39. /* First unroll. */
  40. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  41. addq $8,%rax /* adjust pointer for next word */
  42. movq %r8, %rdx /* magic value */
  43. addq %rcx, %rdx /* add the magic value to the word. We get
  44. carry bits reported for each byte which
  45. is *not* 0 */
  46. jnc 3f /* highest byte is NUL => return pointer */
  47. xorq %rcx, %rdx /* (word+magic)^word */
  48. orq %r8, %rdx /* set all non-carry bits */
  49. incq %rdx /* add 1: if one carry bit was *not* set
  50. the addition will not result in 0. */
  51. jnz 3f /* found NUL => return pointer */
  52. /* Second unroll. */
  53. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  54. addq $8,%rax /* adjust pointer for next word */
  55. movq %r8, %rdx /* magic value */
  56. addq %rcx, %rdx /* add the magic value to the word. We get
  57. carry bits reported for each byte which
  58. is *not* 0 */
  59. jnc 3f /* highest byte is NUL => return pointer */
  60. xorq %rcx, %rdx /* (word+magic)^word */
  61. orq %r8, %rdx /* set all non-carry bits */
  62. incq %rdx /* add 1: if one carry bit was *not* set
  63. the addition will not result in 0. */
  64. jnz 3f /* found NUL => return pointer */
  65. /* Third unroll. */
  66. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  67. addq $8,%rax /* adjust pointer for next word */
  68. movq %r8, %rdx /* magic value */
  69. addq %rcx, %rdx /* add the magic value to the word. We get
  70. carry bits reported for each byte which
  71. is *not* 0 */
  72. jnc 3f /* highest byte is NUL => return pointer */
  73. xorq %rcx, %rdx /* (word+magic)^word */
  74. orq %r8, %rdx /* set all non-carry bits */
  75. incq %rdx /* add 1: if one carry bit was *not* set
  76. the addition will not result in 0. */
  77. jnz 3f /* found NUL => return pointer */
  78. /* Fourth unroll. */
  79. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  80. addq $8,%rax /* adjust pointer for next word */
  81. movq %r8, %rdx /* magic value */
  82. addq %rcx, %rdx /* 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 %rcx, %rdx /* (word+magic)^word */
  87. orq %r8, %rdx /* set all non-carry bits */
  88. incq %rdx /* add 1: if one carry bit was *not* set
  89. the addition will not result in 0. */
  90. jz 4b /* no NUL found => continue loop */
  91. .p2align 4 /* Align, it's a jump target. */
  92. 3: subq $8,%rax /* correct pointer increment. */
  93. testb %cl, %cl /* is first byte NUL? */
  94. jz 2f /* yes => return */
  95. incq %rax /* increment pointer */
  96. testb %ch, %ch /* is second byte NUL? */
  97. jz 2f /* yes => return */
  98. incq %rax /* increment pointer */
  99. testl $0x00ff0000, %ecx /* is third byte NUL? */
  100. jz 2f /* yes => return pointer */
  101. incq %rax /* increment pointer */
  102. testl $0xff000000, %ecx /* is fourth byte NUL? */
  103. jz 2f /* yes => return pointer */
  104. incq %rax /* increment pointer */
  105. shrq $32, %rcx /* look at other half. */
  106. testb %cl, %cl /* is first byte NUL? */
  107. jz 2f /* yes => return */
  108. incq %rax /* increment pointer */
  109. testb %ch, %ch /* is second byte NUL? */
  110. jz 2f /* yes => return */
  111. incq %rax /* increment pointer */
  112. testl $0xff0000, %ecx /* is third byte NUL? */
  113. jz 2f /* yes => return pointer */
  114. incq %rax /* increment pointer */
  115. 2:
  116. /* Second step: Copy source to destination. */
  117. movq %rsi, %rcx /* duplicate */
  118. andl $7,%ecx /* mask alignment bits */
  119. movq %rax, %rdx /* move around */
  120. jz 22f /* aligned => start loop */
  121. neg %ecx /* align to 8 bytes. */
  122. addl $8, %ecx
  123. /* Align the source pointer. */
  124. 21:
  125. movb (%rsi), %al /* Fetch a byte */
  126. testb %al, %al /* Is it NUL? */
  127. movb %al, (%rdx) /* Store it */
  128. jz 24f /* If it was NUL, done! */
  129. incq %rsi
  130. incq %rdx
  131. decl %ecx
  132. jnz 21b
  133. /* Now the sources is aligned. Unfortunatly we cannot force
  134. to have both source and destination aligned, so ignore the
  135. alignment of the destination. */
  136. .p2align 4
  137. 22:
  138. /* 1st unroll. */
  139. movq (%rsi), %rax /* Read double word (8 bytes). */
  140. addq $8, %rsi /* Adjust pointer for next word. */
  141. movq %rax, %r9 /* Save a copy for NUL finding. */
  142. addq %r8, %r9 /* add the magic value to the word. We get
  143. carry bits reported for each byte which
  144. is *not* 0 */
  145. jnc 23f /* highest byte is NUL => return pointer */
  146. xorq %rax, %r9 /* (word+magic)^word */
  147. orq %r8, %r9 /* set all non-carry bits */
  148. incq %r9 /* add 1: if one carry bit was *not* set
  149. the addition will not result in 0. */
  150. jnz 23f /* found NUL => return pointer */
  151. movq %rax, (%rdx) /* Write value to destination. */
  152. addq $8, %rdx /* Adjust pointer. */
  153. /* 2nd unroll. */
  154. movq (%rsi), %rax /* Read double word (8 bytes). */
  155. addq $8, %rsi /* Adjust pointer for next word. */
  156. movq %rax, %r9 /* Save a copy for NUL finding. */
  157. addq %r8, %r9 /* add the magic value to the word. We get
  158. carry bits reported for each byte which
  159. is *not* 0 */
  160. jnc 23f /* highest byte is NUL => return pointer */
  161. xorq %rax, %r9 /* (word+magic)^word */
  162. orq %r8, %r9 /* set all non-carry bits */
  163. incq %r9 /* add 1: if one carry bit was *not* set
  164. the addition will not result in 0. */
  165. jnz 23f /* found NUL => return pointer */
  166. movq %rax, (%rdx) /* Write value to destination. */
  167. addq $8, %rdx /* Adjust pointer. */
  168. /* 3rd unroll. */
  169. movq (%rsi), %rax /* Read double word (8 bytes). */
  170. addq $8, %rsi /* Adjust pointer for next word. */
  171. movq %rax, %r9 /* Save a copy for NUL finding. */
  172. addq %r8, %r9 /* add the magic value to the word. We get
  173. carry bits reported for each byte which
  174. is *not* 0 */
  175. jnc 23f /* highest byte is NUL => return pointer */
  176. xorq %rax, %r9 /* (word+magic)^word */
  177. orq %r8, %r9 /* set all non-carry bits */
  178. incq %r9 /* add 1: if one carry bit was *not* set
  179. the addition will not result in 0. */
  180. jnz 23f /* found NUL => return pointer */
  181. movq %rax, (%rdx) /* Write value to destination. */
  182. addq $8, %rdx /* Adjust pointer. */
  183. /* 4th unroll. */
  184. movq (%rsi), %rax /* Read double word (8 bytes). */
  185. addq $8, %rsi /* Adjust pointer for next word. */
  186. movq %rax, %r9 /* Save a copy for NUL finding. */
  187. addq %r8, %r9 /* add the magic value to the word. We get
  188. carry bits reported for each byte which
  189. is *not* 0 */
  190. jnc 23f /* highest byte is NUL => return pointer */
  191. xorq %rax, %r9 /* (word+magic)^word */
  192. orq %r8, %r9 /* set all non-carry bits */
  193. incq %r9 /* add 1: if one carry bit was *not* set
  194. the addition will not result in 0. */
  195. jnz 23f /* found NUL => return pointer */
  196. movq %rax, (%rdx) /* Write value to destination. */
  197. addq $8, %rdx /* Adjust pointer. */
  198. jmp 22b /* Next iteration. */
  199. /* Do the last few bytes. %rax contains the value to write.
  200. The loop is unrolled twice. */
  201. .p2align 4
  202. 23:
  203. movb %al, (%rdx) /* 1st byte. */
  204. testb %al, %al /* Is it NUL. */
  205. jz 24f /* yes, finish. */
  206. incq %rdx /* Increment destination. */
  207. movb %ah, (%rdx) /* 2nd byte. */
  208. testb %ah, %ah /* Is it NUL?. */
  209. jz 24f /* yes, finish. */
  210. incq %rdx /* Increment destination. */
  211. shrq $16, %rax /* Shift... */
  212. jmp 23b /* and look at next two bytes in %rax. */
  213. 24:
  214. movq %rdi, %rax /* Source is return value. */
  215. retq
  216. END (BP_SYM (strcat))
  217. libc_hidden_def(strcat)