]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-int128.c
acpi-dsdt: procedurally generate _PRT
[mirror_qemu.git] / tests / test-int128.c
CommitLineData
6046c620
PB
1/*
2 * Test Int128 arithmetic
3 *
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.
6 *
7 */
8
9#include <glib.h>
10#include <stdio.h>
11#include "qemu/int128.h"
12#include "qemu/osdep.h"
13
7edd9ddc
PM
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__.
17 */
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
26
6046c620
PB
27static uint32_t tests[8] = {
28 0x00000000, 0x00000001, 0x7FFFFFFE, 0x7FFFFFFF,
29 0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF,
30};
31
32#define LOW 3ULL
33#define HIGH (1ULL << 63)
34#define MIDDLE (-1ULL & ~LOW & ~HIGH)
35
36static uint64_t expand16(unsigned x)
37{
38 return (x & LOW) | ((x & 4) ? MIDDLE : 0) | (x & 0x8000 ? HIGH : 0);
39}
40
41static Int128 expand(uint32_t x)
42{
43 uint64_t l, h;
44 l = expand16(x & 65535);
45 h = expand16(x >> 16);
46 return (Int128) {l, h};
47};
48
49static void test_and(void)
50{
51 int i, j;
52
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);
61 }
62 }
63}
64
65static void test_add(void)
66{
67 int i, j;
68
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);
77 }
78 }
79}
80
81static void test_sub(void)
82{
83 int i, j;
84
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);
93 }
94 }
95}
96
97static void test_neg(void)
98{
99 int i;
100
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);
107 }
108}
109
110static void test_nz(void)
111{
112 int i, j;
113
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);
118 }
119 }
120}
121
122static void test_le(void)
123{
124 int i, j;
125
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);
132 }
133 }
134}
135
136static void test_lt(void)
137{
138 int i, j;
139
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);
146 }
147 }
148}
149
150static void test_ge(void)
151{
152 int i, j;
153
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);
160 }
161 }
162}
163
164static void test_gt(void)
165{
166 int i, j;
167
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);
174 }
175 }
176}
177
178/* Make sure to test undefined behavior at runtime! */
179
7edd9ddc 180static void __attribute__((__noinline__)) ATTRIBUTE_NOCLONE
6046c620
PB
181test_rshift_one(uint32_t x, int n, uint64_t h, uint64_t l)
182{
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);
187}
188
189static void test_rshift(void)
190{
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);
209}
210
211int main(int argc, char **argv)
212{
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();
225}