1 /* Measure strncat functions.
2 Copyright (C) 2013-2022 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 "strncat"
23 # define TEST_NAME "wcsncat"
24 # define generic_strncat generic_wcsncat
26 #include "bench-string.h"
28 #define BIG_CHAR MAX_CHAR
31 # define SMALL_CHAR 127
33 # define SMALL_CHAR 1273
36 typedef CHAR
*(*proto_t
) (CHAR
*, const CHAR
*, size_t);
39 generic_strncat (CHAR
*dst
, const CHAR
*src
, size_t n
)
41 CHAR
*end
= dst
+ STRLEN (dst
);
49 IMPL (generic_strncat
, 0)
52 do_one_test (impl_t
*impl
, CHAR
*dst
, const CHAR
*src
, size_t n
)
54 size_t k
= STRLEN (dst
), i
, iters
= INNER_LOOP_ITERS8
;
55 timing_t start
, stop
, cur
;
57 if (CALL (impl
, dst
, src
, n
) != dst
)
59 error (0, 0, "Wrong result in function %s %p != %p", impl
->name
,
60 CALL (impl
, dst
, src
, n
), dst
);
65 size_t len
= STRLEN (src
);
66 if (MEMCMP (dst
+ k
, src
, len
+ 1 > n
? n
: len
+ 1) != 0)
68 error (0, 0, "Incorrect concatenation in function %s",
73 if (n
< len
&& dst
[k
+ n
] != '\0')
75 error (0, 0, "There is no zero in the end of output string in %s",
82 for (i
= 0; i
< iters
; ++i
)
85 CALL (impl
, dst
, src
, n
);
89 TIMING_DIFF (cur
, start
, stop
);
91 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
95 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
,
96 size_t n
, int max_char
)
102 if ((align1
+ len1
) * sizeof (CHAR
) >= page_size
)
104 if ((align1
+ n
) * sizeof (CHAR
) > page_size
)
107 if ((align2
+ len1
+ len2
) * sizeof (CHAR
) >= page_size
)
109 if ((align2
+ len1
+ n
) * sizeof (CHAR
) > page_size
)
111 s1
= (CHAR
*) (buf1
) + align1
;
112 s2
= (CHAR
*) (buf2
) + align2
;
114 for (i
= 0; i
< len1
; ++i
)
115 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
118 for (i
= 0; i
< len2
; i
++)
119 s2
[i
] = 32 + 23 * i
% (max_char
- 32);
121 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
122 len1
, len2
, align1
, align2
, n
);
124 FOR_EACH_IMPL (impl
, 0)
127 do_one_test (impl
, s2
, s1
, n
);
141 FOR_EACH_IMPL (impl
, 0)
142 printf ("\t%s", impl
->name
);
145 for (n
= 2; n
<= 2048; n
*=4)
147 do_test (0, 2, 2, 2, n
, SMALL_CHAR
);
148 do_test (0, 0, 4, 4, n
, SMALL_CHAR
);
149 do_test (4, 0, 4, 4, n
, BIG_CHAR
);
150 do_test (0, 0, 8, 8, n
, SMALL_CHAR
);
151 do_test (0, 8, 8, 8, n
, SMALL_CHAR
);
153 for (i
= 1; i
< 8; ++i
)
155 do_test (0, 0, 8 << i
, 8 << i
, n
, SMALL_CHAR
);
156 do_test (8 - i
, 2 * i
, 8 << i
, 8 << i
, n
, SMALL_CHAR
);
157 do_test (0, 0, 8 << i
, 2 << i
, n
, SMALL_CHAR
);
158 do_test (8 - i
, 2 * i
, 8 << i
, 2 << i
, n
, SMALL_CHAR
);
161 for (i
= 1; i
< 8; ++i
)
163 do_test (i
, 2 * i
, 8 << i
, 1, n
, SMALL_CHAR
);
164 do_test (2 * i
, i
, 8 << i
, 1, n
, BIG_CHAR
);
165 do_test (i
, i
, 8 << i
, 10, n
, SMALL_CHAR
);