1/* Measure STRCHR functions.
2 Copyright (C) 2013-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
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"
34
35#define BIG_CHAR MAX_CHAR
36
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 */
54
55#ifdef USE_FOR_STRCHRNUL
56# define NULLRET(endptr) endptr
57#else
58# define NULLRET(endptr) NULL
59#endif /* !USE_FOR_STRCHRNUL */
60
61
62typedef CHAR *(*proto_t) (const CHAR *, int);
63
64CHAR *
65simple_STRCHR (const CHAR *s, int c)
66{
67 for (; *s != (CHAR) c; ++s)
68 if (*s == '\0')
69 return NULLRET ((CHAR *) s);
70 return (CHAR *) s;
71}
72
73IMPL (simple_STRCHR, 0)
74IMPL (STRCHR, 1)
75
76static void
77do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
78{
79 size_t i, iters = INNER_LOOP_ITERS_LARGE;
80 timing_t start, stop, cur;
81
82 TIMING_NOW (start);
83 for (i = 0; i < iters; ++i)
84 {
85 CALL (impl, s, c);
86 }
87 TIMING_NOW (stop);
88
89 TIMING_DIFF (cur, start, stop);
90
91 TIMING_PRINT_MEAN ((double) cur, (double) iters);
92}
93
94static void
95do_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. */
99{
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;
106
107 for (i = 0; i < len; ++i)
108 {
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;
114 }
115 buf[align + len] = 0;
116
117 if (pos < len)
118 {
119 buf[align + pos] = seek_char;
120 result = buf + align + pos;
121 }
122 else if (seek_char == 0)
123 result = buf + align + len;
124 else
125 result = NULLRET (buf + align + len);
126
127 printf (format: "Length %4zd, alignment in bytes %2zd:",
128 pos, align * sizeof (CHAR));
129
130 FOR_EACH_IMPL (impl, 0)
131 do_one_test (impl, s: buf + align, c: seek_char, exp_res: result);
132
133 putchar (c: '\n');
134}
135
136int
137test_main (void)
138{
139 size_t i;
140
141 test_init ();
142
143 printf (format: "%20s", "");
144 FOR_EACH_IMPL (impl, 0)
145 printf (format: "\t%s", impl->name);
146 putchar (c: '\n');
147
148 for (i = 1; i < 8; ++i)
149 {
150 do_test (align: 0, pos: 16 << i, len: 2048, SMALL_CHAR, MIDDLE_CHAR);
151 do_test (align: i, pos: 16 << i, len: 2048, SMALL_CHAR, MIDDLE_CHAR);
152 }
153
154 for (i = 1; i < 8; ++i)
155 {
156 do_test (align: 0, pos: 16 << i, len: 4096, SMALL_CHAR, MIDDLE_CHAR);
157 do_test (align: i, pos: 16 << i, len: 4096, SMALL_CHAR, MIDDLE_CHAR);
158 }
159
160 for (i = 1; i < 8; ++i)
161 {
162 do_test (align: i, pos: 64, len: 256, SMALL_CHAR, MIDDLE_CHAR);
163 do_test (align: i, pos: 64, len: 256, SMALL_CHAR, BIG_CHAR);
164 }
165
166 for (i = 0; i < 8; ++i)
167 {
168 do_test (align: 16 * i, pos: 256, len: 512, SMALL_CHAR, MIDDLE_CHAR);
169 do_test (align: 16 * i, pos: 256, len: 512, SMALL_CHAR, BIG_CHAR);
170 }
171
172 for (i = 0; i < 32; ++i)
173 {
174 do_test (align: 0, pos: i, len: i + 1, SMALL_CHAR, MIDDLE_CHAR);
175 do_test (align: 0, pos: i, len: i + 1, SMALL_CHAR, BIG_CHAR);
176 }
177
178 for (i = 1; i < 8; ++i)
179 {
180 do_test (align: 0, pos: 16 << i, len: 2048, seek_char: 0, MIDDLE_CHAR);
181 do_test (align: i, pos: 16 << i, len: 2048, seek_char: 0, MIDDLE_CHAR);
182 }
183
184 for (i = 1; i < 8; ++i)
185 {
186 do_test (align: 0, pos: 16 << i, len: 4096, seek_char: 0, MIDDLE_CHAR);
187 do_test (align: i, pos: 16 << i, len: 4096, seek_char: 0, MIDDLE_CHAR);
188 }
189
190 for (i = 1; i < 8; ++i)
191 {
192 do_test (align: i, pos: 64, len: 256, seek_char: 0, MIDDLE_CHAR);
193 do_test (align: i, pos: 64, len: 256, seek_char: 0, BIG_CHAR);
194 }
195
196 for (i = 0; i < 8; ++i)
197 {
198 do_test (align: 16 * i, pos: 256, len: 512, seek_char: 0, MIDDLE_CHAR);
199 do_test (align: 16 * i, pos: 256, len: 512, seek_char: 0, BIG_CHAR);
200 }
201
202 for (i = 0; i < 32; ++i)
203 {
204 do_test (align: 0, pos: i, len: i + 1, seek_char: 0, MIDDLE_CHAR);
205 do_test (align: 0, pos: i, len: i + 1, seek_char: 0, BIG_CHAR);
206 }
207
208 return ret;
209}
210
211#include <support/test-driver.c>
212

source code of glibc/benchtests/bench-strchr.c