Reorganize and revise NEWS for 2.26.
[glibc.git] / benchtests / bench-memcpy.c
blob92ecc64e824a541cfde2e02231634be5e2a95b6c
1 /* Measure memcpy functions.
2 Copyright (C) 2013-2017 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 <http://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 char *simple_memcpy (char *, const char *, size_t);
27 char *builtin_memcpy (char *, const char *, size_t);
29 IMPL (simple_memcpy, 0)
30 IMPL (builtin_memcpy, 0)
31 IMPL (memcpy, 1)
33 char *
34 simple_memcpy (char *dst, const char *src, size_t n)
36 char *ret = dst;
37 while (n--)
38 *dst++ = *src++;
39 return ret;
42 char *
43 builtin_memcpy (char *dst, const char *src, size_t n)
45 return __builtin_memcpy (dst, src, n);
47 #endif
49 # include "json-lib.h"
51 typedef char *(*proto_t) (char *, const char *, size_t);
53 static void
54 do_one_test (json_ctx_t *json_ctx, impl_t *impl, char *dst, const char *src,
55 size_t len)
57 size_t i, iters = INNER_LOOP_ITERS;
58 timing_t start, stop, cur;
60 /* Must clear the destination buffer set by the previous run. */
61 for (i = 0; i < len; i++)
62 dst[i] = 0;
64 if (CALL (impl, dst, src, len) != MEMCPY_RESULT (dst, len))
66 error (0, 0, "Wrong result in function %s %p %p", impl->name,
67 CALL (impl, dst, src, len), MEMCPY_RESULT (dst, len));
68 ret = 1;
69 return;
72 if (memcmp (dst, src, len) != 0)
74 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
75 impl->name, dst, src);
76 ret = 1;
77 return;
80 TIMING_NOW (start);
81 for (i = 0; i < iters; ++i)
83 CALL (impl, dst, src, len);
85 TIMING_NOW (stop);
87 TIMING_DIFF (cur, start, stop);
89 json_element_double (json_ctx, (double) cur / (double) iters);
92 static void
93 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len)
95 size_t i, j;
96 char *s1, *s2;
98 align1 &= 63;
99 if (align1 + len >= page_size)
100 return;
102 align2 &= 63;
103 if (align2 + len >= page_size)
104 return;
106 s1 = (char *) (buf1 + align1);
107 s2 = (char *) (buf2 + align2);
109 for (i = 0, j = 1; i < len; i++, j += 23)
110 s1[i] = j;
112 json_element_object_begin (json_ctx);
113 json_attr_uint (json_ctx, "length", (double) len);
114 json_attr_uint (json_ctx, "align1", (double) align1);
115 json_attr_uint (json_ctx, "align2", (double) align2);
116 json_array_begin (json_ctx, "timings");
118 FOR_EACH_IMPL (impl, 0)
119 do_one_test (json_ctx, impl, s2, s1, len);
121 json_array_end (json_ctx);
122 json_element_object_end (json_ctx);
126 test_main (void)
128 json_ctx_t json_ctx;
129 size_t i;
131 test_init ();
133 json_init (&json_ctx, 0, stdout);
135 json_document_begin (&json_ctx);
136 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
138 json_attr_object_begin (&json_ctx, "functions");
139 json_attr_object_begin (&json_ctx, "memcpy");
140 json_attr_string (&json_ctx, "bench-variant", "default");
142 json_array_begin (&json_ctx, "ifuncs");
143 FOR_EACH_IMPL (impl, 0)
144 json_element_string (&json_ctx, impl->name);
145 json_array_end (&json_ctx);
147 json_array_begin (&json_ctx, "results");
148 for (i = 0; i < 18; ++i)
150 do_test (&json_ctx, 0, 0, 1 << i);
151 do_test (&json_ctx, i, 0, 1 << i);
152 do_test (&json_ctx, 0, i, 1 << i);
153 do_test (&json_ctx, i, i, 1 << i);
156 for (i = 0; i < 32; ++i)
158 do_test (&json_ctx, 0, 0, i);
159 do_test (&json_ctx, i, 0, i);
160 do_test (&json_ctx, 0, i, i);
161 do_test (&json_ctx, i, i, i);
164 for (i = 3; i < 32; ++i)
166 if ((i & (i - 1)) == 0)
167 continue;
168 do_test (&json_ctx, 0, 0, 16 * i);
169 do_test (&json_ctx, i, 0, 16 * i);
170 do_test (&json_ctx, 0, i, 16 * i);
171 do_test (&json_ctx, i, i, 16 * i);
174 for (i = 32; i < 64; ++i)
176 do_test (&json_ctx, 0, 0, 32 * i);
177 do_test (&json_ctx, i, 0, 32 * i);
178 do_test (&json_ctx, 0, i, 32 * i);
179 do_test (&json_ctx, i, i, 32 * i);
182 do_test (&json_ctx, 0, 0, getpagesize ());
184 json_array_end (&json_ctx);
185 json_attr_object_end (&json_ctx);
186 json_attr_object_end (&json_ctx);
187 json_document_end (&json_ctx);
189 return ret;
192 #include <support/test-driver.c>