1 /* Measure strncpy functions.
2 Copyright (C) 2013-2015 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/>. */
19 #ifndef STRNCPY_RESULT
20 # define STRNCPY_RESULT(dst, len, n) dst
22 # define TEST_NAME "strncpy"
23 # include "bench-string.h"
25 char *simple_strncpy (char *, const char *, size_t);
26 char *stupid_strncpy (char *, const char *, size_t);
28 IMPL (stupid_strncpy
, 0)
29 IMPL (simple_strncpy
, 0)
33 simple_strncpy (char *dst
, const char *src
, size_t n
)
37 if ((*dst
++ = *src
++) == '\0')
47 stupid_strncpy (char *dst
, const char *src
, size_t n
)
49 size_t nc
= strnlen (src
, n
);
52 for (i
= 0; i
< nc
; ++i
)
60 typedef char *(*proto_t
) (char *, const char *, size_t);
63 do_one_test (impl_t
*impl
, char *dst
, const char *src
, size_t len
, size_t n
)
65 size_t i
, iters
= INNER_LOOP_ITERS
;
66 timing_t start
, stop
, cur
;
68 if (CALL (impl
, dst
, src
, n
) != STRNCPY_RESULT (dst
, len
, n
))
70 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
71 CALL (impl
, dst
, src
, n
), dst
);
76 if (memcmp (dst
, src
, len
> n
? n
: len
) != 0)
78 error (0, 0, "Wrong result in function %s", impl
->name
);
87 for (i
= len
; i
< n
; ++i
)
90 error (0, 0, "Wrong result in function %s", impl
->name
);
97 for (i
= 0; i
< iters
; ++i
)
99 CALL (impl
, dst
, src
, n
);
103 TIMING_DIFF (cur
, start
, stop
);
105 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
109 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
)
115 if (align1
+ len
>= page_size
)
119 if (align2
+ len
>= page_size
)
122 s1
= (char *) (buf1
+ align1
);
123 s2
= (char *) (buf2
+ align2
);
125 for (i
= 0; i
< len
; ++i
)
126 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
128 for (i
= len
+ 1; i
+ align1
< page_size
&& i
< len
+ 64; ++i
)
129 s1
[i
] = 32 + 32 * i
% (max_char
- 32);
131 printf ("Length %4zd, n %4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
133 FOR_EACH_IMPL (impl
, 0)
134 do_one_test (impl
, s2
, s1
, len
, n
);
147 FOR_EACH_IMPL (impl
, 0)
148 printf ("\t%s", impl
->name
);
151 for (i
= 1; i
< 8; ++i
)
153 do_test (i
, i
, 16, 16, 127);
154 do_test (i
, i
, 16, 16, 255);
155 do_test (i
, 2 * i
, 16, 16, 127);
156 do_test (2 * i
, i
, 16, 16, 255);
157 do_test (8 - i
, 2 * i
, 1 << i
, 2 << i
, 127);
158 do_test (2 * i
, 8 - i
, 2 << i
, 1 << i
, 127);
159 do_test (8 - i
, 2 * i
, 1 << i
, 2 << i
, 255);
160 do_test (2 * i
, 8 - i
, 2 << i
, 1 << i
, 255);
163 for (i
= 1; i
< 8; ++i
)
165 do_test (0, 0, 4 << i
, 8 << i
, 127);
166 do_test (0, 0, 16 << i
, 8 << i
, 127);
167 do_test (8 - i
, 2 * i
, 4 << i
, 8 << i
, 127);
168 do_test (8 - i
, 2 * i
, 16 << i
, 8 << i
, 127);
174 #include "../test-skeleton.c"