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, see
19 <http://www.gnu.org/licenses/>. */
22 #include "asm-syntax.h"
28 ENTRY (BP_SYM (strcat))
29 movq %rdi, %rcx /* Dest. register. */
30 andl $7, %ecx /* mask alignment bits */
31 movq %rdi, %rax /* Duplicate destination pointer. */
32 movq $0xfefefefefefefeff,%r8
34 /* First step: Find end of destination. */
35 jz 4f /* aligned => start loop */
37 neg %ecx /* We need to align to 8 bytes. */
39 /* Search the first bytes directly. */
40 0: cmpb $0x0,(%rax) /* is byte NUL? */
41 je 2f /* yes => start copy */
42 incq %rax /* increment pointer */
48 /* Now the source is aligned. Scan for NUL byte. */
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
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 */
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
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 */
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
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 jnz 3f /* found NUL => return pointer */
94 movq (%rax), %rcx /* get double word (= 8 bytes) in question */
95 addq $8,%rax /* adjust pointer for next word */
96 movq %r8, %rdx /* magic value */
97 addq %rcx, %rdx /* add the magic value to the word. We get
98 carry bits reported for each byte which
100 jnc 3f /* highest byte is NUL => return pointer */
101 xorq %rcx, %rdx /* (word+magic)^word */
102 orq %r8, %rdx /* set all non-carry bits */
103 incq %rdx /* add 1: if one carry bit was *not* set
104 the addition will not result in 0. */
105 jz 4b /* no NUL found => continue loop */
107 .p2align 4 /* Align, it's a jump target. */
108 3: subq $8,%rax /* correct pointer increment. */
110 testb %cl, %cl /* is first byte NUL? */
111 jz 2f /* yes => return */
112 incq %rax /* increment pointer */
114 testb %ch, %ch /* is second byte NUL? */
115 jz 2f /* yes => return */
116 incq %rax /* increment pointer */
118 testl $0x00ff0000, %ecx /* is third byte NUL? */
119 jz 2f /* yes => return pointer */
120 incq %rax /* increment pointer */
122 testl $0xff000000, %ecx /* is fourth byte NUL? */
123 jz 2f /* yes => return pointer */
124 incq %rax /* increment pointer */
126 shrq $32, %rcx /* look at other half. */
128 testb %cl, %cl /* is first byte NUL? */
129 jz 2f /* yes => return */
130 incq %rax /* increment pointer */
132 testb %ch, %ch /* is second byte NUL? */
133 jz 2f /* yes => return */
134 incq %rax /* increment pointer */
136 testl $0xff0000, %ecx /* is third byte NUL? */
137 jz 2f /* yes => return pointer */
138 incq %rax /* increment pointer */
141 /* Second step: Copy source to destination. */
143 movq %rsi, %rcx /* duplicate */
144 andl $7,%ecx /* mask alignment bits */
145 movq %rax, %rdx /* move around */
146 jz 22f /* aligned => start loop */
148 neg %ecx /* align to 8 bytes. */
150 /* Align the source pointer. */
152 movb (%rsi), %al /* Fetch a byte */
153 testb %al, %al /* Is it NUL? */
154 movb %al, (%rdx) /* Store it */
155 jz 24f /* If it was NUL, done! */
161 /* Now the sources is aligned. Unfortunatly we cannot force
162 to have both source and destination aligned, so ignore the
163 alignment of the destination. */
167 movq (%rsi), %rax /* Read double word (8 bytes). */
168 addq $8, %rsi /* Adjust pointer for next word. */
169 movq %rax, %r9 /* Save a copy for NUL finding. */
170 addq %r8, %r9 /* add the magic value to the word. We get
171 carry bits reported for each byte which
173 jnc 23f /* highest byte is NUL => return pointer */
174 xorq %rax, %r9 /* (word+magic)^word */
175 orq %r8, %r9 /* set all non-carry bits */
176 incq %r9 /* add 1: if one carry bit was *not* set
177 the addition will not result in 0. */
179 jnz 23f /* found NUL => return pointer */
181 movq %rax, (%rdx) /* Write value to destination. */
182 addq $8, %rdx /* Adjust pointer. */
185 movq (%rsi), %rax /* Read double word (8 bytes). */
186 addq $8, %rsi /* Adjust pointer for next word. */
187 movq %rax, %r9 /* Save a copy for NUL finding. */
188 addq %r8, %r9 /* add the magic value to the word. We get
189 carry bits reported for each byte which
191 jnc 23f /* highest byte is NUL => return pointer */
192 xorq %rax, %r9 /* (word+magic)^word */
193 orq %r8, %r9 /* set all non-carry bits */
194 incq %r9 /* add 1: if one carry bit was *not* set
195 the addition will not result in 0. */
197 jnz 23f /* found NUL => return pointer */
199 movq %rax, (%rdx) /* Write value to destination. */
200 addq $8, %rdx /* Adjust pointer. */
203 movq (%rsi), %rax /* Read double word (8 bytes). */
204 addq $8, %rsi /* Adjust pointer for next word. */
205 movq %rax, %r9 /* Save a copy for NUL finding. */
206 addq %r8, %r9 /* add the magic value to the word. We get
207 carry bits reported for each byte which
209 jnc 23f /* highest byte is NUL => return pointer */
210 xorq %rax, %r9 /* (word+magic)^word */
211 orq %r8, %r9 /* set all non-carry bits */
212 incq %r9 /* add 1: if one carry bit was *not* set
213 the addition will not result in 0. */
215 jnz 23f /* found NUL => return pointer */
217 movq %rax, (%rdx) /* Write value to destination. */
218 addq $8, %rdx /* Adjust pointer. */
221 movq (%rsi), %rax /* Read double word (8 bytes). */
222 addq $8, %rsi /* Adjust pointer for next word. */
223 movq %rax, %r9 /* Save a copy for NUL finding. */
224 addq %r8, %r9 /* add the magic value to the word. We get
225 carry bits reported for each byte which
227 jnc 23f /* highest byte is NUL => return pointer */
228 xorq %rax, %r9 /* (word+magic)^word */
229 orq %r8, %r9 /* set all non-carry bits */
230 incq %r9 /* add 1: if one carry bit was *not* set
231 the addition will not result in 0. */
233 jnz 23f /* found NUL => return pointer */
235 movq %rax, (%rdx) /* Write value to destination. */
236 addq $8, %rdx /* Adjust pointer. */
237 jmp 22b /* Next iteration. */
239 /* Do the last few bytes. %rax contains the value to write.
240 The loop is unrolled twice. */
243 movb %al, (%rdx) /* 1st byte. */
244 testb %al, %al /* Is it NUL. */
245 jz 24f /* yes, finish. */
246 incq %rdx /* Increment destination. */
247 movb %ah, (%rdx) /* 2nd byte. */
248 testb %ah, %ah /* Is it NUL?. */
249 jz 24f /* yes, finish. */
250 incq %rdx /* Increment destination. */
251 shrq $16, %rax /* Shift... */
252 jmp 23b /* and look at next two bytes in %rax. */
256 movq %rdi, %rax /* Source is return value. */
258 END (BP_SYM (strcat))
259 libc_hidden_builtin_def (strcat)