1 /* strcat(dest, src) -- Append SRC on the end of DEST.
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.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include "_glibc_inc.h"
26 ENTRY (BP_SYM (strcat))
27 movq %rdi, %rcx /* Dest. register. */
28 andl $7, %ecx /* mask alignment bits */
29 movq %rdi, %rax /* Duplicate destination pointer. */
30 movq $0xfefefefefefefeff,%r8
32 /* First step: Find end of destination. */
33 jz 4f /* aligned => start loop */
35 neg %ecx /* We need to align to 8 bytes. */
37 /* Search the first bytes directly. */
38 0: cmpb $0x0,(%rax) /* is byte NUL? */
39 je 2f /* yes => start copy */
40 incq %rax /* increment pointer */
46 /* Now the source is aligned. Scan for NUL byte. */
50 movq (%rax), %rcx /* get double word (= 8 bytes) in question */
51 addq $8,%rax /* adjust pointer for next word */
52 movq %r8, %rdx /* magic value */
53 addq %rcx, %rdx /* add the magic value to the word. We get
54 carry bits reported for each byte which
56 jnc 3f /* highest byte is NUL => return pointer */
57 xorq %rcx, %rdx /* (word+magic)^word */
58 orq %r8, %rdx /* set all non-carry bits */
59 incq %rdx /* add 1: if one carry bit was *not* set
60 the addition will not result in 0. */
61 jnz 3f /* found NUL => return pointer */
64 movq (%rax), %rcx /* get double word (= 8 bytes) in question */
65 addq $8,%rax /* adjust pointer for next word */
66 movq %r8, %rdx /* magic value */
67 addq %rcx, %rdx /* add the magic value to the word. We get
68 carry bits reported for each byte which
70 jnc 3f /* highest byte is NUL => return pointer */
71 xorq %rcx, %rdx /* (word+magic)^word */
72 orq %r8, %rdx /* set all non-carry bits */
73 incq %rdx /* add 1: if one carry bit was *not* set
74 the addition will not result in 0. */
75 jnz 3f /* found NUL => return pointer */
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
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 jnz 3f /* found NUL => return pointer */
92 movq (%rax), %rcx /* get double word (= 8 bytes) in question */
93 addq $8,%rax /* adjust pointer for next word */
94 movq %r8, %rdx /* magic value */
95 addq %rcx, %rdx /* add the magic value to the word. We get
96 carry bits reported for each byte which
98 jnc 3f /* highest byte is NUL => return pointer */
99 xorq %rcx, %rdx /* (word+magic)^word */
100 orq %r8, %rdx /* set all non-carry bits */
101 incq %rdx /* add 1: if one carry bit was *not* set
102 the addition will not result in 0. */
103 jz 4b /* no NUL found => continue loop */
105 .p2align 4 /* Align, it's a jump target. */
106 3: subq $8,%rax /* correct pointer increment. */
108 testb %cl, %cl /* is first byte NUL? */
109 jz 2f /* yes => return */
110 incq %rax /* increment pointer */
112 testb %ch, %ch /* is second byte NUL? */
113 jz 2f /* yes => return */
114 incq %rax /* increment pointer */
116 testl $0x00ff0000, %ecx /* is third byte NUL? */
117 jz 2f /* yes => return pointer */
118 incq %rax /* increment pointer */
120 testl $0xff000000, %ecx /* is fourth byte NUL? */
121 jz 2f /* yes => return pointer */
122 incq %rax /* increment pointer */
124 shrq $32, %rcx /* look at other half. */
126 testb %cl, %cl /* is first byte NUL? */
127 jz 2f /* yes => return */
128 incq %rax /* increment pointer */
130 testb %ch, %ch /* is second byte NUL? */
131 jz 2f /* yes => return */
132 incq %rax /* increment pointer */
134 testl $0xff0000, %ecx /* is third byte NUL? */
135 jz 2f /* yes => return pointer */
136 incq %rax /* increment pointer */
139 /* Second step: Copy source to destination. */
141 movq %rsi, %rcx /* duplicate */
142 andl $7,%ecx /* mask alignment bits */
143 movq %rax, %rdx /* move around */
144 jz 22f /* aligned => start loop */
146 neg %ecx /* align to 8 bytes. */
148 /* Align the source pointer. */
150 movb (%rsi), %al /* Fetch a byte */
151 testb %al, %al /* Is it NUL? */
152 movb %al, (%rdx) /* Store it */
153 jz 24f /* If it was NUL, done! */
159 /* Now the sources is aligned. Unfortunatly we cannot force
160 to have both source and destination aligned, so ignore the
161 alignment of the destination. */
165 movq (%rsi), %rax /* Read double word (8 bytes). */
166 addq $8, %rsi /* Adjust pointer for next word. */
167 movq %rax, %r9 /* Save a copy for NUL finding. */
168 addq %r8, %r9 /* add the magic value to the word. We get
169 carry bits reported for each byte which
171 jnc 23f /* highest byte is NUL => return pointer */
172 xorq %rax, %r9 /* (word+magic)^word */
173 orq %r8, %r9 /* set all non-carry bits */
174 incq %r9 /* add 1: if one carry bit was *not* set
175 the addition will not result in 0. */
177 jnz 23f /* found NUL => return pointer */
179 movq %rax, (%rdx) /* Write value to destination. */
180 addq $8, %rdx /* Adjust pointer. */
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
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. */
195 jnz 23f /* found NUL => return pointer */
197 movq %rax, (%rdx) /* Write value to destination. */
198 addq $8, %rdx /* Adjust pointer. */
201 movq (%rsi), %rax /* Read double word (8 bytes). */
202 addq $8, %rsi /* Adjust pointer for next word. */
203 movq %rax, %r9 /* Save a copy for NUL finding. */
204 addq %r8, %r9 /* add the magic value to the word. We get
205 carry bits reported for each byte which
207 jnc 23f /* highest byte is NUL => return pointer */
208 xorq %rax, %r9 /* (word+magic)^word */
209 orq %r8, %r9 /* set all non-carry bits */
210 incq %r9 /* add 1: if one carry bit was *not* set
211 the addition will not result in 0. */
213 jnz 23f /* found NUL => return pointer */
215 movq %rax, (%rdx) /* Write value to destination. */
216 addq $8, %rdx /* Adjust pointer. */
219 movq (%rsi), %rax /* Read double word (8 bytes). */
220 addq $8, %rsi /* Adjust pointer for next word. */
221 movq %rax, %r9 /* Save a copy for NUL finding. */
222 addq %r8, %r9 /* add the magic value to the word. We get
223 carry bits reported for each byte which
225 jnc 23f /* highest byte is NUL => return pointer */
226 xorq %rax, %r9 /* (word+magic)^word */
227 orq %r8, %r9 /* set all non-carry bits */
228 incq %r9 /* add 1: if one carry bit was *not* set
229 the addition will not result in 0. */
231 jnz 23f /* found NUL => return pointer */
233 movq %rax, (%rdx) /* Write value to destination. */
234 addq $8, %rdx /* Adjust pointer. */
235 jmp 22b /* Next iteration. */
237 /* Do the last few bytes. %rax contains the value to write.
238 The loop is unrolled twice. */
241 movb %al, (%rdx) /* 1st byte. */
242 testb %al, %al /* Is it NUL. */
243 jz 24f /* yes, finish. */
244 incq %rdx /* Increment destination. */
245 movb %ah, (%rdx) /* 2nd byte. */
246 testb %ah, %ah /* Is it NUL?. */
247 jz 24f /* yes, finish. */
248 incq %rdx /* Increment destination. */
249 shrq $16, %rax /* Shift... */
250 jmp 23b /* and look at next two bytes in %rax. */
254 movq %rdi, %rax /* Source is return value. */
256 END (BP_SYM (strcat))
258 libc_hidden_def(strcat)