blob: 35266e5bf9e24b01a3115ba0a263ae79ba622417 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Measure strlen functions.
2 Copyright (C) 2013-2015 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 <http://www.gnu.org/licenses/>. */
18
19#define TEST_MAIN
20#define TEST_NAME "strnlen"
21#include "bench-string.h"
22
23typedef size_t (*proto_t) (const char *, size_t);
24size_t simple_strnlen (const char *, size_t);
25
26IMPL (simple_strnlen, 0)
27IMPL (strnlen, 1)
28
29size_t
30simple_strnlen (const char *s, size_t maxlen)
31{
32 size_t i;
33
34 for (i = 0; i < maxlen && s[i]; ++i);
35 return i;
36}
37
38static void
39do_one_test (impl_t *impl, const char *s, size_t maxlen, size_t exp_len)
40{
41 size_t len = CALL (impl, s, maxlen), i, iters = INNER_LOOP_ITERS;
42 timing_t start, stop, cur;
43
44 if (len != exp_len)
45 {
46 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
47 len, exp_len);
48 ret = 1;
49 return;
50 }
51
52 TIMING_NOW (start);
53 for (i = 0; i < iters; ++i)
54 {
55 CALL (impl, s, maxlen);
56 }
57 TIMING_NOW (stop);
58
59 TIMING_DIFF (cur, start, stop);
60
61 TIMING_PRINT_MEAN ((double) cur, (double) iters);
62}
63
64static void
65do_test (size_t align, size_t len, size_t maxlen, int max_char)
66{
67 size_t i;
68
69 align &= 7;
70 if (align + len >= page_size)
71 return;
72
73 for (i = 0; i < len; ++i)
74 buf1[align + i] = 1 + 7 * i % max_char;
75 buf1[align + len] = 0;
76
77 printf ("Length %4zd, alignment %2zd:", len, align);
78
79 FOR_EACH_IMPL (impl, 0)
80 do_one_test (impl, (char *) (buf1 + align), maxlen, MIN (len, maxlen));
81
82 putchar ('\n');
83}
84
85int
86test_main (void)
87{
88 size_t i;
89
90 test_init ();
91
92 printf ("%20s", "");
93 FOR_EACH_IMPL (impl, 0)
94 printf ("\t%s", impl->name);
95 putchar ('\n');
96
97 for (i = 1; i < 8; ++i)
98 {
99 do_test (0, i, i - 1, 127);
100 do_test (0, i, i, 127);
101 do_test (0, i, i + 1, 127);
102 }
103
104 for (i = 1; i < 8; ++i)
105 {
106 do_test (i, i, i - 1, 127);
107 do_test (i, i, i, 127);
108 do_test (i, i, i + 1, 127);
109 }
110
111 for (i = 2; i <= 10; ++i)
112 {
113 do_test (0, 1 << i, 5000, 127);
114 do_test (1, 1 << i, 5000, 127);
115 }
116
117 for (i = 1; i < 8; ++i)
118 do_test (0, i, 5000, 255);
119
120 for (i = 1; i < 8; ++i)
121 do_test (i, i, 5000, 255);
122
123 for (i = 2; i <= 10; ++i)
124 {
125 do_test (0, 1 << i, 5000, 255);
126 do_test (1, 1 << i, 5000, 255);
127 }
128
129 return ret;
130}
131
132#include "../test-skeleton.c"