aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test-int128.c
blob: b86a3c76e6159a4c0fe3442e10ad7868611c3e10 (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
/*
 * Test Int128 arithmetic
 *
 * 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/int128.h"

/* clang doesn't support __noclone__ but it does have a mechanism for
 * telling us this. We assume that if we don't have __has_attribute()
 * then this is GCC and that GCC always supports __noclone__.
 */
#if defined(__has_attribute)
#if !__has_attribute(__noclone__)
#define ATTRIBUTE_NOCLONE
#endif
#endif
#ifndef ATTRIBUTE_NOCLONE
#define ATTRIBUTE_NOCLONE __attribute__((__noclone__))
#endif

static uint32_t tests[8] = {
    0x00000000, 0x00000001, 0x7FFFFFFE, 0x7FFFFFFF,
    0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF,
};

#define LOW    3ULL
#define HIGH   (1ULL << 63)
#define MIDDLE (-1ULL & ~LOW & ~HIGH)

static uint64_t expand16(unsigned x)
{
    return (x & LOW) | ((x & 4) ? MIDDLE : 0) | (x & 0x8000 ? HIGH : 0);
}

static Int128 expand(uint32_t x)
{
    uint64_t l, h;
    l = expand16(x & 65535);
    h = expand16(x >> 16);
    return (Int128) int128_make128(l, h);
};

static void test_and(void)
{
    int i, j;

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        for (j = 0; j < ARRAY_SIZE(tests); ++j) {
            Int128 a = expand(tests[i]);
            Int128 b = expand(tests[j]);
            Int128 r = expand(tests[i] & tests[j]);
            Int128 s = int128_and(a, b);
            g_assert_cmpuint(int128_getlo(r), ==, int128_getlo(s));
            g_assert_cmpuint(int128_gethi(r), ==, int128_gethi(s));
        }
    }
}

static void test_add(void)
{
    int i, j;

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        for (j = 0; j < ARRAY_SIZE(tests); ++j) {
            Int128 a = expand(tests[i]);
            Int128 b = expand(tests[j]);
            Int128 r = expand(tests[i] + tests[j]);
            Int128 s = int128_add(a, b);
            g_assert_cmpuint(int128_getlo(r), ==, int128_getlo(s));
            g_assert_cmpuint(int128_gethi(r), ==, int128_gethi(s));
        }
    }
}

static void test_sub(void)
{
    int i, j;

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        for (j = 0; j < ARRAY_SIZE(tests); ++j) {
            Int128 a = expand(tests[i]);
            Int128 b = expand(tests[j]);
            Int128 r = expand(tests[i] - tests[j]);
            Int128 s = int128_sub(a, b);
            g_assert_cmpuint(int128_getlo(r), ==, int128_getlo(s));
            g_assert_cmpuint(int128_gethi(r), ==, int128_gethi(s));
        }
    }
}

static void test_neg(void)
{
    int i;

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        Int128 a = expand(tests[i]);
        Int128 r = expand(-tests[i]);
        Int128 s = int128_neg(a);
        g_assert_cmpuint(int128_getlo(r), ==, int128_getlo(s));
        g_assert_cmpuint(int128_gethi(r), ==, int128_gethi(s));
    }
}

static void test_nz(void)
{
    int i, j;

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        for (j = 0; j < ARRAY_SIZE(tests); ++j) {
            Int128 a = expand(tests[i]);
            g_assert_cmpuint(int128_nz(a), ==, tests[i] != 0);
        }
    }
}

static void test_le(void)
{
    int i, j;

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        for (j = 0; j < ARRAY_SIZE(tests); ++j) {
            /* Signed comparison */
            int32_t a = (int32_t) tests[i];
            int32_t b = (int32_t) tests[j];
            g_assert_cmpuint(int128_le(expand(a), expand(b)), ==, a <= b);
        }
    }
}

static void test_lt(void)
{
    int i, j;

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        for (j = 0; j < ARRAY_SIZE(tests); ++j) {
            /* Signed comparison */
            int32_t a = (int32_t) tests[i];
            int32_t b = (int32_t) tests[j];
            g_assert_cmpuint(int128_lt(expand(a), expand(b)), ==, a < b);
        }
    }
}

