blob: 9786ce71d1520fec171817f40cd90c824a1b4533 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Measure memset functions.
2 Copyright (C) 2013-2015 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#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"
27
28char *simple_memset (char *, int, size_t);
29
30#ifdef TEST_BZERO
31typedef void (*proto_t) (char *, size_t);
32void simple_bzero (char *, size_t);
33void builtin_bzero (char *, size_t);
34
35IMPL (simple_bzero, 0)
36IMPL (builtin_bzero, 0)
37IMPL (bzero, 1)
38
39void
40simple_bzero (char *s, size_t n)
41{
42 simple_memset (s, 0, n);
43}
44
45void
46builtin_bzero (char *s, size_t n)
47{
48 __builtin_bzero (s, n);
49}
50#else
51typedef char *(*proto_t) (char *, int, size_t);
52char *builtin_memset (char *, int, size_t);
53
54IMPL (simple_memset, 0)
55IMPL (builtin_memset, 0)
56IMPL (memset, 1)
57
58char *
59builtin_memset (char *s, int c, size_t n)
60{
61 return __builtin_memset (s, c, n);
62}
63#endif
64
65char *
66inhibit_loop_to_libcall
67simple_memset (char *s, int c, size_t n)
68{
69 char *r = s, *end = s + n;
70 while (r < end)
71 *r++ = c;
72 return s;
73}
74
75static void
76do_one_test (impl_t *impl, char *s, int c __attribute ((unused)), size_t n)
77{
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
91 {
92 error (0, 0, "Wrong result in function %s", impl->name);
93 ret = 1;
94 return;
95 }
96
97 TIMING_NOW (start);
98 for (i = 0; i < iters; ++i)
99 {
100#ifdef TEST_BZERO
101 CALL (impl, s, n);
102#else
103 CALL (impl, s, c, n);
104#endif
105 }
106 TIMING_NOW (stop);
107
108 TIMING_DIFF (cur, start, stop);
109
110 TIMING_PRINT_MEAN ((double) cur, (double) iters);
111}
112
113static void
114do_test (size_t align, int c, size_t len)
115{
116 align &= 7;
117 if (align + len > page_size)
118 return;
119
120 printf ("Length %4zd, alignment %2zd, c %2d:", len, align, c);
121
122 FOR_EACH_IMPL (impl, 0)
123 do_one_test (impl, (char *) buf1 + align, c, len);
124
125 putchar ('\n');
126}
127
128int
129test_main (void)
130{
131 size_t i;
132 int c = 0;
133
134 test_init ();
135
136 printf ("%24s", "");
137 FOR_EACH_IMPL (impl, 0)
138 printf ("\t%s", impl->name);
139 putchar ('\n');
140
141#ifndef TEST_BZERO
142 for (c = -65; c <= 130; c += 65)
143#endif
144 {
145 for (i = 0; i < 18; ++i)
146 do_test (0, c, 1 << i);
147 for (i = 1; i < 32; ++i)
148 {
149 do_test (i, c, i);
150 if (i & (i - 1))
151 do_test (0, c, i);
152 }
153 for (i = 32; i < 512; i+=32)
154 {
155 do_test (0, c, i);
156 do_test (i, c, i);
157 }
158 do_test (1, c, 14);
159 do_test (3, c, 1024);
160 do_test (4, c, 64);
161 do_test (2, c, 25);
162 }
163
164 return ret;
165}
166
167#include "../test-skeleton.c"