~linaro-toolchain-dev/cortex-strings/trunk

« back to all changes in this revision

Viewing changes to tests/test-memcpy.c

  • Committer: Michael Hope
  • Date: 2010-09-02 02:26:33 UTC
  • Revision ID: michael.hope@linaro.org-20100902022633-xbddfwagsup43ykn
Added a README.  Re-factored the build into a top level Makefile.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test and measure memcpy 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
#ifndef MEMCPY_RESULT
 
22
# define MEMCPY_RESULT(dst, len) dst
 
23
# define MIN_PAGE_SIZE 131072
 
24
# define TEST_MAIN
 
25
# include "test-string.h"
 
26
 
 
27
char *simple_memcpy (char *, const char *, size_t);
 
28
char *builtin_memcpy (char *, const char *, size_t);
 
29
 
 
30
IMPL (simple_memcpy, 0)
 
31
IMPL (builtin_memcpy, 0)
 
32
IMPL (memcpy, 1)
 
33
 
 
34
char *
 
35
simple_memcpy (char *dst, const char *src, size_t n)
 
36
{
 
37
  char *ret = dst;
 
38
  while (n--)
 
39
    *dst++ = *src++;
 
40
  return ret;
 
41
}
 
42
 
 
43
char *
 
44
builtin_memcpy (char *dst, const char *src, size_t n)
 
45
{
 
46
  return __builtin_memcpy (dst, src, n);
 
47
}
 
48
#endif
 
49
 
 
50
typedef char *(*proto_t) (char *, const char *, size_t);
 
51
 
 
52
static void
 
53
do_one_test (impl_t *impl, char *dst, const char *src,
 
54
             size_t len)
 
55
{
 
56
  if (CALL (impl, dst, src, len) != MEMCPY_RESULT (dst, len))
 
57
    {
 
58
      error (0, 0, "Wrong result in function %s %p %p", impl->name,
 
59
             CALL (impl, dst, src, len), MEMCPY_RESULT (dst, len));
 
60
      ret = 1;
 
61
      return;
 
62
    }
 
63
 
 
64
  if (memcmp (dst, src, len) != 0)
 
65
    {
 
66
      error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
 
67
             impl->name, dst, src);
 
68
      ret = 1;
 
69
      return;
 
70
    }
 
71
 
 
72
  if (HP_TIMING_AVAIL)
 
73
    {
 
74
      hp_timing_t start __attribute ((unused));
 
75
      hp_timing_t stop __attribute ((unused));
 
76
      hp_timing_t best_time = ~ (hp_timing_t) 0;
 
77
      size_t i;
 
78
 
 
79
      for (i = 0; i < 32; ++i)
 
80
        {
 
81
          HP_TIMING_NOW (start);
 
82
          CALL (impl, dst, src, len);
 
83
          HP_TIMING_NOW (stop);
 
84
          HP_TIMING_BEST (best_time, start, stop);
 
85
        }
 
86
 
 
87
      printf ("\t%zd", (size_t) best_time);
 
88
    }
 
89
}
 
90
 
 
91
static void
 
92
do_test (size_t align1, size_t align2, size_t len)
 
93
{
 
94
  size_t i, j;
 
95
  char *s1, *s2;
 
96
 
 
97
  align1 &= 63;
 
98
  if (align1 + len >= page_size)
 
99
    return;
 
100
 
 
101
  align2 &= 63;
 
102
  if (align2 + len >= page_size)
 
103
    return;
 
104
 
 
105
  s1 = (char *) (buf1 + align1);
 
106
  s2 = (char *) (buf2 + align2);
 
107
 
 
108
  for (i = 0, j = 1; i < len; i++, j += 23)
 
109
    s1[i] = j;
 
110
 
 
111
  if (HP_TIMING_AVAIL)
 
112
    printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
 
113
 
 
114
  FOR_EACH_IMPL (impl, 0)
 
115
    do_one_test (impl, s2, s1, len);
 
116
 
 
117
  if (HP_TIMING_AVAIL)
 
118
    putchar ('\n');
 
119
}
 
120
 
 
121
static void
 
122
do_random_tests (void)
 
