aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test-bitops.c
blob: 5a791d2e17dffd4e230052607dff46d08963ddc3 (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
/*
 * Test bitops routines
 *
 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
 * See the COPYING.LIB file in the top-level directory.
 *
 */

#include "qemu/osdep.h"
#include "qemu/bitops.h"

typedef struct {
    uint32_t value;
    int start;
    int length;
    int32_t result;
} S32Test;

typedef struct {
    uint64_t value;
    int start;
    int length;
    int64_t result;
} S64Test;

static const S32Test test_s32_data[] = {
    { 0x38463983, 4, 4, -8 },
    { 0x38463983, 12, 8, 0x63 },
    { 0x38463983, 0, 32, 0x38463983 },
};

static const S64Test test_s64_data[] = {
    { 0x8459826734967223ULL, 60, 4, -8 },
    { 0x8459826734967223ULL, 0, 64, 0x8459826734967223LL },
};

static void test_sextract32(void)
{
    int i;

    for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
        const S32Test *test = &test_s32_data[i];
        int32_t r = sextract32(test->value, test->start, test->length);

        g_assert_cmpint(r, ==, test->result);
    }
}

static void test_sextract64(void)
{
    int i;

    for (i = 0; i < ARRAY_SIZE(test_s32_data); i++) {
        const S32Test *test = &test_s32_data[i];
        int64_t r = sextract64(test->value, test->start, test->length);

        g_assert_cmpint(r, ==, test->result);
    }

    for (i = 0; i < ARRAY_SIZE(test_s64_data); i++) {
        const S64Test *test = &test_s64_data[i];
        int64_t r = sextract64(test->value, test->start, test->length);

        g_assert_cmpint(r, ==, test->result);
    }
}

typedef struct {
    uint32_t unshuffled;
    uint32_t shuffled;
} Shuffle32Test;

typedef struct {
    uint64_t unshuffled;
    uint64_t shuffled;
} Shuffle64Test;

static const Shuffle32Test test_shuffle32_data[] = {
    { 0x0000FFFF, 0x55555555 },
    { 0x000081C5, 0x40015011 },
};

static const Shuffle64Test test_shuffle64_data[] = {
    { 0x00000000FFFFFFFFULL, 0x5555555555555555ULL },
    { 0x00000000493AB02CULL, 0x1041054445000450ULL },
};

static void test_half_shuffle32(void)
{
    int i;

    for (i = 0; i < ARRAY_SIZE(test_shuffle32_data); i++) {
        const Shuffle32Test *test = &test_shuffle32_data[i];
        uint32_t r = half_shuffle32(test->unshuffled);

        g_assert_cmpint(r, ==, test->shuffled);
    }
}

static void test_half_shuffle64(void)
{
    int i;

    for (i = 0; i < ARRAY_SIZE(test_shuffle64_data); i++) {
        const Shuffle64Test *test = &test_shuffle64_data[i];
        uint64_t r = half_shuffle64(test->unshuffled);

        g_assert_cmpint(r, ==, test->shuffled);
    }
}

static void test_half_unshuffle32(void)
{
    int i;

    for (i = 0; i < ARRAY_SIZE(test_shuffle32_data); i++) {
        const Shuffle32Test *test = &test_shuffle32_data[i];
        uint32_t r = half_unshuffle32(test->shuffled);

        g_assert_cmpint(r, ==, test->unshuffled);
    }
}

static void test_half_unshuffle64(void)
{
    int i;

    for (i = 0; i < ARRAY_SIZE(test_shuffle64_data); i++) {
        const Shuffle64Test *test = &test_shuffle64_data[i];
        uint64_t r = half_unshuffle64(test->shuffled);

        g_assert_cmpint(r, ==, test->unshuffled);
    }
}

int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);
    g_test_add_func("/bitops/sextract32", test_sextract32);
    g_test_add_func("/bitops/sextract64", test_sextract64);
    g_test_add_func("/bitops/half_shuffle32", test_half_shuffle32);
    g_test_add_func("/bitops/half_shuffle64", test_half_shuffle64);
    g_test_add_func("/bitops/half_unshuffle32", test_half_unshuffle32);
    g_test_add_func("/bitops/half_unshuffle64", test_half_unshuffle64);
    return g_test_run();
}