Deprecate inclusion of <sys/sysmacros.h> by <sys/types.h>
[glibc.git] / benchtests / bench-memset.c
blob98ec257b3ee49b1e3d551b3c5a936c0bd808b364
1 /* Measure memset functions.
2 Copyright (C) 2013-2016 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 #define TEST_MAIN
20 #ifdef TEST_BZERO
21 # define TEST_NAME "bzero"
22 #else
23 # ifndef WIDE
24 # define TEST_NAME "memset"
25 # else
26 # define TEST_NAME "wmemset"
27 # endif /* WIDE */
28 #endif /* !TEST_BZERO */
29 #define MIN_PAGE_SIZE 131072
30 #include "bench-string.h"
32 #ifndef WIDE
33 # define MEMSET memset
34 # define CHAR char
35 # define SIMPLE_MEMSET simple_memset
36 # define MEMCMP memcmp
37 #else
38 # include <wchar.h>
39 # define MEMSET wmemset
40 # define CHAR wchar_t
41 # define SIMPLE_MEMSET simple_wmemset
42 # define MEMCMP wmemcmp
43 #endif /* WIDE */
45 CHAR *SIMPLE_MEMSET (CHAR *, int, size_t);
47 #ifdef TEST_BZERO
48 typedef void (*proto_t) (char *, size_t);
49 void simple_bzero (char *, size_t);
50 void builtin_bzero (char *, size_t);
52 IMPL (simple_bzero, 0)
53 IMPL (builtin_bzero, 0)
54 IMPL (bzero, 1)
56 void
57 simple_bzero (char *s, size_t n)
59 SIMPLE_MEMSET (s, 0, n);
62 void
63 builtin_bzero (char *s, size_t n)
65 __builtin_bzero (s, n);
67 #else
68 typedef CHAR *(*proto_t) (CHAR *, int, size_t);
70 IMPL (SIMPLE_MEMSET, 0)
71 # ifndef WIDE
72 char *builtin_memset (char *, int, size_t);
73 IMPL (builtin_memset, 0)
74 # endif /* !WIDE */
75 IMPL (MEMSET, 1)
77 # ifndef WIDE
78 char *
79 builtin_memset (char *s, int c, size_t n)
81 return __builtin_memset (s, c, n);
83 # endif /* !WIDE */
84 #endif /* !TEST_BZERO */
86 CHAR *
87 inhibit_loop_to_libcall
88 SIMPLE_MEMSET (CHAR *s, int c, size_t n)
90 CHAR *r = s, *end = s + n;
91 while (r < end)
92 *r++ = c;
93 return s;
96 static void
97 do_one_test (impl_t *impl, CHAR *s, int c __attribute ((unused)), size_t n)
99 size_t i, iters = INNER_LOOP_ITERS;
100 timing_t start, stop, cur;
101 CHAR tstbuf[n];
102 #ifdef TEST_BZERO
103 simple_bzero (tstbuf, n);
104 CALL (impl, s, n);
105 if (memcmp (s, tstbuf, n) != 0)
106 #else
107 CHAR *res = CALL (impl, s, c, n);
108 if (res != s
109 || SIMPLE_MEMSET (tstbuf, c, n) != tstbuf
110 || MEMCMP (s, tstbuf, n) != 0)
111 #endif /* !TEST_BZERO */
113 error (0, 0, "Wrong result in function %s", impl->name);
114 ret = 1;
115 return;
118 TIMING_NOW (start);
119 for (i = 0; i < iters; ++i)
121 #ifdef TEST_BZERO
122 CALL (impl, s, n);
123 #else
124 CALL (impl, s, c, n);
125 #endif /* !TEST_BZERO */
127 TIMING_NOW (stop);
129 TIMING_DIFF (cur, start, stop);
131 TIMING_PRINT_MEAN ((double) cur, (double) iters);
134 static void
135 do_test (size_t align, int c, size_t len)
137 align &= 63;
138 if ((align + len) * sizeof (CHAR) > page_size)
139 return;
141 printf ("Length %4zd, alignment %2zd, c %2d:", len, align, c);
143 FOR_EACH_IMPL (impl, 0)
144 do_one_test (impl, (CHAR *) (buf1) + align, c, len);
146 putchar ('\n');
150 test_main (void)
152 size_t i;
153 int c = 0;
155 test_init ();
157 printf ("%24s", "");
158 FOR_EACH_IMPL (impl, 0)
159 printf ("\t%s", impl->name);
160 putchar ('\n');
162 #ifndef TEST_BZERO
163 for (c = -65; c <= 130; c += 65)
164 #endif
166 for (i = 0; i < 18; ++i)
167 do_test (0, c, 1 << i);
168 for (i = 1; i < 32; ++i)
170 do_test (i, c, i);
171 if (i & (i - 1))
172 do_test (0, c, i);
174 for (i = 32; i < 512; i+=32)
176 do_test (0, c, i);
177 do_test (i, c, i);
179 do_test (1, c, 14);
180 do_test (3, c, 1024);
181 do_test (4, c, 64);
182 do_test (2, c, 25);
184 for (i = 33; i <= 256; i += 4)
186 do_test (0, c, 32 * i);
187 do_test (i, c, 32 * i);
190 return ret;
193 #include "../test-skeleton.c"