xref: /openbmc/qemu/tests/unit/test-div128.c (revision 7d87775f)
1 /*
2  * Test 128-bit division functions
3  *
4  * Copyright (c) 2021 Instituto de Pesquisas Eldorado (eldorado.org.br)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qemu/host-utils.h"
22 
23 typedef struct {
24     uint64_t high;
25     uint64_t low;
26     uint64_t rhigh;
27     uint64_t rlow;
28     uint64_t divisor;
29     uint64_t remainder;
30 } test_data_unsigned;
31 
32 typedef struct {
33     int64_t high;
34     uint64_t low;
35     int64_t rhigh;
36     uint64_t rlow;
37     int64_t divisor;
38     int64_t remainder;
39 } test_data_signed;
40 
41 static const test_data_unsigned test_table_unsigned[] = {
42     /* Dividend fits in 64 bits */
43     { 0x0000000000000000ULL, 0x0000000000000000ULL,
44       0x0000000000000000ULL, 0x0000000000000000ULL,
45       0x0000000000000001ULL, 0x0000000000000000ULL},
46     { 0x0000000000000000ULL, 0x0000000000000001ULL,
47       0x0000000000000000ULL, 0x0000000000000001ULL,
48       0x0000000000000001ULL, 0x0000000000000000ULL},
49     { 0x0000000000000000ULL, 0x0000000000000003ULL,
50       0x0000000000000000ULL, 0x0000000000000001ULL,
51       0x0000000000000002ULL, 0x0000000000000001ULL},
52     { 0x0000000000000000ULL, 0x8000000000000000ULL,
53       0x0000000000000000ULL, 0x8000000000000000ULL,
54       0x0000000000000001ULL, 0x0000000000000000ULL},
55     { 0x0000000000000000ULL, 0xa000000000000000ULL,
56       0x0000000000000000ULL, 0x0000000000000002ULL,
57       0x4000000000000000ULL, 0x2000000000000000ULL},
58     { 0x0000000000000000ULL, 0x8000000000000000ULL,
59       0x0000000000000000ULL, 0x0000000000000001ULL,
60       0x8000000000000000ULL, 0x0000000000000000ULL},
61 
62     /* Dividend > 64 bits, with MSB 0 */
63     { 0x123456789abcdefeULL, 0xefedcba987654321ULL,
64       0x123456789abcdefeULL, 0xefedcba987654321ULL,
65       0x0000000000000001ULL, 0x0000000000000000ULL},
66     { 0x123456789abcdefeULL, 0xefedcba987654321ULL,
67       0x0000000000000001ULL, 0x000000000000000dULL,
68       0x123456789abcdefeULL, 0x03456789abcdf03bULL},
69     { 0x123456789abcdefeULL, 0xefedcba987654321ULL,
70       0x0123456789abcdefULL, 0xeefedcba98765432ULL,
71       0x0000000000000010ULL, 0x0000000000000001ULL},
72 
73     /* Dividend > 64 bits, with MSB 1 */
74     { 0xfeeddccbbaa99887ULL, 0x766554433221100fULL,
75       0xfeeddccbbaa99887ULL, 0x766554433221100fULL,
76       0x0000000000000001ULL, 0x0000000000000000ULL},
77     { 0xfeeddccbbaa99887ULL, 0x766554433221100fULL,
78       0x0000000000000001ULL, 0x0000000000000000ULL,
79       0xfeeddccbbaa99887ULL, 0x766554433221100fULL},
80     { 0xfeeddccbbaa99887ULL, 0x766554433221100fULL,
81       0x0feeddccbbaa9988ULL, 0x7766554433221100ULL,
82       0x0000000000000010ULL, 0x000000000000000fULL},
83     { 0xfeeddccbbaa99887ULL, 0x766554433221100fULL,
84       0x000000000000000eULL, 0x00f0f0f0f0f0f35aULL,
85       0x123456789abcdefeULL, 0x0f8922bc55ef90c3ULL},
86 
87     /**
88      * Divisor == 64 bits, with MSB 1
89      * and high 64 bits of dividend >= divisor
90      * (for testing normalization)
91      */
92     { 0xfeeddccbbaa99887ULL, 0x766554433221100fULL,
93       0x0000000000000001ULL, 0x0000000000000000ULL,
94       0xfeeddccbbaa99887ULL, 0x766554433221100fULL},
95     { 0xfeeddccbbaa99887ULL, 0x766554433221100fULL,
96       0x0000000000000001ULL, 0xfddbb9977553310aULL,
97       0x8000000000000001ULL, 0x78899aabbccddf05ULL},
98 
99     /* Dividend > 64 bits, divisor almost as big */
100     { 0x0000000000000001ULL, 0x23456789abcdef01ULL,
101       0x0000000000000000ULL, 0x000000000000000fULL,
102       0x123456789abcdefeULL, 0x123456789abcde1fULL},
103 };
104 
105 static const test_data_signed test_table_signed[] = {
106     /* Positive dividend, positive/negative divisors */
107     { 0x0000000000000000LL, 0x0000000000bc614eULL,
108       0x0000000000000000LL, 0x0000000000bc614eULL,
109       0x0000000000000001LL, 0x0000000000000000LL},
110     { 0x0000000000000000LL, 0x0000000000bc614eULL,
111       0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
112       0xffffffffffffffffLL, 0x0000000000000000LL},
113     { 0x0000000000000000LL, 0x0000000000bc614eULL,
114       0x0000000000000000LL, 0x00000000005e30a7ULL,
115       0x0000000000000002LL, 0x0000000000000000LL},
116     { 0x0000000000000000LL, 0x0000000000bc614eULL,
117       0xffffffffffffffffLL, 0xffffffffffa1cf59ULL,
118       0xfffffffffffffffeLL, 0x0000000000000000LL},
119     { 0x0000000000000000LL, 0x0000000000bc614eULL,
120       0x0000000000000000LL, 0x0000000000178c29ULL,
121       0x0000000000000008LL, 0x0000000000000006LL},
122     { 0x0000000000000000LL, 0x0000000000bc614eULL,
123       0xffffffffffffffffLL, 0xffffffffffe873d7ULL,
124       0xfffffffffffffff8LL, 0x0000000000000006LL},
125     { 0x0000000000000000LL, 0x0000000000bc614eULL,
126       0x0000000000000000LL, 0x000000000000550dULL,
127       0x0000000000000237LL, 0x0000000000000183LL},
128     { 0x0000000000000000LL, 0x0000000000bc614eULL,
129       0xffffffffffffffffLL, 0xffffffffffffaaf3ULL,
130       0xfffffffffffffdc9LL, 0x0000000000000183LL},
131 
132     /* Negative dividend, positive/negative divisors */
133     { 0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
134       0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
135       0x0000000000000001LL, 0x0000000000000000LL},
136     { 0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
137       0x0000000000000000LL, 0x0000000000bc614eULL,
138       0xffffffffffffffffLL, 0x0000000000000000LL},
139     { 0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
140       0xffffffffffffffffLL, 0xffffffffffa1cf59ULL,
141       0x0000000000000002LL, 0x0000000000000000LL},
142     { 0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
143       0x0000000000000000LL, 0x00000000005e30a7ULL,
144       0xfffffffffffffffeLL, 0x0000000000000000LL},
145     { 0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
146       0xffffffffffffffffLL, 0xffffffffffe873d7ULL,
147       0x0000000000000008LL, 0xfffffffffffffffaLL},
148     { 0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
149       0x0000000000000000LL, 0x0000000000178c29ULL,
150       0xfffffffffffffff8LL, 0xfffffffffffffffaLL},
151     { 0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
152       0xffffffffffffffffLL, 0xffffffffffffaaf3ULL,
153       0x0000000000000237LL, 0xfffffffffffffe7dLL},
154     { 0xffffffffffffffffLL, 0xffffffffff439eb2ULL,
155       0x0000000000000000LL, 0x000000000000550dULL,
156       0xfffffffffffffdc9LL, 0xfffffffffffffe7dLL},
157 };
158 
159 static void test_divu128(void)
160 {
161     int i;
162     uint64_t rem;
163     test_data_unsigned tmp;
164 
165     for (i = 0; i < ARRAY_SIZE(test_table_unsigned); ++i) {
166         tmp = test_table_unsigned[i];
167 
168         rem = divu128(&tmp.low, &tmp.high, tmp.divisor);
169         g_assert_cmpuint(tmp.low, ==, tmp.rlow);
170         g_assert_cmpuint(tmp.high, ==, tmp.rhigh);
171         g_assert_cmpuint(rem, ==, tmp.remainder);
172     }
173 }
174 
175 static void test_divs128(void)
176 {
177     int i;
178     int64_t rem;
179     test_data_signed tmp;
180 
181     for (i = 0; i < ARRAY_SIZE(test_table_signed); ++i) {
182         tmp = test_table_signed[i];
183 
184         rem = divs128(&tmp.low, &tmp.high, tmp.divisor);
185         g_assert_cmpuint(tmp.low, ==, tmp.rlow);
186         g_assert_cmpuint(tmp.high, ==, tmp.rhigh);
187         g_assert_cmpuint(rem, ==, tmp.remainder);
188     }
189 }
190 
191 int main(int argc, char **argv)
192 {
193     g_test_init(&argc, &argv, NULL);
194     g_test_add_func("/host-utils/test_divu128", test_divu128);
195     g_test_add_func("/host-utils/test_divs128", test_divs128);
196     return g_test_run();
197 }
198