2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 Copyright (c) 2001, 2002, 2003, 2005 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #ifndef included_clib_bitmap_h
39 #define included_clib_bitmap_h
42 Bitmaps built as vectors of machine words
45 #include <vppinfra/vec.h>
46 #include <vppinfra/random.h>
47 #include <vppinfra/error.h>
48 #include <vppinfra/bitops.h> /* for count_set_bits */
50 typedef uword clib_bitmap_t;
52 /** predicate function; is an entire bitmap empty?
53 @param ai - pointer to a bitmap
54 @returns 1 if the entire bitmap is zero, 0 otherwise
57 clib_bitmap_is_zero (uword * ai)
60 for (i = 0; i < vec_len (ai); i++)
66 /** predicate function; are two bitmaps equal?
67 @param a - pointer to a bitmap
68 @param b - pointer to a bitmap
69 @returns 1 if the bitmaps are equal, 0 otherwise
72 clib_bitmap_is_equal (uword * a, uword * b)
75 if (vec_len (a) != vec_len (b))
77 for (i = 0; i < vec_len (a); i++)
83 /** Duplicate a bitmap
84 @param v - pointer to a bitmap
85 @returns a duplicate of the bitmap
87 #define clib_bitmap_dup(v) vec_dup(v)
90 @param v - pointer to the bitmap to free
92 #define clib_bitmap_free(v) vec_free(v)
94 /** Number of bytes in a bitmap
95 @param v - pointer to the bitmap
97 #define clib_bitmap_bytes(v) vec_bytes(v)
100 @param v - pointer to the bitmap to clear
102 #define clib_bitmap_zero(v) vec_zero(v)
104 /** Allocate a bitmap with the supplied number of bits
105 @param [out] v - the resulting bitmap
106 @param n_bits - the required number of bits
109 #define clib_bitmap_alloc(v,n_bits) \
110 v = vec_new (uword, ((n_bits) + BITS (uword) - 1) / BITS (uword))
112 #define clib_bitmap_vec_validate(v,i) vec_validate_aligned((v),(i),sizeof(uword))
114 /* Make sure that a bitmap is at least n_bits in size */
115 #define clib_bitmap_validate(v,n_bits) \
116 clib_bitmap_vec_validate ((v), ((n_bits) - 1) / BITS (uword))
120 @param src - copy from
122 static_always_inline void
123 clib_bitmap_copy (clib_bitmap_t **dst, const clib_bitmap_t *src)
127 clib_bitmap_vec_validate (*dst, vec_len (src) - 1);
128 vec_copy (*dst, src);
132 vec_reset_length (*dst);
136 /* low-level routine to remove trailing zeros from a bitmap */
137 always_inline uword *
138 _clib_bitmap_remove_trailing_zeros (uword * a)
143 for (i = _vec_len (a) - 1; i >= 0; i--)
146 _vec_len (a) = i + 1;
151 /** Sets the ith bit of a bitmap to new_value.
152 No sanity checking. Be careful.
153 @param a - pointer to the bitmap
154 @param i - the bit position to interrogate
155 @param new_value - new value for the bit
156 @returns the old value of the bit
159 clib_bitmap_set_no_check (uword * a, uword i, uword new_value)
161 uword i0 = i / BITS (a[0]);
162 uword i1 = i % BITS (a[0]);
163 uword bit = (uword) 1 << i1;
166 /* Removed ASSERT since uword * a may not be a vector. */
167 /* ASSERT (i0 < vec_len (a)); */
170 old_value = (ai & bit) != 0;
172 ai |= ((uword) (new_value != 0)) << i1;
177 /** Sets the ith bit of a bitmap to new_value
178 Removes trailing zeros from the bitmap
179 @param ai - pointer to the bitmap
180 @param i - the bit position to interrogate
181 @param value - new value for the bit
182 @returns the old value of the bit
184 always_inline uword *
185 clib_bitmap_set (uword * ai, uword i, uword value)
187 uword i0 = i / BITS (ai[0]);
188 uword i1 = i % BITS (ai[0]);
191 /* Check for writing a zero to beyond end of bitmap. */
192 if (value == 0 && i0 >= vec_len (ai))
193 return ai; /* Implied trailing zeros. */
195 clib_bitmap_vec_validate (ai, i0);
198 a &= ~((uword) 1 << i1);
199 a |= ((uword) (value != 0)) << i1;
202 /* If bits have been cleared, test for zero. */
204 ai = _clib_bitmap_remove_trailing_zeros (ai);
210 clib_bitmap_will_expand (uword *ai, uword i)
212 uword i0 = i / BITS (ai[0]);
213 return _vec_resize_will_expand (ai, 1, i0 * sizeof (ai[0]), 0,
217 /** Gets the ith bit value from a bitmap
218 @param ai - pointer to the bitmap
219 @param i - the bit position to interrogate
220 @returns the indicated bit value
223 clib_bitmap_get (uword * ai, uword i)
225 uword i0 = i / BITS (ai[0]);
226 uword i1 = i % BITS (ai[0]);
227 return i0 < vec_len (ai) && 0 != ((ai[i0] >> i1) & 1);
230 /** Gets the ith bit value from a bitmap
231 Does not sanity-check the bit position. Be careful.
232 @param ai - pointer to the bitmap
233 @param i - the bit position to interrogate
234 @returns the indicated bit value, or garbage if the bit position is
238 clib_bitmap_get_no_check (uword * ai, uword i)
240 uword i0 = i / BITS (ai[0]);
241 uword i1 = i % BITS (ai[0]);
242 return 0 != ((ai[i0] >> i1) & 1);
246 clib_bitmap_get_multiple_no_check (uword * ai, uword i, uword n_bits)
248 uword i0 = i / BITS (ai[0]);
249 uword i1 = i % BITS (ai[0]);
250 ASSERT (i1 + n_bits <= BITS (uword));
251 return 0 != ((ai[i0] >> i1) & pow2_mask (n_bits));
254 /** Gets the ith through ith + n_bits bit values from a bitmap
255 @param bitmap - pointer to the bitmap
256 @param i - the first bit position to retrieve
257 @param n_bits - the number of bit positions to retrieve
258 @returns the indicated range of bits
261 clib_bitmap_get_multiple (uword * bitmap, uword i, uword n_bits)
263 uword i0, i1, result;
264 uword l = vec_len (bitmap);
266 ASSERT (n_bits <= BITS (result));
268 i0 = i / BITS (bitmap[0]);
269 i1 = i % BITS (bitmap[0]);
271 /* Check first word. */
275 result |= (bitmap[i0] >> i1);
276 if (n_bits < BITS (bitmap[0]))
277 result &= (((uword) 1 << n_bits) - 1);
280 /* Check for overlap into next word. */
282 if (i1 + n_bits > BITS (bitmap[0]) && i0 < l)
284 n_bits -= BITS (bitmap[0]) - i1;
286 (bitmap[i0] & (((uword) 1 << n_bits) - 1)) << (BITS (bitmap[0]) - i1);
292 /** sets the ith through ith + n_bits bits in a bitmap
293 @param bitmap - pointer to the bitmap
294 @param i - the first bit position to retrieve
295 @param value - the values to set
296 @param n_bits - the number of bit positions to set
297 @returns a pointer to the updated bitmap, which may expand and move
300 always_inline uword *
301 clib_bitmap_set_multiple (uword * bitmap, uword i, uword value, uword n_bits)
303 uword i0, i1, l, t, m;
305 ASSERT (n_bits <= BITS (value));
307 i0 = i / BITS (bitmap[0]);
308 i1 = i % BITS (bitmap[0]);
310 /* Allocate bitmap. */
311 clib_bitmap_vec_validate (bitmap, (i + n_bits) / BITS (bitmap[0]));
312 l = vec_len (bitmap);
315 if (n_bits < BITS (value))
316 m = (((uword) 1 << n_bits) - 1);
319 /* Insert into first word. */
325 /* Insert into second word. */
327 if (i1 + n_bits > BITS (bitmap[0]) && i0 < l)
329 t = BITS (bitmap[0]) - i1;
333 m = ((uword) 1 << n_bits) - 1;
342 always_inline uword *
343 clib_bitmap_set_region (uword * bitmap, uword i, uword value, uword n_bits)
348 a0 = i / BITS (bitmap[0]);
349 a1 = i % BITS (bitmap[0]);
352 b0 = i_end / BITS (bitmap[0]);
354 clib_bitmap_vec_validate (bitmap, b0);
357 mask = n_bits < BITS (bitmap[0]) ? pow2_mask (n_bits) : ~0;
365 for (a0++; a0 < b0; a0++)
366 bitmap[a0] = value ? ~0 : 0;
370 word n_bits_left = n_bits - (BITS (bitmap[0]) - a1);
371 mask = pow2_mask (n_bits_left);
381 /** Macro to iterate across set bits in a bitmap
383 @param i - the current set bit
384 @param ai - the bitmap
385 @param body - the expression to evaluate for each set bit
387 #define clib_bitmap_foreach(i,ai) \
389 for (i = clib_bitmap_first_set (ai); \
391 i = clib_bitmap_next_set (ai, i + 1))
393 /** Return the lowest numbered set bit in a bitmap
394 @param ai - pointer to the bitmap
395 @returns lowest numbered set bit, or ~0 if the entire bitmap is zero
398 clib_bitmap_first_set (uword * ai)
402 #if defined(CLIB_HAVE_VEC256)
403 while (i + 7 < vec_len (ai))
406 v = u64x4_load_unaligned (ai + i) | u64x4_load_unaligned (ai + i + 4);
407 if (!u64x4_is_all_zero (v))
411 #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE)
412 while (i + 3 < vec_len (ai))
415 v = u64x2_load_unaligned (ai + i) | u64x2_load_unaligned (ai + i + 2);
416 if (!u64x2_is_all_zero (v))
422 for (; i < vec_len (ai); i++)
426 return i * BITS (ai[0]) + log2_first_set (x);
431 /** Return the higest numbered set bit in a bitmap
432 @param ai - pointer to the bitmap
433 @returns lowest numbered set bit, or ~0 if the entire bitmap is zero
436 clib_bitmap_last_set (uword * ai)
440 for (i = vec_len (ai); i > 0; i--)
446 first_bit = count_leading_zeros (x);
447 return (i) * BITS (ai[0]) - first_bit - 1;
453 /** Return the lowest numbered clear bit in a bitmap
454 @param ai - pointer to the bitmap
455 @returns lowest numbered clear bit
458 clib_bitmap_first_clear (uword * ai)
461 for (i = 0; i < vec_len (ai); i++)
465 return i * BITS (ai[0]) + log2_first_set (x);
467 return i * BITS (ai[0]);
470 /** Return the number of set bits in a bitmap
471 @param ai - pointer to the bitmap
472 @returns the number of set bits in the bitmap
475 clib_bitmap_count_set_bits (uword * ai)
479 for (i = 0; i < vec_len (ai); i++)
480 n_set += count_set_bits (ai[i]);
484 /** Logical operator across two bitmaps
486 @param ai - pointer to the destination bitmap
487 @param bi - pointer to the source bitmap
488 @returns ai = ai and bi. ai is modified, bi is not modified
490 always_inline uword *clib_bitmap_and (uword * ai, uword * bi);
492 /** Logical operator across two bitmaps
494 @param ai - pointer to the destination bitmap
495 @param bi - pointer to the source bitmap
496 @returns ai = ai & ~bi. ai is modified, bi is not modified
498 always_inline uword *clib_bitmap_andnot (uword * ai, uword * bi);
500 /** Logical operator across two bitmaps
502 @param ai - pointer to the destination bitmap
503 @param bi - pointer to the source bitmap
504 @returns ai = ai & ~bi. ai is modified, bi is not modified
506 always_inline uword *clib_bitmap_or (uword * ai, uword * bi);
507 /** Logical operator across two bitmaps
509 @param ai - pointer to the destination bitmap
510 @param bi - pointer to the source bitmap
511 @returns ai = ai or bi. ai is modified, bi is not modified
513 always_inline uword *clib_bitmap_or (uword * ai, uword * bi);
515 /** Logical operator across two bitmaps
517 @param ai - pointer to the destination bitmap
518 @param bi - pointer to the source bitmap
519 @returns ai = ai xor bi. ai is modified, bi is not modified
521 always_inline uword *clib_bitmap_xor (uword * ai, uword * bi);
523 /* ALU function definition macro for functions taking two bitmaps. */
524 #define _(name, body, check_zero) \
525 always_inline uword * \
526 clib_bitmap_##name (uword * ai, uword * bi) \
528 uword i, a, b, bi_len, n_trailing_zeros; \
530 n_trailing_zeros = 0; \
531 bi_len = vec_len (bi); \
533 clib_bitmap_vec_validate (ai, bi_len - 1); \
534 for (i = 0; i < vec_len (ai); i++) \
537 b = i < bi_len ? bi[i] : 0; \
538 do { body; } while (0); \
541 n_trailing_zeros = a ? 0 : (n_trailing_zeros + 1); \
544 _vec_len (ai) -= n_trailing_zeros; \
551 _(andnot, a = a & ~b, 1)
556 /** Logical operator across two bitmaps which duplicates the first bitmap
558 @param ai - pointer to the destination bitmap
559 @param bi - pointer to the source bitmap
560 @returns aiDup = ai and bi. Neither ai nor bi are modified
562 always_inline uword *clib_bitmap_dup_and (uword * ai, uword * bi);
564 /** Logical operator across two bitmaps which duplicates the first bitmap
566 @param ai - pointer to the destination bitmap
567 @param bi - pointer to the source bitmap
568 @returns aiDup = ai & ~bi. Neither ai nor bi are modified
570 always_inline uword *clib_bitmap_dup_andnot (uword * ai, uword * bi);
572 /** Logical operator across two bitmaps which duplicates the first bitmap
574 @param ai - pointer to the destination bitmap
575 @param bi - pointer to the source bitmap
576 @returns aiDup = ai or bi. Neither ai nor bi are modified
578 always_inline uword *clib_bitmap_dup_or (uword * ai, uword * bi);
580 /** Logical operator across two bitmaps which duplicates the first bitmap
582 @param ai - pointer to the destination bitmap
583 @param bi - pointer to the source bitmap
584 @returns aiDup = ai xor bi. Neither ai nor bi are modified
586 always_inline uword *clib_bitmap_dup_xor (uword * ai, uword * bi);
589 always_inline uword * \
590 clib_bitmap_dup_##name (uword * ai, uword * bi) \
591 { return clib_bitmap_##name (clib_bitmap_dup (ai), bi); }
601 /* ALU function definition macro for functions taking one bitmap and an
603 #define _(name, body, check_zero) \
604 always_inline uword * \
605 clib_bitmap_##name (uword * ai, uword i) \
607 uword i0 = i / BITS (ai[0]); \
608 uword i1 = i % BITS (ai[0]); \
610 clib_bitmap_vec_validate (ai, i0); \
612 b = (uword) 1 << i1; \
613 do { body; } while (0); \
615 if (check_zero && a == 0) \
616 ai = _clib_bitmap_remove_trailing_zeros (ai); \
620 /* ALU functions immediate: */
622 _(andi, a = a & b, 1)
623 _(andnoti, a = a & ~b, 1)
625 _(xori, a = a ^ b, 1)
629 /* ALU function definition macro for functions taking one bitmap and an
630 * immediate. No tail trimming */
631 #define _(name, body) \
632 always_inline uword * \
633 clib_bitmap_##name##_notrim (uword * ai, uword i) \
635 uword i0 = i / BITS (ai[0]); \
636 uword i1 = i % BITS (ai[0]); \
638 clib_bitmap_vec_validate (ai, i0); \
640 b = (uword) 1 << i1; \
641 do { body; } while (0); \
646 /* ALU functions immediate: */
649 _(andnoti, a = a & ~b)
655 /** Return a random bitmap of the requested length
656 @param ai - pointer to the destination bitmap
657 @param n_bits - number of bits to allocate
658 @param [in,out] seed - pointer to the random number seed
659 @returns a reasonably random bitmap based. See random.h.
661 always_inline uword *
662 clib_bitmap_random (uword * ai, uword n_bits, u32 * seed)
664 vec_reset_length (ai);
668 uword i = n_bits - 1;
672 log2_rand_max = min_log2 (random_u32_max ());
674 i0 = i / BITS (ai[0]);
675 i1 = i % BITS (ai[0]);
677 clib_bitmap_vec_validate (ai, i0);
678 for (i = 0; i <= i0; i++)
681 for (n = 0; n < BITS (ai[i]); n += log2_rand_max)
682 ai[i] |= random_u32 (seed) << n;
684 if (i1 + 1 < BITS (ai[0]))
685 ai[i0] &= (((uword) 1 << (i1 + 1)) - 1);
690 /** Return the next set bit in a bitmap starting at bit i
691 @param ai - pointer to the bitmap
692 @param i - first bit position to test
693 @returns first set bit position at or after i,
694 ~0 if no further set bits are found
697 clib_bitmap_next_set (uword * ai, uword i)
699 uword i0 = i / BITS (ai[0]);
700 uword i1 = i % BITS (ai[0]);
703 if (i0 < vec_len (ai))
705 t = (ai[i0] >> i1) << i1;
707 return log2_first_set (t) + i0 * BITS (ai[0]);
709 for (i0++; i0 < vec_len (ai); i0++)
713 return log2_first_set (t) + i0 * BITS (ai[0]);
720 /** Return the next clear bit in a bitmap starting at bit i
721 @param ai - pointer to the bitmap
722 @param i - first bit position to test
723 @returns first clear bit position at or after i
726 clib_bitmap_next_clear (uword * ai, uword i)
728 uword i0 = i / BITS (ai[0]);
729 uword i1 = i % BITS (ai[0]);
732 if (i0 < vec_len (ai))
734 t = (~ai[i0] >> i1) << i1;
736 return log2_first_set (t) + i0 * BITS (ai[0]);
738 for (i0++; i0 < vec_len (ai); i0++)
742 return log2_first_set (t) + i0 * BITS (ai[0]);
745 return i0 * BITS (ai[0]);
750 uword unformat_bitmap_mask (unformat_input_t *input, va_list *va);
751 uword unformat_bitmap_list (unformat_input_t *input, va_list *va);
752 u8 *format_bitmap_hex (u8 *s, va_list *args);
753 u8 *format_bitmap_list (u8 *s, va_list *args);
755 #endif /* included_clib_bitmap_h */
758 * fd.io coding-style-patch-verification: ON
761 * eval: (c-set-style "gnu")