summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/testsuite/test-acmemcpy-speed.c
blob: dabef87f65f0108c627e18d45eefd5d90d742a27 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*compile-command
set -x
gcc -O3 -g -I. -I.. "$0" -DARCH_X86
exit $?
*/

/*
 * test-acmemcpy-speed.c - time all accelerated memcpy() implementations
 * Written by Andrew Church <achurch@achurch.org>
 *
 * This file is part of transcode, a video stream processing tool.
 * transcode is free software, distributable under the terms of the GNU
 * General Public License (version 2 or later).  See the file COPYING
 * for details.
 */

#include "config.h"

#define _GNU_SOURCE  /* for strsignal */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/time.h>

#define ac_memcpy local_ac_memcpy  /* to avoid clash with libac.a */
#define ac_memcpy_init local_ac_memcpy_init  /* to avoid clash with libac.a */
#include "aclib/ac.h"

/* Include memcpy.c directly to get access to the particular implementations */
#include "../aclib/memcpy.c"
#undef ac_memcpy
/* Make sure all names are available, to simplify function table */
#if !defined(ARCH_X86) || !defined(HAVE_ASM_MMX)
# define memcpy_mmx memcpy
#endif
#if !defined(ARCH_X86) || !defined(HAVE_ASM_SSE)
# define memcpy_sse memcpy
#endif
#if !defined(ARCH_X86_64) || !defined(HAVE_ASM_SSE2)
# define memcpy_amd64 memcpy
#endif

/* Default copy size and test length */
#define DEF_BLOCKSIZE    0x10000
#define DEF_TESTTIME  2000  /* milliseconds */

/*************************************************************************/

static void *old_SIGSEGV = NULL, *old_SIGILL = NULL, *old_SIGVTALRM = NULL;
static sigjmp_buf env;


static void sighandler(int sig)
{
    if (sig != SIGVTALRM)
        printf("*** %s\n", strsignal(sig));
    siglongjmp(env, sig);
}

static void set_signals(void)
{
    old_SIGSEGV   = signal(SIGSEGV  , sighandler);
    old_SIGILL    = signal(SIGILL   , sighandler);
    old_SIGVTALRM = signal(SIGVTALRM, sighandler);
}

static void clear_signals(void)
{
    signal(SIGSEGV  , old_SIGSEGV  );
    signal(SIGILL   , old_SIGILL   );
    signal(SIGVTALRM, old_SIGVTALRM);
}

/*************************************************************************/

/* align1 and align2 are 0..63 */
static void testit(void *(*func)(void *, const void *, size_t), int size,
                   int align1, int align2, int msec, uint64_t *iterations)
{
    char *chunk1, *chunk1_base;
    char *chunk2, *chunk2_base;
    int sig;
    volatile uint64_t iter = 0;

    chunk1_base = malloc(size+128);
    chunk2_base = malloc(size+128);
    chunk1 = (char *)(((long)chunk1_base+63) & -64) + align1;
    chunk2 = (char *)(((long)chunk2_base+63) & -64) + align2;
    memset(chunk1, 0x11, size);
    memset(chunk2, 0x22, size);

    set_signals();
    if ((sig = sigsetjmp(env, 1)) != 0) {
        if (sig == SIGVTALRM)
            *iterations = iter;
        else
            *iterations = 0;
    } else {
        struct itimerval timer;

        timer.it_interval.tv_sec = 0;
        timer.it_interval.tv_usec = 0;
        timer.it_value.tv_sec = msec/1000;
        timer.it_value.tv_usec = msec%1000;
        if (setitimer(ITIMER_VIRTUAL, &timer, NULL) < 0) {
            perror("setitimer");
            exit(1);
        }
        for (;;) {
            (*func)(chunk2, chunk1, size);
            iter++;
        }
    }
    clear_signals();

    free(chunk1_base);
    free(chunk2_base);
}

/*************************************************************************/

/* Turn presence/absence of #define into a number */
#if defined(ARCH_X86)
# define defined_ARCH_X86 1
#else
# define defined_ARCH_X86 0
#endif
#if defined(ARCH_X86_64)
# define defined_ARCH_X86_64 1
#else
# define defined_ARCH_X86_64 0
#endif
#if defined(HAVE_ASM_MMX)
# define defined_HAVE_ASM_MMX 1
#else
# define defined_HAVE_ASM_MMX 0
#endif
#if defined(HAVE_ASM_SSE)
# define defined_HAVE_ASM_SSE 1
#else
# define defined_HAVE_ASM_SSE 0
#endif
#if defined(HAVE_ASM_SSE2)
# define defined_HAVE_ASM_SSE2 1
#else
# define defined_HAVE_ASM_SSE2 0
#endif

