]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/bitmap.h
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 static inline unsigned long *
25 bitmap_unit__(const unsigned long *bitmap
, size_t offset
)
27 return CONST_CAST(unsigned long *, &bitmap
[offset
/ BITMAP_ULONG_BITS
]);
30 static inline unsigned long
31 bitmap_bit__(size_t offset
)
33 return 1UL << (offset
% BITMAP_ULONG_BITS
);
37 bitmap_n_longs(size_t n_bits
)
39 return BITMAP_N_LONGS(n_bits
);
43 bitmap_n_bytes(size_t n_bits
)
45 return bitmap_n_longs(n_bits
) * sizeof(unsigned long int);
48 static inline unsigned long *
49 bitmap_allocate(size_t n_bits
)
51 return xzalloc(bitmap_n_bytes(n_bits
));
54 /* Initializes bitmap to all-1-bits and returns the bitmap pointer. */
55 static inline unsigned long *
56 bitmap_init1(unsigned long *bitmap
, size_t n_bits
)
58 size_t n_longs
= bitmap_n_longs(n_bits
);
59 size_t n_bytes
= bitmap_n_bytes(n_bits
);
60 size_t r_bits
= n_bits
% BITMAP_ULONG_BITS
;
62 memset(bitmap
, 0xff, n_bytes
);
64 bitmap
[n_longs
- 1] = (1UL << r_bits
) - 1;
69 /* Allocates and returns a bitmap initialized to all-1-bits. */
70 static inline unsigned long *
71 bitmap_allocate1(size_t n_bits
)
73 return bitmap_init1(xmalloc(bitmap_n_bytes(n_bits
)), n_bits
);
76 static inline unsigned long *
77 bitmap_clone(const unsigned long *bitmap
, size_t n_bits
)
79 return xmemdup(bitmap
, bitmap_n_bytes(n_bits
));
83 bitmap_free(unsigned long *bitmap
)
89 bitmap_is_set(const unsigned long *bitmap
, size_t offset
)
91 return (*bitmap_unit__(bitmap
, offset
) & bitmap_bit__(offset
)) != 0;
94 static inline unsigned long *
95 bitmap_set1(unsigned long *bitmap
, size_t offset
)
97 *bitmap_unit__(bitmap
, offset
) |= bitmap_bit__(offset
);
101 static inline unsigned long *
102 bitmap_set0(unsigned long *bitmap
, size_t offset
)
104 *bitmap_unit__(bitmap
, offset
) &= ~bitmap_bit__(offset
);
108 static inline unsigned long *
109 bitmap_set(unsigned long *bitmap
, size_t offset
, bool value
)
111 return (value
) ? bitmap_set1(bitmap
, offset
) : bitmap_set0(bitmap
, offset
);
114 /* Sets 'n' bits of a single unit. */
116 bitmap_set_n__(unsigned long *bitmap
, size_t start
, size_t n
, bool value
)
118 unsigned long mask
= ((1UL << n
) - 1) << start
% BITMAP_ULONG_BITS
;
121 *bitmap_unit__(bitmap
, start
) |= mask
;
123 *bitmap_unit__(bitmap
, start
) &= ~mask
;
127 /* Sets 'count' consecutive bits in 'bitmap', starting at bit offset 'start',
129 static inline unsigned long *
130 bitmap_set_multiple(unsigned long *bitmap
, size_t start
, size_t count
,
133 if (count
&& start
% BITMAP_ULONG_BITS
) {
134 size_t n
= MIN(count
, BITMAP_ULONG_BITS
- start
% BITMAP_ULONG_BITS
);
136 bitmap_set_n__(bitmap
, start
, n
, value
);
140 for (; count
>= BITMAP_ULONG_BITS
; count
-= BITMAP_ULONG_BITS
) {
141 *bitmap_unit__(bitmap
, start
) = (unsigned long)!value
- 1;
142 start
+= BITMAP_ULONG_BITS
;
145 bitmap_set_n__(bitmap
, start
, count
, value
);
150 /* Returns the number of 1-bits in the 'n'-bit bitmap at 'bitmap'. */
152 bitmap_count1(const unsigned long int *bitmap
, size_t n
)
157 BUILD_ASSERT(ULONG_MAX
<= UINT64_MAX
);
158 for (i
= 0; i
< BITMAP_N_LONGS(n
); i
++) {
159 count
+= count_1bits(bitmap
[i
]);
164 /* "dst &= arg;" for n-bit dst and arg. */
165 static inline unsigned long *
166 bitmap_and(unsigned long *dst
, const unsigned long *arg
, size_t n
)
170 for (i
= 0; i
< BITMAP_N_LONGS(n
); i
++) {
176 /* "dst |= arg;" for n-bit dst and arg. */
177 static inline unsigned long *
178 bitmap_or(unsigned long *dst
, const unsigned long *arg
, size_t n
)
182 for (i
= 0; i
< BITMAP_N_LONGS(n
); i
++) {
188 /* "dst = ~dst;" for n-bit dst. */
189 static inline unsigned long *
190 bitmap_not(unsigned long *dst
, size_t n
)
194 for (i
= 0; i
< n
/ BITMAP_ULONG_BITS
; i
++) {
197 if (n
% BITMAP_ULONG_BITS
) {
198 dst
[i
] ^= (1UL << (n
% BITMAP_ULONG_BITS
)) - 1;
203 /* Compares the 'n' bits in bitmaps 'a' and 'b'. Returns true if all bits are
204 * equal, false otherwise. */
206 bitmap_equal(const unsigned long *a
, const unsigned long *b
, size_t n
)
208 if (memcmp(a
, b
, n
/ BITMAP_ULONG_BITS
* sizeof(unsigned long))) {
211 if (n
% BITMAP_ULONG_BITS
) {
212 unsigned long mask
= (1UL << n
% BITMAP_ULONG_BITS
) - 1;
213 unsigned long diff
= *bitmap_unit__(a
, n
) ^ *bitmap_unit__(b
, n
);
215 return !(diff
& mask
);
220 /* Scans 'bitmap' from bit offset 'start' to 'end', excluding 'end' itself.
221 * Returns the bit offset of the lowest-numbered bit set to 'target', or 'end'
222 * if all of the bits are set to '!target'. 'target' is typically a
223 * compile-time constant, so it makes sense to inline this. Compiler may also
224 * optimize parts away depending on the 'start' and 'end' values passed in. */
226 bitmap_scan(const unsigned long *bitmap
, bool target
, size_t start
, size_t end
)
228 if (OVS_LIKELY(start
< end
)) {
229 unsigned long *p
, unit
;
231 p
= bitmap_unit__(bitmap
, start
);
232 unit
= (target
? *p
: ~*p
) >> (start
% BITMAP_ULONG_BITS
);
234 start
-= start
% BITMAP_ULONG_BITS
; /* Round down. */
235 start
+= BITMAP_ULONG_BITS
; /* Start of the next unit. */
237 for (; start
< end
; start
+= BITMAP_ULONG_BITS
) {
238 unit
= target
? *++p
: ~*++p
;
246 start
+= raw_ctz(unit
); /* unit != 0 */
247 if (OVS_LIKELY(start
< end
)) {
254 /* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
255 * false otherwise. 'super' and 'sub' both have 'n_bits' bits. */
257 bitmap_is_superset(const unsigned long int *super
,
258 const unsigned long int *sub
, size_t n_bits
)
260 size_t n_longs
= bitmap_n_longs(n_bits
);
261 for (size_t i
= 0; i
< n_longs
; i
++) {
262 if (!uint_is_superset(super
[i
], sub
[i
])) {
269 /* Returns true if all of the 'n' bits in 'bitmap' are 0,
270 * false if at least one bit is a 1.*/
272 bitmap_is_all_zeros(const unsigned long *bitmap
, size_t n
)
274 return bitmap_scan(bitmap
, true, 0, n
) == n
;
277 #define BITMAP_FOR_EACH_1_RANGE(IDX, BEGIN, END, BITMAP) \
278 for ((IDX) = bitmap_scan(BITMAP, true, BEGIN, END); (IDX) < (END); \
279 (IDX) = bitmap_scan(BITMAP, true, (IDX) + 1, END))
280 #define BITMAP_FOR_EACH_1(IDX, SIZE, BITMAP) \
281 BITMAP_FOR_EACH_1_RANGE(IDX, 0, SIZE, BITMAP)
283 /* More efficient access to a map of single ullong. */
284 #define ULLONG_FOR_EACH_1(IDX, MAP) \
285 for (uint64_t map__ = (MAP); \
286 map__ && (((IDX) = raw_ctz(map__)), true); \
287 map__ = zero_rightmost_1bit(map__))
289 #define ULLONG_SET0(MAP, OFFSET) ((MAP) &= ~(1ULL << (OFFSET)))
290 #define ULLONG_SET1(MAP, OFFSET) ((MAP) |= 1ULL << (OFFSET))
292 /* Returns the value of a bit in a map as a bool. */
293 #define ULLONG_GET(MAP, OFFSET) !!((MAP) & (1ULL << (OFFSET)))
295 #endif /* bitmap.h */