1 /* Measure strlen 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/>. */
21 # define TEST_NAME "strnlen"
23 # define TEST_NAME "wcsnlen"
24 # define generic_strnlen generic_wcsnlen
25 # define memchr_strnlen wcschr_wcsnlen
27 #include "bench-string.h"
29 #define BIG_CHAR MAX_CHAR
32 # define MIDDLE_CHAR 127
34 # define MIDDLE_CHAR 1121
37 typedef size_t (*proto_t) (const CHAR *, size_t);
38 size_t generic_strnlen (const CHAR *, size_t);
41 memchr_strnlen (const CHAR *s, size_t maxlen)
43 const CHAR *s1 = MEMCHR (s, 0, maxlen);
44 return (s1 == NULL) ? maxlen : s1 - s;
48 IMPL (memchr_strnlen, 0)
49 IMPL (generic_strnlen, 0)
52 do_one_test (impl_t *impl, const CHAR *s, size_t maxlen, size_t exp_len)
54 size_t len = CALL (impl, s, maxlen), i, iters = INNER_LOOP_ITERS_LARGE;
55 timing_t start, stop, cur;
59 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
66 for (i = 0; i < iters; ++i)
68 CALL (impl, s, maxlen);
72 TIMING_DIFF (cur, start, stop);
74 TIMING_PRINT_MEAN ((double) cur, (double) iters);
78 do_test (size_t align, size_t len, size_t maxlen, int max_char)
83 if ((align + len) * sizeof (CHAR) >= page_size)
86 CHAR *buf = (CHAR *) (buf1);
88 for (i = 0; i < len; ++i)
89 buf[align + i] = 1 + 7 * i % max_char;
92 printf ("Length %4zd, alignment %2zd:", len, align);
94 FOR_EACH_IMPL (impl, 0)
95 do_one_test (impl, (CHAR *) (buf + align), maxlen, MIN (len, maxlen));
108 FOR_EACH_IMPL (impl, 0)
109 printf ("\t%s", impl->name);
112 for (i = 1; i < 8; ++i)
114 do_test (0, i, i - 1, MIDDLE_CHAR);
115 do_test (0, i, i, MIDDLE_CHAR);
116 do_test (0, i, i + 1, MIDDLE_CHAR);
119 for (i = 1; i < 8; ++i)
121 do_test (i, i, i - 1, MIDDLE_CHAR);
122 do_test (i, i, i, MIDDLE_CHAR);
123 do_test (i, i, i + 1, MIDDLE_CHAR);
126 for (i = 2; i <= 10; ++i)
128 do_test (0, 1 << i, 5000, MIDDLE_CHAR);
129 do_test (1, 1 << i, 5000, MIDDLE_CHAR);
132 for (i = 1; i < 8; ++i)
133 do_test (0, i, 5000, BIG_CHAR);
135 for (i = 1; i < 8; ++i)
136 do_test (i, i, 5000, BIG_CHAR);
138 for (i = 2; i <= 10; ++i)
140 do_test (0, 1 << i, 5000, BIG_CHAR);
141 do_test (1, 1 << i, 5000, BIG_CHAR);
147 #include <support/test-driver.c>
149 #define libc_hidden_def(X)
152 # define STRNLEN generic_strnlen
153 # include <string/strnlen.c>
155 # define WCSNLEN generic_strnlen
156 # include <wcsmbs/wcsnlen.c>