summaryrefslogtreecommitdiff
path: root/test/debug.h
blob: d9584756af886f4982a2b52f51f30384f9d792a3 (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
#ifndef DEBUG_H
#define DEBUG_H

#include <inttypes.h>
#include <stdio.h>


extern FILE *DUMP; /* Define this variable somewhere (eg with DUMP = stderr). */


static inline void debug_dump_lanes(const char *header, size_t len, const uint8_t buf[len], int indent)
{
    fprintf(DUMP, "%s\n", header);

    for (size_t line=0; line<len/8; line++)
    {
        fprintf(DUMP, "%*s", indent, "");
        for (size_t b=0; b<8; b++)
        {
            fprintf(DUMP, "%*s%02x", 5, "", buf[line*8+b]);
        }
        fprintf(DUMP, "\n");
    }
    fprintf(DUMP, "\n");
}

static inline void debug_dump_buffer(const char *header, size_t len, const uint8_t buf[len], int indent)
{
    fprintf(DUMP, "%*s%s\n", indent, "", header);

    for (size_t line=0; line<len/8; line++)
    {
        fprintf(DUMP, "%*s", indent, "");
        for (size_t b=0; b<8; b++)
        {
            /* fprintf(DUMP, "[%zu / %zu => %zu]", line, b, line*8+b); */
            fprintf(DUMP, "%02x ", buf[line*8+b]);
        }
        fprintf(DUMP, "\n");
    }

    size_t rest = len%8;
    if (rest != 0)
    {
        fprintf(DUMP, "%*s", indent, "");
        for (size_t b=0; b<rest; b++)
        {
            fprintf(DUMP, "%02x ", buf[len-rest+b]);
        }
        fprintf(DUMP, "\n");
    }

    fprintf(DUMP, "\n");
}

static inline void debug_open_dump(const char *folder, const char *suite, const char *vector_name)
{
    size_t namelen = snprintf(
        NULL, 0, "%s/traces-%s-%s.txt", folder, suite, vector_name
    );
    char name[namelen+1];
    snprintf(name, sizeof(name), "%s/traces-%s-%s.txt", folder, suite, vector_name);
    DUMP = fopen(name, "w");
}


#endif /* DEBUG_H */