1 /* Measure strncpy functions.
2 Copyright (C) 2013-2024 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 <https://www.gnu.org/licenses/>. */
19 #define BIG_CHAR MAX_CHAR
22 # define SMALL_CHAR 1273
24 # define SMALL_CHAR 127
29 #ifndef STRNCPY_RESULT
30 # define STRNCPY_RESULT(dst, len, n) dst
33 # define TEST_NAME "strncpy"
35 # define TEST_NAME "wcsncpy"
36 # define generic_strncpy generic_wcsncpy
38 # include "bench-string.h"
41 generic_strncpy (CHAR *dst, const CHAR *src, size_t n)
43 size_t nc = STRNLEN (src, n);
45 MEMSET (dst + nc, 0, n - nc);
46 return MEMCPY (dst, src, nc);
50 IMPL (generic_strncpy, 0)
52 #endif /* !STRNCPY_RESULT */
54 typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
57 do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *dst, const CHAR *src,
60 size_t i, iters = INNER_LOOP_ITERS_LARGE / CHARBYTES;
61 timing_t start, stop, cur;
63 if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n))
65 error (0, 0, "Wrong result in function %s %p %p", impl->name,
66 CALL (impl, dst, src, n), dst);
71 if (memcmp (dst, src, (len > n ? n : len) * sizeof (CHAR)) != 0)
73 error (0, 0, "Wrong result in function %s", impl->name);
82 for (i = len; i < n; ++i)
85 error (0, 0, "Wrong result in function %s", impl->name);
92 for (i = 0; i < iters; ++i)
94 CALL (impl, dst, src, n);
98 TIMING_DIFF (cur, start, stop);
100 json_element_double (json_ctx, (double) cur / (double) iters);
104 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
105 size_t n, int max_char)
110 /* For wcsncpy: align1 and align2 here mean alignment not in bytes,
111 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */
113 if ((align1 + len) * sizeof (CHAR) >= page_size)
117 if ((align2 + len) * sizeof (CHAR) >= page_size)
120 s1 = (CHAR *) (buf1) + align1;
121 s2 = (CHAR *) (buf2) + align2;
123 for (i = 0; i < len; ++i)
124 s1[i] = 32 + 23 * i % (max_char - 32);
126 for (i = len + 1; (i + align1) * sizeof (CHAR) < page_size && i < len + 64;
128 s1[i] = 32 + 32 * i % (max_char - 32);
130 json_element_object_begin (json_ctx);
131 json_attr_uint (json_ctx, "align1", align1);
132 json_attr_uint (json_ctx, "align2", align2);
133 json_attr_uint (json_ctx, "len", len);
134 json_attr_uint (json_ctx, "n", n);
135 json_attr_uint (json_ctx, "max_char", max_char);
137 json_array_begin (json_ctx, "timings");
139 FOR_EACH_IMPL (impl, 0)
140 do_one_test (json_ctx, impl, s2, s1, len, n);
142 json_array_end (json_ctx);
143 json_element_object_end (json_ctx);
154 json_init (&json_ctx, 0, stdout);
156 json_document_begin (&json_ctx);
157 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
159 json_attr_object_begin (&json_ctx, "functions");
160 json_attr_object_begin (&json_ctx, TEST_NAME);
161 json_attr_string (&json_ctx, "bench-variant", "");
163 json_array_begin (&json_ctx, "ifuncs");
164 FOR_EACH_IMPL (impl, 0)
165 json_element_string (&json_ctx, impl->name);
166 json_array_end (&json_ctx);
168 json_array_begin (&json_ctx, "results");
170 for (i = 1; i < 8; ++i)
172 do_test (&json_ctx, i, i, 16, 16, SMALL_CHAR);
173 do_test (&json_ctx, i, i, 16, 16, BIG_CHAR);
174 do_test (&json_ctx, i, 2 * i, 16, 16, SMALL_CHAR);
175 do_test (&json_ctx, 2 * i, i, 16, 16, BIG_CHAR);
176 do_test (&json_ctx, 8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR);
177 do_test (&json_ctx, 2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR);
178 do_test (&json_ctx, 8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR);
179 do_test (&json_ctx, 2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR);
182 for (i = 1; i < 8; ++i)
184 do_test (&json_ctx, 0, 0, 4 << i, 8 << i, SMALL_CHAR);
185 do_test (&json_ctx, 0, 0, 16 << i, 8 << i, SMALL_CHAR);
186 do_test (&json_ctx, 8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR);
187 do_test (&json_ctx, 8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR);
190 for (i = 128; i < 2048; i += i)
192 for (j = i - 64; j <= i + 64; j += 32)
194 do_test (&json_ctx, 1, 0, i, j, SMALL_CHAR);
195 do_test (&json_ctx, 0, i, i, j, SMALL_CHAR);
196 do_test (&json_ctx, 0, 0, i, j, SMALL_CHAR);
197 do_test (&json_ctx, i, i, i, j, SMALL_CHAR);
198 do_test (&json_ctx, 1, 0, j, i, SMALL_CHAR);
199 do_test (&json_ctx, 0, i, j, i, SMALL_CHAR);
200 do_test (&json_ctx, 0, 0, j, i, SMALL_CHAR);
201 do_test (&json_ctx, i, i, j, i, SMALL_CHAR);
205 json_array_end (&json_ctx);
206 json_attr_object_end (&json_ctx);
207 json_attr_object_end (&json_ctx);
208 json_document_end (&json_ctx);
213 #include <support/test-driver.c>