123
{
 
124
  size_t i, j, n, align1, align2, len, size1, size2, size;
 
125
  int c;
 
126
  unsigned char *p1, *p2;
 
127
  unsigned char *res;
 
128
 
 
129
  for (n = 0; n < ITERATIONS; n++)
 
130
    {
 
131
      if (n == 0)
 
132
        {
 
133
          len = getpagesize ();
 
134
          size = len + 512;
 
135
          size1 = size;
 
136
          size2 = size;
 
137
          align1 = 512;
 
138
          align2 = 512;
 
139
        }
 
140
      else
 
141
        {
 
142
          if ((random () & 255) == 0)
 
143
            size = 65536;
 
144
          else
 
145
            size = 768;
 
146
          if (size > page_size)
 
147
            size = page_size;
 
148
          size1 = size;
 
149
          size2 = size;
 
150
          i = random ();
 
151
          if (i & 3)
 
152
            size -= 256;
 
153
          if (i & 1)
 
154
            size1 -= 256;
 
155
          if (i & 2)
 
156
            size2 -= 256;
 
157
          if (i & 4)
 
158
            {
 
159
              len = random () % size;
 
160
              align1 = size1 - len - (random () & 31);
 
161
              align2 = size2 - len - (random () & 31);
 
162
              if (align1 > size1)
 
163
                align1 = 0;
 
164
              if (align2 > size2)
 
165
                align2 = 0;
 
166
            }
 
167
          else
 
168
            {
 
169
              align1 = random () & 63;
 
170
              align2 = random () & 63;
 
171
              len = random () % size;
 
172
              if (align1 + len > size1)
 
173
                align1 = size1 - len;
 
174
              if (align2 + len > size2)
 
175
                align2 = size2 - len;
 
176
            }
 
177
        }
 
178
      p1 = buf1 + page_size - size1;
 
179
      p2 = buf2 + page_size - size2;
 
180
      c = random () & 255;
 
181
      j = align1 + len + 256;
 
182
      if (j > size1)
 
183
        j = size1;
 
184
      for (i = 0; i < j; ++i)
 
185
        p1[i] = random () & 255;
 
186
 
 
187
      FOR_EACH_IMPL (impl, 1)
 
188
        {
 
189
          j = align2 + len + 256;
 
190
          if (j > size2)
 
191
            j = size2;
 
192
          memset (p2, c, j);
 
193
          res = (unsigned char *) CALL (impl,
 
194
                                        (char *) (p2 + align2),
 
195
                                        (char *) (p1 + align1), len);
 
196
          if (res != MEMCPY_RESULT (p2 + align2, len))
 
197
            {
 
198
              error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
 
199
                     n, impl->name, align1, align2, len, res,
 
200
                     MEMCPY_RESULT (p2 + align2, len));
 
201
              ret = 1;
 
202
            }
 
203
          for (i = 0; i < align2; ++i)
 
204
            {
 
205
              if (p2[i] != c)
 
206
                {
 
207
                  error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
 
208
                         n, impl->name, align1, align2, len);
 
209
                  ret = 1;
 
210
                  break;
 
211
                }
 
212
            }
 
213
          for (i = align2 + len; i < j; ++i)
 
214
            {
 
215
              if (p2[i] != c)
 
216
                {
 
217
                  error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
 
218
                         n, impl->name, align1, align2, len);
 
219
                  ret = 1;
 
220
                  break;
 
221
                }
 
222
            }
 
223
          if (memcmp (p1 + align1, p2 + align2, len))
 
224
            {
 
225
              error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
 
226
                     n, impl->name, align1, align2, len);
 
227
              ret = 1;
 
228
            }
 
229
        }
 
230
    }
 
231
}
 
232
 
 
233
int
 
234
test_main (void)
 
235
{
 
236
  size_t i;
 
237
 
 
238
  test_init ();
 
239
 
 
240
  printf ("%23s", "");
 
241
  FOR_EACH_IMPL (impl, 0)
 
242
    printf ("\t%s", impl->name);
 
243
  putchar ('\n');
 
244
 
 
245
  for (i = 0; i < 18; ++i)
 
246
    {
 
247
      do_test (0, 0, 1 << i);
 
248
      do_test (i, 0, 1 << i);
 
249
      do_test (0, i, 1 << i);
 
250
      do_test (i, i, 1 << i);
 
251
    }
 
252
 
 
253
  for (i = 0; i < 32; ++i)
 
254
    {
 
255
      do_test (0, 0, i);
 
256
      do_test (i, 0, i);
 
257
      do_test (0, i, i);
 
258
      do_test (i, i, i);
 
259
    }
 
260
 
 
261
  for (i = 3; i < 32; ++i)
 
262
    {
 
263
      if ((i & (i - 1)) == 0)
 
264
        continue;
 
265
      do_test (0, 0, 16 * i);
 
266
      do_test (i, 0, 16 * i);
 
267
      do_test (0, i, 16 * i);
 
268
      do_test (i, i, 16 * i);
 
269
    }
 
270
 
 
271
  do_test (0, 0, getpagesize ());
 
272
 
 
273
  do_random_tests ();
 
274
  return ret;
 
275
}
 
276
 
 
277
#include "test-skeleton.c"