1 /* Test and measure memcpy functions.
2 Copyright (C) 1999, 2002 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # define MEMCPY_RESULT(dst, len) dst
23 # define MIN_PAGE_SIZE 131072
25 # include "test-string.h"
27 char *simple_memcpy (char *, const char *, size_t);
28 char *builtin_memcpy (char *, const char *, size_t);
30 IMPL (simple_memcpy
, 0)
31 IMPL (builtin_memcpy
, 0)
35 simple_memcpy (char *dst
, const char *src
, size_t n
)
44 builtin_memcpy (char *dst
, const char *src
, size_t n
)
46 return __builtin_memcpy (dst
, src
, n
);
50 typedef char *(*proto_t
) (char *, const char *, size_t);
53 do_one_test (impl_t
*impl
, char *dst
, const char *src
,
56 if (CALL (impl
, dst
, src
, len
) != MEMCPY_RESULT (dst
, len
))
58 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
59 CALL (impl
, dst
, src
, len
), MEMCPY_RESULT (dst
, len
));
64 if (memcmp (dst
, src
, len
) != 0)
66 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
67 impl
->name
, dst
, src
);
74 hp_timing_t start
, stop
, best_time
= ~ (hp_timing_t
) 0;
77 for (i
= 0; i
< 32; ++i
)
79 HP_TIMING_NOW (start
);
80 CALL (impl
, dst
, src
, len
);
82 HP_TIMING_BEST (best_time
, start
, stop
);
85 printf ("\t%zd", (size_t) best_time
);
90 do_test (size_t align1
, size_t align2
, size_t len
)
96 if (align1
+ len
>= page_size
)
100 if (align2
+ len
>= page_size
)
106 for (i
= 0, j
= 1; i
< len
; i
++, j
+= 23)
110 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
112 FOR_EACH_IMPL (impl
, 0)
113 do_one_test (impl
, s2
, s1
, len
);
120 do_random_tests (void)
122 size_t i
, j
, n
, align1
, align2
, len
, size1
, size2
, size
;
124 unsigned char *p1
, *p2
;
127 for (n
= 0; n
< ITERATIONS
; n
++)
131 len
= getpagesize ();
140 if ((random () & 255) == 0)
144 if (size
> page_size
)
157 len
= random () % size
;
158 align1
= size1
- len
- (random () & 31);
159 align2
= size2
- len
- (random () & 31);
167 align1
= random () & 63;
168 align2
= random () & 63;
169 len
= random () % size
;
170 if (align1
+ len
> size1
)
171 align1
= size1
- len
;
172 if (align2
+ len
> size2
)
173 align2
= size2
- len
;
176 p1
= buf1
+ page_size
- size1
;
177 p2
= buf2
+ page_size
- size2
;
179 j
= align1
+ len
+ 256;
182 for (i
= 0; i
< j
; ++i
)
183 p1
[i
] = random () & 255;
185 FOR_EACH_IMPL (impl
, 1)
187 j
= align2
+ len
+ 256;
191 res
= CALL (impl
, p2
+ align2
, p1
+ align1
, len
);
192 if (res
!= MEMCPY_RESULT (p2
+ align2
, len
))
194 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
195 n
, impl
->name
, align1
, align2
, len
, res
,
196 MEMCPY_RESULT (p2
+ align2
, len
));
199 for (i
= 0; i
< align2
; ++i
)
203 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
204 n
, impl
->name
, align1
, align2
, len
);
209 for (i
= align2
+ len
; i
< j
; ++i
)
213 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
214 n
, impl
->name
, align1
, align2
, len
);
219 if (memcmp (p1
+ align1
, p2
+ align2
, len
))
221 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
222 n
, impl
->name
, align1
, align2
, len
);
237 FOR_EACH_IMPL (impl
, 0)
238 printf ("\t%s", impl
->name
);
241 for (i
= 0; i
< 18; ++i
)
243 do_test (0, 0, 1 << i
);
244 do_test (i
, 0, 1 << i
);
245 do_test (0, i
, 1 << i
);
246 do_test (i
, i
, 1 << i
);
249 for (i
= 0; i
< 32; ++i
)
257 for (i
= 3; i
< 32; ++i
)
259 if ((i
& (i
- 1)) == 0)
261 do_test (0, 0, 16 * i
);
262 do_test (i
, 0, 16 * i
);
263 do_test (0, i
, 16 * i
);
264 do_test (i
, i
, 16 * i
);
267 do_test (0, 0, getpagesize ());
273 #include "../test-skeleton.c"