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
31typedef size_t (*proto_t) (const CHAR *);
32
33size_t generic_strlen (const CHAR *);
34size_t memchr_strlen (const CHAR *);
35
36IMPL (memchr_strlen, 0)
37IMPL (generic_strlen, 0)
38
39size_t
40memchr_strlen (const CHAR *p)
41{
42 return (const CHAR *)MEMCHR (p, 0, PTRDIFF_MAX) - p;
43}
44
45IMPL (STRLEN, 1)
46
47
48static void
49do_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 (status: 0, errnum: 0, format: "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 (ctx: json_ctx, d: (double) cur / (double) iters);
72}
73
74static void
75do_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 (ctx: json_ctx);
84 json_attr_uint (ctx: json_ctx, name: "length", d: len);
85 json_attr_uint (ctx: json_ctx, name: "alignment", d: align);
86 json_array_begin (ctx: json_ctx, name: "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, s: (CHAR *) (buf + align), exp_len: len);
98 alloc_bufs ();
99 }
100
101 json_array_end (ctx: json_ctx);
102 json_element_object_end (ctx: json_ctx);
103}
104
105int
106test_main (void)
107{
108 json_ctx_t json_ctx;
109 size_t i;
110
111 test_init ();
112
113 json_init (ctx: &json_ctx, indent_level: 0, stdout);
114
115 json_document_begin (ctx: &json_ctx);
116 json_attr_string (ctx: &json_ctx, name: "timing_type", TIMING_TYPE);
117
118 json_attr_object_begin (ctx: &json_ctx, name: "functions");
119 json_attr_object_begin (ctx: &json_ctx, TEST_NAME);
120 json_attr_string (ctx: &json_ctx, name: "bench-variant", s: "");
121
122 json_array_begin (ctx: &json_ctx, name: "ifuncs");
123 FOR_EACH_IMPL (impl, 0)
124 json_element_string (ctx: &json_ctx, s: impl->name);
125 json_array_end (ctx: &json_ctx);
126
127 json_array_begin (ctx: &json_ctx, name: "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: &json_ctx, align: sizeof (CHAR) * i, len: i);
133 do_test (json_ctx: &json_ctx, align: 0, len: i);
134 }
135
136 for (i = 2; i <= 12; ++i)
137 {
138 do_test (json_ctx: &json_ctx, align: 0, len: 1 << i);
139 do_test (json_ctx: &json_ctx, align: sizeof (CHAR) * 7, len: 1 << i);
140 do_test (json_ctx: &json_ctx, align: sizeof (CHAR) * i, len: 1 << i);
141 do_test (json_ctx: &json_ctx, align: sizeof (CHAR) * i, len: (size_t)((1 << i) / 1.5));
142 }
143
144 json_array_end (ctx: &json_ctx);
145 json_attr_object_end (ctx: &json_ctx);
146 json_attr_object_end (ctx: &json_ctx);
147 json_document_end (ctx: &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

source code of glibc/benchtests/bench-strlen.c