9418b1fcced0d2d452f131a2ff7a26fe878a39f4
[vpp.git] / src / vppinfra / bitmap.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16   Copyright (c) 2001, 2002, 2003, 2005 Eliot Dresselhaus
17
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:
25
26   The above copyright notice and this permission notice shall be
27   included in all copies or substantial portions of the Software.
28
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.
36 */
37
38 #ifndef included_clib_bitmap_h
39 #define included_clib_bitmap_h
40
41 /** \file
42     Bitmaps built as vectors of machine words
43 */
44
45 #include <vppinfra/vec.h>
46 #include <vppinfra/random.h>
47 #include <vppinfra/error.h>
48 #include <vppinfra/bitops.h>    /* for count_set_bits */
49
50 typedef uword clib_bitmap_t;
51
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
55 */
56 always_inline uword
57 clib_bitmap_is_zero (uword * ai)
58 {
59   uword i;
60   for (i = 0; i < vec_len (ai); i++)
61     if (ai[i] != 0)
62       return 0;
63   return 1;
64 }
65
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
70 */
71 always_inline uword
72 clib_bitmap_is_equal (uword * a, uword * b)
73 {
74   uword i;
75   if (vec_len (a) != vec_len (b))
76     return 0;
77   for (i = 0; i < vec_len (a); i++)
78     if (a[i] != b[i])
79       return 0;
80   return 1;
81 }
82
83 /** Duplicate a bitmap
84     @param v - pointer to a bitmap
85     @returns a duplicate of the bitmap
86 */
87 #define clib_bitmap_dup(v) vec_dup(v)
88
89 /** Free a bitmap
90     @param v - pointer to the bitmap to free
91 */
92 #define clib_bitmap_free(v) vec_free(v)
93
94 /** Number of bytes in a bitmap
95     @param v - pointer to the bitmap
96 */
97 #define clib_bitmap_bytes(v) vec_bytes(v)
98
99 /** Clear a bitmap
100     @param v - pointer to the bitmap to clear
101 */
102 #define clib_bitmap_zero(v) vec_zero(v)
103
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
107 */
108
109 #define clib_bitmap_alloc(v,n_bits) \
110   v = vec_new (uword, ((n_bits) + BITS (uword) - 1) / BITS (uword))
111
112 #define clib_bitmap_vec_validate(v,i) vec_validate_aligned((v),(i),sizeof(uword))
113
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))
117
118 /* low-level routine to remove trailing zeros from a bitmap */
119 always_inline uword *
120 _clib_bitmap_remove_trailing_zeros (uword * a)
121 {
122   word i;
123   if (a)
124     {
125       for (i = _vec_len (a) - 1; i >= 0; i--)
126         if (a[i] != 0)
127           break;
128       _vec_len (a) = i + 1;
129     }
130   return a;
131 }
132
133 /** Sets the ith bit of a bitmap to new_value.
134     No sanity checking. Be careful.
135     @param a - pointer to the bitmap
136     @param i - the bit position to interrogate
137     @param new_value - new value for the bit
138     @returns the old value of the bit
139 */
140 always_inline uword
141 clib_bitmap_set_no_check (uword * a, uword i, uword new_value)
142 {
143   uword i0 = i / BITS (a[0]);
144   uword i1 = i % BITS (a[0]);
145   uword bit = (uword) 1 << i1;
146   uword ai, old_value;
147
148   /* Removed ASSERT since uword * a may not be a vector. */
149   /* ASSERT (i0 < vec_len (a)); */
150
151   ai = a[i0];
152   old_value = (ai & bit) != 0;
153   ai &= ~bit;
154   ai |= ((uword) (new_value != 0)) << i1;
155   a[i0] = ai;
156   return old_value;
157 }
158
159 /** Sets the ith bit of a bitmap to new_value
160     Removes trailing zeros from the bitmap
161     @param ai - pointer to the bitmap
162     @param i - the bit position to interrogate
163     @param value - new value for the bit
164     @returns the old value of the bit
165 */
166 always_inline uword *
167 clib_bitmap_set (uword * ai, uword i, uword value)
168 {
169   uword i0 = i / BITS (ai[0]);
170   uword i1 = i % BITS (ai[0]);
171   uword a;
172
173   /* Check for writing a zero to beyond end of bitmap. */
174   if (value == 0 && i0 >= vec_len (ai))
175     return ai;                  /* Implied trailing zeros. */
176
177   clib_bitmap_vec_validate (ai, i0);
178
179   a = ai[i0];
180   a &= ~((uword) 1 << i1);
181   a |= ((uword) (value != 0)) << i1;
182   ai[i0] = a;
183
184   /* If bits have been cleared, test for zero. */
185   if (a == 0)
186     ai = _clib_bitmap_remove_trailing_zeros (ai);
187
188   return ai;
189 }
190
191 /** Gets the ith bit value from a bitmap
192     @param ai - pointer to the bitmap
193     @param i - the bit position to interrogate
194     @returns the indicated bit value
195 */
196 always_inline uword
197 clib_bitmap_get (uword * ai, uword i)
198 {
199   uword i0 = i / BITS (ai[0]);
200   uword i1 = i % BITS (ai[0]);
201   return i0 < vec_len (ai) && 0 != ((ai[i0] >> i1) & 1);
202 }
203
204 /** Gets the ith bit value from a bitmap
205     Does not sanity-check the bit position. Be careful.
206     @param ai - pointer to the bitmap
207     @param i - the bit position to interrogate
208     @returns the indicated bit value, or garbage if the bit position is
209     out of range.
210 */
211 always_inline uword
212 clib_bitmap_get_no_check (uword * ai, uword i)
213 {
214   uword i0 = i / BITS (ai[0]);
215   uword i1 = i % BITS (ai[0]);
216   return 0 != ((ai[i0] >> i1) & 1);
217 }
218
219 always_inline uword
220 clib_bitmap_get_multiple_no_check (uword * ai, uword i, uword n_bits)
221 {
222   uword i0 = i / BITS (ai[0]);
223   uword i1 = i % BITS (ai[0]);
224   ASSERT (i1 + n_bits <= BITS (uword));
225   return 0 != ((ai[i0] >> i1) & pow2_mask (n_bits));
226 }
227
228 /** Gets the ith through ith + n_bits bit values from a bitmap
229     @param bitmap - pointer to the bitmap
230     @param i - the first bit position to retrieve
231     @param n_bits - the number of bit positions to retrieve
232     @returns the indicated range of bits
233 */
234 always_inline uword
235 clib_bitmap_get_multiple (uword * bitmap, uword i, uword n_bits)
236 {
237   uword i0, i1, result;
238   uword l = vec_len (bitmap);
239
240   ASSERT (n_bits <= BITS (result));
241
242   i0 = i / BITS (bitmap[0]);
243   i1 = i % BITS (bitmap[0]);
244
245   /* Check first word. */
246   result = 0;
247   if (i0 < l)
248     {
249       result |= (bitmap[i0] >> i1);
250       if (n_bits < BITS (bitmap[0]))
251         result &= (((uword) 1 << n_bits) - 1);
252     }
253
254   /* Check for overlap into next word. */
255   i0++;
256   if (i1 + n_bits > BITS (bitmap[0]) && i0 < l)
257     {
258       n_bits -= BITS (bitmap[0]) - i1;
259       result |=
260         (bitmap[i0] & (((uword) 1 << n_bits) - 1)) << (BITS (bitmap[0]) - i1);
261     }
262
263   return result;
264 }
265
266 /** sets the ith through ith + n_bits bits in a bitmap
267     @param bitmap - pointer to the bitmap
268     @param i - the first bit position to retrieve
269     @param value - the values to set
270     @param n_bits - the number of bit positions to set
271     @returns a pointer to the updated bitmap, which may expand and move
272 */
273
274 always_inline uword *
275 clib_bitmap_set_multiple (uword * bitmap, uword i, uword value, uword n_bits)
276 {
277   uword i0, i1, l, t, m;
278
279   ASSERT (n_bits <= BITS (value));
280
281   i0 = i / BITS (bitmap[0]);
282   i1 = i % BITS (bitmap[0]);
283
284   /* Allocate bitmap. */
285   clib_bitmap_vec_validate (bitmap, (i + n_bits) / BITS (bitmap[0]));
286   l = vec_len (bitmap);
287
288   m = ~0;
289   if (n_bits < BITS (value))
290     m = (((uword) 1 << n_bits) - 1);
291   value &= m;
292
293   /* Insert into first word. */
294   t = bitmap[i0];
295   t &= ~(m << i1);
296   t |= value << i1;
297   bitmap[i0] = t;
298
299   /* Insert into second word. */
300   i0++;
301   if (i1 + n_bits > BITS (bitmap[0]) && i0 < l)
302     {
303       t = BITS (bitmap[0]) - i1;
304       value >>= t;
305       n_bits -= t;
306       t = bitmap[i0];
307       m = ((uword) 1 << n_bits) - 1;
308       t &= ~m;
309       t |= value;
310       bitmap[i0] = t;
311     }
312
313   return bitmap;
314 }
315
316 always_inline uword *
317 clfib_bitmap_set_region (uword * bitmap, uword i, uword value, uword n_bits)
318 {
319   uword a0, a1, b0;
320   uword i_end, mask;
321
322   a0 = i / BITS (bitmap[0]);
323   a1 = i % BITS (bitmap[0]);
324
325   i_end = i + n_bits;
326   b0 = i_end / BITS (bitmap[0]);
327
328   clib_bitmap_vec_validate (bitmap, b0);
329
330   /* First word. */
331   mask = n_bits < BITS (bitmap[0]) ? pow2_mask (n_bits) : ~0;
332   mask <<= a1;
333
334   if (value)
335     bitmap[a0] |= mask;
336   else
337     bitmap[a0] &= ~mask;
338
339   for (a0++; a0 < b0; a0++)
340     bitmap[a0] = value ? ~0 : 0;
341
342   if (a0 == b0)
343     {
344       word n_bits_left = n_bits - (BITS (bitmap[0]) - a1);
345       mask = pow2_mask (n_bits_left);
346       if (value)
347         bitmap[a0] |= mask;
348       else
349         bitmap[a0] &= ~mask;
350     }
351
352   return bitmap;
353 }
354
355 /** Macro to iterate across set bits in a bitmap
356
357     @param i - the current set bit
358     @param ai - the bitmap
359     @param body - the expression to evaluate for each set bit
360 */
361 #define clib_bitmap_foreach(i,ai,body)                                  \
362 do {                                                                    \
363   uword __bitmap_i, __bitmap_ai, __bitmap_len, __bitmap_first_set;      \
364   __bitmap_len = vec_len ((ai));                                        \
365   for (__bitmap_i = 0; __bitmap_i < __bitmap_len; __bitmap_i++)         \
366     {                                                                   \
367       __bitmap_ai = (ai)[__bitmap_i];                                   \
368       while (__bitmap_ai != 0)                                          \
369         {                                                               \
370           __bitmap_first_set = first_set (__bitmap_ai);                 \
371           (i) = (__bitmap_i * BITS ((ai)[0])                            \
372                  + min_log2 (__bitmap_first_set));                      \
373           do { body; } while (0);                                       \
374           __bitmap_ai ^= __bitmap_first_set;                            \
375         }                                                               \
376     }                                                                   \
377 } while (0)
378
379
380 /** Return the lowest numbered set bit in a bitmap
381     @param ai - pointer to the bitmap
382     @returns lowest numbered set bit, or ~0 if the entire bitmap is zero
383 */
384 always_inline uword
385 clib_bitmap_first_set (uword * ai)
386 {
387   uword i = 0;
388 #if uword_bits == 64
389 #if defined (CLIB_HAVE_VEC256)
390   while (i + 7 < vec_len (ai))
391     {
392       u64x4 v;
393       v = u64x4_load_unaligned (ai + i) | u64x4_load_unaligned (ai + i + 4);
394       if (!u64x4_is_all_zero (v))
395         break;
396       i += 8;
397     }
398 #elif defined (CLIB_HAVE_VEC128)
399   while (i + 3 < vec_len (ai))
400     {
401       u64x2 v;
402       v = u64x2_load_unaligned (ai + i) | u64x2_load_unaligned (ai + i + 2);
403       if (!u64x2_is_all_zero (v))
404         break;
405       i += 4;
406     }
407 #endif
408 #endif
409   for (; i < vec_len (ai); i++)
410     {
411       uword x = ai[i];
412       if (x != 0)
413         return i * BITS (ai[0]) + log2_first_set (x);
414     }
415   return ~0;
416 }
417
418 /** Return the higest numbered set bit in a bitmap
419     @param ai - pointer to the bitmap
420     @returns lowest numbered set bit, or ~0 if the entire bitmap is zero
421 */
422 always_inline uword
423 clib_bitmap_last_set (uword * ai)
424 {
425   uword i;
426
427   for (i = vec_len (ai); i > 0; i--)
428     {
429       uword x = ai[i - 1];
430       if (x != 0)
431         {
432           uword first_bit;
433           first_bit = count_leading_zeros (x);
434           return (i) * BITS (ai[0]) - first_bit - 1;
435         }
436     }
437   return ~0;
438 }
439
440 /** Return the lowest numbered clear bit in a bitmap
441     @param ai - pointer to the bitmap
442     @returns lowest numbered clear bit
443 */
444 always_inline uword
445 clib_bitmap_first_clear (uword * ai)
446 {
447   uword i;
448   for (i = 0; i < vec_len (ai); i++)
449     {
450       uword x = ~ai[i];
451       if (x != 0)
452         return i * BITS (ai[0]) + log2_first_set (x);
453     }
454   return i * BITS (ai[0]);
455 }
456
457 /** Return the number of set bits in a bitmap
458     @param ai - pointer to the bitmap
459     @returns the number of set bits in the bitmap
460 */
461 always_inline uword
462 clib_bitmap_count_set_bits (uword * ai)
463 {
464   uword i;
465   uword n_set = 0;
466   for (i = 0; i < vec_len (ai); i++)
467     n_set += count_set_bits (ai[i]);
468   return n_set;
469 }
470
471 /** Logical operator across two bitmaps
472
473     @param ai - pointer to the destination bitmap
474     @param bi - pointer to the source bitmap
475     @returns ai = ai and bi. ai is modified, bi is not modified
476 */
477 always_inline uword *clib_bitmap_and (uword * ai, uword * bi);
478
479 /** Logical operator across two bitmaps
480
481     @param ai - pointer to the destination bitmap
482     @param bi - pointer to the source bitmap
483     @returns ai = ai & ~bi. ai is modified, bi is not modified
484 */
485 always_inline uword *clib_bitmap_andnot (uword * ai, uword * bi);
486
487 /** Logical operator across two bitmaps
488
489     @param ai - pointer to the destination bitmap
490     @param bi - pointer to the source bitmap
491     @returns ai = ai & ~bi. ai is modified, bi is not modified
492 */
493 always_inline uword *clib_bitmap_or (uword * ai, uword * bi);
494 /** Logical operator across two bitmaps
495
496     @param ai - pointer to the destination bitmap
497     @param bi - pointer to the source bitmap
498     @returns ai = ai or bi. ai is modified, bi is not modified
499 */
500 always_inline uword *clib_bitmap_or (uword * ai, uword * bi);
501
502 /** Logical operator across two bitmaps
503
504     @param ai - pointer to the destination bitmap
505     @param bi - pointer to the source bitmap
506     @returns ai = ai xor bi. ai is modified, bi is not modified
507 */
508 always_inline uword *clib_bitmap_xor (uword * ai, uword * bi);
509
510 /* ALU function definition macro for functions taking two bitmaps. */
511 #define _(name, body, check_zero)                               \
512 always_inline uword *                                           \
513 clib_bitmap_##name (uword * ai, uword * bi)                     \
514 {                                                               \
515   uword i, a, b, bi_len, n_trailing_zeros;                      \
516                                                                 \
517   n_trailing_zeros = 0;                                         \
518   bi_len = vec_len (bi);                                        \
519   if (bi_len > 0)                                               \
520     clib_bitmap_vec_validate (ai, bi_len - 1);                  \
521   for (i = 0; i < vec_len (ai); i++)                            \
522     {                                                           \
523       a = ai[i];                                                \
524       b = i < bi_len ? bi[i] : 0;                               \
525       do { body; } while (0);                                   \
526       ai[i] = a;                                                \
527       if (check_zero)                                           \
528         n_trailing_zeros = a ? 0 : (n_trailing_zeros + 1);      \
529     }                                                           \
530   if (check_zero)                                               \
531     _vec_len (ai) -= n_trailing_zeros;                          \
532   return ai;                                                    \
533 }
534
535 /* ALU functions: */
536 _(and, a = a & b, 1)
537 _(andnot, a = a & ~b, 1) _(or, a = a | b, 0) _(xor, a = a ^ b, 1)
538 #undef _
539 /** Logical operator across two bitmaps which duplicates the first bitmap
540
541     @param ai - pointer to the destination bitmap
542     @param bi - pointer to the source bitmap
543     @returns aiDup = ai and bi. Neither ai nor bi are modified
544 */
545      always_inline uword *
546      clib_bitmap_dup_and (uword * ai, uword * bi);
547
548 /** Logical operator across two bitmaps which duplicates the first bitmap
549
550     @param ai - pointer to the destination bitmap
551     @param bi - pointer to the source bitmap
552     @returns aiDup = ai & ~bi. Neither ai nor bi are modified
553 */
554      always_inline uword *
555      clib_bitmap_dup_andnot (uword * ai, uword * bi);
556
557 /** Logical operator across two bitmaps which duplicates the first bitmap
558
559     @param ai - pointer to the destination bitmap
560     @param bi - pointer to the source bitmap
561     @returns aiDup = ai or bi. Neither ai nor bi are modified
562 */
563      always_inline uword *
564      clib_bitmap_dup_or (uword * ai, uword * bi);
565
566 /** Logical operator across two bitmaps which duplicates the first bitmap
567
568     @param ai - pointer to the destination bitmap
569     @param bi - pointer to the source bitmap
570     @returns aiDup = ai xor bi. Neither ai nor bi are modified
571 */
572      always_inline uword *
573      clib_bitmap_dup_xor (uword * ai, uword * bi);
574
575 #define _(name)                                         \
576   always_inline uword *                                 \
577   clib_bitmap_dup_##name (uword * ai, uword * bi)       \
578 { return clib_bitmap_##name (clib_bitmap_dup (ai), bi); }
579
580 _(and);
581 _(andnot);
582 _(or);
583 _(xor);
584
585 #undef _
586
587 /* ALU function definition macro for functions taking one bitmap and an immediate. */
588 #define _(name, body, check_zero)                       \
589 always_inline uword *                                   \
590 clib_bitmap_##name (uword * ai, uword i)                \
591 {                                                       \
592   uword i0 = i / BITS (ai[0]);                          \
593   uword i1 = i % BITS (ai[0]);                          \
594   uword a, b;                                           \
595   clib_bitmap_vec_validate (ai, i0);                    \
596   a = ai[i0];                                           \
597   b = (uword) 1 << i1;                                  \
598   do { body; } while (0);                               \
599   ai[i0] = a;                                           \
600   if (check_zero && a == 0)                             \
601     ai = _clib_bitmap_remove_trailing_zeros (ai);       \
602   return ai;                                            \
603 }
604
605 /* ALU functions immediate: */
606 _(andi, a = a & b, 1)
607 _(andnoti, a = a & ~b, 1) _(ori, a = a | b, 0) _(xori, a = a ^ b, 1)
608 #undef _
609 /** Return a random bitmap of the requested length
610     @param ai - pointer to the destination bitmap
611     @param n_bits - number of bits to allocate
612     @param [in,out] seed - pointer to the random number seed
613     @returns a reasonably random bitmap based. See random.h.
614 */
615      always_inline uword *
616      clib_bitmap_random (uword * ai, uword n_bits, u32 * seed)
617 {
618   vec_reset_length (ai);
619
620   if (n_bits > 0)
621     {
622       uword i = n_bits - 1;
623       uword i0, i1;
624       uword log2_rand_max;
625
626       log2_rand_max = min_log2 (random_u32_max ());
627
628       i0 = i / BITS (ai[0]);
629       i1 = i % BITS (ai[0]);
630
631       clib_bitmap_vec_validate (ai, i0);
632       for (i = 0; i <= i0; i++)
633         {
634           uword n;
635           for (n = 0; n < BITS (ai[i]); n += log2_rand_max)
636             ai[i] |= random_u32 (seed) << n;
637         }
638       if (i1 + 1 < BITS (ai[0]))
639         ai[i0] &= (((uword) 1 << (i1 + 1)) - 1);
640     }
641   return ai;
642 }
643
644 /** Return the next set bit in a bitmap starting at bit i
645     @param ai - pointer to the bitmap
646     @param i - first bit position to test
647     @returns first set bit position at or after i,
648     ~0 if no further set bits are found
649 */
650 always_inline uword
651 clib_bitmap_next_set (uword * ai, uword i)
652 {
653   uword i0 = i / BITS (ai[0]);
654   uword i1 = i % BITS (ai[0]);
655   uword t;
656
657   if (i0 < vec_len (ai))
658     {
659       t = (ai[i0] >> i1) << i1;
660       if (t)
661         return log2_first_set (t) + i0 * BITS (ai[0]);
662
663       for (i0++; i0 < vec_len (ai); i0++)
664         {
665           t = ai[i0];
666           if (t)
667             return log2_first_set (t) + i0 * BITS (ai[0]);
668         }
669     }
670
671   return ~0;
672 }
673
674 /** Return the next clear bit in a bitmap starting at bit i
675     @param ai - pointer to the bitmap
676     @param i - first bit position to test
677     @returns first clear bit position at or after i
678 */
679 always_inline uword
680 clib_bitmap_next_clear (uword * ai, uword i)
681 {
682   uword i0 = i / BITS (ai[0]);
683   uword i1 = i % BITS (ai[0]);
684   uword t;
685
686   if (i0 < vec_len (ai))
687     {
688       t = (~ai[i0] >> i1) << i1;
689       if (t)
690         return log2_first_set (t) + i0 * BITS (ai[0]);
691
692       for (i0++; i0 < vec_len (ai); i0++)
693         {
694           t = ~ai[i0];
695           if (t)
696             return log2_first_set (t) + i0 * BITS (ai[0]);
697         }
698     }
699   return i;
700 }
701
702 /** unformat a list of bit ranges into a bitmap (eg "0-3,5-7,11" )
703
704     uword * bitmap;
705     rv = unformat ("%U", unformat_bitmap_list, &bitmap);
706
707     Standard unformat_function_t arguments
708
709     @param input - pointer an unformat_input_t
710     @param va - varargs list comprising a single uword **
711     @returns 1 on success, 0 on failure
712 */
713 static inline uword
714 unformat_bitmap_list (unformat_input_t * input, va_list * va)
715 {
716   uword **bitmap_return = va_arg (*va, uword **);
717   uword *bitmap = 0;
718
719   u32 a, b;
720
721   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
722     {
723       int i;
724       if (unformat (input, "%u-%u,", &a, &b))
725         ;
726       else if (unformat (input, "%u,", &a))
727         b = a;
728       else if (unformat (input, "%u-%u", &a, &b))
729         ;
730       else if (unformat (input, "%u", &a))
731         b = a;
732       else if (bitmap)
733         {
734           unformat_put_input (input);
735           break;
736         }
737       else
738         goto error;
739
740       if (b < a)
741         goto error;
742
743       for (i = a; i <= b; i++)
744         bitmap = clib_bitmap_set (bitmap, i, 1);
745     }
746   *bitmap_return = bitmap;
747   return 1;
748 error:
749   clib_bitmap_free (bitmap);
750   return 0;
751 }
752
753 /** Format a bitmap as a string of hex bytes
754
755     uword * bitmap;
756     s = format ("%U", format_bitmap_hex, bitmap);
757
758     Standard format_function_t arguments
759
760     @param s - string under construction
761     @param args - varargs list comprising a single uword *
762     @returns string under construction
763 */
764 static inline u8 *
765 format_bitmap_hex (u8 * s, va_list * args)
766 {
767   uword *bitmap = va_arg (*args, uword *);
768   int i, is_trailing_zero = 1;
769
770   if (!bitmap)
771     return format (s, "0");
772
773   i = vec_bytes (bitmap) * 2;
774
775   while (i > 0)
776     {
777       u8 x = clib_bitmap_get_multiple (bitmap, --i * 4, 4);
778
779       if (x && is_trailing_zero)
780         is_trailing_zero = 0;
781
782       if (x || !is_trailing_zero)
783         s = format (s, "%x", x);
784     }
785   return s;
786 }
787 #endif /* included_clib_bitmap_h */
788
789 /*
790  * fd.io coding-style-patch-verification: ON
791  *
792  * Local Variables:
793  * eval: (c-set-style "gnu")
794  * End:
795  */