1 /* Measure STRCHR functions.
2 Copyright (C) 2013-2019 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 <http://www.gnu.org/licenses/>. */
21 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
24 # define TEST_NAME "strchr"
25 # endif /* !USE_FOR_STRCHRNUL */
27 # ifdef USE_FOR_STRCHRNUL
28 # define TEST_NAME "wcschrnul"
30 # define TEST_NAME "wcschr"
31 # endif /* !USE_FOR_STRCHRNUL */
33 #include "bench-string.h"
35 #define BIG_CHAR MAX_CHAR
38 # ifdef USE_FOR_STRCHRNUL
40 # define STRCHR strchrnul
41 # define simple_STRCHR simple_STRCHRNUL
42 # endif /* !USE_FOR_STRCHRNUL */
43 # define MIDDLE_CHAR 127
44 # define SMALL_CHAR 23
46 # ifdef USE_FOR_STRCHRNUL
48 # define STRCHR wcschrnul
49 # define simple_STRCHR simple_WCSCHRNUL
50 # endif /* !USE_FOR_STRCHRNUL */
51 # define MIDDLE_CHAR 1121
52 # define SMALL_CHAR 851
55 #ifdef USE_FOR_STRCHRNUL
56 # define NULLRET(endptr) endptr
58 # define NULLRET(endptr) NULL
59 #endif /* !USE_FOR_STRCHRNUL */
62 typedef CHAR *(*proto_t) (const CHAR *, int);
65 simple_STRCHR (const CHAR *s, int c)
67 for (; *s != (CHAR) c; ++s)
69 return NULLRET ((CHAR *) s);
73 IMPL (simple_STRCHR, 0)
77 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
79 size_t i, iters = INNER_LOOP_ITERS;
80 timing_t start, stop, cur;
83 for (i = 0; i < iters; ++i)
89 TIMING_DIFF (cur, start, stop);
91 TIMING_PRINT_MEAN ((double) cur, (double) iters);
95 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
96 /* For wcschr: align here means align not in bytes,
97 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
98 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
102 CHAR *buf = (CHAR *) buf1;
104 if ((align + len) * sizeof (CHAR) >= page_size)
107 for (i = 0; i < len; ++i)
109 buf[align + i] = 32 + 23 * i % max_char;
110 if (buf[align + i] == seek_char)
111 buf[align + i] = seek_char + 1;
112 else if (buf[align + i] == 0)
115 buf[align + len] = 0;
119 buf[align + pos] = seek_char;
120 result = buf + align + pos;
122 else if (seek_char == 0)
123 result = buf + align + len;
125 result = NULLRET (buf + align + len);
127 printf ("Length %4zd, alignment in bytes %2zd:",
128 pos, align * sizeof (CHAR));
130 FOR_EACH_IMPL (impl, 0)
131 do_one_test (impl, buf + align, seek_char, result);
144 FOR_EACH_IMPL (impl, 0)
145 printf ("\t%s", impl->name);
148 for (i = 1; i < 8; ++i)
150 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
151 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
154 for (i = 1; i < 8; ++i)
156 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
157 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
160 for (i = 0; i < 32; ++i)
162 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
163 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
166 for (i = 1; i < 8; ++i)
168 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
169 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
172 for (i = 1; i < 8; ++i)
174 do_test (i, 64, 256, 0, MIDDLE_CHAR);
175 do_test (i, 64, 256, 0, BIG_CHAR);
178 for (i = 0; i < 32; ++i)
180 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
181 do_test (0, i, i + 1, 0, BIG_CHAR);
187 #include <support/test-driver.c>