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