Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 41 | /** \file |
| 42 | Bitmaps built as vectors of machine words |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 43 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Damjan Marion | 0b14072 | 2016-06-14 00:36:09 +0200 | [diff] [blame] | 50 | typedef uword clib_bitmap_t; |
| 51 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 83 | /** Duplicate a bitmap |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 84 | @param v - pointer to a bitmap |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 85 | @returns a duplicate of the bitmap |
| 86 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | #define clib_bitmap_dup(v) vec_dup(v) |
| 88 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 89 | /** Free a bitmap |
| 90 | @param v - pointer to the bitmap to free |
| 91 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | #define clib_bitmap_free(v) vec_free(v) |
| 93 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 94 | /** Number of bytes in a bitmap |
| 95 | @param v - pointer to the bitmap |
| 96 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | #define clib_bitmap_bytes(v) vec_bytes(v) |
| 98 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 99 | /** Clear a bitmap |
| 100 | @param v - pointer to the bitmap to clear |
| 101 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | #define clib_bitmap_zero(v) vec_zero(v) |
| 103 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 133 | /** Sets the ith bit of a bitmap to new_value. |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 134 | No sanity checking. Be careful. |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 153 | ai &= ~bit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | ai |= ((uword) (new_value != 0)) << i1; |
| 155 | a[i0] = ai; |
| 156 | return old_value; |
| 157 | } |
| 158 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 159 | /** Sets the ith bit of a bitmap to new_value |
| 160 | Removes trailing zeros from the bitmap |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 161 | @param ai - pointer to the bitmap |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 162 | @param i - the bit position to interrogate |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 163 | @param value - new value for the bit |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 164 | @returns the old value of the bit |
| 165 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 240 | ASSERT (n_bits <= BITS (result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 259 | result |= |
| 260 | (bitmap[i0] & (((uword) 1 << n_bits) - 1)) << (BITS (bitmap[0]) - i1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return result; |
| 264 | } |
| 265 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 279 | ASSERT (n_bits <= BITS (value)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | always_inline uword * |
Korian Edeline | 40e6bdf | 2018-08-08 11:30:30 +0200 | [diff] [blame] | 317 | clib_bitmap_set_region (uword * bitmap, uword i, uword value, uword n_bits) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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 | */ |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 361 | #define clib_bitmap_foreach(i,ai) \ |
| 362 | if (ai) \ |
| 363 | for (i = clib_bitmap_first_set (ai); \ |
| 364 | i != ~0; \ |
| 365 | i = clib_bitmap_next_set (ai, i + 1)) |
| 366 | |
| 367 | #define clib_bitmap_foreach_old(i,ai,body) \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | do { \ |
| 369 | uword __bitmap_i, __bitmap_ai, __bitmap_len, __bitmap_first_set; \ |
| 370 | __bitmap_len = vec_len ((ai)); \ |
| 371 | for (__bitmap_i = 0; __bitmap_i < __bitmap_len; __bitmap_i++) \ |
| 372 | { \ |
| 373 | __bitmap_ai = (ai)[__bitmap_i]; \ |
| 374 | while (__bitmap_ai != 0) \ |
| 375 | { \ |
| 376 | __bitmap_first_set = first_set (__bitmap_ai); \ |
| 377 | (i) = (__bitmap_i * BITS ((ai)[0]) \ |
| 378 | + min_log2 (__bitmap_first_set)); \ |
| 379 | do { body; } while (0); \ |
| 380 | __bitmap_ai ^= __bitmap_first_set; \ |
| 381 | } \ |
| 382 | } \ |
| 383 | } while (0) |
| 384 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 386 | /** Return the lowest numbered set bit in a bitmap |
| 387 | @param ai - pointer to the bitmap |
| 388 | @returns lowest numbered set bit, or ~0 if the entire bitmap is zero |
| 389 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 390 | always_inline uword |
| 391 | clib_bitmap_first_set (uword * ai) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 392 | { |
Damjan Marion | 32ab954 | 2018-06-26 01:29:55 +0200 | [diff] [blame] | 393 | uword i = 0; |
| 394 | #if uword_bits == 64 |
Damjan Marion | 13c98a2 | 2018-06-26 16:05:43 +0200 | [diff] [blame] | 395 | #if defined(CLIB_HAVE_VEC256) |
Damjan Marion | 32ab954 | 2018-06-26 01:29:55 +0200 | [diff] [blame] | 396 | while (i + 7 < vec_len (ai)) |
| 397 | { |
| 398 | u64x4 v; |
| 399 | v = u64x4_load_unaligned (ai + i) | u64x4_load_unaligned (ai + i + 4); |
| 400 | if (!u64x4_is_all_zero (v)) |
| 401 | break; |
| 402 | i += 8; |
| 403 | } |
Damjan Marion | 13c98a2 | 2018-06-26 16:05:43 +0200 | [diff] [blame] | 404 | #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE) |
Damjan Marion | 32ab954 | 2018-06-26 01:29:55 +0200 | [diff] [blame] | 405 | while (i + 3 < vec_len (ai)) |
| 406 | { |
| 407 | u64x2 v; |
| 408 | v = u64x2_load_unaligned (ai + i) | u64x2_load_unaligned (ai + i + 2); |
| 409 | if (!u64x2_is_all_zero (v)) |
| 410 | break; |
| 411 | i += 4; |
| 412 | } |
| 413 | #endif |
| 414 | #endif |
| 415 | for (; i < vec_len (ai); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | { |
| 417 | uword x = ai[i]; |
| 418 | if (x != 0) |
| 419 | return i * BITS (ai[0]) + log2_first_set (x); |
| 420 | } |
| 421 | return ~0; |
| 422 | } |
| 423 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 424 | /** Return the higest numbered set bit in a bitmap |
| 425 | @param ai - pointer to the bitmap |
| 426 | @returns lowest numbered set bit, or ~0 if the entire bitmap is zero |
| 427 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 428 | always_inline uword |
| 429 | clib_bitmap_last_set (uword * ai) |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 430 | { |
| 431 | uword i; |
| 432 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 433 | for (i = vec_len (ai); i > 0; i--) |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 434 | { |
Damjan Marion | ec17510 | 2016-06-30 01:02:17 +0200 | [diff] [blame] | 435 | uword x = ai[i - 1]; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 436 | if (x != 0) |
| 437 | { |
| 438 | uword first_bit; |
Damjan Marion | 1105600 | 2018-05-10 13:40:44 +0200 | [diff] [blame] | 439 | first_bit = count_leading_zeros (x); |
Damjan Marion | ec17510 | 2016-06-30 01:02:17 +0200 | [diff] [blame] | 440 | return (i) * BITS (ai[0]) - first_bit - 1; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | return ~0; |
| 444 | } |
| 445 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 446 | /** Return the lowest numbered clear bit in a bitmap |
| 447 | @param ai - pointer to the bitmap |
| 448 | @returns lowest numbered clear bit |
| 449 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | always_inline uword |
| 451 | clib_bitmap_first_clear (uword * ai) |
| 452 | { |
| 453 | uword i; |
| 454 | for (i = 0; i < vec_len (ai); i++) |
| 455 | { |
| 456 | uword x = ~ai[i]; |
| 457 | if (x != 0) |
| 458 | return i * BITS (ai[0]) + log2_first_set (x); |
| 459 | } |
| 460 | return i * BITS (ai[0]); |
| 461 | } |
| 462 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 463 | /** Return the number of set bits in a bitmap |
| 464 | @param ai - pointer to the bitmap |
| 465 | @returns the number of set bits in the bitmap |
| 466 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 467 | always_inline uword |
| 468 | clib_bitmap_count_set_bits (uword * ai) |
| 469 | { |
| 470 | uword i; |
| 471 | uword n_set = 0; |
| 472 | for (i = 0; i < vec_len (ai); i++) |
| 473 | n_set += count_set_bits (ai[i]); |
| 474 | return n_set; |
| 475 | } |
| 476 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 477 | /** Logical operator across two bitmaps |
| 478 | |
| 479 | @param ai - pointer to the destination bitmap |
| 480 | @param bi - pointer to the source bitmap |
| 481 | @returns ai = ai and bi. ai is modified, bi is not modified |
| 482 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 483 | always_inline uword *clib_bitmap_and (uword * ai, uword * bi); |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 484 | |
| 485 | /** Logical operator across two bitmaps |
| 486 | |
| 487 | @param ai - pointer to the destination bitmap |
| 488 | @param bi - pointer to the source bitmap |
| 489 | @returns ai = ai & ~bi. ai is modified, bi is not modified |
| 490 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 491 | always_inline uword *clib_bitmap_andnot (uword * ai, uword * bi); |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 492 | |
| 493 | /** Logical operator across two bitmaps |
| 494 | |
| 495 | @param ai - pointer to the destination bitmap |
| 496 | @param bi - pointer to the source bitmap |
| 497 | @returns ai = ai & ~bi. ai is modified, bi is not modified |
| 498 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 499 | always_inline uword *clib_bitmap_or (uword * ai, uword * bi); |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 500 | /** Logical operator across two bitmaps |
| 501 | |
| 502 | @param ai - pointer to the destination bitmap |
| 503 | @param bi - pointer to the source bitmap |
| 504 | @returns ai = ai or bi. ai is modified, bi is not modified |
| 505 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 506 | always_inline uword *clib_bitmap_or (uword * ai, uword * bi); |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 507 | |
| 508 | /** Logical operator across two bitmaps |
| 509 | |
| 510 | @param ai - pointer to the destination bitmap |
| 511 | @param bi - pointer to the source bitmap |
| 512 | @returns ai = ai xor bi. ai is modified, bi is not modified |
| 513 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 514 | always_inline uword *clib_bitmap_xor (uword * ai, uword * bi); |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 515 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | /* ALU function definition macro for functions taking two bitmaps. */ |
| 517 | #define _(name, body, check_zero) \ |
| 518 | always_inline uword * \ |
| 519 | clib_bitmap_##name (uword * ai, uword * bi) \ |
| 520 | { \ |
| 521 | uword i, a, b, bi_len, n_trailing_zeros; \ |
| 522 | \ |
| 523 | n_trailing_zeros = 0; \ |
| 524 | bi_len = vec_len (bi); \ |
| 525 | if (bi_len > 0) \ |
| 526 | clib_bitmap_vec_validate (ai, bi_len - 1); \ |
| 527 | for (i = 0; i < vec_len (ai); i++) \ |
| 528 | { \ |
| 529 | a = ai[i]; \ |
| 530 | b = i < bi_len ? bi[i] : 0; \ |
| 531 | do { body; } while (0); \ |
| 532 | ai[i] = a; \ |
| 533 | if (check_zero) \ |
| 534 | n_trailing_zeros = a ? 0 : (n_trailing_zeros + 1); \ |
| 535 | } \ |
| 536 | if (check_zero) \ |
| 537 | _vec_len (ai) -= n_trailing_zeros; \ |
| 538 | return ai; \ |
| 539 | } |
| 540 | |
| 541 | /* ALU functions: */ |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 542 | /* *INDENT-OFF* */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 543 | _(and, a = a & b, 1) |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 544 | _(andnot, a = a & ~b, 1) |
| 545 | _(or, a = a | b, 0) |
| 546 | _(xor, a = a ^ b, 1) |
| 547 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | #undef _ |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 549 | /** Logical operator across two bitmaps which duplicates the first bitmap |
| 550 | |
| 551 | @param ai - pointer to the destination bitmap |
| 552 | @param bi - pointer to the source bitmap |
| 553 | @returns aiDup = ai and bi. Neither ai nor bi are modified |
| 554 | */ |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 555 | always_inline uword *clib_bitmap_dup_and (uword * ai, uword * bi); |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 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 & ~bi. Neither ai nor bi are modified |
| 562 | */ |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 563 | always_inline uword *clib_bitmap_dup_andnot (uword * ai, uword * bi); |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 564 | |
| 565 | /** Logical operator across two bitmaps which duplicates the first bitmap |
| 566 | |
| 567 | @param ai - pointer to the destination bitmap |
| 568 | @param bi - pointer to the source bitmap |
| 569 | @returns aiDup = ai or bi. Neither ai nor bi are modified |
| 570 | */ |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 571 | always_inline uword *clib_bitmap_dup_or (uword * ai, uword * bi); |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 572 | |
| 573 | /** Logical operator across two bitmaps which duplicates the first bitmap |
| 574 | |
| 575 | @param ai - pointer to the destination bitmap |
| 576 | @param bi - pointer to the source bitmap |
| 577 | @returns aiDup = ai xor bi. Neither ai nor bi are modified |
| 578 | */ |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 579 | always_inline uword *clib_bitmap_dup_xor (uword * ai, uword * bi); |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 580 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 581 | #define _(name) \ |
| 582 | always_inline uword * \ |
| 583 | clib_bitmap_dup_##name (uword * ai, uword * bi) \ |
| 584 | { return clib_bitmap_##name (clib_bitmap_dup (ai), bi); } |
| 585 | |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 586 | /* *INDENT-OFF* */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 587 | _(and); |
| 588 | _(andnot); |
| 589 | _(or); |
| 590 | _(xor); |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 591 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | #undef _ |
| 593 | |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 594 | /* ALU function definition macro for functions taking one bitmap and an |
| 595 | * immediate. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 596 | #define _(name, body, check_zero) \ |
| 597 | always_inline uword * \ |
| 598 | clib_bitmap_##name (uword * ai, uword i) \ |
| 599 | { \ |
| 600 | uword i0 = i / BITS (ai[0]); \ |
| 601 | uword i1 = i % BITS (ai[0]); \ |
| 602 | uword a, b; \ |
| 603 | clib_bitmap_vec_validate (ai, i0); \ |
| 604 | a = ai[i0]; \ |
| 605 | b = (uword) 1 << i1; \ |
| 606 | do { body; } while (0); \ |
| 607 | ai[i0] = a; \ |
| 608 | if (check_zero && a == 0) \ |
| 609 | ai = _clib_bitmap_remove_trailing_zeros (ai); \ |
| 610 | return ai; \ |
| 611 | } |
| 612 | |
| 613 | /* ALU functions immediate: */ |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 614 | /* *INDENT-OFF* */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 615 | _(andi, a = a & b, 1) |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 616 | _(andnoti, a = a & ~b, 1) |
| 617 | _(ori, a = a | b, 0) |
| 618 | _(xori, a = a ^ b, 1) |
| 619 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 620 | #undef _ |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 621 | |
| 622 | /* ALU function definition macro for functions taking one bitmap and an |
| 623 | * immediate. No tail trimming */ |
| 624 | #define _(name, body) \ |
| 625 | always_inline uword * \ |
| 626 | clib_bitmap_##name##_notrim (uword * ai, uword i) \ |
| 627 | { \ |
| 628 | uword i0 = i / BITS (ai[0]); \ |
| 629 | uword i1 = i % BITS (ai[0]); \ |
| 630 | uword a, b; \ |
| 631 | clib_bitmap_vec_validate (ai, i0); \ |
| 632 | a = ai[i0]; \ |
| 633 | b = (uword) 1 << i1; \ |
| 634 | do { body; } while (0); \ |
| 635 | ai[i0] = a; \ |
| 636 | return ai; \ |
| 637 | } |
| 638 | |
| 639 | /* ALU functions immediate: */ |
| 640 | /* *INDENT-OFF* */ |
| 641 | _(andi, a = a & b) |
| 642 | _(andnoti, a = a & ~b) |
| 643 | _(ori, a = a | b) |
| 644 | _(xori, a = a ^ b) |
| 645 | #undef _ |
| 646 | /* *INDENT-ON* */ |
| 647 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 648 | /** Return a random bitmap of the requested length |
| 649 | @param ai - pointer to the destination bitmap |
| 650 | @param n_bits - number of bits to allocate |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 651 | @param [in,out] seed - pointer to the random number seed |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 652 | @returns a reasonably random bitmap based. See random.h. |
| 653 | */ |
Florin Coras | fcd2368 | 2018-06-29 03:22:44 -0700 | [diff] [blame] | 654 | always_inline uword * |
| 655 | clib_bitmap_random (uword * ai, uword n_bits, u32 * seed) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | { |
| 657 | vec_reset_length (ai); |
| 658 | |
| 659 | if (n_bits > 0) |
| 660 | { |
| 661 | uword i = n_bits - 1; |
| 662 | uword i0, i1; |
| 663 | uword log2_rand_max; |
| 664 | |
| 665 | log2_rand_max = min_log2 (random_u32_max ()); |
| 666 | |
| 667 | i0 = i / BITS (ai[0]); |
| 668 | i1 = i % BITS (ai[0]); |
| 669 | |
| 670 | clib_bitmap_vec_validate (ai, i0); |
| 671 | for (i = 0; i <= i0; i++) |
| 672 | { |
| 673 | uword n; |
| 674 | for (n = 0; n < BITS (ai[i]); n += log2_rand_max) |
| 675 | ai[i] |= random_u32 (seed) << n; |
| 676 | } |
| 677 | if (i1 + 1 < BITS (ai[0])) |
| 678 | ai[i0] &= (((uword) 1 << (i1 + 1)) - 1); |
| 679 | } |
| 680 | return ai; |
| 681 | } |
| 682 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 683 | /** Return the next set bit in a bitmap starting at bit i |
| 684 | @param ai - pointer to the bitmap |
| 685 | @param i - first bit position to test |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 686 | @returns first set bit position at or after i, |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 687 | ~0 if no further set bits are found |
| 688 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 689 | always_inline uword |
| 690 | clib_bitmap_next_set (uword * ai, uword i) |
| 691 | { |
| 692 | uword i0 = i / BITS (ai[0]); |
| 693 | uword i1 = i % BITS (ai[0]); |
| 694 | uword t; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 695 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | if (i0 < vec_len (ai)) |
| 697 | { |
| 698 | t = (ai[i0] >> i1) << i1; |
| 699 | if (t) |
| 700 | return log2_first_set (t) + i0 * BITS (ai[0]); |
| 701 | |
| 702 | for (i0++; i0 < vec_len (ai); i0++) |
| 703 | { |
| 704 | t = ai[i0]; |
| 705 | if (t) |
| 706 | return log2_first_set (t) + i0 * BITS (ai[0]); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | return ~0; |
| 711 | } |
| 712 | |
Dave Barach | 310f7fe | 2016-08-05 14:36:27 -0400 | [diff] [blame] | 713 | /** Return the next clear bit in a bitmap starting at bit i |
| 714 | @param ai - pointer to the bitmap |
| 715 | @param i - first bit position to test |
| 716 | @returns first clear bit position at or after i |
| 717 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 718 | always_inline uword |
| 719 | clib_bitmap_next_clear (uword * ai, uword i) |
| 720 | { |
| 721 | uword i0 = i / BITS (ai[0]); |
| 722 | uword i1 = i % BITS (ai[0]); |
| 723 | uword t; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 724 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 725 | if (i0 < vec_len (ai)) |
| 726 | { |
| 727 | t = (~ai[i0] >> i1) << i1; |
| 728 | if (t) |
| 729 | return log2_first_set (t) + i0 * BITS (ai[0]); |
| 730 | |
| 731 | for (i0++; i0 < vec_len (ai); i0++) |
| 732 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 733 | t = ~ai[i0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 734 | if (t) |
| 735 | return log2_first_set (t) + i0 * BITS (ai[0]); |
| 736 | } |
John Lo | ef8db36 | 2018-07-04 16:27:59 -0400 | [diff] [blame] | 737 | |
| 738 | /* no clear bit left in bitmap, return bit just beyond bitmap */ |
Dave Barach | 37b4454 | 2020-04-27 18:38:36 -0400 | [diff] [blame] | 739 | return (i0 * BITS (ai[0])) + 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | } |
| 741 | return i; |
| 742 | } |
| 743 | |
Damjan Marion | 7f57f50 | 2021-04-15 16:13:20 +0200 | [diff] [blame] | 744 | uword unformat_bitmap_mask (unformat_input_t *input, va_list *va); |
| 745 | uword unformat_bitmap_list (unformat_input_t *input, va_list *va); |
| 746 | u8 *format_bitmap_hex (u8 *s, va_list *args); |
| 747 | u8 *format_bitmap_list (u8 *s, va_list *args); |
Yi He | e4a9eb7 | 2018-07-17 14:18:41 +0800 | [diff] [blame] | 748 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 749 | #endif /* included_clib_bitmap_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 750 | |
| 751 | /* |
| 752 | * fd.io coding-style-patch-verification: ON |
| 753 | * |
| 754 | * Local Variables: |
| 755 | * eval: (c-set-style "gnu") |
| 756 | * End: |
| 757 | */ |