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 STRDUP wcsdup
31 # define SIMPLE_STRNCMP simple_wcsncmp
33 /* Wcsncmp uses signed semantics for comparison, not unsigned.
34 Avoid using substraction since possible overflow. */
36 simple_wcsncmp (const CHAR
*s1
, const CHAR
*s2
, size_t n
)
44 if (c1
== L ('\0') || c1
!= c2
)
45 return c1
> c2
? 1 : (c1
< c2
? -1 : 0);
52 # define STRDUP strdup
53 # define SIMPLE_STRNCMP simple_strncmp
55 /* Strncmp uses unsigned semantics for comparison. */
57 simple_strncmp (const char *s1
, const char *s2
, size_t n
)
61 while (n
-- && (ret
= *(unsigned char *) s1
- * (unsigned char *) s2
++) == 0
68 typedef int (*proto_t
) (const CHAR
*, const CHAR
*, size_t);
70 IMPL (SIMPLE_STRNCMP
, 0)
75 do_one_test (json_ctx_t
*json_ctx
, impl_t
*impl
, const CHAR
*s1
, const CHAR
76 *s2
, size_t n
, int exp_result
)
78 size_t i
, iters
= INNER_LOOP_ITERS8
;
79 timing_t start
, stop
, cur
;
82 for (i
= 0; i
< iters
; ++i
)
84 CALL (impl
, s1
, s2
, n
);
88 TIMING_DIFF (cur
, start
, stop
);
90 json_element_double (json_ctx
, (double) cur
/ (double) iters
);
94 do_test_limit (json_ctx_t
*json_ctx
, size_t align1
, size_t align2
, size_t len
,
95 size_t n
, int max_char
, int exp_result
)
102 align_n
= (page_size
- n
* CHARBYTES
) & 15;
104 json_element_object_begin (json_ctx
);
105 json_attr_uint (json_ctx
, "strlen", (double) len
);
106 json_attr_uint (json_ctx
, "len", (double) n
);
107 json_attr_uint (json_ctx
, "align1", (double) align1
);
108 json_attr_uint (json_ctx
, "align2", (double) align2
);
109 json_array_begin (json_ctx
, "timings");
111 FOR_EACH_IMPL (impl
, 0)
114 s1
= (CHAR
*) (buf1
+ page_size
- n
* CHARBYTES
);
115 s2
= (CHAR
*) (buf2
+ page_size
- n
* CHARBYTES
);
117 if (align1
< align_n
)
118 s1
= (CHAR
*) ((char *) s1
- (align_n
- align1
));
120 if (align2
< align_n
)
121 s2
= (CHAR
*) ((char *) s2
- (align_n
- align2
));
123 for (i
= 0; i
< n
; i
++)
124 s1
[i
] = s2
[i
] = 1 + 23 * i
% max_char
;
132 else if (exp_result
> 0)
136 do_one_test (json_ctx
, impl
, s1
, s2
, n
, exp_result
);
139 json_array_end (json_ctx
);
140 json_element_object_end (json_ctx
);
144 do_test (json_ctx_t
*json_ctx
, size_t align1
, size_t align2
, size_t len
, size_t
145 n
, int max_char
, int exp_result
)
154 if (align1
+ (n
+ 1) * CHARBYTES
>= page_size
)
158 if (align2
+ (n
+ 1) * CHARBYTES
>= page_size
)
161 json_element_object_begin (json_ctx
);
162 json_attr_uint (json_ctx
, "strlen", (double) len
);
163 json_attr_uint (json_ctx
, "len", (double) n
);
164 json_attr_uint (json_ctx
, "align1", (double) align1
);
165 json_attr_uint (json_ctx
, "align2", (double) align2
);
166 json_array_begin (json_ctx
, "timings");
168 FOR_EACH_IMPL (impl
, 0)
171 s1
= (CHAR
*) (buf1
+ align1
);
172 s2
= (CHAR
*) (buf2
+ align2
);
174 for (i
= 0; i
< n
; i
++)
175 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
177 s1
[n
] = 24 + exp_result
;
183 else if (exp_result
> 0)
186 s2
[n
- 1] -= exp_result
;
188 do_one_test (json_ctx
, impl
, s1
, s2
, n
, exp_result
);
191 json_array_end (json_ctx
);
192 json_element_object_end (json_ctx
);
196 do_one_test_page_boundary (json_ctx_t
*json_ctx
, CHAR
*s1
, CHAR
*s2
,
197 size_t align1
, size_t align2
, size_t len
,
198 size_t n
, int exp_result
)
200 json_element_object_begin (json_ctx
);
201 json_attr_uint (json_ctx
, "strlen", (double) len
);
202 json_attr_uint (json_ctx
, "len", (double) n
);
203 json_attr_uint (json_ctx
, "align1", (double) align1
);
204 json_attr_uint (json_ctx
, "align2", (double) align2
);
205 json_array_begin (json_ctx
, "timings");
206 FOR_EACH_IMPL (impl
, 0)
207 do_one_test (json_ctx
, impl
, s1
, s2
, n
, exp_result
);
208 json_array_end (json_ctx
);
209 json_element_object_end (json_ctx
);
213 do_test_page_boundary (json_ctx_t
*json_ctx
)
215 /* To trigger bug 25933, we need a size that is equal to the vector
216 length times 4. In the case of AVX2 for Intel, we need 32 * 4. We
217 make this test generic and run it for all architectures as additional
218 boundary testing for such related algorithms. */
219 size_t size
= 32 * 4;
221 CHAR
*s1
= (CHAR
*) (buf1
+ (BUF1PAGES
- 1) * page_size
);
222 CHAR
*s2
= (CHAR
*) (buf2
+ (BUF1PAGES
- 1) * page_size
);
225 memset (s1
, 'a', page_size
);
226 memset (s2
, 'a', page_size
);
228 s1
[(page_size
/ CHARBYTES
) - 1] = (CHAR
) 0;
230 /* Iterate over a size that is just below where we expect the bug to
231 trigger up to the size we expect will trigger the bug e.g. [99-128].
232 Likewise iterate the start of two strings between 30 and 31 bytes
233 away from the boundary to simulate alignment changes. */
234 for (size_t s
= 99; s
<= size
; s
++)
235 for (size_t s1a
= 30; s1a
< 32; s1a
++)
236 for (size_t s2a
= 30; s2a
< 32; s2a
++)
238 size_t align1
= (page_size
/ CHARBYTES
- s
) - s1a
;
239 size_t align2
= (page_size
/ CHARBYTES
- s
) - s2a
;
240 CHAR
*s1p
= s1
+ align1
;
241 CHAR
*s2p
= s2
+ align2
;
242 len
= (page_size
/ CHARBYTES
) - 1 - align1
;
243 exp_result
= SIMPLE_STRNCMP (s1p
, s2p
, s
);
244 do_one_test_page_boundary (json_ctx
, s1p
, s2p
, align1
, align2
,
250 do_one_test_page (json_ctx_t
*json_ctx
, size_t offset1
, size_t offset2
,
256 if (offset1
* CHARBYTES
>= page_size
257 || offset2
* CHARBYTES
>= page_size
)
264 size_t len
= (page_size
/ CHARBYTES
) - offset1
;
268 json_element_object_begin (json_ctx
);
269 json_attr_uint (json_ctx
, "strlen", (double) len
);
270 json_attr_uint (json_ctx
, "len", (double) page_size
);
271 json_attr_uint (json_ctx
, "align1", (double) offset1
);
272 json_attr_uint (json_ctx
, "align2", (double) offset2
);
273 json_array_begin (json_ctx
, "timings");
275 FOR_EACH_IMPL (impl
, 0)
276 do_one_test (json_ctx
, impl
, s1
, s2
, page_size
, -exp_result
);
278 json_array_end (json_ctx
);
279 json_element_object_end (json_ctx
);
281 json_element_object_begin (json_ctx
);
282 json_attr_uint (json_ctx
, "strlen", (double) len
);
283 json_attr_uint (json_ctx
, "len", (double) page_size
);
284 json_attr_uint (json_ctx
, "align1", (double) offset1
);
285 json_attr_uint (json_ctx
, "align2", (double) offset2
);
286 json_array_begin (json_ctx
, "timings");
288 FOR_EACH_IMPL (impl
, 0)
289 do_one_test (json_ctx
, impl
, s1
, s2
, page_size
, exp_result
);
291 json_array_end (json_ctx
);
292 json_element_object_end (json_ctx
);
296 do_test_page (json_ctx_t
*json_ctx
)
302 /* Fill buf1 with 23. */
303 for (i
= 0; i
< (page_size
/ CHARBYTES
) - 1; i
++)
307 /* Make a copy of buf1. */
310 /* Test should terminate within the page boundary. */
311 for (i
= 0; i
< (108 / CHARBYTES
); ++i
)
312 do_one_test_page (json_ctx
, ((page_size
- 108) / CHARBYTES
) + i
,
313 ((page_size
- 1460) / CHARBYTES
), s2
);
326 json_init (&json_ctx
, 0, stdout
);
328 json_document_begin (&json_ctx
);
329 json_attr_string (&json_ctx
, "timing_type", TIMING_TYPE
);
331 json_attr_object_begin (&json_ctx
, "functions");
332 json_attr_object_begin (&json_ctx
, TEST_NAME
);
333 json_attr_string (&json_ctx
, "bench-variant", "default");
335 json_array_begin (&json_ctx
, "ifuncs");
336 FOR_EACH_IMPL (impl
, 0)
337 json_element_string (&json_ctx
, impl
->name
);
338 json_array_end (&json_ctx
);
340 json_array_begin (&json_ctx
, "results");
342 for (i
=0; i
< 16; ++i
)
344 do_test (&json_ctx
, 0, 0, 8, i
, 127, 0);
345 do_test (&json_ctx
, 0, 0, 8, i
, 127, -1);
346 do_test (&json_ctx
, 0, 0, 8, i
, 127, 1);
347 do_test (&json_ctx
, i
, i
, 8, i
, 127, 0);
348 do_test (&json_ctx
, i
, i
, 8, i
, 127, 1);
349 do_test (&json_ctx
, i
, i
, 8, i
, 127, -1);
350 do_test (&json_ctx
, i
, 2 * i
, 8, i
, 127, 0);
351 do_test (&json_ctx
, 2 * i
, i
, 8, i
, 127, 1);
352 do_test (&json_ctx
, i
, 3 * i
, 8, i
, 127, -1);
353 do_test (&json_ctx
, 0, 0, 8, i
, 255, 0);
354 do_test (&json_ctx
, 0, 0, 8, i
, 255, -1);
355 do_test (&json_ctx
, 0, 0, 8, i
, 255, 1);
356 do_test (&json_ctx
, i
, i
, 8, i
, 255, 0);
357 do_test (&json_ctx
, i
, i
, 8, i
, 255, 1);
358 do_test (&json_ctx
, i
, i
, 8, i
, 255, -1);
359 do_test (&json_ctx
, i
, 2 * i
, 8, i
, 255, 0);
360 do_test (&json_ctx
, 2 * i
, i
, 8, i
, 255, 1);
361 do_test (&json_ctx
, i
, 3 * i
, 8, i
, 255, -1);
364 for (i
= 1; i
< 8; ++i
)
366 do_test (&json_ctx
, 0, 0, 8 << i
, 16 << i
, 127, 0);
367 do_test (&json_ctx
, 0, 0, 8 << i
, 16 << i
, 127, 1);
368 do_test (&json_ctx
, 0, 0, 8 << i
, 16 << i
, 127, -1);
369 do_test (&json_ctx
, 0, 0, 8 << i
, 16 << i
, 255, 0);
370 do_test (&json_ctx
, 0, 0, 8 << i
, 16 << i
, 255, 1);
371 do_test (&json_ctx
, 0, 0, 8 << i
, 16 << i
, 255, -1);
372 do_test (&json_ctx
, 8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 0);
373 do_test (&json_ctx
, 8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 1);
374 do_test (&json_ctx
, 2 * i
, i
, 8 << i
, 16 << i
, 255, 0);
375 do_test (&json_ctx
, 2 * i
, i
, 8 << i
, 16 << i
, 255, 1);
378 do_test_limit (&json_ctx
, 4, 0, 21, 20, 127, 0);
379 do_test_limit (&json_ctx
, 0, 4, 21, 20, 127, 0);
380 do_test_limit (&json_ctx
, 8, 0, 25, 24, 127, 0);
381 do_test_limit (&json_ctx
, 0, 8, 25, 24, 127, 0);
383 for (i
= 0; i
< 8; ++i
)
385 do_test_limit (&json_ctx
, 0, 0, 17 - i
, 16 - i
, 127, 0);
386 do_test_limit (&json_ctx
, 0, 0, 17 - i
, 16 - i
, 255, 0);
387 do_test_limit (&json_ctx
, 0, 0, 15 - i
, 16 - i
, 127, 0);
388 do_test_limit (&json_ctx
, 0, 0, 15 - i
, 16 - i
, 127, 1);
389 do_test_limit (&json_ctx
, 0, 0, 15 - i
, 16 - i
, 127, -1);
390 do_test_limit (&json_ctx
, 0, 0, 15 - i
, 16 - i
, 255, 0);
391 do_test_limit (&json_ctx
, 0, 0, 15 - i
, 16 - i
, 255, 1);
392 do_test_limit (&json_ctx
, 0, 0, 15 - i
, 16 - i
, 255, -1);
395 do_test_page_boundary (&json_ctx
);
396 do_test_page (&json_ctx
);
398 json_array_end (&json_ctx
);
399 json_attr_object_end (&json_ctx
);
400 json_attr_object_end (&json_ctx
);
401 json_document_end (&json_ctx
);
406 #include <support/test-driver.c>