benchtests: Switch string benchmarks to use bench-timing.h.
[glibc.git] / benchtests / bench-memset.c
blobe45807c0ee6c381a57912b1b5763ce558c164250
1 /* Measure memset functions.
2 Copyright (C) 2013 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 # define TEST_NAME "memset"
24 #endif
25 #define MIN_PAGE_SIZE 131072
26 #include "bench-string.h"
28 char *simple_memset (char *, int, size_t);
30 #ifdef TEST_BZERO
31 typedef void (*proto_t) (char *, size_t);
32 void simple_bzero (char *, size_t);
33 void builtin_bzero (char *, size_t);
35 IMPL (simple_bzero, 0)
36 IMPL (builtin_bzero, 0)
37 IMPL (bzero, 1)
39 void
40 simple_bzero (char *s, size_t n)
42 simple_memset (s, 0, n);
45 void
46 builtin_bzero (char *s, size_t n)
48 __builtin_bzero (s, n);
50 #else
51 typedef char *(*proto_t) (char *, int, size_t);
52 char *builtin_memset (char *, int, size_t);
54 IMPL (simple_memset, 0)
55 IMPL (builtin_memset, 0)
56 IMPL (memset, 1)
58 char *
59 builtin_memset (char *s, int c, size_t n)
61 return __builtin_memset (s, c, n);
63 #endif
65 char *
66 inhibit_loop_to_libcall
67 simple_memset (char *s, int c, size_t n)
69 char *r = s, *end = s + n;
70 while (r < end)
71 *r++ = c;
72 return s;
75 static void
76 do_one_test (impl_t *impl, char *s, int c __attribute ((unused)), size_t n)
78 size_t i, iters = INNER_LOOP_ITERS;
79 timing_t start, stop, cur;
80 char tstbuf[n];
81 #ifdef TEST_BZERO
82 simple_bzero (tstbuf, n);
83 CALL (impl, s, n);
84 if (memcmp (s, tstbuf, n) != 0)
85 #else
86 char *res = CALL (impl, s, c, n);
87 if (res != s
88 || simple_memset (tstbuf, c, n) != tstbuf
89 || memcmp (s, tstbuf, n) != 0)
90 #endif
92 error (0, 0, "Wrong result in function %s", impl->name);
93 ret = 1;
94 return;
97 TIMING_NOW (start);
98 for (i = 0; i < iters; ++i)
100 #ifdef TEST_BZERO
101 CALL (impl, s, n);
102 #else
103 CALL (impl, s, c, n);
104 #endif
106 TIMING_NOW (stop);
108 TIMING_DIFF (cur, start, stop);
110 TIMING_PRINT_MEAN ((double) cur, (double) iters);
113 static void
114 do_test (size_t align, int c, size_t len)
116 align &= 7;
117 if (align + len > page_size)
118 return;
120 printf ("Length %4zd, alignment %2zd, c %2d:", len, align, c);
122 FOR_EACH_IMPL (impl, 0)
123 do_one_test (impl, (char *) buf1 + align, c, len);
125 putchar ('\n');
129 test_main (void)
131 size_t i;
132 int c = 0;
134 test_init ();
136 printf ("%24s", "");
137 FOR_EACH_IMPL (impl, 0)
138 printf ("\t%s", impl->name);
139 putchar ('\n');
141 #ifndef TEST_BZERO
142 for (c = -65; c <= 130; c += 65)
143 #endif
145 for (i = 0; i < 18; ++i)
146 do_test (0, c, 1 << i);
147 for (i = 1; i < 32; ++i)
149 do_test (i, c, i);
150 if (i & (i - 1))
151 do_test (0, c, i);
153 do_test (1, c, 14);
154 do_test (3, c, 1024);
155 do_test (4, c, 64);
156 do_test (2, c, 25);
159 return ret;
162 #include "../test-skeleton.c"