Improve 64 bit strcat functions with SSE2/SSSE3
[platform/upstream/glibc.git] / string / strncat.c
1 /* Copyright (C) 1991, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
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
19 #include <string.h>
20
21 #ifdef _LIBC
22 # include <memcopy.h>
23 #else
24 typedef char reg_char;
25 #endif
26
27 #ifndef STRNCAT
28 # define STRNCAT  strncat
29 #endif
30
31 char *
32 STRNCAT (s1, s2, n)
33      char *s1;
34      const char *s2;
35      size_t n;
36 {
37   reg_char c;
38   char *s = s1;
39
40   /* Find the end of S1.  */
41   do
42     c = *s1++;
43   while (c != '\0');
44
45   /* Make S1 point before next character, so we can increment
46      it while memory is read (wins on pipelined cpus).  */
47   s1 -= 2;
48
49   if (n >= 4)
50     {
51       size_t n4 = n >> 2;
52       do
53         {
54           c = *s2++;
55           *++s1 = c;
56           if (c == '\0')
57             return s;
58           c = *s2++;
59           *++s1 = c;
60           if (c == '\0')
61             return s;
62           c = *s2++;
63           *++s1 = c;
64           if (c == '\0')
65             return s;
66           c = *s2++;
67           *++s1 = c;
68           if (c == '\0')
69             return s;
70         } while (--n4 > 0);
71       n &= 3;
72     }
73
74   while (n > 0)
75     {
76       c = *s2++;
77       *++s1 = c;
78       if (c == '\0')
79         return s;
80       n--;
81     }
82
83   if (c != '\0')
84     *++s1 = '\0';
85
86   return s;
87 }