]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strncmp.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / benchtests / bench-strncmp.c
1 /* Measure strncmp functions.
2 Copyright (C) 2013-2019 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 #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
32 /* Wcsncmp uses signed semantics for comparison, not unsigned.
33 Avoid using substraction since possible overflow. */
34 int
35 simple_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n)
36 {
37 wchar_t c1, c2;
38
39 while (n--)
40 {
41 c1 = *s1++;
42 c2 = *s2++;
43 if (c1 == L ('\0') || c1 != c2)
44 return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0);
45 }
46 return 0;
47 }
48
49 #else
50 # define L(str) str
51 # define SIMPLE_STRNCMP simple_strncmp
52
53 /* Strncmp uses unsigned semantics for comparison. */
54 int
55 simple_strncmp (const char *s1, const char *s2, size_t n)
56 {
57 int ret = 0;
58
59 while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
60 && *s1++);
61 return ret;
62 }
63
64 #endif /* !WIDE */
65
66 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
67
68 IMPL (SIMPLE_STRNCMP, 0)
69 IMPL (STRNCMP, 1)
70
71
72 static void
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)
75 {
76 size_t i, iters = INNER_LOOP_ITERS8;
77 timing_t start, stop, cur;
78
79 TIMING_NOW (start);
80 for (i = 0; i < iters; ++i)
81 {
82 CALL (impl, s1, s2, n);
83 }
84 TIMING_NOW (stop);
85
86 TIMING_DIFF (cur, start, stop);
87
88 json_element_double (json_ctx, (double) cur / (double) iters);
89 }
90
91 static void
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)
94 {
95 size_t i, align_n;
96 CHAR *s1, *s2;
97
98 align1 &= 15;
99 align2 &= 15;
100 align_n = (page_size - n * CHARBYTES) & 15;
101
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");
108
109 FOR_EACH_IMPL (impl, 0)
110 {
111 alloc_bufs ();
112 s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES);
113 s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES);
114
115 if (align1 < align_n)
116 s1 = (CHAR *) ((char *) s1 - (align_n - align1));
117
118 if (align2 < align_n)
119 s2 = (CHAR *) ((char *) s2 - (align_n - align2));
120
121 for (i = 0; i < n; i++)
122 s1[i] = s2[i] = 1 + 23 * i % max_char;
123
124 if (len < n)
125 {
126 s1[len] = 0;
127 s2[len] = 0;
128 if (exp_result < 0)
129 s2[len] = 32;
130 else if (exp_result > 0)
131 s1[len] = 64;
132 }
133
134 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
135 }
136
137 json_array_end (json_ctx);
138 json_element_object_end (json_ctx);
139 }
140
141 static void
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)
144 {
145 size_t i;
146 CHAR *s1, *s2;
147
148 if (n == 0)
149 return;
150
151 align1 &= 63;
152 if (align1 + (n + 1) * CHARBYTES >= page_size)
153 return;
154
155 align2 &= 7;
156 if (align2 + (n + 1) * CHARBYTES >= page_size)
157 return;
158
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");
165
166 FOR_EACH_IMPL (impl, 0)
167 {
168 alloc_bufs ();
169 s1 = (CHAR *) (buf1 + align1);
170 s2 = (CHAR *) (buf2 + align2);
171
172 for (i = 0; i < n; i++)
173 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
174
175 s1[n] = 24 + exp_result;
176 s2[n] = 23;
177 s1[len] = 0;
178 s2[len] = 0;
179 if (exp_result < 0)
180 s2[len] = 32;
181 else if (exp_result > 0)
182 s1[len] = 64;
183 if (len >= n)
184 s2[n - 1] -= exp_result;
185
186 do_one_test (json_ctx, impl, s1, s2, n, exp_result);
187 }
188
189 json_array_end (json_ctx);
190 json_element_object_end (json_ctx);
191 }
192
193 int
194 test_main (void)
195 {
196 json_ctx_t json_ctx;
197 size_t i;
198
199 test_init ();
200
201 json_init (&json_ctx, 0, stdout);
202
203 json_document_begin (&json_ctx);
204 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
205
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");
209
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);
214
215 json_array_begin (&json_ctx, "results");
216
217 for (i =0; i < 16; ++i)
218 {
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);
237 }
238
239 for (i = 1; i < 8; ++i)
240 {
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);
251 }
252
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);
257
258 for (i = 0; i < 8; ++i)
259 {
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);
268 }
269
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);
274
275 return ret;
276 }
277
278 #include <support/test-driver.c>