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