]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strlen.c
Add some spaces before '('.
[thirdparty/glibc.git] / benchtests / bench-strlen.c
1 /* Measure STRLEN functions.
2 Copyright (C) 2013-2019 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 #ifndef WIDE
21 # define TEST_NAME "strlen"
22 #else
23 # define TEST_NAME "wcslen"
24 #endif
25 #include "bench-string.h"
26
27 #include "json-lib.h"
28
29 typedef size_t (*proto_t) (const CHAR *);
30
31 size_t generic_strlen (const CHAR *);
32 size_t memchr_strlen (const CHAR *);
33
34 IMPL (memchr_strlen, 0)
35 IMPL (generic_strlen, 0)
36
37 size_t
38 memchr_strlen (const CHAR *p)
39 {
40 return (const CHAR *)MEMCHR (p, 0, PTRDIFF_MAX) - p;
41 }
42
43 IMPL (STRLEN, 1)
44
45
46 static void
47 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, size_t exp_len)
48 {
49 size_t len = CALL (impl, s), i, iters = INNER_LOOP_ITERS;
50 timing_t start, stop, cur;
51
52 if (len != exp_len)
53 {
54 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
55 len, exp_len);
56 ret = 1;
57 return;
58 }
59
60 TIMING_NOW (start);
61 for (i = 0; i < iters; ++i)
62 {
63 CALL (impl, s);
64 }
65 TIMING_NOW (stop);
66
67 TIMING_DIFF (cur, start, stop);
68
69 json_element_double (json_ctx, (double) cur / (double) iters);
70 }
71
72 static void
73 do_test (json_ctx_t *json_ctx, size_t align, size_t len)
74 {
75 size_t i;
76
77 align &= 63;
78 if (align + sizeof (CHAR) * len >= page_size)
79 return;
80
81 json_element_object_begin (json_ctx);
82 json_attr_uint (json_ctx, "length", len);
83 json_attr_uint (json_ctx, "alignment", align);
84 json_array_begin (json_ctx, "timings");
85
86
87 FOR_EACH_IMPL (impl, 0)
88 {
89 CHAR *buf = (CHAR *) (buf1);
90
91 for (i = 0; i < len; ++i)
92 buf[align + i] = 1 + 11111 * i % MAX_CHAR;
93 buf[align + len] = 0;
94
95 do_one_test (json_ctx, impl, (CHAR *) (buf + align), len);
96 alloc_bufs ();
97 }
98
99 json_array_end (json_ctx);
100 json_element_object_end (json_ctx);
101 }
102
103 int
104 test_main (void)
105 {
106 json_ctx_t json_ctx;
107 size_t i;
108
109 test_init ();
110
111 json_init (&json_ctx, 0, stdout);
112
113 json_document_begin (&json_ctx);
114 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
115
116 json_attr_object_begin (&json_ctx, "functions");
117 json_attr_object_begin (&json_ctx, TEST_NAME);
118 json_attr_string (&json_ctx, "bench-variant", "");
119
120 json_array_begin (&json_ctx, "ifuncs");
121 FOR_EACH_IMPL (impl, 0)
122 json_element_string (&json_ctx, impl->name);
123 json_array_end (&json_ctx);
124
125 json_array_begin (&json_ctx, "results");
126 /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
127
128 for (i = 1; i < 8; ++i)
129 {
130 do_test (&json_ctx, sizeof (CHAR) * i, i);
131 do_test (&json_ctx, 0, i);
132 }
133
134 for (i = 2; i <= 12; ++i)
135 {
136 do_test (&json_ctx, 0, 1 << i);
137 do_test (&json_ctx, sizeof (CHAR) * 7, 1 << i);
138 do_test (&json_ctx, sizeof (CHAR) * i, 1 << i);
139 do_test (&json_ctx, sizeof (CHAR) * i, (size_t)((1 << i) / 1.5));
140 }
141
142 json_array_end (&json_ctx);
143 json_attr_object_end (&json_ctx);
144 json_attr_object_end (&json_ctx);
145 json_document_end (&json_ctx);
146
147 return ret;
148 }
149
150 #include <support/test-driver.c>
151
152 #define libc_hidden_builtin_def(X)
153 #ifndef WIDE
154 # undef STRLEN
155 # define STRLEN generic_strlen
156 # include <string/strlen.c>
157 #else
158 # define WCSLEN generic_strlen
159 # include <wcsmbs/wcslen.c>
160 #endif