2.9
[glibc/nacl-glibc.git] / sysdeps / x86_64 / strcat.S
blob11b26696913e52177f826516aa91609d288a15aa
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.
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
20    02111-1307 USA.  */
22 #include <sysdep.h>
23 #include "asm-syntax.h"
24 #include "bp-sym.h"
25 #include "bp-asm.h"
28         .text
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.  */
39         addl $8,%ecx
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 */
44         decl %ecx
45         jnz 0b
49         /* Now the source is aligned.  Scan for NUL byte.  */
50         .p2align 4
52         /* First 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 */
66         /* Second unroll.  */
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
72                                    is *not* 0 */
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 */
80         /* Third 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         jnz 3f                  /* found NUL => return pointer */
94         /* Fourth unroll.  */
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
100                                    is *not* 0 */
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    %rsi, %rcx      /* duplicate  */
145         andl    $7,%ecx         /* mask alignment bits */
146         movq    %rax, %rdx      /* move around */
147         jz      22f             /* aligned => start loop */
149         neg     %ecx            /* align to 8 bytes.  */
150         addl    $8, %ecx
151         /* Align the source pointer.  */
153         movb    (%rsi), %al     /* Fetch a byte */
154         testb   %al, %al        /* Is it NUL? */
155         movb    %al, (%rdx)     /* Store it */
156         jz      24f             /* If it was NUL, done! */
157         incq    %rsi
158         incq    %rdx
159         decl    %ecx
160         jnz     21b
162         /* Now the sources is aligned.  Unfortunatly we cannot force
163            to have both source and destination aligned, so ignore the
164            alignment of the destination.  */
165         .p2align 4
167         /* 1st unroll.  */
168         movq    (%rsi), %rax    /* Read double word (8 bytes).  */
169         addq    $8, %rsi        /* Adjust pointer for next word.  */
170         movq    %rax, %r9       /* Save a copy for NUL finding.  */
171         addq    %r8, %r9        /* add the magic value to the word.  We get
172                                    carry bits reported for each byte which
173                                    is *not* 0 */
174         jnc     23f             /* highest byte is NUL => return pointer */
175         xorq    %rax, %r9       /* (word+magic)^word */
176         orq     %r8, %r9        /* set all non-carry bits */
177         incq    %r9             /* add 1: if one carry bit was *not* set
178                                    the addition will not result in 0.  */
180         jnz     23f             /* found NUL => return pointer */
182         movq    %rax, (%rdx)    /* Write value to destination.  */
183         addq    $8, %rdx        /* Adjust pointer.  */
185         /* 2nd 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.  */
198         jnz     23f             /* found NUL => return pointer */
200         movq    %rax, (%rdx)    /* Write value to destination.  */
201         addq    $8, %rdx        /* Adjust pointer.  */
203         /* 3rd unroll.  */
204         movq    (%rsi), %rax    /* Read double word (8 bytes).  */
205         addq    $8, %rsi        /* Adjust pointer for next word.  */
206         movq    %rax, %r9       /* Save a copy for NUL finding.  */
207         addq    %r8, %r9        /* add the magic value to the word.  We get
208                                    carry bits reported for each byte which
209                                    is *not* 0 */
210         jnc     23f             /* highest byte is NUL => return pointer */
211         xorq    %rax, %r9       /* (word+magic)^word */
212         orq     %r8, %r9        /* set all non-carry bits */
213         incq    %r9             /* add 1: if one carry bit was *not* set
214                                    the addition will not result in 0.  */
216         jnz     23f             /* found NUL => return pointer */
218         movq    %rax, (%rdx)    /* Write value to destination.  */
219         addq    $8, %rdx        /* Adjust pointer.  */
221         /* 4th unroll.  */
222         movq    (%rsi), %rax    /* Read double word (8 bytes).  */
223         addq    $8, %rsi        /* Adjust pointer for next word.  */
224         movq    %rax, %r9       /* Save a copy for NUL finding.  */
225         addq    %r8, %r9        /* add the magic value to the word.  We get
226                                    carry bits reported for each byte which
227                                    is *not* 0 */
228         jnc     23f             /* highest byte is NUL => return pointer */
229         xorq    %rax, %r9       /* (word+magic)^word */
230         orq     %r8, %r9        /* set all non-carry bits */
231         incq    %r9             /* add 1: if one carry bit was *not* set
232                                    the addition will not result in 0.  */
234         jnz     23f             /* found NUL => return pointer */
236         movq    %rax, (%rdx)    /* Write value to destination.  */
237         addq    $8, %rdx        /* Adjust pointer.  */
238         jmp     22b             /* Next iteration.  */
240         /* Do the last few bytes. %rax contains the value to write.
241            The loop is unrolled twice.  */
242         .p2align 4
244         movb    %al, (%rdx)     /* 1st byte.  */
245         testb   %al, %al        /* Is it NUL.  */
246         jz      24f             /* yes, finish.  */
247         incq    %rdx            /* Increment destination.  */
248         movb    %ah, (%rdx)     /* 2nd byte.  */
249         testb   %ah, %ah        /* Is it NUL?.  */
250         jz      24f             /* yes, finish.  */
251         incq    %rdx            /* Increment destination.  */
252         shrq    $16, %rax       /* Shift...  */
253         jmp     23b             /* and look at next two bytes in %rax.  */
257         movq    %rdi, %rax      /* Source is return value.  */
258         retq
259 END (BP_SYM (strcat))
260 libc_hidden_builtin_def (strcat)