summaryrefslogtreecommitdiff
path: root/test/debug.h
blob: 24b778741ab3459aed00f9d577f31e5923f09ea1 (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
#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++)
        {
            /* start with MSB */
            size_t byte_index = len-(1+line*8+b);
            fprintf(DUMP, "%*s%02x", 5, "", buf[byte_index]);
        }
        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);

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

    for (size_t line=0; line<len/8; line++)
    {
        fprintf(DUMP, "%*s", indent, "");
        for (size_t b=0; b<8; b++)
        {
            /* start with MSB */
            size_t byte_index = 8*(len/8 - 1 - line) + 7-b;
            /* fprintf(DUMP, "[%zu / %zu => %zu]", line, b, byte_index); */
            fprintf(DUMP, "%02x ", buf[byte_index]);
        }
        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 */