AArch64: Improve strrchr
[glibc.git] / benchtests / bench-strrchr.c
bloba33a657a275416b0cb0c42ba2689e3685c1b62aa
1 /* Measure STRCHR 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 #define TEST_MAIN
20 #ifdef WIDE
21 # define TEST_NAME "wcsrchr"
22 #else
23 # define TEST_NAME "strrchr"
24 #endif
25 #include "bench-string.h"
26 #include "json-lib.h"
28 #define BIG_CHAR MAX_CHAR
30 #ifdef WIDE
31 # define SIMPLE_STRRCHR simple_wcsrchr
32 # define SMALL_CHAR 1273
33 #else
34 # define SIMPLE_STRRCHR simple_strrchr
35 # define SMALL_CHAR 127
36 #endif
38 typedef CHAR *(*proto_t) (const CHAR *, int);
39 CHAR *SIMPLE_STRRCHR (const CHAR *, int);
41 IMPL (SIMPLE_STRRCHR, 0)
42 IMPL (STRRCHR, 1)
44 CHAR *
45 SIMPLE_STRRCHR (const CHAR *s, int c)
47 const CHAR *ret = NULL;
49 for (; *s != '\0'; ++s)
50 if (*s == (CHAR) c)
51 ret = s;
53 return (CHAR *) (c == '\0' ? s : ret);
56 static void
57 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, int c,
58 CHAR *exp_res)
60 CHAR *res = CALL (impl, s, c);
61 size_t i, iters = INNER_LOOP_ITERS8;
62 timing_t start, stop, cur;
64 if (res != exp_res)
66 error (0, 0, "Wrong result in function %s %p %p", impl->name, res,
67 exp_res);
68 ret = 1;
69 return;
72 TIMING_NOW (start);
73 for (i = 0; i < iters; ++i)
75 CALL (impl, s, c);
77 TIMING_NOW (stop);
78 TIMING_DIFF (cur, start, stop);
80 json_element_double (json_ctx, (double) cur / (double) iters);
83 static void
84 do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len,
85 int seek_char, int max_char, size_t freq)
86 /* For wcsrchr: align here means align not in bytes,
87 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
88 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
90 size_t i;
91 size_t pos_chunk_sz = freq ? (pos / freq) : pos;
92 size_t last_pos = len;
93 CHAR *result;
94 CHAR *buf = (CHAR *) buf1;
96 align &= (getpagesize () - 1);
97 if ((align + len) * sizeof (CHAR) >= page_size)
98 return;
100 for (i = 0; i < len; ++i)
102 buf[align + i] = (random () * random ()) & max_char;
103 if (!buf[align + i])
104 buf[align + i] = (random () * random ()) & max_char;
105 if (!buf[align + i])
106 buf[align + i] = 1;
107 if ((i > pos || pos >= len) && buf[align + i] == seek_char)
108 buf[align + i] = seek_char + 10 + (random () & 15);
111 if (pos_chunk_sz == 0 && pos)
112 pos_chunk_sz = 1;
114 for (i = pos_chunk_sz; i < pos && i < len; i += pos_chunk_sz)
116 buf[align + i] = seek_char;
117 last_pos = i;
120 buf[align + len] = 0;
122 if (pos < len)
124 buf[align + pos] = seek_char;
125 result = (CHAR *) (buf + align + pos);
127 else if (last_pos < len)
128 result = (CHAR *) (buf + align + last_pos);
129 else if (seek_char == 0)
130 result = (CHAR *) (buf + align + len);
131 else
132 result = NULL;
134 json_element_object_begin (json_ctx);
135 json_attr_uint (json_ctx, "len", len);
136 json_attr_uint (json_ctx, "pos", pos);
137 json_attr_uint (json_ctx, "align", align);
138 json_attr_uint (json_ctx, "freq", freq);
139 json_attr_uint (json_ctx, "seek", seek_char);
140 json_attr_uint (json_ctx, "max_char", max_char);
141 json_array_begin (json_ctx, "timings");
143 FOR_EACH_IMPL (impl, 0)
144 do_one_test (json_ctx, impl, (CHAR *) (buf + align), seek_char, result);
146 json_array_end (json_ctx);
147 json_element_object_end (json_ctx);
151 test_main (void)
153 json_ctx_t json_ctx;
154 size_t i, j, k;
155 int seek;
157 test_init ();
158 json_init (&json_ctx, 0, stdout);
160 json_document_begin (&json_ctx);
161 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
163 json_attr_object_begin (&json_ctx, "functions");
164 json_attr_object_begin (&json_ctx, TEST_NAME);
165 json_attr_string (&json_ctx, "bench-variant", "");
167 json_array_begin (&json_ctx, "ifuncs");
168 FOR_EACH_IMPL (impl, 0)
169 json_element_string (&json_ctx, impl->name);
170 json_array_end (&json_ctx);
172 json_array_begin (&json_ctx, "results");
174 for (seek = 0; seek <= 23; seek += 23)
176 for (j = 1; j <= 256; j = (j * 4))
178 for (i = 1; i < 9; ++i)
180 do_test (&json_ctx, 0, 16 << i, 2048, seek, SMALL_CHAR, j);
181 do_test (&json_ctx, i, 16 << i, 2048, seek, SMALL_CHAR, j);
184 for (i = 1; i < 8; ++i)
186 do_test (&json_ctx, i, 64, 256, seek, SMALL_CHAR, j);
187 do_test (&json_ctx, i, 64, 256, seek, BIG_CHAR, j);
189 do_test (&json_ctx, i * 15, 64, 256, seek, SMALL_CHAR, j);
190 do_test (&json_ctx, i * 15, 64, 256, seek, BIG_CHAR, j);
193 for (i = 0; i < 32; ++i)
195 do_test (&json_ctx, 0, i, i + 1, seek, SMALL_CHAR, j);
196 do_test (&json_ctx, 0, i, i + 1, seek, BIG_CHAR, j);
197 do_test (&json_ctx, getpagesize () - i / 2 - 1, i, i + 1, seek,
198 SMALL_CHAR, j);
201 for (i = (16 / sizeof (CHAR)); i <= (288 / sizeof (CHAR)); i += 32)
203 do_test (&json_ctx, 0, i - 16, i, seek, SMALL_CHAR, j);
204 do_test (&json_ctx, 0, i, i + 16, seek, SMALL_CHAR, j);
207 for (i = (16 / sizeof (CHAR)); i <= (2048 / sizeof (CHAR)); i += i)
209 for (k = 0; k <= (288 / sizeof (CHAR));
210 k += (48 / sizeof (CHAR)))
212 do_test (&json_ctx, 0, k, i, seek, SMALL_CHAR, j);
213 do_test (&json_ctx, 0, i, i + k, seek, SMALL_CHAR, j);
215 if (k < i)
217 do_test (&json_ctx, 0, i - k, i, seek, SMALL_CHAR, j);
218 do_test (&json_ctx, 0, k, i - k, seek, SMALL_CHAR, j);
219 do_test (&json_ctx, 0, i, i - k, seek, SMALL_CHAR, j);
224 if (seek == 0)
226 break;
231 json_array_end (&json_ctx);
232 json_attr_object_end (&json_ctx);
233 json_attr_object_end (&json_ctx);
234 json_document_end (&json_ctx);
236 return ret;
239 #include <support/test-driver.c>