1 /* Measure strncat functions.
2 Copyright (C) 2013-2023 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
38 typedef CHAR
*(*proto_t
) (CHAR
*, const CHAR
*, size_t);
41 generic_strncat (CHAR
*dst
, const CHAR
*src
, size_t n
)
43 CHAR
*end
= dst
+ STRLEN (dst
);
51 IMPL (generic_strncat
, 0)
54 do_one_test (json_ctx_t
*json_ctx
, impl_t
*impl
, CHAR
*dst
, const CHAR
*src
,
57 size_t k
= STRLEN (dst
), i
, iters
= INNER_LOOP_ITERS8
;
58 timing_t start
, stop
, cur
;
60 if (CALL (impl
, dst
, src
, n
) != dst
)
62 error (0, 0, "Wrong result in function %s %p != %p", impl
->name
,
63 CALL (impl
, dst
, src
, n
), dst
);
68 size_t len
= STRLEN (src
);
69 if (MEMCMP (dst
+ k
, src
, len
+ 1 > n
? n
: len
+ 1) != 0)
71 error (0, 0, "Incorrect concatenation in function %s", impl
->name
);
75 if (n
< len
&& dst
[k
+ n
] != '\0')
77 error (0, 0, "There is no zero in the end of output string in %s",
84 for (i
= 0; i
< iters
; ++i
)
87 CALL (impl
, dst
, src
, n
);
91 TIMING_DIFF (cur
, start
, stop
);
93 json_element_double (json_ctx
, (double) cur
/ (double) iters
);
97 do_test (json_ctx_t
*json_ctx
, size_t align1
, size_t align2
, size_t len1
,
98 size_t len2
, size_t n
, int max_char
)
104 if ((align1
+ len1
) * sizeof (CHAR
) >= page_size
)
106 if ((align1
+ n
) * sizeof (CHAR
) > page_size
)
109 if ((align2
+ len1
+ len2
) * sizeof (CHAR
) >= page_size
)
111 if ((align2
+ len1
+ n
) * sizeof (CHAR
) > page_size
)
113 s1
= (CHAR
*) (buf1
) + align1
;
114 s2
= (CHAR
*) (buf2
) + align2
;
116 for (i
= 0; i
< len1
; ++i
)
117 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
120 for (i
= 0; i
< len2
; i
++)
121 s2
[i
] = 32 + 23 * i
% (max_char
- 32);
123 json_element_object_begin (json_ctx
);
124 json_attr_uint (json_ctx
, "align1", align1
);
125 json_attr_uint (json_ctx
, "align2", align2
);
126 json_attr_uint (json_ctx
, "len1", len1
);
127 json_attr_uint (json_ctx
, "len2", len2
);
128 json_attr_uint (json_ctx
, "n", n
);
129 json_attr_uint (json_ctx
, "max_char", max_char
);
131 json_array_begin (json_ctx
, "timings");
133 FOR_EACH_IMPL (impl
, 0)
136 do_one_test (json_ctx
, impl
, s2
, s1
, n
);
139 json_array_end (json_ctx
);
140 json_element_object_end (json_ctx
);
151 json_init (&json_ctx
, 0, stdout
);
153 json_document_begin (&json_ctx
);
154 json_attr_string (&json_ctx
, "timing_type", TIMING_TYPE
);
156 json_attr_object_begin (&json_ctx
, "functions");
157 json_attr_object_begin (&json_ctx
, TEST_NAME
);
158 json_attr_string (&json_ctx
, "bench-variant", "");
160 json_array_begin (&json_ctx
, "ifuncs");
161 FOR_EACH_IMPL (impl
, 0)
162 json_element_string (&json_ctx
, impl
->name
);
163 json_array_end (&json_ctx
);
165 json_array_begin (&json_ctx
, "results");
167 for (n
= 2; n
<= 2048; n
*= 4)
169 do_test (&json_ctx
, 0, 2, 2, 2, n
, SMALL_CHAR
);
170 do_test (&json_ctx
, 0, 0, 4, 4, n
, SMALL_CHAR
);
171 do_test (&json_ctx
, 4, 0, 4, 4, n
, BIG_CHAR
);
172 do_test (&json_ctx
, 0, 0, 8, 8, n
, SMALL_CHAR
);
173 do_test (&json_ctx
, 0, 8, 8, 8, n
, SMALL_CHAR
);
175 for (i
= 1; i
< 8; ++i
)
177 do_test (&json_ctx
, 0, 0, 8 << i
, 8 << i
, n
, SMALL_CHAR
);
178 do_test (&json_ctx
, 8 - i
, 2 * i
, 8 << i
, 8 << i
, n
, SMALL_CHAR
);
179 do_test (&json_ctx
, 0, 0, 8 << i
, 2 << i
, n
, SMALL_CHAR
);
180 do_test (&json_ctx
, 8 - i
, 2 * i
, 8 << i
, 2 << i
, n
, SMALL_CHAR
);
183 for (i
= 1; i
< 8; ++i
)
185 do_test (&json_ctx
, i
, 2 * i
, 8 << i
, 1, n
, SMALL_CHAR
);
186 do_test (&json_ctx
, 2 * i
, i
, 8 << i
, 1, n
, BIG_CHAR
);
187 do_test (&json_ctx
, i
, i
, 8 << i
, 10, n
, SMALL_CHAR
);
191 for (i
= 128; i
< 2048; i
+= i
)
193 for (n
= i
- 64; n
<= i
+ 64; n
+= 32)
195 do_test (&json_ctx
, 1, 0, i
, i
, n
, SMALL_CHAR
);
196 do_test (&json_ctx
, 0, i
, i
, i
, n
, SMALL_CHAR
);
197 do_test (&json_ctx
, 0, 0, i
, i
, n
, SMALL_CHAR
);
198 do_test (&json_ctx
, i
, i
, i
, i
, n
, SMALL_CHAR
);
199 do_test (&json_ctx
, 1, 0, i
, n
, i
, SMALL_CHAR
);
200 do_test (&json_ctx
, 0, i
, i
, n
, i
, SMALL_CHAR
);
201 do_test (&json_ctx
, 0, 0, i
, n
, i
, SMALL_CHAR
);
202 do_test (&json_ctx
, i
, i
, i
, n
, i
, SMALL_CHAR
);
206 json_array_end (&json_ctx
);
207 json_attr_object_end (&json_ctx
);
208 json_attr_object_end (&json_ctx
);
209 json_document_end (&json_ctx
);