x86-64: Check Prefer_FSRM in ifunc-memmove.h
[glibc.git] / benchtests / bench-strncmp.c
blob97dc39069d70ee6b0cff62c6366f19e2ca57246c
1 /* Measure strncmp functions.
2 Copyright (C) 2013-2018 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 <http://www.gnu.org/licenses/>. */
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"
28 #ifdef WIDE
29 # include <wchar.h>
31 # define L(str) L##str
32 # define STRNCMP wcsncmp
33 # define SIMPLE_STRNCMP simple_wcsncmp
34 # define STUPID_STRNCMP stupid_wcsncmp
35 # define CHAR wchar_t
36 # define CHARBYTES 4
38 /* Wcsncmp uses signed semantics for comparison, not unsigned.
39 Avoid using substraction since possible overflow. */
40 int
41 simple_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n)
43 wchar_t c1, c2;
45 while (n--)
47 c1 = *s1++;
48 c2 = *s2++;
49 if (c1 == L ('\0') || c1 != c2)
50 return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0);
52 return 0;
55 int
56 stupid_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n)
58 wchar_t c1, c2;
59 size_t ns1 = wcsnlen (s1, n) + 1, ns2 = wcsnlen (s2, n) + 1;
61 n = ns1 < n ? ns1 : n;
62 n = ns2 < n ? ns2 : n;
64 while (n--)
66 c1 = *s1++;
67 c2 = *s2++;
68 if (c1 != c2)
69 return c1 > c2 ? 1 : -1;
71 return 0;
74 #else
75 # define L(str) str
76 # define STRNCMP strncmp
77 # define SIMPLE_STRNCMP simple_strncmp
78 # define STUPID_STRNCMP stupid_strncmp
79 # define CHAR char
80 # define CHARBYTES 1
82 /* Strncmp uses unsigned semantics for comparison. */
83 int
84 simple_strncmp (const char *s1, const char *s2, size_t n)
86 int ret = 0;
88 while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
89 && *s1++);
90 return ret;
93 int
94 stupid_strncmp (const char *s1, const char *s2, size_t n)
96 size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
97 int ret = 0;
99 n = ns1 < n ? ns1 : n;
100 n = ns2 < n ? ns2 : n;
101 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
102 return ret;
105 #endif /* !WIDE */
107 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
109 IMPL (STUPID_STRNCMP, 0)
110 IMPL (SIMPLE_STRNCMP, 0)
111 IMPL (STRNCMP, 1)
114 static void
115 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR
116 *s2, size_t n, int exp_result)
118 size_t i, iters = INNER_LOOP_ITERS;
119 timing_t start, stop, cur;
121 TIMING_NOW (start);
122 for (i = 0; i < iters; ++i)
124 CALL (impl, s1, s2, n);
126 TIMING_NOW (stop);
128 TIMING_DIFF (cur, start, stop);
130 json_element_double (json_ctx, (double) cur / (double) iters);
133 static void
134 do_test_limit (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
135 size_t n, int max_char, int exp_result)
137 size_t i, align_n;
138 CHAR *s1, *s2;
140 align1 &= 15;
141 align2 &= 15;
142 align_n = (page_size - n * CHARBYTES) & 15;
144 json_element_object_begin (json_ctx);
145 json_attr_uint (json_ctx, "strlen", (double) len);
146 json_attr_uint (json_ctx, "len", (double) n);
147 json_attr_uint (json_ctx, "align1", (double) align1);
148 json_attr_uint (json_ctx, "align2", (double) align2);
149 json_array_begin (json_ctx, "timings");
151 FOR_EACH_IMPL (impl, 0)
153 realloc_bufs ();
154 s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES);
155 s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES);
157 if (align1 < align_n)
158 s1 = (CHAR *) ((char *) s1 - (align_n - align1));
160 if (align2 < align_n)
161 s2 = (CHAR *) ((char *) s2 - (align_n - align2));
163 for (i = 0; i < n; i++)
164 s1[i] = s2[i] = 1 + 23 * i % max_char;
166 if (len < n)
168 s1[len] = 0;
169 s2[len] = 0;
170 if (exp_result < 0)
171 s2[len] = 32;
172 else if (exp_result > 0)
173 s1[len] = 64;
176 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
179 json_array_end (json_ctx);
180 json_element_object_end (json_ctx);
183 static void
184 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, size_t
185 n, int max_char, int exp_result)
187 size_t i;
188 CHAR *s1, *s2;
190 if (n == 0)
191 return;
193 align1 &= 63;
194 if (align1 + (n + 1) * CHARBYTES >= page_size)
195 return;
197 align2 &= 7;
198 if (align2 + (n + 1) * CHARBYTES >= page_size)
199 return;
201 json_element_object_begin (json_ctx);
202 json_attr_uint (json_ctx, "strlen", (double) len);
203 json_attr_uint (json_ctx, "len", (double) n);
204 json_attr_uint (json_ctx, "align1", (double) align1);
205 json_attr_uint (json_ctx, "align2", (double) align2);
206 json_array_begin (json_ctx, "timings");
208 FOR_EACH_IMPL (impl, 0)
210 realloc_bufs ();
211 s1 = (CHAR *) (buf1 + align1);
212 s2 = (CHAR *) (buf2 + align2);
214 for (i = 0; i < n; i++)
215 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
217 s1[n] = 24 + exp_result;
218 s2[n] = 23;
219 s1[len] = 0;
220 s2[len] = 0;
221 if (exp_result < 0)
222 s2[len] = 32;
223 else if (exp_result > 0)
224 s1[len] = 64;
225 if (len >= n)
226 s2[n - 1] -= exp_result;
228 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
231 json_array_end (json_ctx);
232 json_element_object_end (json_ctx);
236 test_main (void)
238 json_ctx_t json_ctx;
239 size_t i;
241 test_init ();
243 json_init (&json_ctx, 0, stdout);
245 json_document_begin (&json_ctx);
246 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
248 json_attr_object_begin (&json_ctx, "functions");
249 json_attr_object_begin (&json_ctx, TEST_NAME);
250 json_attr_string (&json_ctx, "bench-variant", "default");
252 json_array_begin (&json_ctx, "ifuncs");
253 FOR_EACH_IMPL (impl, 0)
254 json_element_string (&json_ctx, impl->name);
255 json_array_end (&json_ctx);
257 json_array_begin (&json_ctx, "results");
259 for (i =0; i < 16; ++i)
261 do_test (&json_ctx, 0, 0, 8, i, 127, 0);
262 do_test (&json_ctx, 0, 0, 8, i, 127, -1);
263 do_test (&json_ctx, 0, 0, 8, i, 127, 1);
264 do_test (&json_ctx, i, i, 8, i, 127, 0);
265 do_test (&json_ctx, i, i, 8, i, 127, 1);
266 do_test (&json_ctx, i, i, 8, i, 127, -1);
267 do_test (&json_ctx, i, 2 * i, 8, i, 127, 0);
268 do_test (&json_ctx, 2 * i, i, 8, i, 127, 1);
269 do_test (&json_ctx, i, 3 * i, 8, i, 127, -1);
270 do_test (&json_ctx, 0, 0, 8, i, 255, 0);
271 do_test (&json_ctx, 0, 0, 8, i, 255, -1);
272 do_test (&json_ctx, 0, 0, 8, i, 255, 1);
273 do_test (&json_ctx, i, i, 8, i, 255, 0);
274 do_test (&json_ctx, i, i, 8, i, 255, 1);
275 do_test (&json_ctx, i, i, 8, i, 255, -1);
276 do_test (&json_ctx, i, 2 * i, 8, i, 255, 0);
277 do_test (&json_ctx, 2 * i, i, 8, i, 255, 1);
278 do_test (&json_ctx, i, 3 * i, 8, i, 255, -1);
281 for (i = 1; i < 8; ++i)
283 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 0);
284 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 1);
285 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, -1);
286 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 0);
287 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 1);
288 do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, -1);
289 do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
290 do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
291 do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 0);
292 do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 1);
295 do_test_limit (&json_ctx, 4, 0, 21, 20, 127, 0);
296 do_test_limit (&json_ctx, 0, 4, 21, 20, 127, 0);
297 do_test_limit (&json_ctx, 8, 0, 25, 24, 127, 0);
298 do_test_limit (&json_ctx, 0, 8, 25, 24, 127, 0);
300 for (i = 0; i < 8; ++i)
302 do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 127, 0);
303 do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 255, 0);
304 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 0);
305 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 1);
306 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, -1);
307 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 0);
308 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 1);
309 do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, -1);
312 json_array_end (&json_ctx);
313 json_attr_object_end (&json_ctx);
314 json_attr_object_end (&json_ctx);
315 json_document_end (&json_ctx);
317 return ret;
320 #include <support/test-driver.c>