static void test_ge(void)
{
    int i, j;

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        for (j = 0; j < ARRAY_SIZE(tests); ++j) {
            /* Signed comparison */
            int32_t a = (int32_t) tests[i];
            int32_t b = (int32_t) tests[j];
            g_assert_cmpuint(int128_ge(expand(a), expand(b)), ==, a >= b);
        }
    }
}

static void test_gt(void)
{
    int i, j;

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        for (j = 0; j < ARRAY_SIZE(tests); ++j) {
            /* Signed comparison */
            int32_t a = (int32_t) tests[i];
            int32_t b = (int32_t) tests[j];
            g_assert_cmpuint(int128_gt(expand(a), expand(b)), ==, a > b);
        }
    }
}

/* Make sure to test undefined behavior at runtime! */

static void __attribute__((__noinline__)) ATTRIBUTE_NOCLONE
test_rshift_one(uint32_t x, int n, uint64_t h, uint64_t l)
{
    Int128 a = expand(x);
    Int128 r = int128_rshift(a, n);
    g_assert_cmpuint(int128_getlo(r), ==, l);
    g_assert_cmpuint(int128_gethi(r), ==, h);
}

static void test_rshift(void)
{
    test_rshift_one(0x00010000U, 64, 0x0000000000000000ULL, 0x0000000000000001ULL);
    test_rshift_one(0x80010000U, 64, 0xFFFFFFFFFFFFFFFFULL, 0x8000000000000001ULL);
    test_rshift_one(0x7FFE0000U, 64, 0x0000000000000000ULL, 0x7FFFFFFFFFFFFFFEULL);
    test_rshift_one(0xFFFE0000U, 64, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFEULL);
    test_rshift_one(0x00010000U, 60, 0x0000000000000000ULL, 0x0000000000000010ULL);
    test_rshift_one(0x80010000U, 60, 0xFFFFFFFFFFFFFFF8ULL, 0x0000000000000010ULL);
    test_rshift_one(0x00018000U, 60, 0x0000000000000000ULL, 0x0000000000000018ULL);
    test_rshift_one(0x80018000U, 60, 0xFFFFFFFFFFFFFFF8ULL, 0x0000000000000018ULL);
    test_rshift_one(0x7FFE0000U, 60, 0x0000000000000007ULL, 0xFFFFFFFFFFFFFFE0ULL);
    test_rshift_one(0xFFFE0000U, 60, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFE0ULL);
    test_rshift_one(0x7FFE8000U, 60, 0x0000000000000007ULL, 0xFFFFFFFFFFFFFFE8ULL);
    test_rshift_one(0xFFFE8000U, 60, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFE8ULL);
    test_rshift_one(0x00018000U,  0, 0x0000000000000001ULL, 0x8000000000000000ULL);
    test_rshift_one(0x80018000U,  0, 0x8000000000000001ULL, 0x8000000000000000ULL);
    test_rshift_one(0x7FFE0000U,  0, 0x7FFFFFFFFFFFFFFEULL, 0x0000000000000000ULL);
    test_rshift_one(0xFFFE0000U,  0, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000000ULL);
    test_rshift_one(0x7FFE8000U,  0, 0x7FFFFFFFFFFFFFFEULL, 0x8000000000000000ULL);
    test_rshift_one(0xFFFE8000U,  0, 0xFFFFFFFFFFFFFFFEULL, 0x8000000000000000ULL);
}

int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);
    g_test_add_func("/int128/int128_and", test_and);
    g_test_add_func("/int128/int128_add", test_add);
    g_test_add_func("/int128/int128_sub", test_sub);
    g_test_add_func("/int128/int128_neg", test_neg);
    g_test_add_func("/int128/int128_nz", test_nz);
    g_test_add_func("/int128/int128_le", test_le);
    g_test_add_func("/int128/int128_lt", test_lt);
    g_test_add_func("/int128/int128_ge", test_ge);
    g_test_add_func("/int128/int128_gt", test_gt);
    g_test_add_func("/int128/int128_rshift", test_rshift);
    return g_test_run();
}