1 /* Measure strncmp functions.
2 Copyright (C) 2013-2014 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/>. */
20 #define TEST_NAME "strncmp"
21 #include "bench-string.h"
23 typedef int (*proto_t
) (const char *, const char *, size_t);
24 int simple_strncmp (const char *, const char *, size_t);
25 int stupid_strncmp (const char *, const char *, size_t);
27 IMPL (stupid_strncmp
, 0)
28 IMPL (simple_strncmp
, 0)
32 simple_strncmp (const char *s1
, const char *s2
, size_t n
)
36 while (n
-- && (ret
= *(unsigned char *) s1
- * (unsigned char *) s2
++) == 0
42 stupid_strncmp (const char *s1
, const char *s2
, size_t n
)
44 size_t ns1
= strnlen (s1
, n
) + 1, ns2
= strnlen (s2
, n
) + 1;
47 n
= ns1
< n
? ns1
: n
;
48 n
= ns2
< n
? ns2
: n
;
49 while (n
-- && (ret
= *(unsigned char *) s1
++ - * (unsigned char *) s2
++) == 0);
54 do_one_test (impl_t
*impl
, const char *s1
, const char *s2
, size_t n
,
57 size_t i
, iters
= INNER_LOOP_ITERS
;
58 timing_t start
, stop
, cur
;
61 for (i
= 0; i
< iters
; ++i
)
63 CALL (impl
, s1
, s2
, n
);
67 TIMING_DIFF (cur
, start
, stop
);
69 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
73 do_test_limit (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
81 s1
= (char*)(buf1
+ page_size
);
82 s2
= (char*)(buf2
+ page_size
);
83 printf ("Length %4zd/%4zd:", len
, n
);
85 FOR_EACH_IMPL (impl
, 0)
86 do_one_test (impl
, s1
, s2
, n
, 0);
95 align_n
= (page_size
- n
) & 15;
97 s1
= (char*)(buf1
+ page_size
- n
);
98 s2
= (char*)(buf2
+ page_size
- n
);
100 if (align1
< align_n
)
101 s1
-= (align_n
- align1
);
103 if (align2
< align_n
)
104 s2
-= (align_n
- align2
);
106 for (i
= 0; i
< n
; i
++)
107 s1
[i
] = s2
[i
] = 1 + 23 * i
% max_char
;
115 else if (exp_result
> 0)
119 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
121 FOR_EACH_IMPL (impl
, 0)
122 do_one_test (impl
, s1
, s2
, n
, exp_result
);
128 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
138 if (align1
+ n
+ 1 >= page_size
)
142 if (align2
+ n
+ 1 >= page_size
)
145 s1
= (char*)(buf1
+ align1
);
146 s2
= (char*)(buf2
+ align2
);
148 for (i
= 0; i
< n
; i
++)
149 s1
[i
] = s2
[i
] = 1 + 23 * i
% max_char
;
151 s1
[n
] = 24 + exp_result
;
157 else if (exp_result
> 0)
160 s2
[n
- 1] -= exp_result
;
162 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
164 FOR_EACH_IMPL (impl
, 0)
165 do_one_test (impl
, (char*)s1
, (char*)s2
, n
, exp_result
);
178 FOR_EACH_IMPL (impl
, 0)
179 printf ("\t%s", impl
->name
);
182 for (i
=0; i
< 16; ++i
)
184 do_test (0, 0, 8, i
, 127, 0);
185 do_test (0, 0, 8, i
, 127, -1);
186 do_test (0, 0, 8, i
, 127, 1);
187 do_test (i
, i
, 8, i
, 127, 0);
188 do_test (i
, i
, 8, i
, 127, 1);
189 do_test (i
, i
, 8, i
, 127, -1);
190 do_test (i
, 2 * i
, 8, i
, 127, 0);
191 do_test (2 * i
, i
, 8, i
, 127, 1);
192 do_test (i
, 3 * i
, 8, i
, 127, -1);
193 do_test (0, 0, 8, i
, 255, 0);
194 do_test (0, 0, 8, i
, 255, -1);
195 do_test (0, 0, 8, i
, 255, 1);
196 do_test (i
, i
, 8, i
, 255, 0);
197 do_test (i
, i
, 8, i
, 255, 1);
198 do_test (i
, i
, 8, i
, 255, -1);
199 do_test (i
, 2 * i
, 8, i
, 255, 0);
200 do_test (2 * i
, i
, 8, i
, 255, 1);
201 do_test (i
, 3 * i
, 8, i
, 255, -1);
204 for (i
= 1; i
< 8; ++i
)
206 do_test (0, 0, 8 << i
, 16 << i
, 127, 0);
207 do_test (0, 0, 8 << i
, 16 << i
, 127, 1);
208 do_test (0, 0, 8 << i
, 16 << i
, 127, -1);
209 do_test (0, 0, 8 << i
, 16 << i
, 255, 0);
210 do_test (0, 0, 8 << i
, 16 << i
, 255, 1);
211 do_test (0, 0, 8 << i
, 16 << i
, 255, -1);
212 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 0);
213 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 1);
214 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 0);
215 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 1);
218 do_test_limit (0, 0, 0, 0, 127, 0);
219 do_test_limit (4, 0, 21, 20, 127, 0);
220 do_test_limit (0, 4, 21, 20, 127, 0);
221 do_test_limit (8, 0, 25, 24, 127, 0);
222 do_test_limit (0, 8, 25, 24, 127, 0);
224 for (i
= 0; i
< 8; ++i
)
226 do_test_limit (0, 0, 17 - i
, 16 - i
, 127, 0);
227 do_test_limit (0, 0, 17 - i
, 16 - i
, 255, 0);
228 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 0);
229 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 1);
230 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, -1);
231 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 0);
232 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 1);
233 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, -1);
239 #include "../test-skeleton.c"