1/* Measure strspn functions.
2 Copyright (C) 2013-2024 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 "strspn"
22#else
23# define TEST_NAME "wcsspn"
24#endif /* WIDE */
25#include "bench-string.h"
26#include "json-lib.h"
27
28#define BIG_CHAR MAX_CHAR
29
30#ifndef WIDE
31# define SMALL_CHAR 127
32#else
33# define SMALL_CHAR 1273
34#endif /* WIDE */
35
36typedef size_t (*proto_t) (const CHAR *, const CHAR *);
37
38IMPL (STRSPN, 1)
39
40static void
41do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s,
42 const CHAR *acc, size_t exp_res)
43{
44 size_t res = CALL (impl, s, acc), i, iters = INNER_LOOP_ITERS8 / CHARBYTES;
45 timing_t start, stop, cur;
46
47 if (res != exp_res)
48 {
49 error (status: 0, errnum: 0, format: "Wrong result in function %s %p %p", impl->name,
50 (void *) res, (void *) exp_res);
51 ret = 1;
52 return;
53 }
54
55 TIMING_NOW (start);
56 for (i = 0; i < iters; ++i)
57 {
58 CALL (impl, s, acc);
59 }
60 TIMING_NOW (stop);
61
62 TIMING_DIFF (cur, start, stop);
63
64 json_element_double (ctx: json_ctx, d: (double)cur / (double)iters);
65}
66
67static void
68do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t pos,
69 size_t len)
70{
71 size_t i;
72 CHAR *acc, *s;
73
74 align1 &= 7;
75 if ((align1 + pos + 10) * sizeof (CHAR) >= page_size || len > 240 || !len)
76 return;
77 if ((align2 + len) * sizeof (CHAR) >= page_size)
78 return;
79
80 acc = (CHAR *) (buf2) + align2;
81 s = (CHAR *) (buf1) + align1;
82
83 for (i = 0; i < len; ++i)
84 {
85 acc[i] = random () & BIG_CHAR;
86 if (!acc[i])
87 acc[i] = random () & BIG_CHAR;
88 if (!acc[i])
89 acc[i] = 1 + (random () & SMALL_CHAR);
90 }
91 acc[len] = '\0';
92
93 for (i = 0; i < pos; ++i)
94 s[i] = acc[random () % len];
95 s[pos] = random () & BIG_CHAR;
96 if (STRCHR (acc, s[pos]))
97 s[pos] = '\0';
98 else
99 {
100 for (i = pos + 1; i < pos + 10; ++i)
101 s[i] = random () & BIG_CHAR;
102 s[i] = '\0';
103 }
104
105 json_element_object_begin (ctx: json_ctx);
106 json_attr_uint (ctx: json_ctx, name: "len", d: len);
107 json_attr_uint (ctx: json_ctx, name: "pos", d: pos);
108 json_attr_uint (ctx: json_ctx, name: "align1", d: align1);
109 json_attr_uint (ctx: json_ctx, name: "align2", d: align2);
110 json_array_begin (ctx: json_ctx, name: "timings");
111
112 FOR_EACH_IMPL (impl, 0)
113 do_one_test (json_ctx, impl, s, acc, exp_res: pos);
114
115 json_array_end (ctx: json_ctx);
116 json_element_object_end (ctx: json_ctx);
117}
118
119int
120test_main (void)
121{
122 json_ctx_t json_ctx;
123 size_t i;
124
125 test_init ();
126
127 json_init (ctx: &json_ctx, indent_level: 0, stdout);
128
129 json_document_begin (ctx: &json_ctx);
130 json_attr_string (ctx: &json_ctx, name: "timing_type", TIMING_TYPE);
131
132 json_attr_object_begin (ctx: &json_ctx, name: "functions");
133 json_attr_object_begin (ctx: &json_ctx, TEST_NAME);
134 json_attr_string (ctx: &json_ctx, name: "bench-variant", s: "");
135
136 json_array_begin (ctx: &json_ctx, name: "ifuncs");
137 FOR_EACH_IMPL (impl, 0)
138 json_element_string (ctx: &json_ctx, s: impl->name);
139 json_array_end (ctx: &json_ctx);
140
141 json_array_begin (ctx: &json_ctx, name: "results");
142
143 for (i = 0; i < 32; ++i)
144 {
145 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, pos: 512, len: i);
146 do_test (json_ctx: &json_ctx, align1: i, align2: 0, pos: 512, len: i);
147 do_test (json_ctx: &json_ctx, align1: 0, align2: i, pos: 512, len: i);
148 do_test (json_ctx: &json_ctx, align1: i, align2: i, pos: 512, len: i);
149 }
150
151 for (i = 1; i < 8; ++i)
152 {
153 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, pos: 16 << i, len: 4);
154 do_test (json_ctx: &json_ctx, align1: i, align2: 0, pos: 16 << i, len: 4);
155 do_test (json_ctx: &json_ctx, align1: 0, align2: i, pos: 16 << i, len: 4);
156 do_test (json_ctx: &json_ctx, align1: i, align2: i, pos: 16 << i, len: 4);
157 }
158
159 for (i = 1; i < 8; ++i)
160 {
161 do_test (json_ctx: &json_ctx, align1: i, align2: 0, pos: 64, len: 10);
162 do_test (json_ctx: &json_ctx, align1: i, align2: i, pos: 64, len: 10);
163 }
164
165 for (i = 0; i < 64; ++i)
166 {
167 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, pos: i, len: 6);
168 do_test (json_ctx: &json_ctx, align1: 0, align2: i, pos: i, len: 6);
169 }
170
171 json_array_end (ctx: &json_ctx);
172 json_attr_object_end (ctx: &json_ctx);
173 json_attr_object_end (ctx: &json_ctx);
174 json_document_end (ctx: &json_ctx);
175
176 return ret;
177}
178
179#include <support/test-driver.c>
180

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