]> git.ipfire.org Git - thirdparty/glibc.git/blame - benchtests/bench-strnlen.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / benchtests / bench-strnlen.c
CommitLineData
97020474 1/* Measure strlen functions.
2b778ceb 2 Copyright (C) 2013-2021 Free Software Foundation, Inc.
97020474
SP
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
5a82c748 17 <https://www.gnu.org/licenses/>. */
97020474
SP
18
19#define TEST_MAIN
fcf40ebe
SL
20#ifndef WIDE
21# define TEST_NAME "strnlen"
22#else
23# define TEST_NAME "wcsnlen"
648279f4
WD
24# define generic_strnlen generic_wcsnlen
25# define memchr_strnlen wcschr_wcsnlen
fcf40ebe 26#endif /* WIDE */
97020474
SP
27#include "bench-string.h"
28
90d3320d
WD
29#define BIG_CHAR MAX_CHAR
30
fcf40ebe 31#ifndef WIDE
fcf40ebe 32# define MIDDLE_CHAR 127
fcf40ebe 33#else
fcf40ebe 34# define MIDDLE_CHAR 1121
fcf40ebe
SL
35#endif /* WIDE */
36
37typedef size_t (*proto_t) (const CHAR *, size_t);
648279f4 38size_t generic_strnlen (const CHAR *, size_t);
97020474
SP
39
40size_t
648279f4 41memchr_strnlen (const CHAR *s, size_t maxlen)
97020474 42{
648279f4
WD
43 const CHAR *s1 = MEMCHR (s, 0, maxlen);
44 return (s1 == NULL) ? maxlen : s1 - s;
97020474
SP
45}
46
648279f4
WD
47IMPL (STRNLEN, 1)
48IMPL (memchr_strnlen, 0)
49IMPL (generic_strnlen, 0)
50
97020474 51static void
fcf40ebe 52do_one_test (impl_t *impl, const CHAR *s, size_t maxlen, size_t exp_len)
97020474 53{
afe23eb0 54 size_t len = CALL (impl, s, maxlen), i, iters = INNER_LOOP_ITERS_LARGE;
44558701
WN
55 timing_t start, stop, cur;
56
97020474
SP
57 if (len != exp_len)
58 {
59 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
60 len, exp_len);
61 ret = 1;
62 return;
63 }
64
44558701
WN
65 TIMING_NOW (start);
66 for (i = 0; i < iters; ++i)
97020474 67 {
44558701 68 CALL (impl, s, maxlen);
97020474 69 }
44558701
WN
70 TIMING_NOW (stop);
71
72 TIMING_DIFF (cur, start, stop);
73
74 TIMING_PRINT_MEAN ((double) cur, (double) iters);
97020474
SP
75}
76
77static void
78do_test (size_t align, size_t len, size_t maxlen, int max_char)
79{
80 size_t i;
81
fcf40ebe
SL
82 align &= 63;
83 if ((align + len) * sizeof (CHAR) >= page_size)
97020474
SP
84 return;
85
fcf40ebe
SL
86 CHAR *buf = (CHAR *) (buf1);
87
97020474 88 for (i = 0; i < len; ++i)
fcf40ebe
SL
89 buf[align + i] = 1 + 7 * i % max_char;
90 buf[align + len] = 0;
97020474 91
44558701 92 printf ("Length %4zd, alignment %2zd:", len, align);
97020474
SP
93
94 FOR_EACH_IMPL (impl, 0)
fcf40ebe 95 do_one_test (impl, (CHAR *) (buf + align), maxlen, MIN (len, maxlen));
97020474 96
44558701 97 putchar ('\n');
97020474
SP
98}
99
100int
101test_main (void)
102{
103 size_t i;
104
105 test_init ();
106
107 printf ("%20s", "");
108 FOR_EACH_IMPL (impl, 0)
109 printf ("\t%s", impl->name);
110 putchar ('\n');
111
112 for (i = 1; i < 8; ++i)
113 {
fcf40ebe
SL
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);
97020474
SP
117 }
118
119 for (i = 1; i < 8; ++i)
120 {
fcf40ebe
SL
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);
97020474
SP
124 }
125
126 for (i = 2; i <= 10; ++i)
127 {
fcf40ebe
SL
128 do_test (0, 1 << i, 5000, MIDDLE_CHAR);
129 do_test (1, 1 << i, 5000, MIDDLE_CHAR);
97020474
SP
130 }
131
132 for (i = 1; i < 8; ++i)
fcf40ebe 133 do_test (0, i, 5000, BIG_CHAR);
97020474
SP
134
135 for (i = 1; i < 8; ++i)
fcf40ebe 136 do_test (i, i, 5000, BIG_CHAR);
97020474
SP
137
138 for (i = 2; i <= 10; ++i)
139 {
fcf40ebe
SL
140 do_test (0, 1 << i, 5000, BIG_CHAR);
141 do_test (1, 1 << i, 5000, BIG_CHAR);
97020474
SP
142 }
143
144 return ret;
145}
146
b598e134 147#include <support/test-driver.c>
648279f4
WD
148
149#define libc_hidden_def(X)
150#ifndef WIDE
151# undef STRNLEN
152# define STRNLEN generic_strnlen
153# include <string/strnlen.c>
154#else
155# define WCSNLEN generic_strnlen
156# include <wcsmbs/wcsnlen.c>
157#endif