]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strnlen.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / benchtests / bench-strnlen.c
1 /* Measure strlen functions.
2 Copyright (C) 2013-2021 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 # define TEST_NAME "strnlen"
22 #else
23 # define TEST_NAME "wcsnlen"
24 # define generic_strnlen generic_wcsnlen
25 # define memchr_strnlen wcschr_wcsnlen
26 #endif /* WIDE */
27 #include "bench-string.h"
28
29 #define BIG_CHAR MAX_CHAR
30
31 #ifndef WIDE
32 # define MIDDLE_CHAR 127
33 #else
34 # define MIDDLE_CHAR 1121
35 #endif /* WIDE */
36
37 typedef size_t (*proto_t) (const CHAR *, size_t);
38 size_t generic_strnlen (const CHAR *, size_t);
39
40 size_t
41 memchr_strnlen (const CHAR *s, size_t maxlen)
42 {
43 const CHAR *s1 = MEMCHR (s, 0, maxlen);
44 return (s1 == NULL) ? maxlen : s1 - s;
45 }
46
47 IMPL (STRNLEN, 1)
48 IMPL (memchr_strnlen, 0)
49 IMPL (generic_strnlen, 0)
50
51 static void
52 do_one_test (impl_t *impl, const CHAR *s, size_t maxlen, size_t exp_len)
53 {
54 size_t len = CALL (impl, s, maxlen), i, iters = INNER_LOOP_ITERS_LARGE;
55 timing_t start, stop, cur;
56
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
65 TIMING_NOW (start);
66 for (i = 0; i < iters; ++i)
67 {
68 CALL (impl, s, maxlen);
69 }
70 TIMING_NOW (stop);
71
72 TIMING_DIFF (cur, start, stop);
73
74 TIMING_PRINT_MEAN ((double) cur, (double) iters);
75 }
76
77 static void
78 do_test (size_t align, size_t len, size_t maxlen, int max_char)
79 {
80 size_t i;
81
82 align &= 63;
83 if ((align + len) * sizeof (CHAR) >= page_size)
84 return;
85
86 CHAR *buf = (CHAR *) (buf1);
87
88 for (i = 0; i < len; ++i)
89 buf[align + i] = 1 + 7 * i % max_char;
90 buf[align + len] = 0;
91
92 printf ("Length %4zd, alignment %2zd:", len, align);
93
94 FOR_EACH_IMPL (impl, 0)
95 do_one_test (impl, (CHAR *) (buf + align), maxlen, MIN (len, maxlen));
96
97 putchar ('\n');
98 }
99
100 int
101 test_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 {
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);
117 }
118
119 for (i = 1; i < 8; ++i)
120 {
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);
124 }
125
126 for (i = 2; i <= 10; ++i)
127 {
128 do_test (0, 1 << i, 5000, MIDDLE_CHAR);
129 do_test (1, 1 << i, 5000, MIDDLE_CHAR);
130 }
131
132 for (i = 1; i < 8; ++i)
133 do_test (0, i, 5000, BIG_CHAR);
134
135 for (i = 1; i < 8; ++i)
136 do_test (i, i, 5000, BIG_CHAR);
137
138 for (i = 2; i <= 10; ++i)
139 {
140 do_test (0, 1 << i, 5000, BIG_CHAR);
141 do_test (1, 1 << i, 5000, BIG_CHAR);
142 }
143
144 return ret;
145 }
146
147 #include <support/test-driver.c>
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