Fix stringop-overflow warning in test-strncat.
[glibc.git] / benchtests / bench-memchr.c
blobb99995e293f30a0ddbc68722552764b804521eb4
1 /* Measure memchr functions.
2 Copyright (C) 2013-2023 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 #ifndef WIDE
20 # define SMALL_CHAR 127
21 #else
22 # define SMALL_CHAR 1273
23 #endif /* WIDE */
25 #ifndef USE_AS_MEMRCHR
26 # define TEST_MAIN
27 # ifndef WIDE
28 # define TEST_NAME "memchr"
29 # else
30 # define TEST_NAME "wmemchr"
31 # endif /* WIDE */
32 # include "bench-string.h"
34 # ifndef WIDE
35 # define SIMPLE_MEMCHR simple_memchr
36 # else
37 # define SIMPLE_MEMCHR simple_wmemchr
38 # endif /* WIDE */
40 typedef CHAR *(*proto_t) (const CHAR *, int, size_t);
41 CHAR *SIMPLE_MEMCHR (const CHAR *, int, size_t);
43 IMPL (SIMPLE_MEMCHR, 0)
44 IMPL (MEMCHR, 1)
46 CHAR *
47 SIMPLE_MEMCHR (const CHAR *s, int c, size_t n)
49 while (n--)
50 if (*s++ == (CHAR) c)
51 return (CHAR *) s - 1;
52 return NULL;
54 #endif /* !USE_AS_MEMRCHR */
56 #include "json-lib.h"
58 static void
59 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, int c,
60 size_t n)
62 size_t i, iters = INNER_LOOP_ITERS_LARGE;
63 timing_t start, stop, cur;
65 TIMING_NOW (start);
66 for (i = 0; i < iters; ++i)
68 CALL (impl, s, c, n);
70 TIMING_NOW (stop);
72 TIMING_DIFF (cur, start, stop);
74 json_element_double (json_ctx, (double) cur / (double) iters);
77 static void
78 do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len,
79 int seek_char, int invert_pos)
81 size_t i;
83 align &= getpagesize () - 1;
84 if ((align + len) * sizeof (CHAR) >= page_size)
85 return;
87 CHAR *buf = (CHAR *) (buf1);
89 for (i = 0; i < len; ++i)
91 buf[align + i] = 1 + 23 * i % SMALL_CHAR;
92 if (buf[align + i] == seek_char)
93 buf[align + i] = seek_char + 1;
95 buf[align + len] = 0;
97 if (pos < len)
99 if (invert_pos)
100 buf[align + len - pos] = seek_char;
101 else
102 buf[align + pos] = seek_char;
103 buf[align + len] = -seek_char;
105 else
107 buf[align + len] = seek_char;
110 json_element_object_begin (json_ctx);
111 json_attr_uint (json_ctx, "align", align);
112 json_attr_uint (json_ctx, "pos", pos);
113 json_attr_uint (json_ctx, "len", len);
114 json_attr_uint (json_ctx, "seek_char", seek_char);
115 json_attr_uint (json_ctx, "invert_pos", invert_pos);
117 json_array_begin (json_ctx, "timings");
119 FOR_EACH_IMPL (impl, 0)
120 do_one_test (json_ctx, impl, (CHAR *) (buf + align), seek_char, len);
122 json_array_end (json_ctx);
123 json_element_object_end (json_ctx);
127 test_main (void)
129 size_t i, j, al, al_max;
130 int repeats;
131 json_ctx_t json_ctx;
132 test_init ();
134 json_init (&json_ctx, 0, stdout);
136 json_document_begin (&json_ctx);
137 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
139 json_attr_object_begin (&json_ctx, "functions");
140 json_attr_object_begin (&json_ctx, TEST_NAME);
141 json_attr_string (&json_ctx, "bench-variant", "");
143 json_array_begin (&json_ctx, "ifuncs");
144 FOR_EACH_IMPL (impl, 0)
145 json_element_string (&json_ctx, impl->name);
146 json_array_end (&json_ctx);
148 json_array_begin (&json_ctx, "results");
150 al_max = 0;
151 #ifdef USE_AS_MEMRCHR
152 al_max = getpagesize () / 2;
153 #endif
155 for (repeats = 0; repeats < 2; ++repeats)
157 for (al = 0; al <= al_max; al += getpagesize () / 2)
159 for (i = 1; i < 8; ++i)
161 do_test (&json_ctx, al, 16 << i, 2048, 23, repeats);
162 do_test (&json_ctx, al + i, 64, 256, 23, repeats);
163 do_test (&json_ctx, al, 16 << i, 2048, 0, repeats);
164 do_test (&json_ctx, al + i, 64, 256, 0, repeats);
166 do_test (&json_ctx, al + getpagesize () - 15, 64, 256, 0,
167 repeats);
168 #ifdef USE_AS_MEMRCHR
169 /* Also test the position close to the beginning for memrchr. */
170 do_test (&json_ctx, al, i, 256, 23, repeats);
171 do_test (&json_ctx, al, i, 256, 0, repeats);
172 do_test (&json_ctx, al + i, i, 256, 23, repeats);
173 do_test (&json_ctx, al + i, i, 256, 0, repeats);
174 #endif
176 for (i = 1; i < 8; ++i)
178 do_test (&json_ctx, al + i, i << 5, 192, 23, repeats);
179 do_test (&json_ctx, al + i, i << 5, 192, 0, repeats);
180 do_test (&json_ctx, al + i, i << 5, 256, 23, repeats);
181 do_test (&json_ctx, al + i, i << 5, 256, 0, repeats);
182 do_test (&json_ctx, al + i, i << 5, 512, 23, repeats);
183 do_test (&json_ctx, al + i, i << 5, 512, 0, repeats);
185 do_test (&json_ctx, al + getpagesize () - 15, i << 5, 256, 23,
186 repeats);
190 for (i = 1; i < 32; ++i)
192 do_test (&json_ctx, 0, i, i + 1, 23, repeats);
193 do_test (&json_ctx, 0, i, i + 1, 0, repeats);
194 do_test (&json_ctx, i, i, i + 1, 23, repeats);
195 do_test (&json_ctx, i, i, i + 1, 0, repeats);
196 do_test (&json_ctx, 0, i, i - 1, 23, repeats);
197 do_test (&json_ctx, 0, i, i - 1, 0, repeats);
198 do_test (&json_ctx, i, i, i - 1, 23, repeats);
199 do_test (&json_ctx, i, i, i - 1, 0, repeats);
201 do_test (&json_ctx, getpagesize () / 2, i, i + 1, 23, repeats);
202 do_test (&json_ctx, getpagesize () / 2, i, i + 1, 0, repeats);
203 do_test (&json_ctx, getpagesize () / 2 + i, i, i + 1, 23, repeats);
204 do_test (&json_ctx, getpagesize () / 2 + i, i, i + 1, 0, repeats);
205 do_test (&json_ctx, getpagesize () / 2, i, i - 1, 23, repeats);
206 do_test (&json_ctx, getpagesize () / 2, i, i - 1, 0, repeats);
207 do_test (&json_ctx, getpagesize () / 2 + i, i, i - 1, 23, repeats);
208 do_test (&json_ctx, getpagesize () / 2 + i, i, i - 1, 0, repeats);
210 do_test (&json_ctx, getpagesize () - 15, i, i - 1, 23, repeats);
211 do_test (&json_ctx, getpagesize () - 15, i, i - 1, 0, repeats);
213 do_test (&json_ctx, getpagesize () - 15, i, i + 1, 23, repeats);
214 do_test (&json_ctx, getpagesize () - 15, i, i + 1, 0, repeats);
216 #ifdef USE_AS_MEMRCHR
217 do_test (&json_ctx, 0, 1, i + 1, 23, repeats);
218 do_test (&json_ctx, 0, 2, i + 1, 0, repeats);
219 #endif
221 for (al = 0; al <= al_max; al += getpagesize () / 2)
223 for (i = (16 / sizeof (CHAR)); i <= (8192 / sizeof (CHAR)); i += i)
225 for (j = 0; j <= (384 / sizeof (CHAR));
226 j += (32 / sizeof (CHAR)))
228 do_test (&json_ctx, al, i + j, i, 23, repeats);
229 do_test (&json_ctx, al, i, i + j, 23, repeats);
230 if (j < i)
232 do_test (&json_ctx, al, i - j, i, 23, repeats);
233 do_test (&json_ctx, al, i, i - j, 23, repeats);
239 #ifndef USE_AS_MEMRCHR
240 break;
241 #endif
244 json_array_end (&json_ctx);
245 json_attr_object_end (&json_ctx);
246 json_attr_object_end (&json_ctx);
247 json_document_end (&json_ctx);
249 return ret;
252 #include <support/test-driver.c>