1/* Measure strlen functions.
2 Copyright (C) 2013-2024 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 <https://www.gnu.org/licenses/>. */
18
19#define TEST_MAIN
20#ifndef WIDE
21# define TEST_NAME "strnlen"
22#else
23# define TEST_NAME "wcsnlen"
24# define generic_strnlen generic_wcsnlen
25#endif /* WIDE */
26#include "bench-string.h"
27#include "json-lib.h"
28
29#define BIG_CHAR MAX_CHAR
30
31#ifndef WIDE
32# define MIDDLE_CHAR 127
33#else
34# define MIDDLE_CHAR 1121
35#endif /* WIDE */
36
37typedef size_t (*proto_t) (const CHAR *, size_t);
38size_t generic_strnlen (const CHAR *, size_t);
39
40IMPL (STRNLEN, 1)
41IMPL (generic_strnlen, 0)
42
43static void
44do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, size_t maxlen,
45 size_t exp_len)
46{
47 size_t len = CALL (impl, s, maxlen), i, iters = INNER_LOOP_ITERS;
48 timing_t start, stop, cur;
49
50 if (len != exp_len)
51 {
52 error (status: 0, errnum: 0, format: "Wrong result in function %s %zd %zd", impl->name, len,
53 exp_len);
54 ret = 1;
55 return;
56 }
57 /* Warmup. */
58 for (i = 0; i < iters / 16; ++i)
59 {
60 CALL (impl, s, maxlen);
61 }
62
63 TIMING_NOW (start);
64 for (i = 0; i < iters; ++i)
65 {
66 CALL (impl, s, maxlen);
67 }
68 TIMING_NOW (stop);
69
70 TIMING_DIFF (cur, start, stop);
71
72 json_element_double (ctx: json_ctx, d: (double) cur / (double) iters);
73}
74
75static void
76do_test (json_ctx_t *json_ctx, size_t align, size_t len, size_t maxlen,
77 int max_char)
78{
79 size_t i;
80
81 align &= getpagesize () - 1;
82 if ((align + len) * sizeof (CHAR) >= page_size)
83 return;
84
85 CHAR *buf = (CHAR *) (buf1);
86
87 json_element_object_begin (ctx: json_ctx);
88 json_attr_uint (ctx: json_ctx, name: "len", d: len);
89 json_attr_uint (ctx: json_ctx, name: "maxlen", d: maxlen);
90 json_attr_uint (ctx: json_ctx, name: "max_char", d: max_char);
91 json_attr_uint (ctx: json_ctx, name: "align", d: align);
92 json_array_begin (ctx: json_ctx, name: "timings");
93
94 for (i = 0; i < len; ++i)
95 buf[align + i] = 1 + 7 * i % max_char;
96 buf[align + len] = 0;
97
98 FOR_EACH_IMPL (impl, 0)
99 do_one_test (json_ctx, impl, s: (CHAR *) (buf + align), maxlen,
100 MIN (len, maxlen));
101
102 json_array_end (ctx: json_ctx);
103 json_element_object_end (ctx: json_ctx);
104}
105
106int
107test_main (void)
108{
109 size_t i, j;
110 json_ctx_t json_ctx;
111
112 test_init ();
113
114 json_init (ctx: &json_ctx, indent_level: 0, stdout);
115
116 json_document_begin (ctx: &json_ctx);
117 json_attr_string (ctx: &json_ctx, name: "timing_type", TIMING_TYPE);
118
119 json_attr_object_begin (ctx: &json_ctx, name: "functions");
120 json_attr_object_begin (ctx: &json_ctx, TEST_NAME);
121 json_attr_string (ctx: &json_ctx, name: "bench-variant", s: "");
122
123 json_array_begin (ctx: &json_ctx, name: "ifuncs");
124 FOR_EACH_IMPL (impl, 0)
125 json_element_string (ctx: &json_ctx, s: impl->name);
126 json_array_end (ctx: &json_ctx);
127
128 json_array_begin (ctx: &json_ctx, name: "results");
129
130 for (i = 0; i <= 1; ++i)
131 {
132 do_test (json_ctx: &json_ctx, align: i, len: 1, maxlen: 128, MIDDLE_CHAR);
133 do_test (json_ctx: &json_ctx, align: i, len: 128, maxlen: 1, MIDDLE_CHAR);
134 do_test (json_ctx: &json_ctx, align: i, len: 1, maxlen: 2, MIDDLE_CHAR);
135 do_test (json_ctx: &json_ctx, align: i, len: 2, maxlen: 1, MIDDLE_CHAR);
136
137 do_test (json_ctx: &json_ctx, align: 32 + i, len: 1, maxlen: 128, MIDDLE_CHAR);
138 do_test (json_ctx: &json_ctx, align: 32 + i, len: 128, maxlen: 1, MIDDLE_CHAR);
139 do_test (json_ctx: &json_ctx, align: 32 + i, len: 1, maxlen: 2, MIDDLE_CHAR);
140 do_test (json_ctx: &json_ctx, align: 32 + i, len: 2, maxlen: 1, MIDDLE_CHAR);
141 }
142
143 for (i = 1; i < 8; ++i)
144 {
145 do_test (json_ctx: &json_ctx, align: 0, len: i, maxlen: i - 1, MIDDLE_CHAR);
146 do_test (json_ctx: &json_ctx, align: 0, len: i, maxlen: i, MIDDLE_CHAR);
147 do_test (json_ctx: &json_ctx, align: 0, len: i, maxlen: i + 1, MIDDLE_CHAR);
148 }
149
150 for (i = 1; i < 8; ++i)
151 {
152 do_test (json_ctx: &json_ctx, align: i, len: i, maxlen: i - 1, MIDDLE_CHAR);
153 do_test (json_ctx: &json_ctx, align: i, len: i, maxlen: i, MIDDLE_CHAR);
154 do_test (json_ctx: &json_ctx, align: i, len: i, maxlen: i + 1, MIDDLE_CHAR);
155 }
156
157 for (i = 2; i <= 10; ++i)
158 {
159 do_test (json_ctx: &json_ctx, align: 0, len: 1 << i, maxlen: 5000, MIDDLE_CHAR);
160 do_test (json_ctx: &json_ctx, align: 1, len: 1 << i, maxlen: 5000, MIDDLE_CHAR);
161 do_test (json_ctx: &json_ctx, align: 0, len: 5000, maxlen: 1 << i, MIDDLE_CHAR);
162 do_test (json_ctx: &json_ctx, align: 1, len: 5000, maxlen: 1 << i, MIDDLE_CHAR);
163 }
164
165 for (i = 1; i < 8; ++i)
166 {
167 do_test (json_ctx: &json_ctx, align: 0, len: i, maxlen: 5000, BIG_CHAR);
168 do_test (json_ctx: &json_ctx, align: 0, len: 5000, maxlen: i, BIG_CHAR);
169 }
170
171 for (i = 1; i < 8; ++i)
172 {
173 do_test (json_ctx: &json_ctx, align: i, len: i, maxlen: 5000, BIG_CHAR);
174 do_test (json_ctx: &json_ctx, align: i, len: 5000, maxlen: i, BIG_CHAR);
175 }
176
177 for (i = 2; i <= 10; ++i)
178 {
179 do_test (json_ctx: &json_ctx, align: 0, len: 1 << i, maxlen: 5000, BIG_CHAR);
180 do_test (json_ctx: &json_ctx, align: 1, len: 1 << i, maxlen: 5000, BIG_CHAR);
181 do_test (json_ctx: &json_ctx, align: 0, len: 5000, maxlen: 1 << i, BIG_CHAR);
182 do_test (json_ctx: &json_ctx, align: 1, len: 5000, maxlen: 1 << i, BIG_CHAR);
183 }
184
185 for (i = (16 / sizeof (CHAR)); i <= (8192 / sizeof (CHAR)); i += i)
186 {
187 for (j = 0; j <= (704 / sizeof (CHAR)); j += (32 / sizeof (CHAR)))
188 {
189 do_test (json_ctx: &json_ctx, align: 0, len: i + j, maxlen: i, BIG_CHAR);
190 do_test (json_ctx: &json_ctx, align: 64, len: i + j, maxlen: i, BIG_CHAR);
191
192 do_test (json_ctx: &json_ctx, align: 0, len: i, maxlen: i + j, BIG_CHAR);
193 do_test (json_ctx: &json_ctx, align: 64, len: i, maxlen: i + j, BIG_CHAR);
194
195 if (j < i)
196 {
197 do_test (json_ctx: &json_ctx, align: 0, len: i - j, maxlen: i, BIG_CHAR);
198 do_test (json_ctx: &json_ctx, align: 64, len: i - j, maxlen: i, BIG_CHAR);
199
200 do_test (json_ctx: &json_ctx, align: 0, len: i, maxlen: i - j, BIG_CHAR);
201 do_test (json_ctx: &json_ctx, align: 64, len: i, maxlen: i - j, BIG_CHAR);
202 }
203 }
204 }
205
206 json_array_end (ctx: &json_ctx);
207 json_attr_object_end (ctx: &json_ctx);
208 json_attr_object_end (ctx: &json_ctx);
209 json_document_end (ctx: &json_ctx);
210
211 return ret;
212}
213
214#include <support/test-driver.c>
215
216#define libc_hidden_def(X)
217#ifndef WIDE
218# undef STRNLEN
219# define STRNLEN generic_strnlen
220# include <string/strnlen.c>
221#else
222# define WCSNLEN generic_strnlen
223# include <wcsmbs/wcsnlen.c>
224#endif
225

source code of glibc/benchtests/bench-strnlen.c