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
23 #include "asm-syntax.h"
29 ENTRY (BP_SYM (strcat))
30 movq %rdi, %rcx /* Dest. register. */
31 andl $7, %ecx /* mask alignment bits */
32 movq %rdi, %rax /* Duplicate destination pointer. */
33 movq $0xfefefefefefefeff,%r8
35 /* First step: Find end of destination. */
36 jz 4f /* aligned => start loop */
38 neg %ecx /* We need to align to 8 bytes. */
40 /* Search the first bytes directly. */
41 0: cmpb $0x0,(%rax) /* is byte NUL? */
42 je 2f /* yes => start copy */
43 incq %rax /* increment pointer */
49 /* Now the source is aligned. Scan for NUL byte. */
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
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 */
67 movq (%rax), %rcx /* get double word (= 8 bytes) in question */
68 addq $8,%rax /* adjust pointer for next word */
69 movq %r8, %rdx /* magic value */
70 addq %rcx, %rdx /* add the magic value to the word. We get
71 carry bits reported for each byte which
73 jnc 3f /* highest byte is NUL => return pointer */
74 xorq %rcx, %rdx /* (word+magic)^word */
75 orq %r8, %rdx /* set all non-carry bits */
76 incq %rdx /* add 1: if one carry bit was *not* set
77 the addition will not result in 0. */
78 jnz 3f /* found NUL => return pointer */
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
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 jnz 3f /* found NUL => return pointer */
95 movq (%rax), %rcx /* get double word (= 8 bytes) in question */
96 addq $8,%rax /* adjust pointer for next word */
97 movq %r8, %rdx /* magic value */
98 addq %rcx, %rdx /* add the magic value to the word. We get
99 carry bits reported for each byte which
101 jnc 3f /* highest byte is NUL => return pointer */
102 xorq %rcx, %rdx /* (word+magic)^word */
103 orq %r8, %rdx /* set all non-carry bits */
104 incq %rdx /* add 1: if one carry bit was *not* set
105 the addition will not result in 0. */
106 jz 4b /* no NUL found => continue loop */
108 .p2align 4 /* Align, it's a jump target. */
109 3: subq $8,%rax /* correct pointer increment. */
111 testb %cl, %cl /* is first byte NUL? */
112 jz 2f /* yes => return */
113 incq %rax /* increment pointer */
115 testb %ch, %ch /* is second byte NUL? */
116 jz 2f /* yes => return */
117 incq %rax /* increment pointer */
119 testl $0x00ff0000, %ecx /* is third byte NUL? */
120 jz 2f /* yes => return pointer */
121 incq %rax /* increment pointer */
123 testl $0xff000000, %ecx /* is fourth byte NUL? */
124 jz 2f /* yes => return pointer */
125 incq %rax /* increment pointer */
127 shrq $32, %rcx /* look at other half. */
129 testb %cl, %cl /* is first byte NUL? */
130 jz 2f /* yes => return */
131 incq %rax /* increment pointer */
133 testb %ch, %ch /* is second byte NUL? */
134 jz 2f /* yes => return */
135 incq %rax /* increment pointer */
137 testl $0xff0000, %ecx /* is third byte NUL? */
138 jz 2f /* yes => return pointer */
139 incq %rax /* increment pointer */
142 /* Second step: Copy source to destination. */
144 movq %rax, %rcx /* duplicate */
145 andl $7,%ecx /* mask alignment bits */
146 movq %rax, %rdx /* move around */
147 jz 22f /* aligned => start loop */
149 /* Align the source pointer. */
151 movb (%rsi), %al /* Fetch a byte */
152 testb %al, %al /* Is it NUL? */
153 movb %al, (%rdx) /* Store it */
154 jz 24f /* If it was NUL, done! */
160 /* Now the sources is aligned. Unfortunatly we cannot force
161 to have both source and destination aligned, so ignore the
162 alignment of the destination. */
166 movq (%rsi), %rax /* Read double word (8 bytes). */
167 addq $8, %rsi /* Adjust pointer for next word. */
168 movq %rax, %r9 /* Save a copy for NUL finding. */
169 addq %r8, %r9 /* add the magic value to the word. We get
170 carry bits reported for each byte which
172 jnc 23f /* highest byte is NUL => return pointer */
173 xorq %rax, %r9 /* (word+magic)^word */
174 orq %r8, %r9 /* set all non-carry bits */
175 incq %r9 /* add 1: if one carry bit was *not* set
176 the addition will not result in 0. */
178 jnz 23f /* found NUL => return pointer */
180 movq %rax, (%rdx) /* Write value to destination. */
181 addq $8, %rdx /* Adjust pointer. */
184 movq (%rsi), %rax /* Read double word (8 bytes). */
185 addq $8, %rsi /* Adjust pointer for next word. */
186 movq %rax, %r9 /* Save a copy for NUL finding. */
187 addq %r8, %r9 /* add the magic value to the word. We get
188 carry bits reported for each byte which
190 jnc 23f /* highest byte is NUL => return pointer */
191 xorq %rax, %r9 /* (word+magic)^word */
192 orq %r8, %r9 /* set all non-carry bits */
193 incq %r9 /* add 1: if one carry bit was *not* set
194 the addition will not result in 0. */
196 jnz 23f /* found NUL => return pointer */
198 movq %rax, (%rdx) /* Write value to destination. */
199 addq $8, %rdx /* Adjust pointer. */
202 movq (%rsi), %rax /* Read double word (8 bytes). */
203 addq $8, %rsi /* Adjust pointer for next word. */
204 movq %rax, %r9 /* Save a copy for NUL finding. */
205 addq %r8, %r9 /* add the magic value to the word. We get
206 carry bits reported for each byte which
208 jnc 23f /* highest byte is NUL => return pointer */
209 xorq %rax, %r9 /* (word+magic)^word */
210 orq %r8, %r9 /* set all non-carry bits */
211 incq %r9 /* add 1: if one carry bit was *not* set
212 the addition will not result in 0. */
214 jnz 23f /* found NUL => return pointer */
216 movq %rax, (%rdx) /* Write value to destination. */
217 addq $8, %rdx /* Adjust pointer. */
220 movq (%rsi), %rax /* Read double word (8 bytes). */
221 addq $8, %rsi /* Adjust pointer for next word. */
222 movq %rax, %r9 /* Save a copy for NUL finding. */
223 addq %r8, %r9 /* add the magic value to the word. We get
224 carry bits reported for each byte which
226 jnc 23f /* highest byte is NUL => return pointer */
227 xorq %rax, %r9 /* (word+magic)^word */
228 orq %r8, %r9 /* set all non-carry bits */
229 incq %r9 /* add 1: if one carry bit was *not* set
230 the addition will not result in 0. */
232 jnz 23f /* found NUL => return pointer */
234 movq %rax, (%rdx) /* Write value to destination. */
235 addq $8, %rdx /* Adjust pointer. */
236 jmp 22b /* Next iteration. */
238 /* Do the last few bytes. %rax contains the value to write.
239 The loop is unrolled twice. */
242 movb %al, (%rdx) /* 1st byte. */
243 testb %al, %al /* Is it NUL. */
244 jz 24f /* yes, finish. */
245 incq %rdx /* Increment destination. */
246 movb %ah, (%rdx) /* 2nd byte. */
247 testb %ah, %ah /* Is it NUL?. */
248 jz 24f /* yes, finish. */
249 incq %rdx /* Increment destination. */
250 shrq $16, %rax /* Shift... */
251 jmp 23b /* and look at next two bytes in %rax. */
255 movq %rdi, %rax /* Source is return value. */
257 END (BP_SYM (strcat))