xref: /openbmc/qemu/tests/unit/test-shift128.c (revision 757acb9a8295e8be4a37b2cfc1cd947e357fd29c)
1*da668aa1SThomas Huth /*
2*da668aa1SThomas Huth  * Test unsigned left and right shift
3*da668aa1SThomas Huth  *
4*da668aa1SThomas Huth  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
5*da668aa1SThomas Huth  * See the COPYING.LIB file in the top-level directory.
6*da668aa1SThomas Huth  *
7*da668aa1SThomas Huth  */
8*da668aa1SThomas Huth 
9*da668aa1SThomas Huth #include "qemu/osdep.h"
10*da668aa1SThomas Huth #include "qemu/host-utils.h"
11*da668aa1SThomas Huth 
12*da668aa1SThomas Huth typedef struct {
13*da668aa1SThomas Huth     uint64_t low;
14*da668aa1SThomas Huth     uint64_t high;
15*da668aa1SThomas Huth     uint64_t rlow;
16*da668aa1SThomas Huth     uint64_t rhigh;
17*da668aa1SThomas Huth     int32_t shift;
18*da668aa1SThomas Huth     bool overflow;
19*da668aa1SThomas Huth } test_data;
20*da668aa1SThomas Huth 
21*da668aa1SThomas Huth static const test_data test_ltable[] = {
22*da668aa1SThomas Huth     { 0x4C7ULL, 0x0ULL, 0x00000000000004C7ULL,
23*da668aa1SThomas Huth       0x0000000000000000ULL,   0, false },
24*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000002ULL,
25*da668aa1SThomas Huth       0x0000000000000000ULL,   1, false },
26*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000004ULL,
27*da668aa1SThomas Huth       0x0000000000000000ULL,   2, false },
28*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000010ULL,
29*da668aa1SThomas Huth       0x0000000000000000ULL,   4, false },
30*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000100ULL,
31*da668aa1SThomas Huth       0x0000000000000000ULL,   8, false },
32*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000010000ULL,
33*da668aa1SThomas Huth       0x0000000000000000ULL,  16, false },
34*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000080000000ULL,
35*da668aa1SThomas Huth       0x0000000000000000ULL,  31, false },
36*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000200000000000ULL,
37*da668aa1SThomas Huth       0x0000000000000000ULL,  45, false },
38*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x1000000000000000ULL,
39*da668aa1SThomas Huth       0x0000000000000000ULL,  60, false },
40*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
41*da668aa1SThomas Huth       0x0000000000000001ULL,  64, false },
42*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
43*da668aa1SThomas Huth       0x0000000000010000ULL,  80, false },
44*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
45*da668aa1SThomas Huth       0x8000000000000000ULL, 127, false },
46*da668aa1SThomas Huth     { 0x000ULL, 0x1ULL, 0x0000000000000000ULL,
47*da668aa1SThomas Huth       0x0000000000000000ULL,  64,  true },
48*da668aa1SThomas Huth     { 0x008ULL, 0x0ULL, 0x0000000000000000ULL,
49*da668aa1SThomas Huth       0x0000000000000008ULL,  64, false },
50*da668aa1SThomas Huth     { 0x008ULL, 0x0ULL, 0x0000000000000000ULL,
51*da668aa1SThomas Huth       0x8000000000000000ULL, 124, false },
52*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
53*da668aa1SThomas Huth       0x4000000000000000ULL, 126, false },
54*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
55*da668aa1SThomas Huth       0x8000000000000000ULL, 127, false },
56*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000001ULL,
57*da668aa1SThomas Huth       0x0000000000000000ULL, 128,  false },
58*da668aa1SThomas Huth     { 0x000ULL, 0x0ULL, 0x0000000000000000ULL,
59*da668aa1SThomas Huth       0x0000000000000000ULL, 200, false },
60*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
61*da668aa1SThomas Huth       0x0000000000000100ULL, 200,  false },
62*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
63*da668aa1SThomas Huth       0x8000000000000000ULL,  -1, false },
64*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
65*da668aa1SThomas Huth       0x8000000000000000ULL, INT32_MAX, false },
66*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
67*da668aa1SThomas Huth       0x4000000000000000ULL,  -2, false },
68*da668aa1SThomas Huth     { 0x001ULL, 0x0ULL, 0x0000000000000000ULL,
69*da668aa1SThomas Huth       0x4000000000000000ULL, INT32_MAX - 1, false },
70*da668aa1SThomas Huth     { 0x8888888888888888ULL, 0x9999999999999999ULL,
71*da668aa1SThomas Huth       0x8000000000000000ULL, 0x9888888888888888ULL, 60, true },
72*da668aa1SThomas Huth     { 0x8888888888888888ULL, 0x9999999999999999ULL,
73*da668aa1SThomas Huth       0x0000000000000000ULL, 0x8888888888888888ULL, 64, true },
74*da668aa1SThomas Huth };
75*da668aa1SThomas Huth 
76*da668aa1SThomas Huth static const test_data test_rtable[] = {
77*da668aa1SThomas Huth     { 0x00000000000004C7ULL, 0x0ULL, 0x00000000000004C7ULL, 0x0ULL,  0, false },
78*da668aa1SThomas Huth     { 0x0800000000000000ULL, 0x0ULL, 0x0400000000000000ULL, 0x0ULL,  1, false },
79*da668aa1SThomas Huth     { 0x0800000000000000ULL, 0x0ULL, 0x0200000000000000ULL, 0x0ULL,  2, false },
80*da668aa1SThomas Huth     { 0x0800000000000000ULL, 0x0ULL, 0x0008000000000000ULL, 0x0ULL,  8, false },
81*da668aa1SThomas Huth     { 0x0800000000000000ULL, 0x0ULL, 0x0000080000000000ULL, 0x0ULL, 16, false },
82*da668aa1SThomas Huth     { 0x0800000000000000ULL, 0x0ULL, 0x0000000008000000ULL, 0x0ULL, 32, false },
83*da668aa1SThomas Huth     { 0x8000000000000000ULL, 0x0ULL, 0x0000000000000001ULL, 0x0ULL, 63, false },
84*da668aa1SThomas Huth     { 0x8000000000000000ULL, 0x0ULL, 0x0000000000000000ULL, 0x0ULL, 64, false },
85*da668aa1SThomas Huth     { 0x0000000000000000ULL, 0x8000000000000000ULL,
86*da668aa1SThomas Huth       0x0000000000000000ULL, 0x8000000000000000ULL, 128, false },
87*da668aa1SThomas Huth     { 0x0000000000000000ULL, 0x8000000000000000ULL,
88*da668aa1SThomas Huth       0x0080000000000000ULL, 0x0000000000000000ULL, 200, false },
89*da668aa1SThomas Huth     { 0x0000000000000000ULL, 0x0000000000000000ULL,
90*da668aa1SThomas Huth       0x0000000000000000ULL, 0x0000000000000000ULL, 200, false },
91*da668aa1SThomas Huth     { 0x0000000000000000ULL, 0x8000000000000000ULL,
92*da668aa1SThomas Huth       0x0000000000000000ULL, 0x0000000000000080ULL, -200, false },
93*da668aa1SThomas Huth     { 0x8000000000000000ULL, 0x8000000000000000ULL,
94*da668aa1SThomas Huth       0x0000000080000000ULL, 0x0000000080000000ULL, 32, false },
95*da668aa1SThomas Huth     { 0x0800000000000000ULL, 0x0800000000000000ULL,
96*da668aa1SThomas Huth       0x0800000000000000ULL, 0x0000000000000000ULL, 64, false },
97*da668aa1SThomas Huth     { 0x0800000000000000ULL, 0x0800000000000000ULL,
98*da668aa1SThomas Huth       0x0008000000000000ULL, 0x0000000000000000ULL, 72, false },
99*da668aa1SThomas Huth     { 0x8000000000000000ULL, 0x8000000000000000ULL,
100*da668aa1SThomas Huth       0x0000000000000001ULL, 0x0000000000000000ULL, 127, false },
101*da668aa1SThomas Huth     { 0x0000000000000000ULL, 0x8000000000000000ULL,
102*da668aa1SThomas Huth       0x0000000000000001ULL, 0x0000000000000000ULL, -1, false },
103*da668aa1SThomas Huth     { 0x0000000000000000ULL, 0x8000000000000000ULL,
104*da668aa1SThomas Huth       0x0000000000000002ULL, 0x0000000000000000ULL, -2, false },
105*da668aa1SThomas Huth };
106*da668aa1SThomas Huth 
test_lshift(void)107*da668aa1SThomas Huth static void test_lshift(void)
108*da668aa1SThomas Huth {
109*da668aa1SThomas Huth     int i;
110*da668aa1SThomas Huth 
111*da668aa1SThomas Huth     for (i = 0; i < ARRAY_SIZE(test_ltable); ++i) {
112*da668aa1SThomas Huth         bool overflow = false;
113*da668aa1SThomas Huth         test_data tmp = test_ltable[i];
114*da668aa1SThomas Huth         ulshift(&tmp.low, &tmp.high, tmp.shift, &overflow);
115*da668aa1SThomas Huth         g_assert_cmpuint(tmp.low, ==, tmp.rlow);
116*da668aa1SThomas Huth         g_assert_cmpuint(tmp.high, ==, tmp.rhigh);
117*da668aa1SThomas Huth         g_assert_cmpuint(tmp.overflow, ==, overflow);
118*da668aa1SThomas Huth     }
119*da668aa1SThomas Huth }
120*da668aa1SThomas Huth 
test_rshift(void)121*da668aa1SThomas Huth static void test_rshift(void)
122*da668aa1SThomas Huth {
123*da668aa1SThomas Huth     int i;
124*da668aa1SThomas Huth 
125*da668aa1SThomas Huth     for (i = 0; i < ARRAY_SIZE(test_rtable); ++i) {
126*da668aa1SThomas Huth         test_data tmp = test_rtable[i];
127*da668aa1SThomas Huth         urshift(&tmp.low, &tmp.high, tmp.shift);
128*da668aa1SThomas Huth         g_assert_cmpuint(tmp.low, ==, tmp.rlow);
129*da668aa1SThomas Huth         g_assert_cmpuint(tmp.high, ==, tmp.rhigh);
130*da668aa1SThomas Huth     }
131*da668aa1SThomas Huth }
132*da668aa1SThomas Huth 
main(int argc,char ** argv)133*da668aa1SThomas Huth int main(int argc, char **argv)
134*da668aa1SThomas Huth {
135*da668aa1SThomas Huth     g_test_init(&argc, &argv, NULL);
136*da668aa1SThomas Huth     g_test_add_func("/host-utils/test_lshift", test_lshift);
137*da668aa1SThomas Huth     g_test_add_func("/host-utils/test_rshift", test_rshift);
138*da668aa1SThomas Huth     return g_test_run();
139*da668aa1SThomas Huth }
140