hppa: Sync with pthread.h.
[platform/upstream/glibc.git] / benchtests / bench-strcat.c
1 /* Measure strcat 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 "strcat"
21 #include "bench-string.h"
22
23 typedef char *(*proto_t) (char *, const char *);
24 char *simple_strcat (char *, const char *);
25
26 IMPL (simple_strcat, 0)
27 IMPL (strcat, 1)
28
29 char *
30 simple_strcat (char *dst, const char *src)
31 {
32   char *ret = dst;
33   while (*dst++ != '\0');
34   --dst;
35   while ((*dst++ = *src++) != '\0');
36   return ret;
37 }
38
39 static void
40 do_one_test (impl_t *impl, char *dst, const char *src)
41 {
42   size_t k = strlen (dst), i, iters = INNER_LOOP_ITERS;
43   timing_t start, stop, cur;
44
45   if (CALL (impl, dst, src) != dst)
46     {
47       error (0, 0, "Wrong result in function %s %p %p", impl->name,
48              CALL (impl, dst, src), dst);
49       ret = 1;
50       return;
51     }
52
53   if (strcmp (dst + k, src) != 0)
54     {
55       error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
56              impl->name, dst, src);
57       ret = 1;
58       return;
59     }
60
61   TIMING_NOW (start);
62   for (i = 0; i < iters; ++i)
63     {
64       dst[k] = '\0';
65       CALL (impl, dst, src);
66     }
67   TIMING_NOW (stop);
68
69   TIMING_DIFF (cur, start, stop);
70
71   TIMING_PRINT_MEAN ((double) cur, (double) iters);
72 }
73
74 static void
75 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
76 {
77   size_t i;
78   char *s1, *s2;
79
80   align1 &= 7;
81   if (align1 + len1 >= page_size)
82     return;
83
84   align2 &= 7;
85   if (align2 + len1 + len2 >= page_size)
86     return;
87
88   s1 = (char *) (buf1 + align1);
89   s2 = (char *) (buf2 + align2);
90
91   for (i = 0; i < len1; ++i)
92     s1[i] = 32 + 23 * i % (max_char - 32);
93   s1[len1] = '\0';
94
95   for (i = 0; i < len2; i++)
96     s2[i] = 32 + 23 * i % (max_char - 32);
97
98   printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len1, len2, align1, align2);
99
100   FOR_EACH_IMPL (impl, 0)
101     {
102       s2[len2] = '\0';
103       do_one_test (impl, s2, s1);
104     }
105
106   putchar ('\n');
107 }
108
109 int
110 test_main (void)
111 {
112   size_t i;
113
114   test_init ();
115
116   printf ("%28s", "");
117   FOR_EACH_IMPL (impl, 0)
118     printf ("\t%s", impl->name);
119   putchar ('\n');
120
121   for (i = 0; i < 16; ++i)
122     {
123       do_test (0, 0, i, i, 127);
124       do_test (0, 0, i, i, 255);
125       do_test (0, i, i, i, 127);
126       do_test (i, 0, i, i, 255);
127     }
128
129   for (i = 1; i < 8; ++i)
130     {
131       do_test (0, 0, 8 << i, 8 << i, 127);
132       do_test (8 - i, 2 * i, 8 << i, 8 << i, 127);
133       do_test (0, 0, 8 << i, 2 << i, 127);
134       do_test (8 - i, 2 * i, 8 << i, 2 << i, 127);
135     }
136
137   for (i = 1; i < 8; ++i)
138     {
139       do_test (i, 2 * i, 8 << i, 1, 127);
140       do_test (2 * i, i, 8 << i, 1, 255);
141       do_test (i, i, 8 << i, 10, 127);
142       do_test (i, i, 8 << i, 10, 255);
143     }
144
145   return ret;
146 }
147
148 #include "../test-skeleton.c"