hppa: Sync with pthread.h.
[platform/upstream/glibc.git] / benchtests / bench-strncat.c
1 /* Measure strncat functions.
2    Copyright (C) 2013-2015 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #define TEST_MAIN
20 #define TEST_NAME "strncat"
21 #include "bench-string.h"
22
23 typedef char *(*proto_t) (char *, const char *, size_t);
24 char *stupid_strncat (char *, const char *, size_t);
25 char *simple_strncat (char *, const char *, size_t);
26
27 IMPL (stupid_strncat, 0)
28 IMPL (strncat, 2)
29
30 char *
31 stupid_strncat (char *dst, const char *src, size_t n)
32 {
33   char *ret = dst;
34   while (*dst++ != '\0');
35   --dst;
36   while (n--)
37     if ( (*dst++ = *src++) == '\0')
38       return ret;
39   *dst = '\0';
40   return ret;
41 }
42
43 static void
44 do_one_test (impl_t *impl, char *dst, const char *src, size_t n)
45 {
46   size_t k = strlen (dst), i, iters = INNER_LOOP_ITERS;
47   timing_t start, stop, cur;
48
49   if (CALL (impl, dst, src, n) != dst)
50     {
51       error (0, 0, "Wrong result in function %s %p != %p", impl->name,
52              CALL (impl, dst, src, n), dst);
53       ret = 1;
54       return;
55     }
56
57   size_t len = strlen (src);
58   if (memcmp (dst + k, src, len + 1 > n ? n : len + 1) != 0)
59     {
60       error (0, 0, "Incorrect cancatination in function %s",
61              impl->name);
62       ret = 1;
63       return;
64     }
65   if (n < len && dst[k + n] != '\0')
66     {
67       error (0, 0, "There is no zero in the end of output string in %s",
68              impl->name);
69       ret = 1;
70       return;
71     }
72
73   TIMING_NOW (start);
74   for (i = 0; i < iters; ++i)
75     {
76       dst[k] = '\0';
77       CALL (impl, dst, src, n);
78     }
79   TIMING_NOW (stop);
80
81   TIMING_DIFF (cur, start, stop);
82
83   TIMING_PRINT_MEAN ((double) cur, (double) iters);
84 }
85
86 static void
87 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
88          size_t n, int max_char)
89 {
90   size_t i;
91   char *s1, *s2;
92
93   align1 &= 7;
94   if (align1 + len1 >= page_size)
95     return;
96   if (align1 + n > page_size)
97     return;
98   align2 &= 7;
99   if (align2 + len1 + len2 >= page_size)
100     return;
101   if (align2 + len1 + n > page_size)
102     return;
103   s1 = (char *) (buf1 + align1);
104   s2 = (char *) (buf2 + align2);
105
106   for (i = 0; i < len1; ++i)
107     s1[i] = 32 + 23 * i % (max_char - 32);
108   s1[len1] = '\0';
109
110   for (i = 0; i < len2; i++)
111     s2[i] = 32 + 23 * i % (max_char - 32);
112
113   printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
114           len1, len2, align1, align2, n);
115
116   FOR_EACH_IMPL (impl, 0)
117     {
118       s2[len2] = '\0';
119       do_one_test (impl, s2, s1, n);
120     }
121
122   putchar ('\n');
123 }
124
125 int
126 main (void)
127 {
128   size_t i, n;
129
130   test_init ();
131
132   printf ("%28s", "");
133   FOR_EACH_IMPL (impl, 0)
134     printf ("\t%s", impl->name);
135   putchar ('\n');
136
137   for (n = 2; n <= 2048; n*=4)
138     {
139       do_test (0, 2, 2, 2, n, 127);
140       do_test (0, 0, 4, 4, n, 127);
141       do_test (4, 0, 4, 4, n, 255);
142       do_test (0, 0, 8, 8, n, 127);
143       do_test (0, 8, 8, 8, n, 127);
144
145       for (i = 1; i < 8; ++i)
146         {
147           do_test (0, 0, 8 << i, 8 << i, n, 127);
148           do_test (8 - i, 2 * i, 8 << i, 8 << i, n, 127);
149           do_test (0, 0, 8 << i, 2 << i, n, 127);
150           do_test (8 - i, 2 * i, 8 << i, 2 << i, n, 127);
151         }
152
153       for (i = 1; i < 8; ++i)
154         {
155           do_test (i, 2 * i, 8 << i, 1, n, 127);
156           do_test (2 * i, i, 8 << i, 1, n, 255);
157           do_test (i, i, 8 << i, 10, n, 127);
158         }
159     }
160
161   return ret;
162 }