build-many-glibcs.py: --disable-gcov for gcc-first
[glibc.git] / benchtests / bench-strcpy.c
blob0a6bade4b0c6558e3c98421191027ed6dc1e3b6e
1 /* Measure strcpy 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/>. */
19 #define BIG_CHAR MAX_CHAR
21 #ifdef WIDE
22 # define sfmt "ls"
23 # define SMALL_CHAR 1273
24 #else
25 # define sfmt "s"
26 # define SMALL_CHAR 127
27 #endif
29 #include "json-lib.h"
31 #ifndef STRCPY_RESULT
32 # define STRCPY_RESULT(dst, len) dst
33 # define TEST_MAIN
34 # ifndef WIDE
35 # define TEST_NAME "strcpy"
36 # else
37 # define TEST_NAME "wcscpy"
38 # define generic_strcpy generic_wcscpy
39 # endif
40 # include "bench-string.h"
42 CHAR *
43 generic_strcpy (CHAR *dst, const CHAR *src)
45 return MEMCPY (dst, src, STRLEN (src) + 1);
48 IMPL (STRCPY, 1)
49 IMPL (generic_strcpy, 0)
51 #endif
53 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
55 static void
56 do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *dst, const CHAR *src,
57 size_t len __attribute__ ((unused)))
59 size_t i, iters = INNER_LOOP_ITERS;
60 timing_t start, stop, cur;
62 if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len))
64 error (0, 0, "Wrong result in function %s %p %p", impl->name,
65 CALL (impl, dst, src), STRCPY_RESULT (dst, len));
66 ret = 1;
67 return;
70 if (STRCMP (dst, src) != 0)
72 error (0, 0,
73 "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
74 impl->name, dst, src);
75 ret = 1;
76 return;
79 TIMING_NOW (start);
80 for (i = 0; i < iters; ++i)
82 CALL (impl, dst, src);
84 TIMING_NOW (stop);
86 TIMING_DIFF (cur, start, stop);
88 json_element_double (json_ctx, (double) cur / (double) iters);
91 static void
92 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
93 int max_char)
95 size_t i;
96 CHAR *s1, *s2;
97 /* For wcscpy: align1 and align2 here mean alignment not in bytes,
98 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
99 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
100 align1 &= 7;
101 if ((align1 + len) * sizeof (CHAR) >= page_size)
102 return;
104 align2 &= 7;
105 if ((align2 + len) * sizeof (CHAR) >= page_size)
106 return;
108 s1 = (CHAR *) (buf1) + align1;
109 s2 = (CHAR *) (buf2) + align2;
111 for (i = 0; i < len; i++)
112 s1[i] = 32 + 23 * i % (max_char - 32);
113 s1[len] = 0;
115 json_element_object_begin (json_ctx);
116 json_attr_uint (json_ctx, "align1", align1);
117 json_attr_uint (json_ctx, "align2", align2);
118 json_attr_uint (json_ctx, "len", len);
119 json_attr_uint (json_ctx, "max_char", max_char);
121 json_array_begin (json_ctx, "timings");
123 FOR_EACH_IMPL (impl, 0)
124 do_one_test (json_ctx, impl, s2, s1, len);
126 json_array_end (json_ctx);
127 json_element_object_end (json_ctx);
131 test_main (void)
133 json_ctx_t json_ctx;
134 size_t i;
136 test_init ();
138 json_init (&json_ctx, 0, stdout);
140 json_document_begin (&json_ctx);
141 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
143 json_attr_object_begin (&json_ctx, "functions");
144 json_attr_object_begin (&json_ctx, TEST_NAME);
145 json_attr_string (&json_ctx, "bench-variant", "");
147 json_array_begin (&json_ctx, "ifuncs");
148 FOR_EACH_IMPL (impl, 0)
149 json_element_string (&json_ctx, impl->name);
150 json_array_end (&json_ctx);
152 json_array_begin (&json_ctx, "results");
154 for (i = 0; i < 16; ++i)
156 do_test (&json_ctx, 0, 0, i, SMALL_CHAR);
157 do_test (&json_ctx, 0, 0, i, BIG_CHAR);
158 do_test (&json_ctx, 0, i, i, SMALL_CHAR);
159 do_test (&json_ctx, i, 0, i, BIG_CHAR);
162 for (i = 1; i < 8; ++i)
164 do_test (&json_ctx, 0, 0, 8 << i, SMALL_CHAR);
165 do_test (&json_ctx, 8 - i, 2 * i, 8 << i, SMALL_CHAR);
168 for (i = 1; i < 8; ++i)
170 do_test (&json_ctx, i, 2 * i, 8 << i, SMALL_CHAR);
171 do_test (&json_ctx, 2 * i, i, 8 << i, BIG_CHAR);
172 do_test (&json_ctx, i, i, 8 << i, SMALL_CHAR);
173 do_test (&json_ctx, i, i, 8 << i, BIG_CHAR);
176 for (i = 16; i <= 512; i += 4)
178 do_test (&json_ctx, 0, 4, i, SMALL_CHAR);
179 do_test (&json_ctx, 4, 0, i, BIG_CHAR);
180 do_test (&json_ctx, 4, 4, i, SMALL_CHAR);
181 do_test (&json_ctx, 2, 2, i, BIG_CHAR);
182 do_test (&json_ctx, 2, 6, i, SMALL_CHAR);
183 do_test (&json_ctx, 6, 2, i, BIG_CHAR);
184 do_test (&json_ctx, 1, 7, i, SMALL_CHAR);
185 do_test (&json_ctx, 7, 1, i, BIG_CHAR);
186 do_test (&json_ctx, 3, 4, i, SMALL_CHAR);
187 do_test (&json_ctx, 4, 3, i, BIG_CHAR);
188 do_test (&json_ctx, 5, 7, i, SMALL_CHAR);
189 do_test (&json_ctx, 7, 5, i, SMALL_CHAR);
192 for (i = 1; i < 2048; i += i)
194 do_test (&json_ctx, 1, 0, i, SMALL_CHAR);
195 do_test (&json_ctx, 0, i, i, SMALL_CHAR);
196 do_test (&json_ctx, 0, 0, i, SMALL_CHAR);
197 do_test (&json_ctx, i, i, i, SMALL_CHAR);
200 json_array_end (&json_ctx);
201 json_attr_object_end (&json_ctx);
202 json_attr_object_end (&json_ctx);
203 json_document_end (&json_ctx);
205 return ret;
208 #include <support/test-driver.c>