SSSE3 strcpy/stpcpy for x86-64
[platform/upstream/glibc.git] / string / test-memset.c
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.
5
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.
10
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.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #define TEST_MAIN
22 #define MIN_PAGE_SIZE 131072
23 #include "test-string.h"
24
25 typedef char *(*proto_t) (char *, int, size_t);
26 char *simple_memset (char *, int, size_t);
27 char *builtin_memset (char *, int, size_t);
28
29 IMPL (simple_memset, 0)
30 IMPL (builtin_memset, 0)
31 IMPL (memset, 1)
32
33 char *
34 simple_memset (char *s, int c, size_t n)
35 {
36   char *r = s, *end = s + n;
37   while (r < end)
38     *r++ = c;
39   return s;
40 }
41
42 char *
43 builtin_memset (char *s, int c, size_t n)
44 {
45   return __builtin_memset (s, c, n);
46 }
47
48 static void
49 do_one_test (impl_t *impl, char *s, int c, size_t n)
50 {
51   char *res = CALL (impl, s, c, n);
52   char tstbuf[n];
53   if (res != s
54       || simple_memset (tstbuf, c, n) != tstbuf
55       || memcmp (s, tstbuf, n) != 0)
56     {
57       error (0, 0, "Wrong result in function %s", impl->name);
58       ret = 1;
59       return;
60     }
61
62   if (HP_TIMING_AVAIL)
63     {
64       hp_timing_t start __attribute ((unused));
65       hp_timing_t stop __attribute ((unused));
66       hp_timing_t best_time = ~ (hp_timing_t) 0;
67       size_t i;
68
69       for (i = 0; i < 32; ++i)
70         {
71           HP_TIMING_NOW (start);
72           CALL (impl, s, c, n);
73           HP_TIMING_NOW (stop);
74           HP_TIMING_BEST (best_time, start, stop);
75         }
76
77       printf ("\t%zd", (size_t) best_time);
78     }
79 }
80
81 static void
82 do_test (size_t align, int c, size_t len)
83 {
84   align &= 7;
85   if (align + len > page_size)
86     return;
87
88   if (HP_TIMING_AVAIL)
89     printf ("Length %4zd, alignment %2zd, c %2d:", len, align, c);
90
91   FOR_EACH_IMPL (impl, 0)
92     do_one_test (impl, (char *) buf1 + align, c, len);
93
94   if (HP_TIMING_AVAIL)
95     putchar ('\n');
96 }
97
98 static void
99 do_random_tests (void)
100 {
101   size_t i, j, k, n, align, len, size;
102   int c, o;
103   unsigned char *p, *res;
104
105   for (i = 0; i < 65536; ++i)
106     buf2[i] = random () & 255;
107
108   for (n = 0; n < ITERATIONS; n++)
109     {
110       if ((random () & 31) == 0)
111         size = 65536;
112       else
113         size = 512;
114       p = buf1 + page_size - size;
115       len = random () & (size - 1);
116       align = size - len - (random () & 31);
117       if (align > size)
118         align = size - len;
119       if ((random () & 7) == 0)
120         align &= ~63;
121       if ((random () & 7) == 0)
122         c = 0;
123       else
124         c = random () & 255;
125       o = random () & 255;
126       if (o == c)
127         o = (c + 1) & 255;
128       j = len + align + 128;
129       if (j > size)
130         j = size;
131       if (align >= 128)
132         k = align - 128;
133       else
134         k = 0;
135       for (i = k; i < align; ++i)
136         p[i] = o;
137       for (i = align + len; i < j; ++i)
138         p[i] = o;
139
140       FOR_EACH_IMPL (impl, 1)
141         {
142           for (i = 0; i < len; ++i)
143             {
144               p[i + align] = buf2[i];
145               if (p[i + align] == c)
146                 p[i + align] = o;
147             }
148           res = (unsigned char *) CALL (impl, (char *) p + align, c, len);
149           if (res != p + align)
150             {
151               error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
152                      n, impl->name, align, c, len, res, p + align);
153               ret = 1;
154             }
155           for (i = k; i < align; ++i)
156             if (p[i] != o)
157               {
158                 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
159                        n, impl->name, align, c, len);
160                 ret = 1;
161                 break;
162               }
163           for (; i < align + len; ++i)
164             if (p[i] != c)
165               {
166                 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
167                        n, impl->name, align, c, len);
168                 ret = 1;
169                 break;
170               }
171           for (; i < j; ++i)
172             if (p[i] != o)
173               {
174                 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
175                        n, impl->name, align, c, len);
176                 ret = 1;
177                 break;
178               }
179         }
180     }
181 }
182
183 int
184 test_main (void)
185 {
186   size_t i;
187   int c;
188
189   test_init ();
190
191   printf ("%24s", "");
192   FOR_EACH_IMPL (impl, 0)
193     printf ("\t%s", impl->name);
194   putchar ('\n');
195
196   for (c = -65; c <= 130; c += 65)
197     {
198       for (i = 0; i < 18; ++i)
199         do_test (0, c, 1 << i);
200       for (i = 1; i < 32; ++i)
201         {
202           do_test (i, c, i);
203           if (i & (i - 1))
204             do_test (0, c, i);
205         }
206       do_test (1, c, 14);
207       do_test (3, c, 1024);
208       do_test (4, c, 64);
209       do_test (2, c, 25);
210     }
211
212   do_random_tests ();
213   return ret;
214 }
215
216 #include "../test-skeleton.c"