1 /* Measure STRCHR 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/>. */
21 # define TEST_NAME "wcsrchr"
23 # define TEST_NAME "strrchr"
25 #include "bench-string.h"
28 #define BIG_CHAR MAX_CHAR
31 # define SMALL_CHAR 1273
33 # define SMALL_CHAR 127
36 generic_strrchr (const char *, int);
38 IMPL (generic_strrchr, 0)
42 typedef CHAR *(*proto_t) (const CHAR *, int);
47 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, int c,
50 CHAR *res = CALL (impl, s, c);
51 size_t i, iters = INNER_LOOP_ITERS8;
52 timing_t start, stop, cur;
56 error (0, 0, "Wrong result in function %s %p %p", impl->name, res,
63 for (i = 0; i < iters; ++i)
68 TIMING_DIFF (cur, start, stop);
70 json_element_double (json_ctx, (double) cur / (double) iters);
74 do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len,
75 int seek_char, int max_char, size_t freq)
76 /* For wcsrchr: align here means align not in bytes,
77 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
78 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
81 size_t pos_chunk_sz = freq ? (pos / freq) : pos;
82 size_t last_pos = len;
84 CHAR *buf = (CHAR *) buf1;
86 align &= (getpagesize () - 1);
87 if ((align + len) * sizeof (CHAR) >= page_size)
90 for (i = 0; i < len; ++i)
92 buf[align + i] = (random () * random ()) & max_char;
94 buf[align + i] = (random () * random ()) & max_char;
97 if ((i > pos || pos >= len) && buf[align + i] == seek_char)
98 buf[align + i] = seek_char + 10 + (random () & 15);
101 if (pos_chunk_sz == 0 && pos)
104 for (i = pos_chunk_sz; i < pos && i < len; i += pos_chunk_sz)
106 buf[align + i] = seek_char;
110 buf[align + len] = 0;
114 buf[align + pos] = seek_char;
115 result = (CHAR *) (buf + align + pos);
117 else if (last_pos < len)
118 result = (CHAR *) (buf + align + last_pos);
119 else if (seek_char == 0)
120 result = (CHAR *) (buf + align + len);
124 json_element_object_begin (json_ctx);
125 json_attr_uint (json_ctx, "len", len);
126 json_attr_uint (json_ctx, "pos", pos);
127 json_attr_uint (json_ctx, "align", align);
128 json_attr_uint (json_ctx, "freq", freq);
129 json_attr_uint (json_ctx, "seek", seek_char);
130 json_attr_uint (json_ctx, "max_char", max_char);
131 json_array_begin (json_ctx, "timings");
133 FOR_EACH_IMPL (impl, 0)
134 do_one_test (json_ctx, impl, (CHAR *) (buf + align), seek_char, result);
136 json_array_end (json_ctx);
137 json_element_object_end (json_ctx);
148 json_init (&json_ctx, 0, stdout);
150 json_document_begin (&json_ctx);
151 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
153 json_attr_object_begin (&json_ctx, "functions");
154 json_attr_object_begin (&json_ctx, TEST_NAME);
155 json_attr_string (&json_ctx, "bench-variant", "");
157 json_array_begin (&json_ctx, "ifuncs");
158 FOR_EACH_IMPL (impl, 0)
159 json_element_string (&json_ctx, impl->name);
160 json_array_end (&json_ctx);
162 json_array_begin (&json_ctx, "results");
164 for (seek = 0; seek <= 23; seek += 23)
166 for (j = 1; j <= 256; j = (j * 4))
168 for (i = 1; i < 9; ++i)
170 do_test (&json_ctx, 0, 16 << i, 2048, seek, SMALL_CHAR, j);
171 do_test (&json_ctx, i, 16 << i, 2048, seek, SMALL_CHAR, j);
174 for (i = 1; i < 8; ++i)
176 do_test (&json_ctx, i, 64, 256, seek, SMALL_CHAR, j);
177 do_test (&json_ctx, i, 64, 256, seek, BIG_CHAR, j);
179 do_test (&json_ctx, i * 15, 64, 256, seek, SMALL_CHAR, j);
180 do_test (&json_ctx, i * 15, 64, 256, seek, BIG_CHAR, j);
183 for (i = 0; i < 32; ++i)
185 do_test (&json_ctx, 0, i, i + 1, seek, SMALL_CHAR, j);
186 do_test (&json_ctx, 0, i, i + 1, seek, BIG_CHAR, j);
187 do_test (&json_ctx, getpagesize () - i / 2 - 1, i, i + 1, seek,
191 for (i = (16 / sizeof (CHAR)); i <= (288 / sizeof (CHAR)); i += 32)
193 do_test (&json_ctx, 0, i - 16, i, seek, SMALL_CHAR, j);
194 do_test (&json_ctx, 0, i, i + 16, seek, SMALL_CHAR, j);
197 for (i = (16 / sizeof (CHAR)); i <= (2048 / sizeof (CHAR)); i += i)
199 for (k = 0; k <= (288 / sizeof (CHAR));
200 k += (48 / sizeof (CHAR)))
202 do_test (&json_ctx, 0, k, i, seek, SMALL_CHAR, j);
203 do_test (&json_ctx, 0, i, i + k, seek, SMALL_CHAR, j);
207 do_test (&json_ctx, 0, i - k, i, seek, SMALL_CHAR, j);
208 do_test (&json_ctx, 0, k, i - k, seek, SMALL_CHAR, j);
209 do_test (&json_ctx, 0, i, i - k, seek, SMALL_CHAR, j);
221 json_array_end (&json_ctx);
222 json_attr_object_end (&json_ctx);
223 json_attr_object_end (&json_ctx);
224 json_document_end (&json_ctx);
229 #include <support/test-driver.c>
231 #define weak_alias(X,Y)
232 #define libc_hidden_builtin_def(X)
235 # define STRRCHR generic_strrchr
236 # define __memrchr memrchr
237 # include <string/strrchr.c>