Linux: consolidate dup2 implementation
[glibc.git] / benchtests / bench-strncat.c
blob5ccc09a4f8e90051374a84ed0c7d4626b0fe1040
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/>. */
19 #define TEST_MAIN
20 #ifndef WIDE
21 # define TEST_NAME "strncat"
22 #else
23 # define TEST_NAME "wcsncat"
24 # define generic_strncat generic_wcsncat
25 #endif /* WIDE */
26 #include "bench-string.h"
28 #define BIG_CHAR MAX_CHAR
30 #ifndef WIDE
31 # define SMALL_CHAR 127
32 #else
33 # define SMALL_CHAR 1273
34 #endif /* WIDE */
36 #include "json-lib.h"
38 typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
40 CHAR *
41 generic_strncat (CHAR *dst, const CHAR *src, size_t n)
43 CHAR *end = dst + STRLEN (dst);
44 n = STRNLEN (src, n);
45 end[n] = 0;
46 MEMCPY (end, src, n);
47 return dst;
50 IMPL (STRNCAT, 2)
51 IMPL (generic_strncat, 0)
53 static void
54 do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *dst, const CHAR *src,
55 size_t n)
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);
64 ret = 1;
65 return;
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);
72 ret = 1;
73 return;
75 if (n < len && dst[k + n] != '\0')
77 error (0, 0, "There is no zero in the end of output string in %s",
78 impl->name);
79 ret = 1;
80 return;
83 TIMING_NOW (start);
84 for (i = 0; i < iters; ++i)
86 dst[k] = '\0';
87 CALL (impl, dst, src, n);
89 TIMING_NOW (stop);
91 TIMING_DIFF (cur, start, stop);
93 json_element_double (json_ctx, (double) cur / (double) iters);
96 static void
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)
100 size_t i;
101 CHAR *s1, *s2;
103 align1 &= 7;
104 if ((align1 + len1) * sizeof (CHAR) >= page_size)
105 return;
106 if ((align1 + n) * sizeof (CHAR) > page_size)
107 return;
108 align2 &= 7;
109 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
110 return;
111 if ((align2 + len1 + n) * sizeof (CHAR) > page_size)
112 return;
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);
118 s1[len1] = '\0';
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)
135 s2[len2] = '\0';
136 do_one_test (json_ctx, impl, s2, s1, n);
139 json_array_end (json_ctx);
140 json_element_object_end (json_ctx);
144 main (void)
146 json_ctx_t json_ctx;
147 size_t i, n;
149 test_init ();
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);
211 return ret;