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/>. */
21 # define TEST_NAME "wcsncmp"
23 # define TEST_NAME "strncmp"
25 #include "bench-string.h"
31 # define L(str) L##str
32 # define STRNCMP wcsncmp
33 # define SIMPLE_STRNCMP simple_wcsncmp
34 # define STUPID_STRNCMP stupid_wcsncmp
38 /* Wcsncmp uses signed semantics for comparison, not unsigned.
39 Avoid using substraction since possible overflow. */
41 simple_wcsncmp (const CHAR
*s1
, const CHAR
*s2
, size_t n
)
49 if (c1
== L ('\0') || c1
!= c2
)
50 return c1
> c2
? 1 : (c1
< c2
? -1 : 0);
56 stupid_wcsncmp (const CHAR
*s1
, const CHAR
*s2
, size_t n
)
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
;
69 return c1
> c2
? 1 : -1;
76 # define STRNCMP strncmp
77 # define SIMPLE_STRNCMP simple_strncmp
78 # define STUPID_STRNCMP stupid_strncmp
82 /* Strncmp uses unsigned semantics for comparison. */
84 simple_strncmp (const char *s1
, const char *s2
, size_t n
)
88 while (n
-- && (ret
= *(unsigned char *) s1
- * (unsigned char *) s2
++) == 0
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;
99 n
= ns1
< n
? ns1
: n
;
100 n
= ns2
< n
? ns2
: n
;
101 while (n
-- && (ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) == 0);
107 typedef int (*proto_t
) (const CHAR
*, const CHAR
*, size_t);
109 IMPL (STUPID_STRNCMP
, 0)
110 IMPL (SIMPLE_STRNCMP
, 0)
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
;
122 for (i
= 0; i
< iters
; ++i
)
124 CALL (impl
, s1
, s2
, n
);
128 TIMING_DIFF (cur
, start
, stop
);
130 json_element_double (json_ctx
, (double) cur
/ (double) iters
);
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
)
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)
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
;
172 else if (exp_result
> 0)
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
);
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
)
194 if (align1
+ (n
+ 1) * CHARBYTES
>= page_size
)
198 if (align2
+ (n
+ 1) * CHARBYTES
>= page_size
)
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)
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
;
223 else if (exp_result
> 0)
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
);
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
);
320 #include <support/test-driver.c>