]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-memcpy.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / benchtests / bench-memcpy.c
1 /* Measure memcpy functions.
2 Copyright (C) 2013-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
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"
25
26 char *simple_memcpy (char *, const char *, size_t);
27 char *builtin_memcpy (char *, const char *, size_t);
28
29 IMPL (simple_memcpy, 0)
30 IMPL (builtin_memcpy, 0)
31 IMPL (memcpy, 1)
32
33 char *
34 simple_memcpy (char *dst, const char *src, size_t n)
35 {
36 char *ret = dst;
37 while (n--)
38 *dst++ = *src++;
39 return ret;
40 }
41
42 char *
43 builtin_memcpy (char *dst, const char *src, size_t n)
44 {
45 return __builtin_memcpy (dst, src, n);
46 }
47 #endif
48
49 typedef char *(*proto_t) (char *, const char *, size_t);
50
51 static void
52 do_one_test (impl_t *impl, char *dst, const char *src,
53 size_t len)
54 {
55 size_t i, iters = INNER_LOOP_ITERS;
56 timing_t start, stop, cur;
57
58 if (CALL (impl, dst, src, len) != MEMCPY_RESULT (dst, len))
59 {
60 error (0, 0, "Wrong result in function %s %p %p", impl->name,
61 CALL (impl, dst, src, len), MEMCPY_RESULT (dst, len));
62 ret = 1;
63 return;
64 }
65
66 if (memcmp (dst, src, len) != 0)
67 {
68 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
69 impl->name, dst, src);
70 ret = 1;
71 return;
72 }
73
74 TIMING_NOW (start);
75 for (i = 0; i < iters; ++i)
76 {
77 CALL (impl, dst, src, len);
78 }
79 TIMING_NOW (stop);
80
81 TIMING_DIFF (cur, start, stop);
82
83 TIMING_PRINT_MEAN ((double) cur, (double) iters);
84 }
85
86 static void
87 do_test (size_t align1, size_t align2, size_t len)
88 {
89 size_t i, j;
90 char *s1, *s2;
91
92 align1 &= 63;
93 if (align1 + len >= page_size)
94 return;
95
96 align2 &= 63;
97 if (align2 + len >= page_size)
98 return;
99
100 s1 = (char *) (buf1 + align1);
101 s2 = (char *) (buf2 + align2);
102
103 for (i = 0, j = 1; i < len; i++, j += 23)
104 s1[i] = j;
105
106 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
107
108 FOR_EACH_IMPL (impl, 0)
109 do_one_test (impl, s2, s1, len);
110
111 putchar ('\n');
112 }
113
114 int
115 test_main (void)
116 {
117 size_t i;
118
119 test_init ();
120
121 printf ("%23s", "");
122 FOR_EACH_IMPL (impl, 0)
123 printf ("\t%s", impl->name);
124 putchar ('\n');
125
126 for (i = 0; i < 18; ++i)
127 {
128 do_test (0, 0, 1 << i);
129 do_test (i, 0, 1 << i);
130 do_test (0, i, 1 << i);
131 do_test (i, i, 1 << i);
132 }
133
134 for (i = 0; i < 32; ++i)
135 {
136 do_test (0, 0, i);
137 do_test (i, 0, i);
138 do_test (0, i, i);
139 do_test (i, i, i);
140 }
141
142 for (i = 3; i < 32; ++i)
143 {
144 if ((i & (i - 1)) == 0)
145 continue;
146 do_test (0, 0, 16 * i);
147 do_test (i, 0, 16 * i);
148 do_test (0, i, 16 * i);
149 do_test (i, i, 16 * i);
150 }
151
152 do_test (0, 0, getpagesize ());
153
154 return ret;
155 }
156
157 #include "../test-skeleton.c"