strcat.S 9.0 KB

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