]> git.ipfire.org Git - thirdparty/glibc.git/blame - benchtests/bench-strncat.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / benchtests / bench-strncat.c
CommitLineData
97020474 1/* Measure strncat functions.
04277e02 2 Copyright (C) 2013-2019 Free Software Foundation, Inc.
97020474
SP
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
e1fe9118
SL
20#ifndef WIDE
21# define TEST_NAME "strncat"
22#else
23# define TEST_NAME "wcsncat"
24#endif /* WIDE */
97020474
SP
25#include "bench-string.h"
26
90d3320d
WD
27#define BIG_CHAR MAX_CHAR
28
e1fe9118 29#ifndef WIDE
e1fe9118
SL
30# define SIMPLE_STRNCAT simple_strncat
31# define STUPID_STRNCAT stupid_strncat
e1fe9118
SL
32# define SMALL_CHAR 127
33#else
e1fe9118
SL
34# define SIMPLE_STRNCAT simple_wcsncat
35# define STUPID_STRNCAT stupid_wcsncat
e1fe9118
SL
36# define SMALL_CHAR 1273
37#endif /* WIDE */
38
39typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
40CHAR *STUPID_STRNCAT (CHAR *, const CHAR *, size_t);
41CHAR *SIMPLE_STRNCAT (CHAR *, const CHAR *, size_t);
42
43IMPL (STUPID_STRNCAT, 0)
44IMPL (STRNCAT, 2)
45
46CHAR *
47STUPID_STRNCAT (CHAR *dst, const CHAR *src, size_t n)
97020474 48{
e1fe9118 49 CHAR *ret = dst;
97020474
SP
50 while (*dst++ != '\0');
51 --dst;
52 while (n--)
e1fe9118 53 if ((*dst++ = *src++) == '\0')
97020474
SP
54 return ret;
55 *dst = '\0';
56 return ret;
57}
58
59static void
e1fe9118 60do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t n)
97020474 61{
e1fe9118 62 size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS;
44558701
WN
63 timing_t start, stop, cur;
64
97020474
SP
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
e1fe9118
SL
73 size_t len = STRLEN (src);
74 if (MEMCMP (dst + k, src, len + 1 > n ? n : len + 1) != 0)
97020474 75 {
e1fe9118 76 error (0, 0, "Incorrect concatenation in function %s",
97020474
SP
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 }
44558701
WN
88
89 TIMING_NOW (start);
90 for (i = 0; i < iters; ++i)
97020474 91 {
44558701
WN
92 dst[k] = '\0';
93 CALL (impl, dst, src, n);
94 }
95 TIMING_NOW (stop);
97020474 96
44558701 97 TIMING_DIFF (cur, start, stop);
97020474 98
44558701 99 TIMING_PRINT_MEAN ((double) cur, (double) iters);
97020474
SP
100}
101
102static void
103do_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;
e1fe9118 107 CHAR *s1, *s2;
97020474
SP
108
109 align1 &= 7;
e1fe9118 110 if ((align1 + len1) * sizeof (CHAR) >= page_size)
97020474 111 return;
e1fe9118 112 if ((align1 + n) * sizeof (CHAR) > page_size)
97020474
SP
113 return;
114 align2 &= 7;
e1fe9118 115 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
97020474 116 return;
e1fe9118 117 if ((align2 + len1 + n) * sizeof (CHAR) > page_size)
97020474 118 return;
e1fe9118
SL
119 s1 = (CHAR *) (buf1) + align1;
120 s2 = (CHAR *) (buf2) + align2;
97020474
SP
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
44558701
WN
129 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
130 len1, len2, align1, align2, n);
97020474
SP
131
132 FOR_EACH_IMPL (impl, 0)
133 {
134 s2[len2] = '\0';
135 do_one_test (impl, s2, s1, n);
136 }
137
44558701 138 putchar ('\n');
97020474
SP
139}
140
141int
142main (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 {
e1fe9118
SL
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);
97020474
SP
160
161 for (i = 1; i < 8; ++i)
162 {
e1fe9118
SL
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);
97020474
SP
167 }
168
169 for (i = 1; i < 8; ++i)
170 {
e1fe9118
SL
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);
97020474
SP
174 }
175 }
176
177 return ret;
178}