1 /* Measure strncmp functions.
2 Copyright (C) 2013-2020 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/>. */
21 # define TEST_NAME "wcsncmp"
23 # define TEST_NAME "strncmp"
25 #include "bench-string.h"
29 # define L(str) L##str
30 # define SIMPLE_STRNCMP simple_wcsncmp
32 /* Wcsncmp uses signed semantics for comparison, not unsigned.
33 Avoid using substraction since possible overflow. */
35 simple_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n)
43 if (c1 == L ('\0') || c1 != c2)
44 return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0);
51 # define SIMPLE_STRNCMP simple_strncmp
53 /* Strncmp uses unsigned semantics for comparison. */
55 simple_strncmp (const char *s1, const char *s2, size_t n)
59 while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
66 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
68 IMPL (SIMPLE_STRNCMP, 0)
73 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR
74 *s2, size_t n, int exp_result)
76 size_t i, iters = INNER_LOOP_ITERS8;
77 timing_t start, stop, cur;
80 for (i = 0; i < iters; ++i)
82 CALL (impl, s1, s2, n);
86 TIMING_DIFF (cur, start, stop);
88 json_element_double (json_ctx, (double) cur / (double) iters);
92 do_test_limit (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
93 size_t n, int max_char, int exp_result)
100 align_n = (page_size - n * CHARBYTES) & 15;
102 json_element_object_begin (json_ctx);
103 json_attr_uint (json_ctx, "strlen", (double) len);
104 json_attr_uint (json_ctx, "len", (double) n);
105 json_attr_uint (json_ctx, "align1", (double) align1);
106 json_attr_uint (json_ctx, "align2", (double) align2);
107 json_array_begin (json_ctx, "timings");
109 FOR_EACH_IMPL (impl, 0)
112 s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES);
113 s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES);
115 if (align1 < align_n)
116 s1 = (CHAR *) ((char *) s1 - (align_n - align1));
118 if (align2 < align_n)
119 s2 = (CHAR *) ((char *) s2 - (align_n - align2));
121 for (i = 0; i < n; i++)
122 s1[i] = s2[i] = 1 + 23 * i % max_char;
130 else if (exp_result > 0)
134 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
137 json_array_end (json_ctx);
138 json_element_object_end (json_ctx);
142 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, size_t
143 n, int max_char, int exp_result)
152 if (align1 + (n + 1) * CHARBYTES >= page_size)
156 if (align2 + (n + 1) * CHARBYTES >= page_size)
159 json_element_object_begin (json_ctx);
160 json_attr_uint (json_ctx, "strlen", (double) len);
161 json_attr_uint (json_ctx, "len", (double) n);
162 json_attr_uint (json_ctx, "align1", (double) align1);
163 json_attr_uint (json_ctx, "align2", (double) align2);
164 json_array_begin (json_ctx, "timings");
166 FOR_EACH_IMPL (impl, 0)
169 s1 = (CHAR *) (buf1 + align1);
170 s2 = (CHAR *) (buf2 + align2);
172 for (i = 0; i < n; i++)
173 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
175 s1[n] = 24 + exp_result;
181 else if (exp_result > 0)
184 s2[n - 1] -= exp_result;
186 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
189 json_array_end (json_ctx);
190 json_element_object_end (json_ctx);
201 json_init (&json_ctx, 0, stdout);
203 json_document_begin (&json_ctx);
204 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
206 json_attr_object_begin (&json_ctx, "functions");
207 json_attr_object_begin (&json_ctx, TEST_NAME);
208 json_attr_string (&json_ctx, "bench-variant", "default");
210 json_array_begin (&json_ctx, "ifuncs");
211 FOR_EACH_IMPL (impl, 0)
212 json_element_string (&json_ctx, impl->name);
213 json_array_end (&json_ctx);
215 json_array_begin (&json_ctx, "results");
217 for (i =0; i < 16; ++i)
219 do_test (&json_ctx, 0, 0, 8, i, 127, 0);
220 do_test (&json_ctx, 0, 0, 8, i, 127, -1);
221 do_test (&json_ctx, 0, 0, 8, i, 127, 1);
222 do_test (&json_ctx, i, i, 8, i, 127, 0);
223 do_test (&json_ctx, i, i, 8, i, 127, 1);
224 do_test (&json_ctx, i, i, 8, i, 127, -1);
225 do_test (&json_ctx, i, 2 * i, 8, i, 127, 0);
226 do_test (&json_ctx, 2 * i, i, 8, i, 127, 1);
227 do_test (&json_ctx, i, 3 * i, 8, i, 127, -1);
228 do_test (&json_ctx, 0, 0, 8, i, 255, 0);
229 do_test (&json_ctx, 0, 0, 8, i, 255, -1);
230 do_test (&json_ctx, 0, 0, 8, i, 255, 1);
231 do_test (&json_ctx, i, i, 8, i, 255, 0);
232 do_test (&json_ctx, i, i, 8, i, 255, 1);
233 do_test (&json_ctx, i, i, 8, i, 255, -1);
234 do_test (&json_ctx, i, 2 * i, 8, i, 255, 0);
235 do_test (&json_ctx, 2 * i, i, 8, i, 255, 1);
236 do_test (&json_ctx, i, 3 * i, 8, i, 255, -1);
239 for (i = 1; i < 8; ++i)
241 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 0);
242 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 1);
243 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, -1);
244 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 0);
245 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 1);
246 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, -1);
247 do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
248 do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
249 do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 0);
250 do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 1);
253 do_test_limit (&json_ctx, 4, 0, 21, 20, 127, 0);
254 do_test_limit (&json_ctx, 0, 4, 21, 20, 127, 0);
255 do_test_limit (&json_ctx, 8, 0, 25, 24, 127, 0);
256 do_test_limit (&json_ctx, 0, 8, 25, 24, 127, 0);
258 for (i = 0; i < 8; ++i)
260 do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 127, 0);
261 do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 255, 0);
262 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 0);
263 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 1);
264 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, -1);
265 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 0);
266 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 1);
267 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, -1);
270 json_array_end (&json_ctx);
271 json_attr_object_end (&json_ctx);
272 json_attr_object_end (&json_ctx);
273 json_document_end (&json_ctx);
278 #include <support/test-driver.c>