]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strncmp.c
65ebb3445d62e5015ed785dfbc3e3b905dbdcafa
[thirdparty/glibc.git] / benchtests / bench-strncmp.c
1 /* Measure strncmp functions.
2 Copyright (C) 2013-2018 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 <http://www.gnu.org/licenses/>. */
18
19 #define TEST_MAIN
20 #ifdef WIDE
21 # define TEST_NAME "wcsncmp"
22 #else
23 # define TEST_NAME "strncmp"
24 #endif /* !WIDE */
25 #include "bench-string.h"
26 #include "json-lib.h"
27
28 #ifdef WIDE
29 # define L(str) L##str
30 # define SIMPLE_STRNCMP simple_wcsncmp
31 # define STUPID_STRNCMP stupid_wcsncmp
32
33 /* Wcsncmp uses signed semantics for comparison, not unsigned.
34 Avoid using substraction since possible overflow. */
35 int
36 simple_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n)
37 {
38 wchar_t c1, c2;
39
40 while (n--)
41 {
42 c1 = *s1++;
43 c2 = *s2++;
44 if (c1 == L ('\0') || c1 != c2)
45 return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0);
46 }
47 return 0;
48 }
49
50 int
51 stupid_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n)
52 {
53 wchar_t c1, c2;
54 size_t ns1 = wcsnlen (s1, n) + 1, ns2 = wcsnlen (s2, n) + 1;
55
56 n = ns1 < n ? ns1 : n;
57 n = ns2 < n ? ns2 : n;
58
59 while (n--)
60 {
61 c1 = *s1++;
62 c2 = *s2++;
63 if (c1 != c2)
64 return c1 > c2 ? 1 : -1;
65 }
66 return 0;
67 }
68
69 #else
70 # define L(str) str
71 # define SIMPLE_STRNCMP simple_strncmp
72 # define STUPID_STRNCMP stupid_strncmp
73
74 /* Strncmp uses unsigned semantics for comparison. */
75 int
76 simple_strncmp (const char *s1, const char *s2, size_t n)
77 {
78 int ret = 0;
79
80 while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
81 && *s1++);
82 return ret;
83 }
84
85 int
86 stupid_strncmp (const char *s1, const char *s2, size_t n)
87 {
88 size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
89 int ret = 0;
90
91 n = ns1 < n ? ns1 : n;
92 n = ns2 < n ? ns2 : n;
93 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
94 return ret;
95 }
96
97 #endif /* !WIDE */
98
99 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
100
101 IMPL (STUPID_STRNCMP, 0)
102 IMPL (SIMPLE_STRNCMP, 0)
103 IMPL (STRNCMP, 1)
104
105
106 static void
107 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR
108 *s2, size_t n, int exp_result)
109 {
110 size_t i, iters = INNER_LOOP_ITERS;
111 timing_t start, stop, cur;
112
113 TIMING_NOW (start);
114 for (i = 0; i < iters; ++i)
115 {
116 CALL (impl, s1, s2, n);
117 }
118 TIMING_NOW (stop);
119
120 TIMING_DIFF (cur, start, stop);
121
122 json_element_double (json_ctx, (double) cur / (double) iters);
123 }
124
125 static void
126 do_test_limit (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
127 size_t n, int max_char, int exp_result)
128 {
129 size_t i, align_n;
130 CHAR *s1, *s2;
131
132 align1 &= 15;
133 align2 &= 15;
134 align_n = (page_size - n * CHARBYTES) & 15;
135
136 json_element_object_begin (json_ctx);
137 json_attr_uint (json_ctx, "strlen", (double) len);
138 json_attr_uint (json_ctx, "len", (double) n);
139 json_attr_uint (json_ctx, "align1", (double) align1);
140 json_attr_uint (json_ctx, "align2", (double) align2);
141 json_array_begin (json_ctx, "timings");
142
143 FOR_EACH_IMPL (impl, 0)
144 {
145 alloc_bufs ();
146 s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES);
147 s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES);
148
149 if (align1 < align_n)
150 s1 = (CHAR *) ((char *) s1 - (align_n - align1));
151
152 if (align2 < align_n)
153 s2 = (CHAR *) ((char *) s2 - (align_n - align2));
154
155 for (i = 0; i < n; i++)
156 s1[i] = s2[i] = 1 + 23 * i % max_char;
157
158 if (len < n)
159 {
160 s1[len] = 0;
161 s2[len] = 0;
162 if (exp_result < 0)
163 s2[len] = 32;
164 else if (exp_result > 0)
165 s1[len] = 64;
166 }
167
168 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
169 }
170
171 json_array_end (json_ctx);
172 json_element_object_end (json_ctx);
173 }
174
175 static void
176 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, size_t
177 n, int max_char, int exp_result)
178 {
179 size_t i;
180 CHAR *s1, *s2;
181
182 if (n == 0)
183 return;
184
185 align1 &= 63;
186 if (align1 + (n + 1) * CHARBYTES >= page_size)
187 return;
188
189 align2 &= 7;
190 if (align2 + (n + 1) * CHARBYTES >= page_size)
191 return;
192
193 json_element_object_begin (json_ctx);
194 json_attr_uint (json_ctx, "strlen", (double) len);
195 json_attr_uint (json_ctx, "len", (double) n);
196 json_attr_uint (json_ctx, "align1", (double) align1);
197 json_attr_uint (json_ctx, "align2", (double) align2);
198 json_array_begin (json_ctx, "timings");
199
200 FOR_EACH_IMPL (impl, 0)
201 {
202 alloc_bufs ();
203 s1 = (CHAR *) (buf1 + align1);
204 s2 = (CHAR *) (buf2 + align2);
205
206 for (i = 0; i < n; i++)
207 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
208
209 s1[n] = 24 + exp_result;
210 s2[n] = 23;
211 s1[len] = 0;
212 s2[len] = 0;
213 if (exp_result < 0)
214 s2[len] = 32;
215 else if (exp_result > 0)
216 s1[len] = 64;
217 if (len >= n)
218 s2[n - 1] -= exp_result;
219
220 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
221 }
222
223 json_array_end (json_ctx);
224 json_element_object_end (json_ctx);
225 }
226
227 int
228 test_main (void)
229 {
230 json_ctx_t json_ctx;
231 size_t i;
232
233 test_init ();
234
235 json_init (&json_ctx, 0, stdout);
236
237 json_document_begin (&json_ctx);
238 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
239
240 json_attr_object_begin (&json_ctx, "functions");
241 json_attr_object_begin (&json_ctx, TEST_NAME);
242 json_attr_string (&json_ctx, "bench-variant", "default");
243
244 json_array_begin (&json_ctx, "ifuncs");
245 FOR_EACH_IMPL (impl, 0)
246 json_element_string (&json_ctx, impl->name);
247 json_array_end (&json_ctx);
248
249 json_array_begin (&json_ctx, "results");
250
251 for (i =0; i < 16; ++i)
252 {
253 do_test (&json_ctx, 0, 0, 8, i, 127, 0);
254 do_test (&json_ctx, 0, 0, 8, i, 127, -1);
255 do_test (&json_ctx, 0, 0, 8, i, 127, 1);
256 do_test (&json_ctx, i, i, 8, i, 127, 0);
257 do_test (&json_ctx, i, i, 8, i, 127, 1);
258 do_test (&json_ctx, i, i, 8, i, 127, -1);
259 do_test (&json_ctx, i, 2 * i, 8, i, 127, 0);
260 do_test (&json_ctx, 2 * i, i, 8, i, 127, 1);
261 do_test (&json_ctx, i, 3 * i, 8, i, 127, -1);
262 do_test (&json_ctx, 0, 0, 8, i, 255, 0);
263 do_test (&json_ctx, 0, 0, 8, i, 255, -1);
264 do_test (&json_ctx, 0, 0, 8, i, 255, 1);
265 do_test (&json_ctx, i, i, 8, i, 255, 0);
266 do_test (&json_ctx, i, i, 8, i, 255, 1);
267 do_test (&json_ctx, i, i, 8, i, 255, -1);
268 do_test (&json_ctx, i, 2 * i, 8, i, 255, 0);
269 do_test (&json_ctx, 2 * i, i, 8, i, 255, 1);
270 do_test (&json_ctx, i, 3 * i, 8, i, 255, -1);
271 }
272
273 for (i = 1; i < 8; ++i)
274 {
275 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 0);
276 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 1);
277 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, -1);
278 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 0);
279 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 1);
280 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, -1);
281 do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
282 do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
283 do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 0);
284 do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 1);
285 }
286
287 do_test_limit (&json_ctx, 4, 0, 21, 20, 127, 0);
288 do_test_limit (&json_ctx, 0, 4, 21, 20, 127, 0);
289 do_test_limit (&json_ctx, 8, 0, 25, 24, 127, 0);
290 do_test_limit (&json_ctx, 0, 8, 25, 24, 127, 0);
291
292 for (i = 0; i < 8; ++i)
293 {
294 do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 127, 0);
295 do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 255, 0);
296 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 0);
297 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 1);
298 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, -1);
299 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 0);
300 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 1);
301 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, -1);
302 }
303
304 json_array_end (&json_ctx);
305 json_attr_object_end (&json_ctx);
306 json_attr_object_end (&json_ctx);
307 json_document_end (&json_ctx);
308
309 return ret;
310 }
311
312 #include <support/test-driver.c>