xref: /openbmc/qemu/tests/unit/test-bitcnt.c (revision 757acb9a8295e8be4a37b2cfc1cd947e357fd29c)
1*da668aa1SThomas Huth /*
2*da668aa1SThomas Huth  * Test bit count routines
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 struct bitcnt_test_data {
13*da668aa1SThomas Huth     /* value to count */
14*da668aa1SThomas Huth     union {
15*da668aa1SThomas Huth         uint8_t  w8;
16*da668aa1SThomas Huth         uint16_t w16;
17*da668aa1SThomas Huth         uint32_t w32;
18*da668aa1SThomas Huth         uint64_t w64;
19*da668aa1SThomas Huth     } value;
20*da668aa1SThomas Huth     /* expected result */
21*da668aa1SThomas Huth     int popct;
22*da668aa1SThomas Huth };
23*da668aa1SThomas Huth 
24*da668aa1SThomas Huth struct bitcnt_test_data eight_bit_data[] = {
25*da668aa1SThomas Huth     { { .w8 = 0x00 }, .popct=0 },
26*da668aa1SThomas Huth     { { .w8 = 0x01 }, .popct=1 },
27*da668aa1SThomas Huth     { { .w8 = 0x03 }, .popct=2 },
28*da668aa1SThomas Huth     { { .w8 = 0x04 }, .popct=1 },
29*da668aa1SThomas Huth     { { .w8 = 0x0f }, .popct=4 },
30*da668aa1SThomas Huth     { { .w8 = 0x3f }, .popct=6 },
31*da668aa1SThomas Huth     { { .w8 = 0x40 }, .popct=1 },
32*da668aa1SThomas Huth     { { .w8 = 0xf0 }, .popct=4 },
33*da668aa1SThomas Huth     { { .w8 = 0x7f }, .popct=7 },
34*da668aa1SThomas Huth     { { .w8 = 0x80 }, .popct=1 },
35*da668aa1SThomas Huth     { { .w8 = 0xf1 }, .popct=5 },
36*da668aa1SThomas Huth     { { .w8 = 0xfe }, .popct=7 },
37*da668aa1SThomas Huth     { { .w8 = 0xff }, .popct=8 },
38*da668aa1SThomas Huth };
39*da668aa1SThomas Huth 
test_ctpop8(void)40*da668aa1SThomas Huth static void test_ctpop8(void)
41*da668aa1SThomas Huth {
42*da668aa1SThomas Huth     int i;
43*da668aa1SThomas Huth 
44*da668aa1SThomas Huth     for (i = 0; i < ARRAY_SIZE(eight_bit_data); i++) {
45*da668aa1SThomas Huth         struct bitcnt_test_data *d = &eight_bit_data[i];
46*da668aa1SThomas Huth         g_assert(ctpop8(d->value.w8)==d->popct);
47*da668aa1SThomas Huth     }
48*da668aa1SThomas Huth }
49*da668aa1SThomas Huth 
50*da668aa1SThomas Huth struct bitcnt_test_data sixteen_bit_data[] = {
51*da668aa1SThomas Huth     { { .w16 = 0x0000 }, .popct=0 },
52*da668aa1SThomas Huth     { { .w16 = 0x0001 }, .popct=1 },
53*da668aa1SThomas Huth     { { .w16 = 0x0003 }, .popct=2 },
54*da668aa1SThomas Huth     { { .w16 = 0x000f }, .popct=4 },
55*da668aa1SThomas Huth     { { .w16 = 0x003f }, .popct=6 },
56*da668aa1SThomas Huth     { { .w16 = 0x00f0 }, .popct=4 },
57*da668aa1SThomas Huth     { { .w16 = 0x0f0f }, .popct=8 },
58*da668aa1SThomas Huth     { { .w16 = 0x1f1f }, .popct=10 },
59*da668aa1SThomas Huth     { { .w16 = 0x4000 }, .popct=1 },
60*da668aa1SThomas Huth     { { .w16 = 0x4001 }, .popct=2 },
61*da668aa1SThomas Huth     { { .w16 = 0x7000 }, .popct=3 },
62*da668aa1SThomas Huth     { { .w16 = 0x7fff }, .popct=15 },
63*da668aa1SThomas Huth };
64*da668aa1SThomas Huth 
test_ctpop16(void)65*da668aa1SThomas Huth static void test_ctpop16(void)
66*da668aa1SThomas Huth {
67*da668aa1SThomas Huth     int i;
68*da668aa1SThomas Huth 
69*da668aa1SThomas Huth     for (i = 0; i < ARRAY_SIZE(sixteen_bit_data); i++) {
70*da668aa1SThomas Huth         struct bitcnt_test_data *d = &sixteen_bit_data[i];
71*da668aa1SThomas Huth         g_assert(ctpop16(d->value.w16)==d->popct);
72*da668aa1SThomas Huth     }
73*da668aa1SThomas Huth }
74*da668aa1SThomas Huth 
75*da668aa1SThomas Huth struct bitcnt_test_data thirtytwo_bit_data[] = {
76*da668aa1SThomas Huth     { { .w32 = 0x00000000 }, .popct=0 },
77*da668aa1SThomas Huth     { { .w32 = 0x00000001 }, .popct=1 },
78*da668aa1SThomas Huth     { { .w32 = 0x0000000f }, .popct=4 },
79*da668aa1SThomas Huth     { { .w32 = 0x00000f0f }, .popct=8 },
80*da668aa1SThomas Huth     { { .w32 = 0x00001f1f }, .popct=10 },
81*da668aa1SThomas Huth     { { .w32 = 0x00004001 }, .popct=2 },
82*da668aa1SThomas Huth     { { .w32 = 0x00007000 }, .popct=3 },
83*da668aa1SThomas Huth     { { .w32 = 0x00007fff }, .popct=15 },
84*da668aa1SThomas Huth     { { .w32 = 0x55555555 }, .popct=16 },
85*da668aa1SThomas Huth     { { .w32 = 0xaaaaaaaa }, .popct=16 },
86*da668aa1SThomas Huth     { { .w32 = 0xff000000 }, .popct=8 },
87*da668aa1SThomas Huth     { { .w32 = 0xc0c0c0c0 }, .popct=8 },
88*da668aa1SThomas Huth     { { .w32 = 0x0ffffff0 }, .popct=24 },
89*da668aa1SThomas Huth     { { .w32 = 0x80000000 }, .popct=1 },
90*da668aa1SThomas Huth     { { .w32 = 0xffffffff }, .popct=32 },
91*da668aa1SThomas Huth };
92*da668aa1SThomas Huth 
test_ctpop32(void)93*da668aa1SThomas Huth static void test_ctpop32(void)
94*da668aa1SThomas Huth {
95*da668aa1SThomas Huth     int i;
96*da668aa1SThomas Huth 
97*da668aa1SThomas Huth     for (i = 0; i < ARRAY_SIZE(thirtytwo_bit_data); i++) {
98*da668aa1SThomas Huth         struct bitcnt_test_data *d = &thirtytwo_bit_data[i];
99*da668aa1SThomas Huth         g_assert(ctpop32(d->value.w32)==d->popct);
100*da668aa1SThomas Huth     }
101*da668aa1SThomas Huth }
102*da668aa1SThomas Huth 
103*da668aa1SThomas Huth struct bitcnt_test_data sixtyfour_bit_data[] = {
104*da668aa1SThomas Huth     { { .w64 = 0x0000000000000000ULL }, .popct=0 },
105*da668aa1SThomas Huth     { { .w64 = 0x0000000000000001ULL }, .popct=1 },
106*da668aa1SThomas Huth     { { .w64 = 0x000000000000000fULL }, .popct=4 },
107*da668aa1SThomas Huth     { { .w64 = 0x0000000000000f0fULL }, .popct=8 },
108*da668aa1SThomas Huth     { { .w64 = 0x0000000000001f1fULL }, .popct=10 },
109*da668aa1SThomas Huth     { { .w64 = 0x0000000000004001ULL }, .popct=2 },
110*da668aa1SThomas Huth     { { .w64 = 0x0000000000007000ULL }, .popct=3 },
111*da668aa1SThomas Huth     { { .w64 = 0x0000000000007fffULL }, .popct=15 },
112*da668aa1SThomas Huth     { { .w64 = 0x0000005500555555ULL }, .popct=16 },
113*da668aa1SThomas Huth     { { .w64 = 0x00aa0000aaaa00aaULL }, .popct=16 },
114*da668aa1SThomas Huth     { { .w64 = 0x000f000000f00000ULL }, .popct=8 },
115*da668aa1SThomas Huth     { { .w64 = 0x0c0c0000c0c0c0c0ULL }, .popct=12 },
116*da668aa1SThomas Huth     { { .w64 = 0xf00f00f0f0f0f000ULL }, .popct=24 },
117*da668aa1SThomas Huth     { { .w64 = 0x8000000000000000ULL }, .popct=1 },
118*da668aa1SThomas Huth     { { .w64 = 0xf0f0f0f0f0f0f0f0ULL }, .popct=32 },
119*da668aa1SThomas Huth     { { .w64 = 0xffffffffffffffffULL }, .popct=64 },
120*da668aa1SThomas Huth };
121*da668aa1SThomas Huth 
test_ctpop64(void)122*da668aa1SThomas Huth static void test_ctpop64(void)
123*da668aa1SThomas Huth {
124*da668aa1SThomas Huth     int i;
125*da668aa1SThomas Huth 
126*da668aa1SThomas Huth     for (i = 0; i < ARRAY_SIZE(sixtyfour_bit_data); i++) {
127*da668aa1SThomas Huth         struct bitcnt_test_data *d = &sixtyfour_bit_data[i];
128*da668aa1SThomas Huth         g_assert(ctpop64(d->value.w64)==d->popct);
129*da668aa1SThomas Huth     }
130*da668aa1SThomas Huth }
131*da668aa1SThomas Huth 
main(int argc,char ** argv)132*da668aa1SThomas Huth int main(int argc, char **argv)
133*da668aa1SThomas Huth {
134*da668aa1SThomas Huth     g_test_init(&argc, &argv, NULL);
135*da668aa1SThomas Huth     g_test_add_func("/bitcnt/ctpop8", test_ctpop8);
136*da668aa1SThomas Huth     g_test_add_func("/bitcnt/ctpop16", test_ctpop16);
137*da668aa1SThomas Huth     g_test_add_func("/bitcnt/ctpop32", test_ctpop32);
138*da668aa1SThomas Huth     g_test_add_func("/bitcnt/ctpop64", test_ctpop64);
139*da668aa1SThomas Huth     return g_test_run();
140*da668aa1SThomas Huth }
141