1 /* Measure strlen functions.
2 Copyright (C) 2013-2022 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/>. */
21 # define TEST_NAME "strnlen"
23 # define TEST_NAME "wcsnlen"
24 # define generic_strnlen generic_wcsnlen
25 # define memchr_strnlen wcschr_wcsnlen
27 #include "bench-string.h"
30 #define BIG_CHAR MAX_CHAR
33 # define MIDDLE_CHAR 127
35 # define MIDDLE_CHAR 1121
38 typedef size_t (*proto_t) (const CHAR *, size_t);
39 size_t generic_strnlen (const CHAR *, size_t);
42 memchr_strnlen (const CHAR *s, size_t maxlen)
44 const CHAR *s1 = MEMCHR (s, 0, maxlen);
45 return (s1 == NULL) ? maxlen : s1 - s;
49 IMPL (memchr_strnlen, 0)
50 IMPL (generic_strnlen, 0)
53 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, size_t maxlen,
56 size_t len = CALL (impl, s, maxlen), i, iters = INNER_LOOP_ITERS_LARGE;
57 timing_t start, stop, cur;
61 error (0, 0, "Wrong result in function %s %zd %zd", impl->name, len,
68 for (i = 0; i < iters; ++i)
70 CALL (impl, s, maxlen);
74 TIMING_DIFF (cur, start, stop);
76 json_element_double (json_ctx, (double) cur / (double) iters);
80 do_test (json_ctx_t *json_ctx, size_t align, size_t len, size_t maxlen,
85 align &= getpagesize () - 1;
86 if ((align + len) * sizeof (CHAR) >= page_size)
89 CHAR *buf = (CHAR *) (buf1);
91 json_element_object_begin (json_ctx);
92 json_attr_uint (json_ctx, "len", len);
93 json_attr_uint (json_ctx, "maxlen", maxlen);
94 json_attr_uint (json_ctx, "max_char", max_char);
95 json_attr_uint (json_ctx, "align", align);
96 json_array_begin (json_ctx, "timings");
98 for (i = 0; i < len; ++i)
99 buf[align + i] = 1 + 7 * i % max_char;
100 buf[align + len] = 0;
102 FOR_EACH_IMPL (impl, 0)
103 do_one_test (json_ctx, impl, (CHAR *) (buf + align), maxlen,
106 json_array_end (json_ctx);
107 json_element_object_end (json_ctx);
118 json_init (&json_ctx, 0, stdout);
120 json_document_begin (&json_ctx);
121 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
123 json_attr_object_begin (&json_ctx, "functions");
124 json_attr_object_begin (&json_ctx, TEST_NAME);
125 json_attr_string (&json_ctx, "bench-variant", "");
127 json_array_begin (&json_ctx, "ifuncs");
128 FOR_EACH_IMPL (impl, 0)
129 json_element_string (&json_ctx, impl->name);
130 json_array_end (&json_ctx);
132 json_array_begin (&json_ctx, "results");
134 for (i = 1; i < 8; ++i)
136 do_test (&json_ctx, 0, i, i - 1, MIDDLE_CHAR);
137 do_test (&json_ctx, 0, i, i, MIDDLE_CHAR);
138 do_test (&json_ctx, 0, i, i + 1, MIDDLE_CHAR);
141 for (i = 1; i < 8; ++i)
143 do_test (&json_ctx, i, i, i - 1, MIDDLE_CHAR);
144 do_test (&json_ctx, i, i, i, MIDDLE_CHAR);
145 do_test (&json_ctx, i, i, i + 1, MIDDLE_CHAR);
148 for (i = 2; i <= 10; ++i)
150 do_test (&json_ctx, 0, 1 << i, 5000, MIDDLE_CHAR);
151 do_test (&json_ctx, 1, 1 << i, 5000, MIDDLE_CHAR);
154 for (i = 1; i < 8; ++i)
155 do_test (&json_ctx, 0, i, 5000, BIG_CHAR);
157 for (i = 1; i < 8; ++i)
158 do_test (&json_ctx, i, i, 5000, BIG_CHAR);
160 for (i = 2; i <= 10; ++i)
162 do_test (&json_ctx, 0, 1 << i, 5000, BIG_CHAR);
163 do_test (&json_ctx, 1, 1 << i, 5000, BIG_CHAR);
166 json_array_end (&json_ctx);
167 json_attr_object_end (&json_ctx);
168 json_attr_object_end (&json_ctx);
169 json_document_end (&json_ctx);
174 #include <support/test-driver.c>
176 #define libc_hidden_def(X)
179 # define STRNLEN generic_strnlen
180 # include <string/strnlen.c>
182 # define WCSNLEN generic_strnlen
183 # include <wcsmbs/wcsnlen.c>