-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitfield.h
189 lines (157 loc) · 5.11 KB
/
bitfield.h
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifndef _BITFIELD_H_
#define _BITFIELD_H_
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include "helpers.h"
#define BITFIELD_ASSERT(x, d) ALGO_ASSERT("bitfield", x, d)
#if __WORDSIZE >= 32
#if defined(__GNUC__)
#define bitfield_popcount __builtin_popcountl
#else
#define bitfield_popcount __bitfield_popcount
#endif
typedef unsigned long word_t;
#elif __WORDSIZE == 16
#if defined(__GNUC__)
#define bitfield_popcount __builtin_popcount
#else
#define bitfield_popcount __bitfield_popcount
#endif
typedef unsigned int word_t;
#if defined(__GNUC__)
#define bitfield_popcount __builtin_popcount
#else
#define bitfield_popcount __bitfield_popcount
#endif
#elif __WORDSIZE == 8
typedef unsigned char word_t;
#if defined(__GNUC__)
#define bitfield_popcount __builtin_popcount
#else
#define bitfield_popcount __bitfield_popcount
#endif
#else
#error Unknown CPU word size
#endif
typedef word_t bitset_t;
struct bitfield {
word_t size;
bitset_t field[];
};
typedef struct bitfield * bitfield_t;
#define __BS_SIZE (sizeof(bitset_t)*8)
#define __BF_SIZE(sz) ((sz)/__BS_SIZE + ((sz)%__BS_SIZE?1:0))
#define EMBEDDED_BITFIELD(name, sz) \
struct { \
word_t ____________ ## name; \
bitset_t ________ ## name[__BF_SIZE(sz)]; \
} ________ ## name; \
bitfield_t name
#define EMBEDDED_BITFIELD_INITIALIZE(container, name, sz) do { \
(container)->name = (bitfield_t)&(container)->________ ## name; \
(container)->name->size = (sz); \
} while (0)
#define BITFIELD(name, sz) \
struct { \
word_t ____________ ## name; \
bitset_t ________ ## name[__BF_SIZE(sz)]; \
} ________________ ## name = {sz}; \
bitfield_t name = (bitfield_t)&________________ ## name
#define STATIC_BITFIELD(name, sz) \
static struct { \
word_t ____________ ## name; \
bitset_t ________ ## name[__BF_SIZE(sz)]; \
} ________________ ## name = {sz}; \
static bitfield_t name = (bitfield_t)&________________ ## name
#define __bitfield_bounds_test(call, bf, bit, ...) ({ \
BITFIELD_ASSERT((bit) < (bf)->size, "bounds breach"); \
call(bf, bit, ##__VA_ARGS__); \
})
#if !defined(__GNUC__)
#warning slow popcount implementation
static inline unsigned int
__bitfield_popcount(bitset_t bs)
{
unsigned int cnt = 0;
while (bs) {
if (bs&1)
++cnt;
bs >>= 1;
}
return cnt;
}
#endif
static inline bitfield_t
bitfield_alloc(unsigned int size)
{
bitfield_t bf;
bf = malloc(sizeof(struct bitfield) + __BF_SIZE(size)*sizeof(bitset_t));
if (bf)
bf->size = size;
return bf;
}
static inline void
bitfield_free(bitfield_t bf)
{
free(bf);
}
#define bitfield_set(bf, bit, value) __bitfield_bounds_test(__bitfield_set, bf, bit, value)
static inline void
__bitfield_set(bitfield_t bf, unsigned int bit, int value)
{
bf->field[bit/__BS_SIZE] &= ~((word_t)1 << (bit%__BS_SIZE));
bf->field[bit/__BS_SIZE] |= (word_t)(value&1) << (bit%__BS_SIZE);
}
#define bitfield_set_one(bf, bit) __bitfield_bounds_test(__bitfield_set_one, bf, bit)
static inline void
__bitfield_set_one(bitfield_t bf, unsigned int bit)
{
bf->field[bit/__BS_SIZE] |= (word_t)1 << (bit%__BS_SIZE);
}
#define bitfield_set_zero(bf, bit) __bitfield_bounds_test(__bitfield_set_zero, bf, bit)
static inline void
__bitfield_set_zero(bitfield_t bf, unsigned int bit)
{
bf->field[bit/__BS_SIZE] &= ~((word_t)1 << (bit%__BS_SIZE));
}
#define bitfield_toggle(bf, bit) __bitfield_bounds_test(__bitfield_toggle, bf, bit)
static inline void
__bitfield_toggle(bitfield_t bf, unsigned int bit)
{
bf->field[bit/__BS_SIZE] ^= (word_t)1 << (bit%__BS_SIZE);
}
#define bitfield_get(bf, bit) __bitfield_bounds_test(__bitfield_get, bf, bit)
static inline int
__bitfield_get(bitfield_t bf, unsigned int bit)
{
return (bf->field[bit/__BS_SIZE] >> (bit%__BS_SIZE))&1;
}
static inline void
bitfield_set_all_zero(bitfield_t bf)
{
memset(bf->field, 0, __BF_SIZE(bf->size)*sizeof(bitset_t));
}
static inline void
bitfield_set_all_one(bitfield_t bf)
{
const unsigned int __bf_sz_words = __BF_SIZE(bf->size);
const unsigned int __bf_sz_bytes = __bf_sz_words*sizeof(bitset_t);
const unsigned int __bf_sz_bits = __bf_sz_bytes*8;
memset(bf->field, ~0, __bf_sz_bytes);
bf->field[__bf_sz_words-1] &= (word_t)(~0ULL) >> (__bf_sz_bits - bf->size);
}
static inline unsigned int
bitfield_count_ones(bitfield_t bf)
{
unsigned int i, cnt = 0;
for (i=0;i<__BF_SIZE(bf->size);++i)
cnt += bitfield_popcount(bf->field[i]);
return cnt;
}
static inline unsigned int
bitfield_count_zeros(bitfield_t bf)
{
return bf->size - bitfield_count_ones(bf);
}
#endif