1 /* Measure strncmp functions.
2 Copyright (C) 2013-2017 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"
30 # define L(str) L##str
31 # define STRNCMP wcsncmp
32 # define SIMPLE_STRNCMP simple_wcsncmp
33 # define STUPID_STRNCMP stupid_wcsncmp
37 /* Wcsncmp uses signed semantics for comparison, not unsigned.
38 Avoid using substraction since possible overflow. */
40 simple_wcsncmp (const CHAR
*s1
, const CHAR
*s2
, size_t n
)
48 if (c1
== L ('\0') || c1
!= c2
)
49 return c1
> c2
? 1 : (c1
< c2
? -1 : 0);
55 stupid_wcsncmp (const CHAR
*s1
, const CHAR
*s2
, size_t n
)
58 size_t ns1
= wcsnlen (s1
, n
) + 1, ns2
= wcsnlen (s2
, n
) + 1;
60 n
= ns1
< n
? ns1
: n
;
61 n
= ns2
< n
? ns2
: n
;
68 return c1
> c2
? 1 : -1;
75 # define STRNCMP strncmp
76 # define SIMPLE_STRNCMP simple_strncmp
77 # define STUPID_STRNCMP stupid_strncmp
81 /* Strncmp uses unsigned semantics for comparison. */
83 simple_strncmp (const char *s1
, const char *s2
, size_t n
)
87 while (n
-- && (ret
= *(unsigned char *) s1
- * (unsigned char *) s2
++) == 0
93 stupid_strncmp (const char *s1
, const char *s2
, size_t n
)
95 size_t ns1
= strnlen (s1
, n
) + 1, ns2
= strnlen (s2
, n
) + 1;
98 n
= ns1
< n
? ns1
: n
;
99 n
= ns2
< n
? ns2
: n
;
100 while (n
-- && (ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) == 0);
106 typedef int (*proto_t
) (const CHAR
*, const CHAR
*, size_t);
108 IMPL (STUPID_STRNCMP
, 0)
109 IMPL (SIMPLE_STRNCMP
, 0)
114 do_one_test (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t n
,
117 size_t i
, iters
= INNER_LOOP_ITERS
;
118 timing_t start
, stop
, cur
;
121 for (i
= 0; i
< iters
; ++i
)
123 CALL (impl
, s1
, s2
, n
);
127 TIMING_DIFF (cur
, start
, stop
);
129 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
133 do_test_limit (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
141 s1
= (CHAR
*) (buf1
+ page_size
);
142 s2
= (CHAR
*) (buf2
+ page_size
);
143 printf ("Length %4zd/%4zd:", len
, n
);
145 FOR_EACH_IMPL (impl
, 0)
146 do_one_test (impl
, s1
, s2
, n
, 0);
155 align_n
= (page_size
- n
* CHARBYTES
) & 15;
157 s1
= (CHAR
*) (buf1
+ page_size
- n
* CHARBYTES
);
158 s2
= (CHAR
*) (buf2
+ page_size
- n
* CHARBYTES
);
160 if (align1
< align_n
)
161 s1
= (CHAR
*) ((char *) s1
- (align_n
- align1
));
163 if (align2
< align_n
)
164 s2
= (CHAR
*) ((char *) s2
- (align_n
- align2
));
166 for (i
= 0; i
< n
; i
++)
167 s1
[i
] = s2
[i
] = 1 + 23 * i
% max_char
;
175 else if (exp_result
> 0)
179 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
181 FOR_EACH_IMPL (impl
, 0)
182 do_one_test (impl
, s1
, s2
, n
, exp_result
);
188 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
198 if (align1
+ (n
+ 1) * CHARBYTES
>= page_size
)
202 if (align2
+ (n
+ 1) * CHARBYTES
>= page_size
)
205 s1
= (CHAR
*) (buf1
+ align1
);
206 s2
= (CHAR
*) (buf2
+ align2
);
208 for (i
= 0; i
< n
; i
++)
209 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
211 s1
[n
] = 24 + exp_result
;
217 else if (exp_result
> 0)
220 s2
[n
- 1] -= exp_result
;
222 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
224 FOR_EACH_IMPL (impl
, 0)
225 do_one_test (impl
, s1
, s2
, n
, exp_result
);
238 FOR_EACH_IMPL (impl
, 0)
239 printf ("\t%s", impl
->name
);
242 for (i
=0; i
< 16; ++i
)
244 do_test (0, 0, 8, i
, 127, 0);
245 do_test (0, 0, 8, i
, 127, -1);
246 do_test (0, 0, 8, i
, 127, 1);
247 do_test (i
, i
, 8, i
, 127, 0);
248 do_test (i
, i
, 8, i
, 127, 1);
249 do_test (i
, i
, 8, i
, 127, -1);
250 do_test (i
, 2 * i
, 8, i
, 127, 0);
251 do_test (2 * i
, i
, 8, i
, 127, 1);
252 do_test (i
, 3 * i
, 8, i
, 127, -1);
253 do_test (0, 0, 8, i
, 255, 0);
254 do_test (0, 0, 8, i
, 255, -1);
255 do_test (0, 0, 8, i
, 255, 1);
256 do_test (i
, i
, 8, i
, 255, 0);
257 do_test (i
, i
, 8, i
, 255, 1);
258 do_test (i
, i
, 8, i
, 255, -1);
259 do_test (i
, 2 * i
, 8, i
, 255, 0);
260 do_test (2 * i
, i
, 8, i
, 255, 1);
261 do_test (i
, 3 * i
, 8, i
, 255, -1);
264 for (i
= 1; i
< 8; ++i
)
266 do_test (0, 0, 8 << i
, 16 << i
, 127, 0);
267 do_test (0, 0, 8 << i
, 16 << i
, 127, 1);
268 do_test (0, 0, 8 << i
, 16 << i
, 127, -1);
269 do_test (0, 0, 8 << i
, 16 << i
, 255, 0);
270 do_test (0, 0, 8 << i
, 16 << i
, 255, 1);
271 do_test (0, 0, 8 << i
, 16 << i
, 255, -1);
272 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 0);
273 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 1);
274 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 0);
275 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 1);
278 do_test_limit (0, 0, 0, 0, 127, 0);
279 do_test_limit (4, 0, 21, 20, 127, 0);
280 do_test_limit (0, 4, 21, 20, 127, 0);
281 do_test_limit (8, 0, 25, 24, 127, 0);
282 do_test_limit (0, 8, 25, 24, 127, 0);
284 for (i
= 0; i
< 8; ++i
)
286 do_test_limit (0, 0, 17 - i
, 16 - i
, 127, 0);
287 do_test_limit (0, 0, 17 - i
, 16 - i
, 255, 0);
288 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 0);
289 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 1);
290 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, -1);
291 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 0);
292 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 1);
293 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, -1);
299 #include <support/test-driver.c>