Remove TIMING_INIT
[glibc.git] / benchtests / bench-strncpy.c
blob35744c1bd6fd32390d89b87ed81b8260822dba3f
1 /* Measure strncpy functions.
2 Copyright (C) 2013-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #define BIG_CHAR MAX_CHAR
21 #ifdef WIDE
22 # define SMALL_CHAR 1273
23 #else
24 # define SMALL_CHAR 127
25 #endif /* !WIDE */
27 #ifndef STRNCPY_RESULT
28 # define STRNCPY_RESULT(dst, len, n) dst
29 # define TEST_MAIN
30 # ifndef WIDE
31 # define TEST_NAME "strncpy"
32 # else
33 # define TEST_NAME "wcsncpy"
34 # endif /* WIDE */
35 # include "bench-string.h"
36 # ifndef WIDE
37 # define SIMPLE_STRNCPY simple_strncpy
38 # define STUPID_STRNCPY stupid_strncpy
39 # else
40 # define SIMPLE_STRNCPY simple_wcsncpy
41 # define STUPID_STRNCPY stupid_wcsncpy
42 # endif /* WIDE */
44 CHAR *SIMPLE_STRNCPY (CHAR *, const CHAR *, size_t);
45 CHAR *STUPID_STRNCPY (CHAR *, const CHAR *, size_t);
47 IMPL (STUPID_STRNCPY, 0)
48 IMPL (SIMPLE_STRNCPY, 0)
49 IMPL (STRNCPY, 1)
51 CHAR *
52 SIMPLE_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
54 CHAR *ret = dst;
55 while (n--)
56 if ((*dst++ = *src++) == '\0')
58 while (n--)
59 *dst++ = '\0';
60 return ret;
62 return ret;
65 CHAR *
66 STUPID_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
68 size_t nc = STRNLEN (src, n);
69 size_t i;
71 for (i = 0; i < nc; ++i)
72 dst[i] = src[i];
73 for (; i < n; ++i)
74 dst[i] = '\0';
75 return dst;
77 #endif /* !STRNCPY_RESULT */
79 typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
81 static void
82 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t len, size_t n)
84 size_t i, iters = INNER_LOOP_ITERS;
85 timing_t start, stop, cur;
87 if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n))
89 error (0, 0, "Wrong result in function %s %p %p", impl->name,
90 CALL (impl, dst, src, n), dst);
91 ret = 1;
92 return;
95 if (memcmp (dst, src, (len > n ? n : len) * sizeof (CHAR)) != 0)
97 error (0, 0, "Wrong result in function %s", impl->name);
98 ret = 1;
99 return;
102 if (n > len)
104 size_t i;
106 for (i = len; i < n; ++i)
107 if (dst [i] != '\0')
109 error (0, 0, "Wrong result in function %s", impl->name);
110 ret = 1;
111 return;
115 TIMING_NOW (start);
116 for (i = 0; i < iters; ++i)
118 CALL (impl, dst, src, n);
120 TIMING_NOW (stop);
122 TIMING_DIFF (cur, start, stop);
124 TIMING_PRINT_MEAN ((double) cur, (double) iters);
127 static void
128 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
130 size_t i;
131 CHAR *s1, *s2;
133 /* For wcsncpy: align1 and align2 here mean alignment not in bytes,
134 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */
135 align1 &= 7;
136 if ((align1 + len) * sizeof (CHAR) >= page_size)
137 return;
139 align2 &= 7;
140 if ((align2 + len) * sizeof (CHAR) >= page_size)
141 return;
143 s1 = (CHAR *) (buf1) + align1;
144 s2 = (CHAR *) (buf2) + align2;
146 for (i = 0; i < len; ++i)
147 s1[i] = 32 + 23 * i % (max_char - 32);
148 s1[len] = 0;
149 for (i = len + 1; (i + align1) * sizeof (CHAR) < page_size && i < len + 64;
150 ++i)
151 s1[i] = 32 + 32 * i % (max_char - 32);
153 printf ("Length %4zd, n %4zd, alignment %2zd/%2zd:", len, n, align1, align2);
155 FOR_EACH_IMPL (impl, 0)
156 do_one_test (impl, s2, s1, len, n);
158 putchar ('\n');
161 static int
162 test_main (void)
164 size_t i;
166 test_init ();
168 printf ("%28s", "");
169 FOR_EACH_IMPL (impl, 0)
170 printf ("\t%s", impl->name);
171 putchar ('\n');
173 for (i = 1; i < 8; ++i)
175 do_test (i, i, 16, 16, SMALL_CHAR);
176 do_test (i, i, 16, 16, BIG_CHAR);
177 do_test (i, 2 * i, 16, 16, SMALL_CHAR);
178 do_test (2 * i, i, 16, 16, BIG_CHAR);
179 do_test (8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR);
180 do_test (2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR);
181 do_test (8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR);
182 do_test (2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR);
185 for (i = 1; i < 8; ++i)
187 do_test (0, 0, 4 << i, 8 << i, SMALL_CHAR);
188 do_test (0, 0, 16 << i, 8 << i, SMALL_CHAR);
189 do_test (8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR);
190 do_test (8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR);
193 return ret;
196 #include <support/test-driver.c>