1 /* Measure memchr 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/>. */
22 # define SMALL_CHAR 127
24 # define SMALL_CHAR 1273
27 #ifndef USE_AS_MEMRCHR
30 # define TEST_NAME "memchr"
32 # define TEST_NAME "wmemchr"
34 # include "bench-string.h"
36 typedef void *(*proto_t) (const void *, int, size_t);
39 generic_memchr (const void *, int, size_t);
44 IMPL (generic_memchr, 0)
47 #endif /* !USE_AS_MEMRCHR */
51 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, int c,
54 size_t i, iters = INNER_LOOP_ITERS8 / 2;
55 timing_t start, stop, cur;
58 for (i = 0; i < iters; ++i)
64 TIMING_DIFF (cur, start, stop);
66 json_element_double (json_ctx, (double) cur / (double) iters);
70 do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len,
71 int seek_char, int invert_pos)
75 align &= getpagesize () - 1;
76 if ((align + len) * sizeof (CHAR) >= page_size)
79 CHAR *buf = (CHAR *) (buf1);
81 for (i = 0; i < len; ++i)
83 buf[align + i] = 1 + 23 * i % SMALL_CHAR;
84 if (buf[align + i] == seek_char)
85 buf[align + i] = seek_char + 1;
92 buf[align + len - pos] = seek_char;
94 buf[align + pos] = seek_char;
95 buf[align + len] = -seek_char;
99 buf[align + len] = seek_char;
102 json_element_object_begin (json_ctx);
103 json_attr_uint (json_ctx, "align", align);
104 json_attr_uint (json_ctx, "pos", pos);
105 json_attr_uint (json_ctx, "len", len);
106 json_attr_uint (json_ctx, "seek_char", seek_char);
107 json_attr_uint (json_ctx, "invert_pos", invert_pos);
109 json_array_begin (json_ctx, "timings");
111 FOR_EACH_IMPL (impl, 0)
112 do_one_test (json_ctx, impl, (CHAR *) (buf + align), seek_char, len);
114 json_array_end (json_ctx);
115 json_element_object_end (json_ctx);
121 size_t i, j, al, al_max;
126 json_init (&json_ctx, 0, stdout);
128 json_document_begin (&json_ctx);
129 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
131 json_attr_object_begin (&json_ctx, "functions");
132 json_attr_object_begin (&json_ctx, TEST_NAME);
133 json_attr_string (&json_ctx, "bench-variant", "");
135 json_array_begin (&json_ctx, "ifuncs");
136 FOR_EACH_IMPL (impl, 0)
137 json_element_string (&json_ctx, impl->name);
138 json_array_end (&json_ctx);
140 json_array_begin (&json_ctx, "results");
143 #ifdef USE_AS_MEMRCHR
144 al_max = getpagesize () / 2;
147 for (repeats = 0; repeats < 2; ++repeats)
149 for (al = 0; al <= al_max; al += getpagesize () / 2)
151 for (i = 1; i < 8; ++i)
153 do_test (&json_ctx, al, 16 << i, 2048, 23, repeats);
154 do_test (&json_ctx, al + i, 64, 256, 23, repeats);
155 do_test (&json_ctx, al, 16 << i, 2048, 0, repeats);
156 do_test (&json_ctx, al + i, 64, 256, 0, repeats);
158 do_test (&json_ctx, al + getpagesize () - 15, 64, 256, 0,
160 #ifdef USE_AS_MEMRCHR
161 /* Also test the position close to the beginning for memrchr. */
162 do_test (&json_ctx, al, i, 256, 23, repeats);
163 do_test (&json_ctx, al, i, 256, 0, repeats);
164 do_test (&json_ctx, al + i, i, 256, 23, repeats);
165 do_test (&json_ctx, al + i, i, 256, 0, repeats);
168 for (i = 1; i < 8; ++i)
170 do_test (&json_ctx, al + i, i << 5, 192, 23, repeats);
171 do_test (&json_ctx, al + i, i << 5, 192, 0, repeats);
172 do_test (&json_ctx, al + i, i << 5, 256, 23, repeats);
173 do_test (&json_ctx, al + i, i << 5, 256, 0, repeats);
174 do_test (&json_ctx, al + i, i << 5, 512, 23, repeats);
175 do_test (&json_ctx, al + i, i << 5, 512, 0, repeats);
177 do_test (&json_ctx, al + getpagesize () - 15, i << 5, 256, 23,
182 for (i = 1; i < 32; ++i)
184 do_test (&json_ctx, 0, i, i + 1, 23, repeats);
185 do_test (&json_ctx, 0, i, i + 1, 0, repeats);
186 do_test (&json_ctx, i, i, i + 1, 23, repeats);
187 do_test (&json_ctx, i, i, i + 1, 0, repeats);
188 do_test (&json_ctx, 0, i, i - 1, 23, repeats);
189 do_test (&json_ctx, 0, i, i - 1, 0, repeats);
190 do_test (&json_ctx, i, i, i - 1, 23, repeats);
191 do_test (&json_ctx, i, i, i - 1, 0, repeats);
193 do_test (&json_ctx, getpagesize () / 2, i, i + 1, 23, repeats);
194 do_test (&json_ctx, getpagesize () / 2, i, i + 1, 0, repeats);
195 do_test (&json_ctx, getpagesize () / 2 + i, i, i + 1, 23, repeats);
196 do_test (&json_ctx, getpagesize () / 2 + i, i, i + 1, 0, repeats);
197 do_test (&json_ctx, getpagesize () / 2, i, i - 1, 23, repeats);
198 do_test (&json_ctx, getpagesize () / 2, i, i - 1, 0, repeats);
199 do_test (&json_ctx, getpagesize () / 2 + i, i, i - 1, 23, repeats);
200 do_test (&json_ctx, getpagesize () / 2 + i, i, i - 1, 0, repeats);
202 do_test (&json_ctx, getpagesize () - 15, i, i - 1, 23, repeats);
203 do_test (&json_ctx, getpagesize () - 15, i, i - 1, 0, repeats);
205 do_test (&json_ctx, getpagesize () - 15, i, i + 1, 23, repeats);
206 do_test (&json_ctx, getpagesize () - 15, i, i + 1, 0, repeats);
208 #ifdef USE_AS_MEMRCHR
209 do_test (&json_ctx, 0, 1, i + 1, 23, repeats);
210 do_test (&json_ctx, 0, 2, i + 1, 0, repeats);
213 for (al = 0; al <= al_max; al += getpagesize () / 2)
215 for (i = (16 / sizeof (CHAR)); i <= (8192 / sizeof (CHAR)); i += i)
217 for (j = 0; j <= (384 / sizeof (CHAR));
218 j += (32 / sizeof (CHAR)))
220 do_test (&json_ctx, al, i + j, i, 23, repeats);
221 do_test (&json_ctx, al, i, i + j, 23, repeats);
224 do_test (&json_ctx, al, i - j, i, 23, repeats);
225 do_test (&json_ctx, al, i, i - j, 23, repeats);
231 #ifndef USE_AS_MEMRCHR
236 json_array_end (&json_ctx);
237 json_attr_object_end (&json_ctx);
238 json_attr_object_end (&json_ctx);
239 json_document_end (&json_ctx);
244 #include <support/test-driver.c>
247 # ifndef USE_AS_MEMRCHR
249 # define MEMCHR generic_memchr
250 # include <string/memchr.c>
253 # define MEMRCHR generic_memrchr
254 # include <string/memrchr.c>