2 * Test 128-bit division functions
4 * Copyright (c) 2021 Instituto de Pesquisas Eldorado (eldorado.org.br)
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "qemu/host-utils.h"
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
},
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
},
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
},
88 * Divisor == 64 bits, with MSB 1
89 * and high 64 bits of dividend >= divisor
90 * (for testing normalization)
92 { 0xfeeddccbbaa99887ULL
, 0x766554433221100fULL
,
93 0x0000000000000001ULL
, 0x0000000000000000ULL
,
94 0xfeeddccbbaa99887ULL
, 0x766554433221100fULL
},
95 { 0xfeeddccbbaa99887ULL
, 0x766554433221100fULL
,
96 0x0000000000000001ULL
, 0xfddbb9977553310aULL
,
97 0x8000000000000001ULL
, 0x78899aabbccddf05ULL
},
99 /* Dividend > 64 bits, divisor almost as big */
100 { 0x0000000000000001ULL
, 0x23456789abcdef01ULL
,
101 0x0000000000000000ULL
, 0x000000000000000fULL
,
102 0x123456789abcdefeULL
, 0x123456789abcde1fULL
},
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
},
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
},
159 static void test_divu128(void)
163 test_data_unsigned tmp
;
165 for (i
= 0; i
< ARRAY_SIZE(test_table_unsigned
); ++i
) {
166 tmp
= test_table_unsigned
[i
];
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
);
175 static void test_divs128(void)
179 test_data_signed tmp
;
181 for (i
= 0; i
< ARRAY_SIZE(test_table_signed
); ++i
) {
182 tmp
= test_table_signed
[i
];
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
);
191 int main(int argc
, char **argv
)
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
);