Update install.texi, and regenerate INSTALL.
[glibc.git] / benchtests / bench-strchr.c
blob4ce2369d9b236a2843c44b74a7024a08d326860f
1 /* Measure STRCHR functions.
2 Copyright (C) 2013-2021 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 #ifndef WIDE
21 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
23 # else
24 # define TEST_NAME "strchr"
25 # endif /* !USE_FOR_STRCHRNUL */
26 #else
27 # ifdef USE_FOR_STRCHRNUL
28 # define TEST_NAME "wcschrnul"
29 # else
30 # define TEST_NAME "wcschr"
31 # endif /* !USE_FOR_STRCHRNUL */
32 #endif /* WIDE */
33 #include "bench-string.h"
35 #define BIG_CHAR MAX_CHAR
37 #ifndef WIDE
38 # ifdef USE_FOR_STRCHRNUL
39 # undef STRCHR
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
45 #else
46 # ifdef USE_FOR_STRCHRNUL
47 # undef STRCHR
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
53 #endif /* WIDE */
55 #ifdef USE_FOR_STRCHRNUL
56 # define NULLRET(endptr) endptr
57 #else
58 # define NULLRET(endptr) NULL
59 #endif /* !USE_FOR_STRCHRNUL */
62 typedef CHAR *(*proto_t) (const CHAR *, int);
64 CHAR *
65 simple_STRCHR (const CHAR *s, int c)
67 for (; *s != (CHAR) c; ++s)
68 if (*s == '\0')
69 return NULLRET ((CHAR *) s);
70 return (CHAR *) s;
73 IMPL (simple_STRCHR, 0)
74 IMPL (STRCHR, 1)
76 static void
77 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
79 size_t i, iters = INNER_LOOP_ITERS_LARGE;
80 timing_t start, stop, cur;
82 TIMING_NOW (start);
83 for (i = 0; i < iters; ++i)
85 CALL (impl, s, c);
87 TIMING_NOW (stop);
89 TIMING_DIFF (cur, start, stop);
91 TIMING_PRINT_MEAN ((double) cur, (double) iters);
94 static void
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. */
100 size_t i;
101 CHAR *result;
102 CHAR *buf = (CHAR *) buf1;
103 align &= 127;
104 if ((align + len) * sizeof (CHAR) >= page_size)
105 return;
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)
113 buf[align + i] = 1;
115 buf[align + len] = 0;
117 if (pos < len)
119 buf[align + pos] = seek_char;
120 result = buf + align + pos;
122 else if (seek_char == 0)
123 result = buf + align + len;
124 else
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);
133 putchar ('\n');
137 test_main (void)
139 size_t i;
141 test_init ();
143 printf ("%20s", "");
144 FOR_EACH_IMPL (impl, 0)
145 printf ("\t%s", impl->name);
146 putchar ('\n');
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 (0, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
157 do_test (i, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
160 for (i = 1; i < 8; ++i)
162 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
163 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
166 for (i = 0; i < 8; ++i)
168 do_test (16 * i, 256, 512, SMALL_CHAR, MIDDLE_CHAR);
169 do_test (16 * i, 256, 512, SMALL_CHAR, BIG_CHAR);
172 for (i = 0; i < 32; ++i)
174 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
175 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
178 for (i = 1; i < 8; ++i)
180 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
181 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
184 for (i = 1; i < 8; ++i)
186 do_test (0, 16 << i, 4096, 0, MIDDLE_CHAR);
187 do_test (i, 16 << i, 4096, 0, MIDDLE_CHAR);
190 for (i = 1; i < 8; ++i)
192 do_test (i, 64, 256, 0, MIDDLE_CHAR);
193 do_test (i, 64, 256, 0, BIG_CHAR);
196 for (i = 0; i < 8; ++i)
198 do_test (16 * i, 256, 512, 0, MIDDLE_CHAR);
199 do_test (16 * i, 256, 512, 0, BIG_CHAR);
202 for (i = 0; i < 32; ++i)
204 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
205 do_test (0, i, i + 1, 0, BIG_CHAR);
208 return ret;
211 #include <support/test-driver.c>