1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#include <usual/bits.h>
#include "test_common.h"
/*
* is_power_of_2
*/
static void test_pow2(void *p)
{
int_check(is_power_of_2(0), 0);
int_check(is_power_of_2(1), 1);
int_check(is_power_of_2(2), 1);
int_check(is_power_of_2(3), 0);
end:;
}
/*
* rol
*/
static void test_rol(void *p)
{
/* rol16 */
int_check(rol16(1, 1), 2);
int_check(rol16(1, 15), 32768);
int_check(rol16(0x8000, 1), 1);
/* rol32 */
int_check(rol32(1, 1), 2);
int_check(rol32(0x80000000, 1), 1);
/* rol64 */
ull_check(rol64(1, 1), 2);
ull_check(rol64(1, 63), 0x8000000000000000ULL);
end:;
}
/*
* ror
*/
static void test_ror(void *p)
{
/* ror16 */
int_check(ror16(1, 1), 0x8000);
/* ror32 */
int_check(ror32(1, 1), 0x80000000);
/* ror64 */
ull_check(ror64(1, 1), 0x8000000000000000ULL);
end:;
}
/*
* fls
*/
static void test_fls(void *p)
{
/* fls */
int_check(fls(0), 0);
int_check(fls(1), 1);
int_check(fls(3), 2);
int_check(fls((int)-1), 32);
/* flsl */
int_check(flsl(0), 0);
int_check(flsl(1), 1);
int_check(flsl(3), 2);
if (sizeof(long) == 4)
int_check(flsl((long)-1), 32);
else
int_check(flsl((long)-1), 64);
/* flsll */
int_check(flsll(0), 0);
int_check(flsll(1), 1);
int_check(flsll(3), 2);
int_check(flsll((long long)-1), 64);
end:;
}
/*
* ffs
*/
static void test_ffs(void *p)
{
/* ffs */
int_check(ffs(0), 0);
int_check(ffs(1), 1);
int_check(ffs(3), 1);
int_check(ffs((int)-1), 1);
int_check(ffs(ror32(1,1)), 32);
/* flsl */
int_check(ffsl(0), 0);
int_check(ffsl(1), 1);
int_check(ffsl(3), 1);
int_check(ffsl((long)-1), 1);
if (sizeof(long) == 4)
int_check(ffsl(ror32(1,1)), 32);
else
int_check(ffsl(ror64(1,1)), 64);
/* ffsll */
int_check(ffsll(0), 0);
int_check(ffsll(1), 1);
int_check(ffsll(3), 1);
int_check(ffsll((long long)-1), 1);
ull_check((1ULL << 63), ror64(1,1));
int_check(ffsll(1ULL << 63), 64);
int_check(ffsll(ror64(1,1)), 64);
end:;
}
/*
* Describe
*/
struct testcase_t bits_tests[] = {
{ "is_power_of_2", test_pow2 },
{ "rol", test_rol },
{ "ror", test_ror },
{ "ffs", test_ffs },
{ "fls", test_fls },
END_OF_TESTCASES
};
|