Use IFUNC memmove/memset in x86-64 bcopy/bzero
[glibc.git] / string / test-memset.c
blobe8dd406c2013c73dd4f0c1410ce538ad26410a62
1 /* Test and measure memset functions.
2 Copyright (C) 1999-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #define TEST_MAIN
21 #define MIN_PAGE_SIZE 131072
22 #include "test-string.h"
24 char *simple_memset (char *, int, size_t);
26 #ifdef TEST_BZERO
27 typedef void (*proto_t) (char *, size_t);
28 void simple_bzero (char *, size_t);
29 void builtin_bzero (char *, size_t);
31 IMPL (simple_bzero, 0)
32 IMPL (builtin_bzero, 0)
33 IMPL (bzero, 1)
35 void
36 simple_bzero (char *s, size_t n)
38 simple_memset (s, 0, n);
41 void
42 builtin_bzero (char *s, size_t n)
44 __builtin_bzero (s, n);
46 #else
47 typedef char *(*proto_t) (char *, int, size_t);
48 char *builtin_memset (char *, int, size_t);
50 IMPL (simple_memset, 0)
51 IMPL (builtin_memset, 0)
52 IMPL (memset, 1)
54 char *
55 builtin_memset (char *s, int c, size_t n)
57 return __builtin_memset (s, c, n);
59 #endif
61 char *
62 simple_memset (char *s, int c, size_t n)
64 char *r = s, *end = s + n;
65 while (r < end)
66 *r++ = c;
67 return s;
70 static void
71 do_one_test (impl_t *impl, char *s, int c __attribute ((unused)), size_t n)
73 char tstbuf[n];
74 #ifdef TEST_BZERO
75 simple_bzero (tstbuf, n);
76 CALL (impl, s, n);
77 if (memcmp (s, tstbuf, n) != 0)
78 #else
79 char *res = CALL (impl, s, c, n);
80 if (res != s
81 || simple_memset (tstbuf, c, n) != tstbuf
82 || memcmp (s, tstbuf, n) != 0)
83 #endif
85 error (0, 0, "Wrong result in function %s", impl->name);
86 ret = 1;
87 return;
90 if (HP_TIMING_AVAIL)
92 hp_timing_t start __attribute ((unused));
93 hp_timing_t stop __attribute ((unused));
94 hp_timing_t best_time = ~ (hp_timing_t) 0;
95 size_t i;
97 for (i = 0; i < 32; ++i)
99 HP_TIMING_NOW (start);
100 #ifdef TEST_BZERO
101 CALL (impl, s, n);
102 #else
103 CALL (impl, s, c, n);
104 #endif
106 HP_TIMING_NOW (stop);
107 HP_TIMING_BEST (best_time, start, stop);
110 printf ("\t%zd", (size_t) best_time);
114 static void
115 do_test (size_t align, int c, size_t len)
117 align &= 7;
118 if (align + len > page_size)
119 return;
121 if (HP_TIMING_AVAIL)
122 printf ("Length %4zd, alignment %2zd, c %2d:", len, align, c);
124 FOR_EACH_IMPL (impl, 0)
125 do_one_test (impl, (char *) buf1 + align, c, len);
127 if (HP_TIMING_AVAIL)
128 putchar ('\n');
131 #ifndef TEST_BZERO
132 static void
133 do_random_tests (void)
135 size_t i, j, k, n, align, len, size;
136 int c, o;
137 unsigned char *p, *res;
139 for (i = 0; i < 65536; ++i)
140 buf2[i] = random () & 255;
142 for (n = 0; n < ITERATIONS; n++)
144 if ((random () & 31) == 0)
145 size = 65536;
146 else
147 size = 512;
148 p = buf1 + page_size - size;
149 len = random () & (size - 1);
150 align = size - len - (random () & 31);
151 if (align > size)
152 align = size - len;
153 if ((random () & 7) == 0)
154 align &= ~63;
155 if ((random () & 7) == 0)
156 c = 0;
157 else
158 c = random () & 255;
159 o = random () & 255;
160 if (o == c)
161 o = (c + 1) & 255;
162 j = len + align + 128;
163 if (j > size)
164 j = size;
165 if (align >= 128)
166 k = align - 128;
167 else
168 k = 0;
169 for (i = k; i < align; ++i)
170 p[i] = o;
171 for (i = align + len; i < j; ++i)
172 p[i] = o;
174 FOR_EACH_IMPL (impl, 1)
176 for (i = 0; i < len; ++i)
178 p[i + align] = buf2[i];
179 if (p[i + align] == c)
180 p[i + align] = o;
182 res = (unsigned char *) CALL (impl, (char *) p + align, c, len);
183 if (res != p + align)
185 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
186 n, impl->name, align, c, len, res, p + align);
187 ret = 1;
189 for (i = k; i < align; ++i)
190 if (p[i] != o)
192 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
193 n, impl->name, align, c, len);
194 ret = 1;
195 break;
197 for (; i < align + len; ++i)
198 if (p[i] != c)
200 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
201 n, impl->name, align, c, len);
202 ret = 1;
203 break;
205 for (; i < j; ++i)
206 if (p[i] != o)
208 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
209 n, impl->name, align, c, len);
210 ret = 1;
211 break;
216 #endif
219 test_main (void)
221 size_t i;
222 int c = 0;
224 test_init ();
226 printf ("%24s", "");
227 FOR_EACH_IMPL (impl, 0)
228 printf ("\t%s", impl->name);
229 putchar ('\n');
231 #ifndef TEST_BZERO
232 for (c = -65; c <= 130; c += 65)
233 #endif
235 for (i = 0; i < 18; ++i)
236 do_test (0, c, 1 << i);
237 for (i = 1; i < 32; ++i)
239 do_test (i, c, i);
240 if (i & (i - 1))
241 do_test (0, c, i);
243 do_test (1, c, 14);
244 do_test (3, c, 1024);
245 do_test (4, c, 64);
246 do_test (2, c, 25);
249 #ifndef TEST_BZERO
250 do_random_tests ();
251 #endif
253 return ret;
256 #include "../test-skeleton.c"