hurd: fix build of tst-system.c
[glibc.git] / benchtests / bench-memcpy.c
blob1fd41c035fe4fae74d576f38db78ad2eadb8748a
1 /* Measure memcpy 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 #ifndef MEMCPY_RESULT
20 # define MEMCPY_RESULT(dst, len) dst
21 # define MIN_PAGE_SIZE 131072
22 # define TEST_MAIN
23 # define TEST_NAME "memcpy"
24 # include "bench-string.h"
26 void *generic_memcpy (void *, const void *, size_t);
28 IMPL (memcpy, 1)
29 IMPL (generic_memcpy, 0)
31 #endif
33 # include "json-lib.h"
35 typedef void *(*proto_t) (void *, const void *, size_t);
37 static void
38 do_one_test (json_ctx_t *json_ctx, impl_t *impl, char *dst, const char *src,
39 size_t len)
41 size_t i, iters = INNER_LOOP_ITERS;
42 timing_t start, stop, cur;
43 for (i = 0; i < iters / 64; ++i)
45 CALL (impl, dst, src, len);
47 TIMING_NOW (start);
48 for (i = 0; i < iters; ++i)
50 CALL (impl, dst, src, len);
52 TIMING_NOW (stop);
54 TIMING_DIFF (cur, start, stop);
56 json_element_double (json_ctx, (double) cur / (double) iters);
59 static void
60 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
61 int both_ways)
63 size_t i, j;
64 char *s1, *s2;
65 size_t repeats;
66 align1 &= (getpagesize () - 1);
67 if (align1 + len >= page_size)
68 return;
70 align2 &= (getpagesize () - 1);
71 if (align2 + len >= page_size)
72 return;
74 s1 = (char *) (buf1 + align1);
75 s2 = (char *) (buf2 + align2);
77 for (repeats = both_ways ? 2 : 1; repeats; --repeats)
79 for (i = 0, j = 1; i < len; i++, j += 23)
80 s1[i] = j;
82 json_element_object_begin (json_ctx);
83 json_attr_uint (json_ctx, "length", (double) len);
84 json_attr_uint (json_ctx, "align1", (double) align1);
85 json_attr_uint (json_ctx, "align2", (double) align2);
86 json_attr_uint (json_ctx, "dst > src", (double) (s2 > s1));
87 json_array_begin (json_ctx, "timings");
89 FOR_EACH_IMPL (impl, 0)
90 do_one_test (json_ctx, impl, s2, s1, len);
92 json_array_end (json_ctx);
93 json_element_object_end (json_ctx);
95 s1 = (char *) (buf2 + align1);
96 s2 = (char *) (buf1 + align2);
101 test_main (void)
103 json_ctx_t json_ctx;
104 size_t i;
105 size_t half_page = getpagesize () / 2;
106 test_init ();
108 json_init (&json_ctx, 0, stdout);
110 json_document_begin (&json_ctx);
111 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
113 json_attr_object_begin (&json_ctx, "functions");
114 json_attr_object_begin (&json_ctx, TEST_NAME);
115 json_attr_string (&json_ctx, "bench-variant", "default");
117 json_array_begin (&json_ctx, "ifuncs");
118 FOR_EACH_IMPL (impl, 0)
119 json_element_string (&json_ctx, impl->name);
120 json_array_end (&json_ctx);
122 json_array_begin (&json_ctx, "results");
123 for (i = 0; i < 18; ++i)
125 do_test (&json_ctx, 0, 0, 1 << i, 1);
126 do_test (&json_ctx, i, 0, 1 << i, 1);
127 do_test (&json_ctx, i + 32, 0, 1 << i, 1);
128 do_test (&json_ctx, 0, i, 1 << i, 1);
129 do_test (&json_ctx, 0, i + 32, 1 << i, 1);
130 do_test (&json_ctx, i, i, 1 << i, 1);
131 do_test (&json_ctx, i + 32, i + 32, 1 << i, 1);
132 do_test (&json_ctx, half_page, 0, 1 << i, 1);
133 do_test (&json_ctx, half_page + i, 0, 1 << i, 1);
134 do_test (&json_ctx, half_page, i, 1 << i, 1);
135 do_test (&json_ctx, half_page + i, i, 1 << i, 1);
138 for (i = 0; i < 32; ++i)
140 do_test (&json_ctx, 0, 0, i, 0);
141 do_test (&json_ctx, i, 0, i, 0);
142 do_test (&json_ctx, 0, i, i, 0);
143 do_test (&json_ctx, i, i, i, 0);
144 do_test (&json_ctx, half_page, 0, i, 0);
145 do_test (&json_ctx, half_page + i, 0, i, 0);
146 do_test (&json_ctx, half_page, i, i, 0);
147 do_test (&json_ctx, half_page + i, i, i, 0);
148 do_test (&json_ctx, getpagesize () - 1, 0, i, 0);
149 do_test (&json_ctx, 0, getpagesize () - 1, i, 0);
152 for (i = 3; i < 32; ++i)
154 if ((i & (i - 1)) == 0)
155 continue;
156 do_test (&json_ctx, 0, 0, 16 * i, 1);
157 do_test (&json_ctx, i, 0, 16 * i, 1);
158 do_test (&json_ctx, 0, i, 16 * i, 1);
159 do_test (&json_ctx, i, i, 16 * i, 1);
160 do_test (&json_ctx, half_page, 0, 16 * i, 1);
161 do_test (&json_ctx, half_page + i, 0, 16 * i, 1);
162 do_test (&json_ctx, half_page, i, 16 * i, 1);
163 do_test (&json_ctx, half_page + i, i, 16 * i, 1);
166 for (i = 32; i < 64; ++i)
168 do_test (&json_ctx, 0, 0, 32 * i, 1);
169 do_test (&json_ctx, i, 0, 32 * i, 1);
170 do_test (&json_ctx, 0, i, 32 * i, 1);
171 do_test (&json_ctx, i, i, 32 * i, 1);
172 do_test (&json_ctx, half_page, 0, 32 * i, 1);
173 do_test (&json_ctx, half_page + i, 0, 32 * i, 1);
174 do_test (&json_ctx, half_page, i, 32 * i, 1);
175 do_test (&json_ctx, half_page + i, i, 32 * i, 1);
178 do_test (&json_ctx, 0, 0, getpagesize (), 1);
180 for (i = 0; i <= 48; ++i)
182 do_test (&json_ctx, 0, 0, 2048 + 64 * i, 1);
183 do_test (&json_ctx, i, 0, 2048 + 64 * i, 1);
184 do_test (&json_ctx, i + 32, 0, 2048 + 64 * i, 1);
185 do_test (&json_ctx, 0, i, 2048 + 64 * i, 1);
186 do_test (&json_ctx, 0, i + 32, 2048 + 64 * i, 1);
187 do_test (&json_ctx, i, i, 2048 + 64 * i, 1);
188 do_test (&json_ctx, i + 32, i + 32, 2048 + 64 * i, 1);
189 do_test (&json_ctx, half_page, 0, 2048 + 64 * i, 1);
190 do_test (&json_ctx, half_page + i, 0, 2048 + 64 * i, 1);
191 do_test (&json_ctx, half_page, i, 2048 + 64 * i, 1);
192 do_test (&json_ctx, half_page + i, i, 2048 + 64 * i, 1);
193 do_test (&json_ctx, i, 1, 2048 + 64 * i, 1);
194 do_test (&json_ctx, 1, i, 2048 + 64 * i, 1);
195 do_test (&json_ctx, i + 32, 1, 2048 + 64 * i, 1);
196 do_test (&json_ctx, 1, i + 32, 2048 + 64 * i, 1);
197 do_test (&json_ctx, half_page + i, 1, 2048 + 64 * i, 1);
198 do_test (&json_ctx, half_page + 1, i, 2048 + 64 * i, 1);
201 json_array_end (&json_ctx);
202 json_attr_object_end (&json_ctx);
203 json_attr_object_end (&json_ctx);
204 json_document_end (&json_ctx);
206 return ret;
209 #include <support/test-driver.c>
211 #define libc_hidden_builtin_def(X)
212 #undef MEMCPY
213 #define MEMCPY generic_memcpy
214 #include <string/memcpy.c>
215 #include <string/wordcopy.c>