1 /* Licensed under LGPLv2+ - see LICENSE file for details */
9 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
12 * We use an unsigned long to get alignment, but wrap it in a
13 * structure for type checking
19 #define BYTE(_bm, _n) (((unsigned char *)(_bm))[(_n) / 8])
20 #define LONG(_bm, _n) (((unsigned long *)(_bm))[(_n) / BITS_PER_LONG])
21 #define BIT(_n) (0x80 >> ((_n) % 8))
23 #define BYTES(_nbits) ((_nbits) / 8)
24 #define BITS(_nbits) ((~(0xff >> ((_nbits) % 8))) & 0xff)
26 static inline void bitmap_set_bit(bitmap *bitmap, int n)
28 BYTE(bitmap, n) |= BIT(n);
31 static inline void bitmap_clear_bit(bitmap *bitmap, int n)
33 BYTE(bitmap, n) &= ~BIT(n);
36 static inline void bitmap_change_bit(bitmap *bitmap, int n)
38 BYTE(bitmap, n) ^= BIT(n);
41 static inline bool bitmap_test_bit(bitmap *bitmap, int n)
43 return !!(BYTE(bitmap, n) & BIT(n));
47 static inline void bitmap_zero(bitmap *bitmap, int nbits)
49 memset(bitmap, 0, BYTES(nbits));
51 BYTE(bitmap, nbits) &= ~BITS(nbits);
54 static inline void bitmap_fill(bitmap *bitmap, int nbits)
56 memset(bitmap, 0xff, BYTES(nbits));
58 BYTE(bitmap, nbits) |= BITS(nbits);
61 static inline void bitmap_copy(bitmap *dst, bitmap *src, int nbits)
63 memcpy(dst, src, BYTES(nbits));
65 BYTE(dst, nbits) &= ~BITS(nbits);
66 BYTE(dst, nbits) |= BYTE(src, nbits) & BITS(nbits);
70 #define DEF_BINOP(_name, _op) \
71 static inline void bitmap_##_name(bitmap *dst, bitmap *src1, bitmap *src2, \
75 while ((nbits - n) >= BITS_PER_LONG) { \
76 LONG(dst, n) = LONG(src1, n) _op LONG(src2, n); \
79 while ((nbits - n) >= 8) { \
80 BYTE(dst, n) = BYTE(src1, n) _op BYTE(src2, n); \
84 BYTE(dst, nbits) &= ~BITS(nbits); \
85 BYTE(dst, nbits) |= (BYTE(src1, nbits) _op BYTE(src2, nbits)) \
93 DEF_BINOP(andnot, & ~)
97 static inline void bitmap_complement(bitmap *dst, bitmap *src, int nbits)
101 while ((nbits - n) >= BITS_PER_LONG) {
102 LONG(dst, n) = ~LONG(src, n);
105 while ((nbits - n) >= 8) {
106 BYTE(dst, n) = ~BYTE(src, n);
110 BYTE(dst, nbits) &= ~BITS(nbits);
111 BYTE(dst, nbits) |= ~BYTE(src, nbits) & BITS(nbits);
115 static inline bool bitmap_equal(bitmap *src1, bitmap *src2, int nbits)
117 if (memcmp(src1, src2, BYTES(nbits)) != 0)
119 if ((BYTE(src1, nbits) & BITS(nbits))
120 != (BYTE(src2, nbits) & BITS(nbits)))
125 static inline bool bitmap_intersects(bitmap *src1, bitmap *src2, int nbits)
129 while ((nbits - n) >= BITS_PER_LONG) {
130 if (LONG(src1, n) & LONG(src2, n))
134 while ((nbits - n) >= 8) {
135 if (BYTE(src1, n) & BYTE(src2, n))
139 if (BITS(nbits) & BYTE(src1, nbits) & BYTE(src2, nbits)) {
145 static inline bool bitmap_subset(bitmap *src1, bitmap *src2, int nbits)
149 while ((nbits - n) >= BITS_PER_LONG) {
150 if (LONG(src1, n) & ~LONG(src2, n))
154 while ((nbits - n) >= 8) {
155 if (BYTE(src1, n) & ~BYTE(src2, n))
159 if (BITS(nbits) & (BYTE(src1, nbits) & ~BYTE(src2, nbits))) {
165 static inline bool bitmap_full(bitmap *bitmap, int nbits)
169 while ((nbits - n) >= BITS_PER_LONG) {
170 if (LONG(bitmap, n) != -1UL)
174 while ((nbits - n) >= 8) {
175 if (BYTE(bitmap, n) != 0xff)
180 && ((BITS(nbits) & BYTE(bitmap, nbits)) != BITS(nbits))) {
186 static inline bool bitmap_empty(bitmap *bitmap, int nbits)
190 while ((nbits - n) >= BITS_PER_LONG) {
195 while ((nbits - n) >= 8) {
200 if (BITS(nbits) && ((BITS(nbits) & BYTE(bitmap, nbits)))) {
207 static inline bitmap *bitmap_alloc(int nbits)
209 return malloc((nbits + 7) / 8);
218 #endif /* CCAN_BITMAP_H_ */