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