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