qemu-doc: fix typos
[qemu.git] / tests / test-int128.c
blob0772ef7538c9d8ebea3295f0925f2e34ef416ba7
1 /*
2 * Test Int128 arithmetic
4 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
5 * See the COPYING.LIB file in the top-level directory.
7 */
9 #include <glib.h>
10 #include <stdio.h>
11 #include "qemu/int128.h"
12 #include "qemu/osdep.h"
14 /* clang doesn't support __noclone__ but it does have a mechanism for
15 * telling us this. We assume that if we don't have __has_attribute()
16 * then this is GCC and that GCC always supports __noclone__.
18 #if defined(__has_attribute)
19 #if !__has_attribute(__noclone__)
20 #define ATTRIBUTE_NOCLONE
21 #endif
22 #endif
23 #ifndef ATTRIBUTE_NOCLONE
24 #define ATTRIBUTE_NOCLONE __attribute__((__noclone__))
25 #endif
27 static uint32_t tests[8] = {
28 0x00000000, 0x00000001, 0x7FFFFFFE, 0x7FFFFFFF,
29 0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF,
32 #define LOW 3ULL
33 #define HIGH (1ULL << 63)
34 #define MIDDLE (-1ULL & ~LOW & ~HIGH)
36 static uint64_t expand16(unsigned x)
38 return (x & LOW) | ((x & 4) ? MIDDLE : 0) | (x & 0x8000 ? HIGH : 0);
41 static Int128 expand(uint32_t x)
43 uint64_t l, h;
44 l = expand16(x & 65535);
45 h = expand16(x >> 16);
46 return (Int128) {l, h};
49 static void test_and(void)
51 int i, j;
53 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
54 for (j = 0; j < ARRAY_SIZE(tests); ++j) {
55 Int128 a = expand(tests[i]);
56 Int128 b = expand(tests[j]);
57 Int128 r = expand(tests[i] & tests[j]);
58 Int128 s = int128_and(a, b);
59 g_assert_cmpuint(r.lo, ==, s.lo);
60 g_assert_cmpuint(r.hi, ==, s.hi);
65 static void test_add(void)
67 int i, j;
69 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
70 for (j = 0; j < ARRAY_SIZE(tests); ++j) {
71 Int128 a = expand(tests[i]);
72 Int128 b = expand(tests[j]);
73 Int128 r = expand(tests[i] + tests[j]);
74 Int128 s = int128_add(a, b);
75 g_assert_cmpuint(r.lo, ==, s.lo);
76 g_assert_cmpuint(r.hi, ==, s.hi);
81 static void test_sub(void)
83 int i, j;
85 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
86 for (j = 0; j < ARRAY_SIZE(tests); ++j) {
87 Int128 a = expand(tests[i]);
88 Int128 b = expand(tests[j]);
89 Int128 r = expand(tests[i] - tests[j]);
90 Int128 s = int128_sub(a, b);
91 g_assert_cmpuint(r.lo, ==, s.lo);
92 g_assert_cmpuint(r.hi, ==, s.hi);
97 static void test_neg(void)
99 int i;
101 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
102 Int128 a = expand(tests[i]);
103 Int128 r = expand(-tests[i]);
104 Int128 s = int128_neg(a);
105 g_assert_cmpuint(r.lo, ==, s.lo);
106 g_assert_cmpuint(r.hi, ==, s.hi);
110 static void test_nz(void)
112 int i, j;
114 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
115 for (j = 0; j < ARRAY_SIZE(tests); ++j) {
116 Int128 a = expand(tests[i]);
117 g_assert_cmpuint(int128_nz(a), ==, tests[i] != 0);
122 static void test_le(void)
124 int i, j;
126 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
127 for (j = 0; j < ARRAY_SIZE(tests); ++j) {
128 /* Signed comparison */
129 int32_t a = (int32_t) tests[i];
130 int32_t b = (int32_t) tests[j];
131 g_assert_cmpuint(int128_le(expand(a), expand(b)), ==, a <= b);
136 static void test_lt(void)
138 int i, j;
140 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
141 for (j = 0; j < ARRAY_SIZE(tests); ++j) {
142 /* Signed comparison */
143 int32_t a = (int32_t) tests[i];
144 int32_t b = (int32_t) tests[j];
145 g_assert_cmpuint(int128_lt(expand(a), expand(b)), ==, a < b);
150 static void test_ge(void)
152 int i, j;
154 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
155 for (j = 0; j < ARRAY_SIZE(tests); ++j) {
156 /* Signed comparison */
157 int32_t a = (int32_t) tests[i];
158 int32_t b = (int32_t) tests[j];
159 g_assert_cmpuint(int128_ge(expand(a), expand(b)), ==, a >= b);
164 static void test_gt(void)
166 int i, j;
168 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
169 for (j = 0; j < ARRAY_SIZE(tests); ++j) {
170 /* Signed comparison */
171 int32_t a = (int32_t) tests[i];
172 int32_t b = (int32_t) tests[j];
173 g_assert_cmpuint(int128_gt(expand(a), expand(b)), ==, a > b);
178 /* Make sure to test undefined behavior at runtime! */
180 static void __attribute__((__noinline__)) ATTRIBUTE_NOCLONE
181 test_rshift_one(uint32_t x, int n, uint64_t h, uint64_t l)
183 Int128 a = expand(x);
184 Int128 r = int128_rshift(a, n);
185 g_assert_cmpuint(r.lo, ==, l);
186 g_assert_cmpuint(r.hi, ==, h);
189 static void test_rshift(void)
191 test_rshift_one(0x00010000U, 64, 0x0000000000000000ULL, 0x0000000000000001ULL);
192 test_rshift_one(0x80010000U, 64, 0xFFFFFFFFFFFFFFFFULL, 0x8000000000000001ULL);
193 test_rshift_one(0x7FFE0000U, 64, 0x0000000000000000ULL, 0x7FFFFFFFFFFFFFFEULL);
194 test_rshift_one(0xFFFE0000U, 64, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFEULL);
195 test_rshift_one(0x00010000U, 60, 0x0000000000000000ULL, 0x0000000000000010ULL);
196 test_rshift_one(0x80010000U, 60, 0xFFFFFFFFFFFFFFF8ULL, 0x0000000000000010ULL);
197 test_rshift_one(0x00018000U, 60, 0x0000000000000000ULL, 0x0000000000000018ULL);
198 test_rshift_one(0x80018000U, 60, 0xFFFFFFFFFFFFFFF8ULL, 0x0000000000000018ULL);
199 test_rshift_one(0x7FFE0000U, 60, 0x0000000000000007ULL, 0xFFFFFFFFFFFFFFE0ULL);
200 test_rshift_one(0xFFFE0000U, 60, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFE0ULL);
201 test_rshift_one(0x7FFE8000U, 60, 0x0000000000000007ULL, 0xFFFFFFFFFFFFFFE8ULL);
202 test_rshift_one(0xFFFE8000U, 60, 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFE8ULL);
203 test_rshift_one(0x00018000U, 0, 0x0000000000000001ULL, 0x8000000000000000ULL);
204 test_rshift_one(0x80018000U, 0, 0x8000000000000001ULL, 0x8000000000000000ULL);
205 test_rshift_one(0x7FFE0000U, 0, 0x7FFFFFFFFFFFFFFEULL, 0x0000000000000000ULL);
206 test_rshift_one(0xFFFE0000U, 0, 0xFFFFFFFFFFFFFFFEULL, 0x0000000000000000ULL);
207 test_rshift_one(0x7FFE8000U, 0, 0x7FFFFFFFFFFFFFFEULL, 0x8000000000000000ULL);
208 test_rshift_one(0xFFFE8000U, 0, 0xFFFFFFFFFFFFFFFEULL, 0x8000000000000000ULL);
211 int main(int argc, char **argv)
213 g_test_init(&argc, &argv, NULL);
214 g_test_add_func("/int128/int128_and", test_and);
215 g_test_add_func("/int128/int128_add", test_add);
216 g_test_add_func("/int128/int128_sub", test_sub);
217 g_test_add_func("/int128/int128_neg", test_neg);
218 g_test_add_func("/int128/int128_nz", test_nz);
219 g_test_add_func("/int128/int128_le", test_le);
220 g_test_add_func("/int128/int128_lt", test_lt);
221 g_test_add_func("/int128/int128_ge", test_ge);
222 g_test_add_func("/int128/int128_gt", test_gt);
223 g_test_add_func("/int128/int128_rshift", test_rshift);
224 return g_test_run();