strcat.S 8.7 KB

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