Back to index

glibc  2.9
test-memcpy.c
Go to the documentation of this file.
00001 /* Test and measure memcpy functions.
00002    Copyright (C) 1999, 2002, 2003, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Written by Jakub Jelinek <jakub@redhat.com>, 1999.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifndef MEMCPY_RESULT
00022 # define MEMCPY_RESULT(dst, len) dst
00023 # define MIN_PAGE_SIZE 131072
00024 # define TEST_MAIN
00025 # include "test-string.h"
00026 
00027 char *simple_memcpy (char *, const char *, size_t);
00028 char *builtin_memcpy (char *, const char *, size_t);
00029 
00030 IMPL (simple_memcpy, 0)
00031 IMPL (builtin_memcpy, 0)
00032 IMPL (memcpy, 1)
00033 
00034 char *
00035 simple_memcpy (char *dst, const char *src, size_t n)
00036 {
00037   char *ret = dst;
00038   while (n--)
00039     *dst++ = *src++;
00040   return ret;
00041 }
00042 
00043 char *
00044 builtin_memcpy (char *dst, const char *src, size_t n)
00045 {
00046   return __builtin_memcpy (dst, src, n);
00047 }
00048 #endif
00049 
00050 typedef char *(*proto_t) (char *, const char *, size_t);
00051 
00052 static void
00053 do_one_test (impl_t *impl, char *dst, const char *src,
00054             size_t len)
00055 {
00056   if (CALL (impl, dst, src, len) != MEMCPY_RESULT (dst, len))
00057     {
00058       error (0, 0, "Wrong result in function %s %p %p", impl->name,
00059             CALL (impl, dst, src, len), MEMCPY_RESULT (dst, len));
00060       ret = 1;
00061       return;
00062     }
00063 
00064   if (memcmp (dst, src, len) != 0)
00065     {
00066       error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
00067             impl->name, dst, src);
00068       ret = 1;
00069       return;
00070     }
00071 
00072   if (HP_TIMING_AVAIL)
00073     {
00074       hp_timing_t start __attribute ((unused));
00075       hp_timing_t stop __attribute ((unused));
00076       hp_timing_t best_time = ~ (hp_timing_t) 0;
00077       size_t i;
00078 
00079       for (i = 0; i < 32; ++i)
00080        {
00081          HP_TIMING_NOW (start);
00082          CALL (impl, dst, src, len);
00083          HP_TIMING_NOW (stop);
00084          HP_TIMING_BEST (best_time, start, stop);
00085        }
00086 
00087       printf ("\t%zd", (size_t) best_time);
00088     }
00089 }
00090 
00091 static void
00092 do_test (size_t align1, size_t align2, size_t len)
00093 {
00094   size_t i, j;
00095   char *s1, *s2;
00096 
00097   align1 &= 63;
00098   if (align1 + len >= page_size)
00099     return;
00100 
00101   align2 &= 63;
00102   if (align2 + len >= page_size)
00103     return;
00104 
00105   s1 = (char *) (buf1 + align1);
00106   s2 = (char *) (buf2 + align2);
00107 
00108   for (i = 0, j = 1; i < len; i++, j += 23)
00109     s1[i] = j;
00110 
00111   if (HP_TIMING_AVAIL)
00112     printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
00113 
00114   FOR_EACH_IMPL (impl, 0)
00115     do_one_test (impl, s2, s1, len);
00116 
00117   if (HP_TIMING_AVAIL)
00118     putchar ('\n');
00119 }
00120 
00121 static void
00122 do_random_tests (void)
00123 {
00124   size_t i, j, n, align1, align2, len, size1, size2, size;
00125   int c;
00126   unsigned char *p1, *p2;
00127   unsigned char *res;
00128 
00129   for (n = 0; n < ITERATIONS; n++)
00130     {
00131       if (n == 0)
00132        {
00133          len = getpagesize ();
00134          size = len + 512;
00135          size1 = size;
00136          size2 = size;
00137          align1 = 512;
00138          align2 = 512;
00139        }
00140       else
00141        {
00142          if ((random () & 255) == 0)
00143            size = 65536;
00144          else
00145            size = 768;
00146          if (size > page_size)
00147            size = page_size;
00148          size1 = size;
00149          size2 = size;
00150          i = random ();
00151          if (i & 3)
00152            size -= 256;
00153          if (i & 1)
00154            size1 -= 256;
00155          if (i & 2)
00156            size2 -= 256;
00157          if (i & 4)
00158            {
00159              len = random () % size;
00160              align1 = size1 - len - (random () & 31);
00161              align2 = size2 - len - (random () & 31);
00162              if (align1 > size1)
00163               align1 = 0;
00164              if (align2 > size2)
00165               align2 = 0;
00166            }
00167          else
00168            {
00169              align1 = random () & 63;
00170              align2 = random () & 63;
00171              len = random () % size;
00172              if (align1 + len > size1)
00173               align1 = size1 - len;
00174              if (align2 + len > size2)
00175               align2 = size2 - len;
00176            }
00177        }
00178       p1 = buf1 + page_size - size1;
00179       p2 = buf2 + page_size - size2;
00180       c = random () & 255;
00181       j = align1 + len + 256;
00182       if (j > size1)
00183        j = size1;
00184       for (i = 0; i < j; ++i)
00185        p1[i] = random () & 255;
00186 
00187       FOR_EACH_IMPL (impl, 1)
00188        {
00189          j = align2 + len + 256;
00190          if (j > size2)
00191            j = size2;
00192          memset (p2, c, j);
00193          res = (unsigned char *) CALL (impl,
00194                                    (char *) (p2 + align2),
00195                                    (char *) (p1 + align1), len);
00196          if (res != MEMCPY_RESULT (p2 + align2, len))
00197            {
00198              error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
00199                    n, impl->name, align1, align2, len, res,
00200                    MEMCPY_RESULT (p2 + align2, len));
00201              ret = 1;
00202            }
00203          for (i = 0; i < align2; ++i)
00204            {
00205              if (p2[i] != c)
00206               {
00207                 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
00208                       n, impl->name, align1, align2, len);
00209                 ret = 1;
00210                 break;
00211               }
00212            }
00213          for (i = align2 + len; i < j; ++i)
00214            {
00215              if (p2[i] != c)
00216               {
00217                 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
00218                       n, impl->name, align1, align2, len);
00219                 ret = 1;
00220                 break;
00221               }
00222            }
00223          if (memcmp (p1 + align1, p2 + align2, len))
00224            {
00225              error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
00226                    n, impl->name, align1, align2, len);
00227              ret = 1;
00228            }
00229        }
00230     }
00231 }
00232 
00233 int
00234 test_main (void)
00235 {
00236   size_t i;
00237 
00238   test_init ();
00239 
00240   printf ("%23s", "");
00241   FOR_EACH_IMPL (impl, 0)
00242     printf ("\t%s", impl->name);
00243   putchar ('\n');
00244 
00245   for (i = 0; i < 18; ++i)
00246     {
00247       do_test (0, 0, 1 << i);
00248       do_test (i, 0, 1 << i);
00249       do_test (0, i, 1 << i);
00250       do_test (i, i, 1 << i);
00251     }
00252 
00253   for (i = 0; i < 32; ++i)
00254     {
00255       do_test (0, 0, i);
00256       do_test (i, 0, i);
00257       do_test (0, i, i);
00258       do_test (i, i, i);
00259     }
00260 
00261   for (i = 3; i < 32; ++i)
00262     {
00263       if ((i & (i - 1)) == 0)
00264        continue;
00265       do_test (0, 0, 16 * i);
00266       do_test (i, 0, 16 * i);
00267       do_test (0, i, 16 * i);
00268       do_test (i, i, 16 * i);
00269     }
00270 
00271   do_test (0, 0, getpagesize ());
00272 
00273   do_random_tests ();
00274   return ret;
00275 }
00276 
00277 #include "../test-skeleton.c"