1 | /* Measure memchr functions. |
2 | Copyright (C) 2013-2024 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 | #include "json-lib.h" |
20 | |
21 | #ifndef WIDE |
22 | # define SMALL_CHAR 127 |
23 | #else |
24 | # define SMALL_CHAR 1273 |
25 | #endif /* WIDE */ |
26 | |
27 | #ifndef USE_AS_MEMRCHR |
28 | # define TEST_MAIN |
29 | # ifndef WIDE |
30 | # define TEST_NAME "memchr" |
31 | # else |
32 | # define TEST_NAME "wmemchr" |
33 | # endif /* WIDE */ |
34 | # include "bench-string.h" |
35 | |
36 | typedef void *(*proto_t) (const void *, int, size_t); |
37 | |
38 | void * |
39 | generic_memchr (const void *, int, size_t); |
40 | |
41 | IMPL (MEMCHR, 1) |
42 | |
43 | # ifndef WIDE |
44 | IMPL (generic_memchr, 0) |
45 | # endif |
46 | |
47 | #endif /* !USE_AS_MEMRCHR */ |
48 | |
49 | |
50 | static void |
51 | do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, int c, |
52 | size_t n) |
53 | { |
54 | size_t i, iters = INNER_LOOP_ITERS8 / 2; |
55 | timing_t start, stop, cur; |
56 | |
57 | TIMING_NOW (start); |
58 | for (i = 0; i < iters; ++i) |
59 | { |
60 | CALL (impl, s, c, n); |
61 | } |
62 | TIMING_NOW (stop); |
63 | |
64 | TIMING_DIFF (cur, start, stop); |
65 | |
66 | json_element_double (ctx: json_ctx, d: (double) cur / (double) iters); |
67 | } |
68 | |
69 | static void |
70 | do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len, |
71 | int seek_char, int invert_pos) |
72 | { |
73 | size_t i; |
74 | |
75 | align &= getpagesize () - 1; |
76 | if ((align + len) * sizeof (CHAR) >= page_size) |
77 | return; |
78 | |
79 | CHAR *buf = (CHAR *) (buf1); |
80 | |
81 | for (i = 0; i < len; ++i) |
82 | { |
83 | buf[align + i] = 1 + 23 * i % SMALL_CHAR; |
84 | if (buf[align + i] == seek_char) |
85 | buf[align + i] = seek_char + 1; |
86 | } |
87 | buf[align + len] = 0; |
88 | |
89 | if (pos < len) |
90 | { |
91 | if (invert_pos) |
92 | buf[align + len - pos] = seek_char; |
93 | else |
94 | buf[align + pos] = seek_char; |
95 | buf[align + len] = -seek_char; |
96 | } |
97 | else |
98 | { |
99 | buf[align + len] = seek_char; |
100 | } |
101 | |
102 | json_element_object_begin (ctx: json_ctx); |
103 | json_attr_uint (ctx: json_ctx, name: "align" , d: align); |
104 | json_attr_uint (ctx: json_ctx, name: "pos" , d: pos); |
105 | json_attr_uint (ctx: json_ctx, name: "len" , d: len); |
106 | json_attr_uint (ctx: json_ctx, name: "seek_char" , d: seek_char); |
107 | json_attr_uint (ctx: json_ctx, name: "invert_pos" , d: invert_pos); |
108 | |
109 | json_array_begin (ctx: json_ctx, name: "timings" ); |
110 | |
111 | FOR_EACH_IMPL (impl, 0) |
112 | do_one_test (json_ctx, impl, s: (CHAR *) (buf + align), c: seek_char, n: len); |
113 | |
114 | json_array_end (ctx: json_ctx); |
115 | json_element_object_end (ctx: json_ctx); |
116 | } |
117 | |
118 | int |
119 | test_main (void) |
120 | { |
121 | size_t i, j, al, al_max; |
122 | int repeats; |
123 | json_ctx_t json_ctx; |
124 | test_init (); |
125 | |
126 | json_init (ctx: &json_ctx, indent_level: 0, stdout); |
127 | |
128 | json_document_begin (ctx: &json_ctx); |
129 | json_attr_string (ctx: &json_ctx, name: "timing_type" , TIMING_TYPE); |
130 | |
131 | json_attr_object_begin (ctx: &json_ctx, name: "functions" ); |
132 | json_attr_object_begin (ctx: &json_ctx, TEST_NAME); |
133 | json_attr_string (ctx: &json_ctx, name: "bench-variant" , s: "" ); |
134 | |
135 | json_array_begin (ctx: &json_ctx, name: "ifuncs" ); |
136 | FOR_EACH_IMPL (impl, 0) |
137 | json_element_string (ctx: &json_ctx, s: impl->name); |
138 | json_array_end (ctx: &json_ctx); |
139 | |
140 | json_array_begin (ctx: &json_ctx, name: "results" ); |
141 | |
142 | al_max = 0; |
143 | #ifdef USE_AS_MEMRCHR |
144 | al_max = getpagesize () / 2; |
145 | #endif |
146 | |
147 | for (repeats = 0; repeats < 2; ++repeats) |
148 | { |
149 | for (al = 0; al <= al_max; al += getpagesize () / 2) |
150 | { |
151 | for (i = 1; i < 8; ++i) |
152 | { |
153 | do_test (json_ctx: &json_ctx, align: al, pos: 16 << i, len: 2048, seek_char: 23, invert_pos: repeats); |
154 | do_test (json_ctx: &json_ctx, align: al + i, pos: 64, len: 256, seek_char: 23, invert_pos: repeats); |
155 | do_test (json_ctx: &json_ctx, align: al, pos: 16 << i, len: 2048, seek_char: 0, invert_pos: repeats); |
156 | do_test (json_ctx: &json_ctx, align: al + i, pos: 64, len: 256, seek_char: 0, invert_pos: repeats); |
157 | |
158 | do_test (json_ctx: &json_ctx, align: al + getpagesize () - 15, pos: 64, len: 256, seek_char: 0, |
159 | invert_pos: repeats); |
160 | #ifdef USE_AS_MEMRCHR |
161 | /* Also test the position close to the beginning for memrchr. */ |
162 | do_test (&json_ctx, al, i, 256, 23, repeats); |
163 | do_test (&json_ctx, al, i, 256, 0, repeats); |
164 | do_test (&json_ctx, al + i, i, 256, 23, repeats); |
165 | do_test (&json_ctx, al + i, i, 256, 0, repeats); |
166 | #endif |
167 | } |
168 | for (i = 1; i < 8; ++i) |
169 | { |
170 | do_test (json_ctx: &json_ctx, align: al + i, pos: i << 5, len: 192, seek_char: 23, invert_pos: repeats); |
171 | do_test (json_ctx: &json_ctx, align: al + i, pos: i << 5, len: 192, seek_char: 0, invert_pos: repeats); |
172 | do_test (json_ctx: &json_ctx, align: al + i, pos: i << 5, len: 256, seek_char: 23, invert_pos: repeats); |
173 | do_test (json_ctx: &json_ctx, align: al + i, pos: i << 5, len: 256, seek_char: 0, invert_pos: repeats); |
174 | do_test (json_ctx: &json_ctx, align: al + i, pos: i << 5, len: 512, seek_char: 23, invert_pos: repeats); |
175 | do_test (json_ctx: &json_ctx, align: al + i, pos: i << 5, len: 512, seek_char: 0, invert_pos: repeats); |
176 | |
177 | do_test (json_ctx: &json_ctx, align: al + getpagesize () - 15, pos: i << 5, len: 256, seek_char: 23, |
178 | invert_pos: repeats); |
179 | } |
180 | } |
181 | |
182 | for (i = 1; i < 32; ++i) |
183 | { |
184 | do_test (json_ctx: &json_ctx, align: 0, pos: i, len: i + 1, seek_char: 23, invert_pos: repeats); |
185 | do_test (json_ctx: &json_ctx, align: 0, pos: i, len: i + 1, seek_char: 0, invert_pos: repeats); |
186 | do_test (json_ctx: &json_ctx, align: i, pos: i, len: i + 1, seek_char: 23, invert_pos: repeats); |
187 | do_test (json_ctx: &json_ctx, align: i, pos: i, len: i + 1, seek_char: 0, invert_pos: repeats); |
188 | do_test (json_ctx: &json_ctx, align: 0, pos: i, len: i - 1, seek_char: 23, invert_pos: repeats); |
189 | do_test (json_ctx: &json_ctx, align: 0, pos: i, len: i - 1, seek_char: 0, invert_pos: repeats); |
190 | do_test (json_ctx: &json_ctx, align: i, pos: i, len: i - 1, seek_char: 23, invert_pos: repeats); |
191 | do_test (json_ctx: &json_ctx, align: i, pos: i, len: i - 1, seek_char: 0, invert_pos: repeats); |
192 | |
193 | do_test (json_ctx: &json_ctx, align: getpagesize () / 2, pos: i, len: i + 1, seek_char: 23, invert_pos: repeats); |
194 | do_test (json_ctx: &json_ctx, align: getpagesize () / 2, pos: i, len: i + 1, seek_char: 0, invert_pos: repeats); |
195 | do_test (json_ctx: &json_ctx, align: getpagesize () / 2 + i, pos: i, len: i + 1, seek_char: 23, invert_pos: repeats); |
196 | do_test (json_ctx: &json_ctx, align: getpagesize () / 2 + i, pos: i, len: i + 1, seek_char: 0, invert_pos: repeats); |
197 | do_test (json_ctx: &json_ctx, align: getpagesize () / 2, pos: i, len: i - 1, seek_char: 23, invert_pos: repeats); |
198 | do_test (json_ctx: &json_ctx, align: getpagesize () / 2, pos: i, len: i - 1, seek_char: 0, invert_pos: repeats); |
199 | do_test (json_ctx: &json_ctx, align: getpagesize () / 2 + i, pos: i, len: i - 1, seek_char: 23, invert_pos: repeats); |
200 | do_test (json_ctx: &json_ctx, align: getpagesize () / 2 + i, pos: i, len: i - 1, seek_char: 0, invert_pos: repeats); |
201 | |
202 | do_test (json_ctx: &json_ctx, align: getpagesize () - 15, pos: i, len: i - 1, seek_char: 23, invert_pos: repeats); |
203 | do_test (json_ctx: &json_ctx, align: getpagesize () - 15, pos: i, len: i - 1, seek_char: 0, invert_pos: repeats); |
204 | |
205 | do_test (json_ctx: &json_ctx, align: getpagesize () - 15, pos: i, len: i + 1, seek_char: 23, invert_pos: repeats); |
206 | do_test (json_ctx: &json_ctx, align: getpagesize () - 15, pos: i, len: i + 1, seek_char: 0, invert_pos: repeats); |
207 | |
208 | #ifdef USE_AS_MEMRCHR |
209 | do_test (&json_ctx, 0, 1, i + 1, 23, repeats); |
210 | do_test (&json_ctx, 0, 2, i + 1, 0, repeats); |
211 | #endif |
212 | } |
213 | for (al = 0; al <= al_max; al += getpagesize () / 2) |
214 | { |
215 | for (i = (16 / sizeof (CHAR)); i <= (8192 / sizeof (CHAR)); i += i) |
216 | { |
217 | for (j = 0; j <= (384 / sizeof (CHAR)); |
218 | j += (32 / sizeof (CHAR))) |
219 | { |
220 | do_test (json_ctx: &json_ctx, align: al, pos: i + j, len: i, seek_char: 23, invert_pos: repeats); |
221 | do_test (json_ctx: &json_ctx, align: al, pos: i, len: i + j, seek_char: 23, invert_pos: repeats); |
222 | if (j < i) |
223 | { |
224 | do_test (json_ctx: &json_ctx, align: al, pos: i - j, len: i, seek_char: 23, invert_pos: repeats); |
225 | do_test (json_ctx: &json_ctx, align: al, pos: i, len: i - j, seek_char: 23, invert_pos: repeats); |
226 | } |
227 | } |
228 | } |
229 | } |
230 | |
231 | #ifndef USE_AS_MEMRCHR |
232 | break; |
233 | #endif |
234 | } |
235 | |
236 | json_array_end (ctx: &json_ctx); |
237 | json_attr_object_end (ctx: &json_ctx); |
238 | json_attr_object_end (ctx: &json_ctx); |
239 | json_document_end (ctx: &json_ctx); |
240 | |
241 | return ret; |
242 | } |
243 | |
244 | #include <support/test-driver.c> |
245 | |
246 | #ifndef WIDE |
247 | # ifndef USE_AS_MEMRCHR |
248 | # undef MEMCHR |
249 | # define MEMCHR generic_memchr |
250 | # include <string/memchr.c> |
251 | # else |
252 | # undef MEMRCHR |
253 | # define MEMRCHR generic_memrchr |
254 | # include <string/memrchr.c> |
255 | # endif |
256 | #endif |
257 | |