Update copyright dates with scripts/update-copyrights.
[platform/upstream/glibc.git] / benchtests / bench-strncat.c
1 /* Measure strncat functions.
2    Copyright (C) 2013-2019 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 #ifndef WIDE
21 # define TEST_NAME "strncat"
22 #else
23 # define TEST_NAME "wcsncat"
24 #endif /* WIDE */
25 #include "bench-string.h"
26
27 #define BIG_CHAR MAX_CHAR
28
29 #ifndef WIDE
30 # define SIMPLE_STRNCAT simple_strncat
31 # define STUPID_STRNCAT stupid_strncat
32 # define SMALL_CHAR 127
33 #else
34 # define SIMPLE_STRNCAT simple_wcsncat
35 # define STUPID_STRNCAT stupid_wcsncat
36 # define SMALL_CHAR 1273
37 #endif /* WIDE */
38
39 typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
40 CHAR *STUPID_STRNCAT (CHAR *, const CHAR *, size_t);
41 CHAR *SIMPLE_STRNCAT (CHAR *, const CHAR *, size_t);
42
43 IMPL (STUPID_STRNCAT, 0)
44 IMPL (STRNCAT, 2)
45
46 CHAR *
47 STUPID_STRNCAT (CHAR *dst, const CHAR *src, size_t n)
48 {
49   CHAR *ret = dst;
50   while (*dst++ != '\0');
51   --dst;
52   while (n--)
53     if ((*dst++ = *src++) == '\0')
54       return ret;
55   *dst = '\0';
56   return ret;
57 }
58
59 static void
60 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t n)
61 {
62   size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS;
63   timing_t start, stop, cur;
64
65   if (CALL (impl, dst, src, n) != dst)
66     {
67       error (0, 0, "Wrong result in function %s %p != %p", impl->name,
68              CALL (impl, dst, src, n), dst);
69       ret = 1;
70       return;
71     }
72
73   size_t len = STRLEN (src);
74   if (MEMCMP (dst + k, src, len + 1 > n ? n : len + 1) != 0)
75     {
76       error (0, 0, "Incorrect concatenation in function %s",
77              impl->name);
78       ret = 1;
79       return;
80     }
81   if (n < len && dst[k + n] != '\0')
82     {
83       error (0, 0, "There is no zero in the end of output string in %s",
84              impl->name);
85       ret = 1;
86       return;
87     }
88
89   TIMING_NOW (start);
90   for (i = 0; i < iters; ++i)
91     {
92       dst[k] = '\0';
93       CALL (impl, dst, src, n);
94     }
95   TIMING_NOW (stop);
96
97   TIMING_DIFF (cur, start, stop);
98
99   TIMING_PRINT_MEAN ((double) cur, (double) iters);
100 }
101
102 static void
103 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
104          size_t n, int max_char)
105 {
106   size_t i;
107   CHAR *s1, *s2;
108
109   align1 &= 7;
110   if ((align1 + len1) * sizeof (CHAR) >= page_size)
111     return;
112   if ((align1 + n) * sizeof (CHAR) > page_size)
113     return;
114   align2 &= 7;
115   if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
116     return;
117   if ((align2 + len1 + n) * sizeof (CHAR) > page_size)
118     return;
119   s1 = (CHAR *) (buf1) + align1;
120   s2 = (CHAR *) (buf2) + align2;
121
122   for (i = 0; i < len1; ++i)
123     s1[i] = 32 + 23 * i % (max_char - 32);
124   s1[len1] = '\0';
125
126   for (i = 0; i < len2; i++)
127     s2[i] = 32 + 23 * i % (max_char - 32);
128
129   printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
130           len1, len2, align1, align2, n);
131
132   FOR_EACH_IMPL (impl, 0)
133     {
134       s2[len2] = '\0';
135       do_one_test (impl, s2, s1, n);
136     }
137
138   putchar ('\n');
139 }
140
141 int
142 main (void)
143 {
144   size_t i, n;
145
146   test_init ();
147
148   printf ("%28s", "");
149   FOR_EACH_IMPL (impl, 0)
150     printf ("\t%s", impl->name);
151   putchar ('\n');
152
153   for (n = 2; n <= 2048; n*=4)
154     {
155       do_test (0, 2, 2, 2, n, SMALL_CHAR);
156       do_test (0, 0, 4, 4, n, SMALL_CHAR);
157       do_test (4, 0, 4, 4, n, BIG_CHAR);
158       do_test (0, 0, 8, 8, n, SMALL_CHAR);
159       do_test (0, 8, 8, 8, n, SMALL_CHAR);
160
161       for (i = 1; i < 8; ++i)
162         {
163           do_test (0, 0, 8 << i, 8 << i, n, SMALL_CHAR);
164           do_test (8 - i, 2 * i, 8 << i, 8 << i, n, SMALL_CHAR);
165           do_test (0, 0, 8 << i, 2 << i, n, SMALL_CHAR);
166           do_test (8 - i, 2 * i, 8 << i, 2 << i, n, SMALL_CHAR);
167         }
168
169       for (i = 1; i < 8; ++i)
170         {
171           do_test (i, 2 * i, 8 << i, 1, n, SMALL_CHAR);
172           do_test (2 * i, i, 8 << i, 1, n, BIG_CHAR);
173           do_test (i, i, 8 << i, 10, n, SMALL_CHAR);
174         }
175     }
176
177   return ret;
178 }