Use x86-64 bits/byteswap.h for both i386 and x86_64
[glibc.git] / string / test-memset.c
blob839b8a1802d533b02b8a3d83dffad7b336c948cd
1 /* Test and measure memset functions.
2 Copyright (C) 1999, 2002, 2003, 2005 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 typedef char *(*proto_t) (char *, int, size_t);
25 char *simple_memset (char *, int, size_t);
26 char *builtin_memset (char *, int, size_t);
28 IMPL (simple_memset, 0)
29 IMPL (builtin_memset, 0)
30 IMPL (memset, 1)
32 char *
33 simple_memset (char *s, int c, size_t n)
35 char *r = s, *end = s + n;
36 while (r < end)
37 *r++ = c;
38 return s;
41 char *
42 builtin_memset (char *s, int c, size_t n)
44 return __builtin_memset (s, c, n);
47 static void
48 do_one_test (impl_t *impl, char *s, int c, size_t n)
50 char *res = CALL (impl, s, c, n);
51 char tstbuf[n];
52 if (res != s
53 || simple_memset (tstbuf, c, n) != tstbuf
54 || memcmp (s, tstbuf, n) != 0)
56 error (0, 0, "Wrong result in function %s", impl->name);
57 ret = 1;
58 return;
61 if (HP_TIMING_AVAIL)
63 hp_timing_t start __attribute ((unused));
64 hp_timing_t stop __attribute ((unused));
65 hp_timing_t best_time = ~ (hp_timing_t) 0;
66 size_t i;
68 for (i = 0; i < 32; ++i)
70 HP_TIMING_NOW (start);
71 CALL (impl, s, c, n);
72 HP_TIMING_NOW (stop);
73 HP_TIMING_BEST (best_time, start, stop);
76 printf ("\t%zd", (size_t) best_time);
80 static void
81 do_test (size_t align, int c, size_t len)
83 align &= 7;
84 if (align + len > page_size)
85 return;
87 if (HP_TIMING_AVAIL)
88 printf ("Length %4zd, alignment %2zd, c %2d:", len, align, c);
90 FOR_EACH_IMPL (impl, 0)
91 do_one_test (impl, (char *) buf1 + align, c, len);
93 if (HP_TIMING_AVAIL)
94 putchar ('\n');
97 static void
98 do_random_tests (void)
100 size_t i, j, k, n, align, len, size;
101 int c, o;
102 unsigned char *p, *res;
104 for (i = 0; i < 65536; ++i)
105 buf2[i] = random () & 255;
107 for (n = 0; n < ITERATIONS; n++)
109 if ((random () & 31) == 0)
110 size = 65536;
111 else
112 size = 512;
113 p = buf1 + page_size - size;
114 len = random () & (size - 1);
115 align = size - len - (random () & 31);
116 if (align > size)
117 align = size - len;
118 if ((random () & 7) == 0)
119 align &= ~63;
120 if ((random () & 7) == 0)
121 c = 0;
122 else
123 c = random () & 255;
124 o = random () & 255;
125 if (o == c)
126 o = (c + 1) & 255;
127 j = len + align + 128;
128 if (j > size)
129 j = size;
130 if (align >= 128)
131 k = align - 128;
132 else
133 k = 0;
134 for (i = k; i < align; ++i)
135 p[i] = o;
136 for (i = align + len; i < j; ++i)
137 p[i] = o;
139 FOR_EACH_IMPL (impl, 1)
141 for (i = 0; i < len; ++i)
143 p[i + align] = buf2[i];
144 if (p[i + align] == c)
145 p[i + align] = o;
147 res = (unsigned char *) CALL (impl, (char *) p + align, c, len);
148 if (res != p + align)
150 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
151 n, impl->name, align, c, len, res, p + align);
152 ret = 1;
154 for (i = k; i < align; ++i)
155 if (p[i] != o)
157 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
158 n, impl->name, align, c, len);
159 ret = 1;
160 break;
162 for (; i < align + len; ++i)
163 if (p[i] != c)
165 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
166 n, impl->name, align, c, len);
167 ret = 1;
168 break;
170 for (; i < j; ++i)
171 if (p[i] != o)
173 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
174 n, impl->name, align, c, len);
175 ret = 1;
176 break;
183 test_main (void)
185 size_t i;
186 int c;
188 test_init ();
190 printf ("%24s", "");
191 FOR_EACH_IMPL (impl, 0)
192 printf ("\t%s", impl->name);
193 putchar ('\n');
195 for (c = -65; c <= 130; c += 65)
197 for (i = 0; i < 18; ++i)
198 do_test (0, c, 1 << i);
199 for (i = 1; i < 32; ++i)
201 do_test (i, c, i);
202 if (i & (i - 1))
203 do_test (0, c, i);
205 do_test (1, c, 14);
206 do_test (3, c, 1024);
207 do_test (4, c, 64);
208 do_test (2, c, 25);
211 do_random_tests ();
212 return ret;
215 #include "../test-skeleton.c"