build-many-glibcs.py: Add openrisc hard float glibc variant
[glibc.git] / benchtests / bench-strlen.c
blob583603c7e8e6bb051fe3547c31bfeff0afab9d09
1 /* Measure STRLEN functions.
2 Copyright (C) 2013-2024 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 <https://www.gnu.org/licenses/>. */
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"
29 #include "json-lib.h"
31 typedef size_t (*proto_t) (const CHAR *);
33 size_t generic_strlen (const CHAR *);
34 size_t memchr_strlen (const CHAR *);
36 IMPL (memchr_strlen, 0)
37 IMPL (generic_strlen, 0)
39 size_t
40 memchr_strlen (const CHAR *p)
42 return (const CHAR *)MEMCHR (p, 0, PTRDIFF_MAX) - p;
45 IMPL (STRLEN, 1)
48 static void
49 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, size_t exp_len)
51 size_t len = CALL (impl, s), i, iters = INNER_LOOP_ITERS_LARGE;
52 timing_t start, stop, cur;
54 if (len != exp_len)
56 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
57 len, exp_len);
58 ret = 1;
59 return;
62 TIMING_NOW (start);
63 for (i = 0; i < iters; ++i)
65 CALL (impl, s);
67 TIMING_NOW (stop);
69 TIMING_DIFF (cur, start, stop);
71 json_element_double (json_ctx, (double) cur / (double) iters);
74 static void
75 do_test (json_ctx_t *json_ctx, size_t align, size_t len)
77 size_t i;
79 align &= 63;
80 if (align + sizeof (CHAR) * len >= page_size)
81 return;
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");
89 FOR_EACH_IMPL (impl, 0)
91 CHAR *buf = (CHAR *) (buf1);
93 for (i = 0; i < len; ++i)
94 buf[align + i] = 1 + 11111 * i % MAX_CHAR;
95 buf[align + len] = 0;
97 do_one_test (json_ctx, impl, (CHAR *) (buf + align), len);
98 alloc_bufs ();
101 json_array_end (json_ctx);
102 json_element_object_end (json_ctx);
106 test_main (void)
108 json_ctx_t json_ctx;
109 size_t i;
111 test_init ();
113 json_init (&json_ctx, 0, stdout);
115 json_document_begin (&json_ctx);
116 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
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", "");
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);
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*/
130 for (i = 1; i < 8; ++i)
132 do_test (&json_ctx, sizeof (CHAR) * i, i);
133 do_test (&json_ctx, 0, i);
136 for (i = 2; i <= 12; ++i)
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));
144 for (i = 0; i < 512; i += 32)
146 do_test (&json_ctx, 0, i / sizeof (CHAR));
149 for (i = 512; i < 1024; i += 64)
151 do_test (&json_ctx, 0, i / sizeof (CHAR));
154 for (i = 1024; i < 2048; i += 128)
156 do_test (&json_ctx, 0, i / sizeof (CHAR));
159 for (i = 2048; i < 4096; i += 256)
161 do_test (&json_ctx, 0, i / sizeof (CHAR));
164 for (i = 4096; i < 8192; i += 512)
166 do_test (&json_ctx, 0, i / sizeof (CHAR));
169 json_array_end (&json_ctx);
170 json_attr_object_end (&json_ctx);
171 json_attr_object_end (&json_ctx);
172 json_document_end (&json_ctx);
174 return ret;
177 #include <support/test-driver.c>
179 #define libc_hidden_builtin_def(X)
180 #ifndef WIDE
181 # undef STRLEN
182 # define STRLEN generic_strlen
183 # include <string/strlen.c>
184 #else
185 # define WCSLEN generic_strlen
186 # include <wcsmbs/wcslen.c>
187 #endif