Update install.texi, and regenerate INSTALL.
[glibc.git] / benchtests / bench-strncmp.c
bloba714e5a9d42541f8781c3e92f0212cb9ab18e61f
1 /* Measure strncmp 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 "wcsncmp"
22 #else
23 # define TEST_NAME "strncmp"
24 #endif /* !WIDE */
25 #include "bench-string.h"
26 #include "json-lib.h"
28 #ifdef WIDE
29 # define L(str) L##str
30 # define STRDUP wcsdup
31 # define SIMPLE_STRNCMP simple_wcsncmp
33 /* Wcsncmp uses signed semantics for comparison, not unsigned.
34 Avoid using substraction since possible overflow. */
35 int
36 simple_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n)
38 wchar_t c1, c2;
40 while (n--)
42 c1 = *s1++;
43 c2 = *s2++;
44 if (c1 == L ('\0') || c1 != c2)
45 return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0);
47 return 0;
50 #else
51 # define L(str) str
52 # define STRDUP strdup
53 # define SIMPLE_STRNCMP simple_strncmp
55 /* Strncmp uses unsigned semantics for comparison. */
56 int
57 simple_strncmp (const char *s1, const char *s2, size_t n)
59 int ret = 0;
61 while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
62 && *s1++);
63 return ret;
66 #endif /* !WIDE */
68 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
70 IMPL (SIMPLE_STRNCMP, 0)
71 IMPL (STRNCMP, 1)
74 static void
75 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR
76 *s2, size_t n, int exp_result)
78 size_t i, iters = INNER_LOOP_ITERS8;
79 timing_t start, stop, cur;
81 TIMING_NOW (start);
82 for (i = 0; i < iters; ++i)
84 CALL (impl, s1, s2, n);
86 TIMING_NOW (stop);
88 TIMING_DIFF (cur, start, stop);
90 json_element_double (json_ctx, (double) cur / (double) iters);
93 static void
94 do_test_limit (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
95 size_t n, int max_char, int exp_result)
97 size_t i, align_n;
98 CHAR *s1, *s2;
100 align1 &= 15;
101 align2 &= 15;
102 align_n = (page_size - n * CHARBYTES) & 15;
104 json_element_object_begin (json_ctx);
105 json_attr_uint (json_ctx, "strlen", (double) len);
106 json_attr_uint (json_ctx, "len", (double) n);
107 json_attr_uint (json_ctx, "align1", (double) align1);
108 json_attr_uint (json_ctx, "align2", (double) align2);
109 json_array_begin (json_ctx, "timings");
111 FOR_EACH_IMPL (impl, 0)
113 alloc_bufs ();
114 s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES);
115 s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES);
117 if (align1 < align_n)
118 s1 = (CHAR *) ((char *) s1 - (align_n - align1));
120 if (align2 < align_n)
121 s2 = (CHAR *) ((char *) s2 - (align_n - align2));
123 for (i = 0; i < n; i++)
124 s1[i] = s2[i] = 1 + 23 * i % max_char;
126 if (len < n)
128 s1[len] = 0;
129 s2[len] = 0;
130 if (exp_result < 0)
131 s2[len] = 32;
132 else if (exp_result > 0)
133 s1[len] = 64;
136 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
139 json_array_end (json_ctx);
140 json_element_object_end (json_ctx);
143 static void
144 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, size_t
145 n, int max_char, int exp_result)
147 size_t i;
148 CHAR *s1, *s2;
150 if (n == 0)
151 return;
153 align1 &= getpagesize () - 1;
154 if (align1 + (n + 1) * CHARBYTES >= page_size)
155 return;
157 align2 &= getpagesize () - 1;
158 if (align2 + (n + 1) * CHARBYTES >= page_size)
159 return;
161 json_element_object_begin (json_ctx);
162 json_attr_uint (json_ctx, "strlen", (double)len);
163 json_attr_uint (json_ctx, "len", (double)n);
164 json_attr_uint (json_ctx, "align1", (double)align1);
165 json_attr_uint (json_ctx, "align2", (double)align2);
166 json_array_begin (json_ctx, "timings");
168 FOR_EACH_IMPL (impl, 0)
170 alloc_bufs ();
171 s1 = (CHAR *)(buf1 + align1);
172 s2 = (CHAR *)(buf2 + align2);
174 for (i = 0; i < n; i++)
175 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
177 s1[n] = 24 + exp_result;
178 s2[n] = 23;
179 s1[len] = 0;
180 s2[len] = 0;
181 if (exp_result < 0)
182 s2[len] = 32;
183 else if (exp_result > 0)
184 s1[len] = 64;
185 if (len >= n)
186 s2[n - 1] -= exp_result;
188 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
191 json_array_end (json_ctx);
192 json_element_object_end (json_ctx);
195 static void
196 do_one_test_page_boundary (json_ctx_t *json_ctx, CHAR *s1, CHAR *s2,
197 size_t align1, size_t align2, size_t len,
198 size_t n, int exp_result)
200 json_element_object_begin (json_ctx);
201 json_attr_uint (json_ctx, "strlen", (double) len);
202 json_attr_uint (json_ctx, "len", (double) n);
203 json_attr_uint (json_ctx, "align1", (double) align1);
204 json_attr_uint (json_ctx, "align2", (double) align2);
205 json_array_begin (json_ctx, "timings");
206 FOR_EACH_IMPL (impl, 0)
207 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
208 json_array_end (json_ctx);
209 json_element_object_end (json_ctx);
212 static void
213 do_test_page_boundary (json_ctx_t *json_ctx)
215 /* To trigger bug 25933, we need a size that is equal to the vector
216 length times 4. In the case of AVX2 for Intel, we need 32 * 4. We
217 make this test generic and run it for all architectures as additional
218 boundary testing for such related algorithms. */
219 size_t size = 32 * 4;
220 size_t len;
221 CHAR *s1 = (CHAR *) (buf1 + (BUF1PAGES - 1) * page_size);
222 CHAR *s2 = (CHAR *) (buf2 + (BUF1PAGES - 1) * page_size);
223 int exp_result;
225 memset (s1, 'a', page_size);
226 memset (s2, 'a', page_size);
228 s1[(page_size / CHARBYTES) - 1] = (CHAR) 0;
230 /* Iterate over a size that is just below where we expect the bug to
231 trigger up to the size we expect will trigger the bug e.g. [99-128].
232 Likewise iterate the start of two strings between 30 and 31 bytes
233 away from the boundary to simulate alignment changes. */
234 for (size_t s = 99; s <= size; s++)
235 for (size_t s1a = 30; s1a < 32; s1a++)
236 for (size_t s2a = 30; s2a < 32; s2a++)
238 size_t align1 = (page_size / CHARBYTES - s) - s1a;
239 size_t align2 = (page_size / CHARBYTES - s) - s2a;
240 CHAR *s1p = s1 + align1;
241 CHAR *s2p = s2 + align2;
242 len = (page_size / CHARBYTES) - 1 - align1;
243 exp_result = SIMPLE_STRNCMP (s1p, s2p, s);
244 do_one_test_page_boundary (json_ctx, s1p, s2p, align1, align2,
245 len, s, exp_result);
249 static void
250 do_one_test_page (json_ctx_t *json_ctx, size_t offset1, size_t offset2,
251 CHAR *s2)
253 CHAR *s1;
254 int exp_result;
256 if (offset1 * CHARBYTES >= page_size
257 || offset2 * CHARBYTES >= page_size)
258 return;
260 s1 = (CHAR *) buf1;
261 s1 += offset1;
262 s2 += offset2;
264 size_t len = (page_size / CHARBYTES) - offset1;
266 exp_result= *s1;
268 json_element_object_begin (json_ctx);
269 json_attr_uint (json_ctx, "strlen", (double) len);
270 json_attr_uint (json_ctx, "len", (double) page_size);
271 json_attr_uint (json_ctx, "align1", (double) offset1);
272 json_attr_uint (json_ctx, "align2", (double) offset2);
273 json_array_begin (json_ctx, "timings");
275 FOR_EACH_IMPL (impl, 0)
276 do_one_test (json_ctx, impl, s1, s2, page_size, -exp_result);
278 json_array_end (json_ctx);
279 json_element_object_end (json_ctx);
281 json_element_object_begin (json_ctx);
282 json_attr_uint (json_ctx, "strlen", (double) len);
283 json_attr_uint (json_ctx, "len", (double) page_size);
284 json_attr_uint (json_ctx, "align1", (double) offset1);
285 json_attr_uint (json_ctx, "align2", (double) offset2);
286 json_array_begin (json_ctx, "timings");
288 FOR_EACH_IMPL (impl, 0)
289 do_one_test (json_ctx, impl, s1, s2, page_size, exp_result);
291 json_array_end (json_ctx);
292 json_element_object_end (json_ctx);
295 static void
296 do_test_page (json_ctx_t *json_ctx)
298 size_t i;
299 CHAR *s1, *s2;
301 s1 = (CHAR *) buf1;
302 /* Fill buf1 with 23. */
303 for (i = 0; i < (page_size / CHARBYTES) - 1; i++)
304 s1[i] = 23;
305 s1[i] = 0;
307 /* Make a copy of buf1. */
308 s2 = STRDUP (s1);
310 /* Test should terminate within the page boundary. */
311 for (i = 0; i < (108 / CHARBYTES); ++i)
312 do_one_test_page (json_ctx, ((page_size - 108) / CHARBYTES) + i,
313 ((page_size - 1460) / CHARBYTES), s2);
315 free (s2);
319 test_main (void)
321 json_ctx_t json_ctx;
322 size_t i, j, len;
323 size_t pg_sz = getpagesize ();
325 test_init ();
327 json_init (&json_ctx, 0, stdout);
329 json_document_begin (&json_ctx);
330 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
332 json_attr_object_begin (&json_ctx, "functions");
333 json_attr_object_begin (&json_ctx, TEST_NAME);
334 json_attr_string (&json_ctx, "bench-variant", "default");
336 json_array_begin (&json_ctx, "ifuncs");
337 FOR_EACH_IMPL (impl, 0)
338 json_element_string (&json_ctx, impl->name);
339 json_array_end (&json_ctx);
341 json_array_begin (&json_ctx, "results");
343 for (i = 0; i < 16; ++i)
345 do_test (&json_ctx, 0, 0, 8, i, 127, 0);
346 do_test (&json_ctx, 0, 0, 8, i, 127, -1);
347 do_test (&json_ctx, 0, 0, 8, i, 127, 1);
348 do_test (&json_ctx, i, i, 8, i, 127, 0);
349 do_test (&json_ctx, i, i, 8, i, 127, 1);
350 do_test (&json_ctx, i, i, 8, i, 127, -1);
351 do_test (&json_ctx, i, 2 * i, 8, i, 127, 0);
352 do_test (&json_ctx, 2 * i, i, 8, i, 127, 1);
353 do_test (&json_ctx, i, 3 * i, 8, i, 127, -1);
354 do_test (&json_ctx, 0, 0, 8, i, 255, 0);
355 do_test (&json_ctx, 0, 0, 8, i, 255, -1);
356 do_test (&json_ctx, 0, 0, 8, i, 255, 1);
357 do_test (&json_ctx, i, i, 8, i, 255, 0);
358 do_test (&json_ctx, i, i, 8, i, 255, 1);
359 do_test (&json_ctx, i, i, 8, i, 255, -1);
360 do_test (&json_ctx, i, 2 * i, 8, i, 255, 0);
361 do_test (&json_ctx, 2 * i, i, 8, i, 255, 1);
362 do_test (&json_ctx, i, 3 * i, 8, i, 255, -1);
365 for (len = 0; len <= 128; len += 64)
367 for (i = 1; i <= 8192;)
369 /* No page crosses. */
370 do_test (&json_ctx, 0, 0, i, i + len, 127, 0);
371 do_test (&json_ctx, i * CHARBYTES, 0, i, i + len, 127, 0);
372 do_test (&json_ctx, 0, i * CHARBYTES, i, i + len, 127, 0);
374 /* False page crosses. */
375 do_test (&json_ctx, pg_sz / 2, pg_sz / 2 - CHARBYTES, i, i + len,
376 127, 0);
377 do_test (&json_ctx, pg_sz / 2 - CHARBYTES, pg_sz / 2, i, i + len,
378 127, 0);
380 do_test (&json_ctx, pg_sz - (i * CHARBYTES), 0, i, i + len, 127,
382 do_test (&json_ctx, 0, pg_sz - (i * CHARBYTES), i, i + len, 127,
385 /* Real page cross. */
386 for (j = 16; j < 128; j += 16)
388 do_test (&json_ctx, pg_sz - j, 0, i, i + len, 127, 0);
389 do_test (&json_ctx, 0, pg_sz - j, i, i + len, 127, 0);
391 do_test (&json_ctx, pg_sz - j, pg_sz - j / 2, i, i + len,
392 127, 0);
393 do_test (&json_ctx, pg_sz - j / 2, pg_sz - j, i, i + len,
394 127, 0);
397 if (i < 32)
399 ++i;
401 else if (i < 160)
403 i += 8;
405 else if (i < 256)
407 i += 32;
409 else
411 i *= 2;
416 for (i = 1; i < 8; ++i)
418 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 0);
419 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 1);
420 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, -1);
421 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 0);
422 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 1);
423 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, -1);
424 do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
425 do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
426 do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 0);
427 do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 1);
430 do_test_limit (&json_ctx, 4, 0, 21, 20, 127, 0);
431 do_test_limit (&json_ctx, 0, 4, 21, 20, 127, 0);
432 do_test_limit (&json_ctx, 8, 0, 25, 24, 127, 0);
433 do_test_limit (&json_ctx, 0, 8, 25, 24, 127, 0);
435 for (i = 0; i < 8; ++i)
437 do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 127, 0);
438 do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 255, 0);
439 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 0);
440 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 1);
441 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, -1);
442 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 0);
443 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 1);
444 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, -1);
447 do_test_page_boundary (&json_ctx);
448 do_test_page (&json_ctx);
450 json_array_end (&json_ctx);
451 json_attr_object_end (&json_ctx);
452 json_attr_object_end (&json_ctx);
453 json_document_end (&json_ctx);
455 return ret;
458 #include <support/test-driver.c>