strcat.S 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /* Next 3 insns are 10 bytes total, make sure we decode them in one go */
  38. .p2align 4,,10
  39. 4:
  40. /* First unroll. */
  41. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  42. addq $8,%rax /* adjust pointer for next word */
  43. movq %r8, %rdx /* magic value */
  44. addq %rcx, %rdx /* add the magic value to the word. We get
  45. carry bits reported for each byte which
  46. is *not* 0 */
  47. jnc 3f /* highest byte is NUL => return pointer */
  48. xorq %rcx, %rdx /* (word+magic)^word */
  49. orq %r8, %rdx /* set all non-carry bits */
  50. incq %rdx /* add 1: if one carry bit was *not* set
  51. the addition will not result in 0. */
  52. jnz 3f /* found NUL => return pointer */
  53. /* Second unroll. */
  54. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  55. addq $8,%rax /* adjust pointer for next word */
  56. movq %r8, %rdx /* magic value */
  57. addq %rcx, %rdx /* add the magic value to the word. We get
  58. carry bits reported for each byte which
  59. is *not* 0 */
  60. jnc 3f /* highest byte is NUL => return pointer */
  61. xorq %rcx, %rdx /* (word+magic)^word */
  62. orq %r8, %rdx /* set all non-carry bits */
  63. incq %rdx /* add 1: if one carry bit was *not* set
  64. the addition will not result in 0. */
  65. jnz 3f /* found NUL => return pointer */
  66. /* Third unroll. */
  67. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  68. addq $8,%rax /* adjust pointer for next word */
  69. movq %r8, %rdx /* magic value */
  70. addq %rcx, %rdx /* add the magic value to the word. We get
  71. carry bits reported for each byte which
  72. is *not* 0 */
  73. jnc 3f /* highest byte is NUL => return pointer */
  74. xorq %rcx, %rdx /* (word+magic)^word */
  75. orq %r8, %rdx /* set all non-carry bits */
  76. incq %rdx /* add 1: if one carry bit was *not* set
  77. the addition will not result in 0. */
  78. jnz 3f /* found NUL => return pointer */
  79. /* Fourth unroll. */
  80. movq (%rax), %rcx /* get double word (= 8 bytes) in question */
  81. addq $8,%rax /* adjust pointer for next word */
  82. movq %r8, %rdx /* magic value */
  83. addq %rcx, %rdx /* 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 %rcx, %rdx /* (word+magic)^word */
  88. orq %r8, %rdx /* set all non-carry bits */
  89. incq %rdx /* add 1: if one carry bit was *not* set
  90. the addition will not result in 0. */
  91. jz 4b /* no NUL found => continue loop */
  92. /* Align, it is a jump target. */
  93. /* Next 3 insns are 8 bytes total, make sure we decode them in one go */
  94. .p2align 3,,8
  95. 3:
  96. subq $8,%rax /* correct pointer increment. */
  97. testb %cl, %cl /* is first byte NUL? */
  98. jz 2f /* yes => return */
  99. incq %rax /* increment pointer */
  100. testb %ch, %ch /* is second byte NUL? */
  101. jz 2f /* yes => return */
  102. incq %rax /* increment pointer */
  103. testl $0x00ff0000, %ecx /* is third byte NUL? */
  104. jz 2f /* yes => return pointer */
  105. incq %rax /* increment pointer */
  106. testl $0xff000000, %ecx /* is fourth byte NUL? */
  107. jz 2f /* yes => return pointer */
  108. incq %rax /* increment pointer */
  109. shrq $32, %rcx /* look at other half. */
  110. testb %cl, %cl /* is first byte NUL? */
  111. jz 2f /* yes => return */
  112. incq %rax /* increment pointer */
  113. testb %ch, %ch /* is second byte NUL? */
  114. jz 2f /* yes => return */
  115. incq %rax /* increment pointer */
  116. testl $0xff0000, %ecx /* is third byte NUL? */
  117. jz 2f /* yes => return pointer */
  118. incq %rax /* increment pointer */
  119. 2:
  120. /* Second step: Copy source to destination. */
  121. movq %rsi, %rcx /* duplicate */
  122. andl $7,%ecx /* mask alignment bits */
  123. movq %rax, %rdx /* move around */
  124. jz 22f /* aligned => start loop */
  125. neg %ecx /* align to 8 bytes. */
  126. addl $8, %ecx
  127. /* Align the source pointer. */
  128. 21:
  129. movb (%rsi), %al /* Fetch a byte */
  130. testb %al, %al /* Is it NUL? */
  131. movb %al, (%rdx) /* Store it */
  132. jz 24f /* If it was NUL, done! */
  133. incq %rsi
  134. incq %rdx
  135. decl %ecx
  136. jnz 21b
  137. /* Now the sources is aligned. Unfortunatly we cannot force
  138. to have both source and destination aligned, so ignore the
  139. alignment of the destination. */
  140. /* Next 3 insns are 10 bytes total, make sure we decode them in one go */
  141. .p2align 4,,10
  142. 22:
  143. /* 1st unroll. */
  144. movq (%rsi), %rax /* Read double word (8 bytes). */
  145. addq $8, %rsi /* Adjust pointer for next word. */
  146. movq %rax, %r9 /* Save a copy for NUL finding. */
  147. addq %r8, %r9 /* add the magic value to the word. We get
  148. carry bits reported for each byte which
  149. is *not* 0 */
  150. jnc 23f /* highest byte is NUL => return pointer */
  151. xorq %rax, %r9 /* (word+magic)^word */
  152. orq %r8, %r9 /* set all non-carry bits */
  153. incq %r9 /* add 1: if one carry bit was *not* set
  154. the addition will not result in 0. */
  155. jnz 23f /* found NUL => return pointer */
  156. movq %rax, (%rdx) /* Write value to destination. */
  157. addq $8, %rdx /* Adjust pointer. */
  158. /* 2nd unroll. */
  159. movq (%rsi), %rax /* Read double word (8 bytes). */
  160. addq $8, %rsi /* Adjust pointer for next word. */
  161. movq %rax, %r9 /* Save a copy for NUL finding. */
  162. addq %r8, %r9 /* add the magic value to the word. We get
  163. carry bits reported for each byte which
  164. is *not* 0 */
  165. jnc 23f /* highest byte is NUL => return pointer */
  166. xorq %rax, %r9 /* (word+magic)^word */
  167. orq %r8, %r9 /* set all non-carry bits */
  168. incq %r9 /* add 1: if one carry bit was *not* set
  169. the addition will not result in 0. */
  170. jnz 23f /* found NUL => return pointer */
  171. movq %rax, (%rdx) /* Write value to destination. */
  172. addq $8, %rdx /* Adjust pointer. */
  173. /* 3rd unroll. */
  174. movq (%rsi), %rax /* Read double word (8 bytes). */
  175. addq $8, %rsi /* Adjust pointer for next word. */
  176. movq %rax, %r9 /* Save a copy for NUL finding. */
  177. addq %r8, %r9 /* add the magic value to the word. We get
  178. carry bits reported for each byte which
  179. is *not* 0 */
  180. jnc 23f /* highest byte is NUL => return pointer */
  181. xorq %rax, %r9 /* (word+magic)^word */
  182. orq %r8, %r9 /* set all non-carry bits */
  183. incq %r9 /* add 1: if one carry bit was *not* set
  184. the addition will not result in 0. */
  185. jnz 23f /* found NUL => return pointer */
  186. movq %rax, (%rdx) /* Write value to destination. */
  187. addq $8, %rdx /* Adjust pointer. */
  188. /* 4th unroll. */
  189. movq (%rsi), %rax /* Read double word (8 bytes). */
  190. addq $8, %rsi /* Adjust pointer for next word. */
  191. movq %rax, %r9 /* Save a copy for NUL finding. */
  192. addq %r8, %r9 /* add the magic value to the word. We get
  193. carry bits reported for each byte which
  194. is *not* 0 */
  195. jnc 23f /* highest byte is NUL => return pointer */
  196. xorq %rax, %r9 /* (word+magic)^word */
  197. orq %r8, %r9 /* set all non-carry bits */
  198. incq %r9 /* add 1: if one carry bit was *not* set
  199. the addition will not result in 0. */
  200. jnz 23f /* found NUL => return pointer */
  201. movq %rax, (%rdx) /* Write value to destination. */
  202. addq $8, %rdx /* Adjust pointer. */
  203. jmp 22b /* Next iteration. */
  204. /* Do the last few bytes. %rax contains the value to write.
  205. The loop is unrolled twice. */
  206. /* Next 3 insns are 6 bytes total, make sure we decode them in one go */
  207. .p2align 3,,6
  208. 23:
  209. movb %al, (%rdx) /* 1st byte. */
  210. testb %al, %al /* Is it NUL. */
  211. jz 24f /* yes, finish. */
  212. incq %rdx /* Increment destination. */
  213. movb %ah, (%rdx) /* 2nd byte. */
  214. testb %ah, %ah /* Is it NUL?. */
  215. jz 24f /* yes, finish. */
  216. incq %rdx /* Increment destination. */
  217. shrq $16, %rax /* Shift... */
  218. jmp 23b /* and look at next two bytes in %rax. */
  219. 24:
  220. movq %rdi, %rax /* Source is return value. */
  221. retq
  222. END (BP_SYM (strcat))
  223. libc_hidden_def(strcat)