/* List of routines to test, NULL-terminated */
static struct {
    const char *name;  /* centered in 5 chars */
    int arch_ok;       /* defined(ARCH_xxx) */
    int acflags;       /* required ac_cpuinfo() flags */
    void *(*func)(void *, const void *, size_t);
} testfuncs[] = {
    { "libc ", 1,                   
               0,                memcpy },
    { " mmx ", defined_ARCH_X86 && defined_HAVE_ASM_MMX,
               AC_MMX,           memcpy_mmx },
    { " sse ", defined_ARCH_X86 && defined_HAVE_ASM_SSE,
               AC_CMOVE|AC_SSE,  memcpy_sse },
    { "amd64", defined_ARCH_X86_64 && defined_HAVE_ASM_SSE2,
               AC_CMOVE|AC_SSE2, memcpy_amd64 },
    { NULL }
};

/* Alignments/sizes to test */
static struct {
    int align1, align2;
} tests[] = {
    {  0,  0 },
    {  0,  1 },
    {  0,  4 },
    {  0,  8 },
    {  0, 63 },
    {  1,  0 },
    {  1,  1 },
    {  1,  4 },
    {  1,  8 },
    {  1, 63 },
    {  4,  0 },
    {  4,  1 },
    {  8,  0 },
    {  8,  1 },
    { 63,  0 },
    { 63,  1 },
    {  0,  0 },
    {  0,  1 },
    {  0,  4 },
    {  0,  8 },
    {  0, 63 },
    {  1,  0 },
    {  1,  1 },
    {  1,  4 },
    {  1,  8 },
    {  1, 63 },
    { -1, -1 }
};

int main(int argc, char *argv[])
{
    int size = DEF_BLOCKSIZE, testtime = DEF_TESTTIME;
    int ch, i;

    while ((ch = getopt(argc, argv, "hs:t:")) != EOF) {
        if (ch == 's') {
            size = atoi(optarg);
        } else if (ch == 't') {
            testtime = atoi(optarg);
        } else {
          usage:
            fprintf(stderr,
                    "Usage: %s [-s blocksize] [-t msec-per-test]\n"
                    "Defaults: -s %d -t %d\n",
                    argv[0], DEF_BLOCKSIZE, DEF_TESTTIME);
            return 1;
        }
    }
    if (size <= 0 || testtime <= 0)
        goto usage;

    for (i = 0; testfuncs[i].name; i++) {
        if ((ac_cpuinfo() & testfuncs[i].acflags) != testfuncs[i].acflags)
            testfuncs[i].arch_ok = 0;
    }

    printf("Size: %d  msec/test: %d    Table entries in MB/s\n",
           size, testtime);
    printf("Align ");
    for (i = 0; testfuncs[i].name; i++) {
        if (testfuncs[i].arch_ok)
            printf("|%s", testfuncs[i].name);
    }
    printf("\n------");
    for (i = 0; testfuncs[i].name; i++) {
        if (testfuncs[i].arch_ok)
            printf("+-----");
    }
    printf("\n");

    for (i = 0; tests[i].align1 >= 0; i++) {
        int j;
        printf("%2d/%2d ", tests[i].align1, tests[i].align2);
        fflush(stdout);
        for (j = 0; testfuncs[j].name; j++) {
            if (testfuncs[j].arch_ok) {
                uint64_t iterations;
                testit(testfuncs[j].func, size,
                       tests[i].align1, tests[i].align2, testtime,
                       &iterations);
                if (iterations == 0)
                    printf("|-ERR-");
                else
                    printf("|%5d",
                           (int)(iterations*size*1000 / testtime / (1<<20)));
                fflush(stdout);
            }
        }
        printf("\n");
    }
    return 0;
}

/*************************************************************************/

/*
 * Local variables:
 *   c-file-style: "stroustrup"
 *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
 *   indent-tabs-mode: nil
 * End:
 *
 * vim: expandtab shiftwidth=4:
 */