summaryrefslogtreecommitdiff
path: root/src/bin/evil/evil_test_memcpy.c
blob: f8483db5c58dfade5f8fd5cfa423ca51ab783921 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>
#include <stdio.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN

#include "evil_suite.h"
#include "evil_test_memcpy.h"


typedef void *(*memcpy_decl)(void *dest, const void *src, size_t n);

void *memcpy_glibc(void *dest, const void *src, size_t n);


static unsigned char *buf1 = NULL;
static unsigned char *buf2 = NULL;
static size_t         page_size = 0;


#if defined (__MINGW32CE__) || defined (_MSC_VER)
static int
getpagesize()
{
   return 1024;
}
#endif /* __MINGW32CE__ || _MSC_VER */


static void
test_memcpy_test_run(suite *s, memcpy_decl fct, char *dst, const char *src, size_t len)
{
   double best;
   int    i;

   best = 1000000000.0;

   for (i = 0; i < 128; ++i)
     {
        double time;

        suite_time_start(s);
        fct(dst, src, len);
        suite_time_stop(s);
        time = suite_time_get(s);
        if (time < best) best = time;
     }

   printf ("  %e", best);
}

static void
test_memcpy_tests_run(suite *s, size_t align1, size_t align2, size_t len)
{
   size_t i, j;
   char *s1, *s2;

   printf ("running test..\n");

/*    align1 &= 63; */
/*    if (align1 + len >= page_size) */
/*      return; */

/*    align2 &= 63; */
/*    if (align2 + len >= page_size) */
/*      return; */

   s1 = (char *) (buf1 + align1);
   s2 = (char *) (buf2 + align2);

   for (i = 0, j = 1; i < len; i++, j += 23)
     s1[i] = j;

   printf ("length: %6d, align %2Iu/%2Iu:", (int)len, align1, align2);

   test_memcpy_test_run(s, memcpy, s2, s1, len);
#ifdef _WIN32_WCE
   test_memcpy_test_run(s, memcpy_glibc, s2, s1, len);
#endif /* _WIN32_WCE */

   printf ("\n");
}

int
test_memcpy(suite *s)
{
   size_t i;

   page_size = 2 * 1024;

   buf1 = (unsigned char *)malloc(16 * getpagesize());
   if (!buf1) return 0;

   buf2 = (unsigned char *)malloc(16 * getpagesize());
   if (!buf2)
     {
        free(buf1);
        return 0;
     }

   memset (buf1, 0xa5, page_size);
   memset (buf2, 0x5a, page_size);

  for (i = 0; i < 5; ++i)
    {
      test_memcpy_tests_run(s, 0, 0, 1 << i);
      test_memcpy_tests_run(s, i, 0, 1 << i);
      test_memcpy_tests_run(s, 0, i, 1 << i);
      test_memcpy_tests_run(s, i, i, 1 << i);
    }

  for (i = 0; i < 32; ++i)
    {
      test_memcpy_tests_run(s, 0, 0, i);
      test_memcpy_tests_run(s, i, 0, i);
      test_memcpy_tests_run(s, 0, i, i);
      test_memcpy_tests_run(s, i, i, i);
    }

  for (i = 3; i < 32; ++i)
    {
      if ((i & (i - 1)) == 0)
	continue;
      test_memcpy_tests_run(s, 0, 0, 16 * i);
      test_memcpy_tests_run(s, i, 0, 16 * i);
      test_memcpy_tests_run(s, 0, i, 16 * i);
      test_memcpy_tests_run(s, i, i, 16 * i);
    }

  test_memcpy_tests_run(s, 0, 0, getpagesize ());
  test_memcpy_tests_run(s, 0, 0, 2 * getpagesize ());
  test_memcpy_tests_run(s, 0, 0, 4 * getpagesize ());
  test_memcpy_tests_run(s, 0, 0, 8 * getpagesize ());
  test_memcpy_tests_run(s, 0, 0, 16 * getpagesize ());

  free(buf2);
  free(buf1);

  return 1;
}