Update translations.
[glibc.git] / benchtests / bench-strnlen.c
blob72ec30397776a791aa04343727155fdea1f08107
1 /* Measure strlen functions.
2 Copyright (C) 2013-2019 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 <http://www.gnu.org/licenses/>. */
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"
29 #define BIG_CHAR MAX_CHAR
31 #ifndef WIDE
32 # define MIDDLE_CHAR 127
33 #else
34 # define MIDDLE_CHAR 1121
35 #endif /* WIDE */
37 typedef size_t (*proto_t) (const CHAR *, size_t);
38 size_t generic_strnlen (const CHAR *, size_t);
40 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;
47 IMPL (STRNLEN, 1)
48 IMPL (memchr_strnlen, 0)
49 IMPL (generic_strnlen, 0)
51 static void
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;
57 if (len != exp_len)
59 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
60 len, exp_len);
61 ret = 1;
62 return;
65 TIMING_NOW (start);
66 for (i = 0; i < iters; ++i)
68 CALL (impl, s, maxlen);
70 TIMING_NOW (stop);
72 TIMING_DIFF (cur, start, stop);
74 TIMING_PRINT_MEAN ((double) cur, (double) iters);
77 static void
78 do_test (size_t align, size_t len, size_t maxlen, int max_char)
80 size_t i;
82 align &= 63;
83 if ((align + len) * sizeof (CHAR) >= page_size)
84 return;
86 CHAR *buf = (CHAR *) (buf1);
88 for (i = 0; i < len; ++i)
89 buf[align + i] = 1 + 7 * i % max_char;
90 buf[align + len] = 0;
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));
97 putchar ('\n');
101 test_main (void)
103 size_t i;
105 test_init ();
107 printf ("%20s", "");
108 FOR_EACH_IMPL (impl, 0)
109 printf ("\t%s", impl->name);
110 putchar ('\n');
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);
144 return ret;
147 #include <support/test-driver.c>
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