/src/lldpd/src/daemon/bitmap.c
Line | Count | Source |
1 | | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
2 | | /* |
3 | | * Copyright (c) 2020 Vincent Bernat <bernat@luffy.cx> |
4 | | * |
5 | | * Permission to use, copy, modify, and/or distribute this software for any |
6 | | * purpose with or without fee is hereby granted, provided that the above |
7 | | * copyright notice and this permission notice appear in all copies. |
8 | | * |
9 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
10 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
11 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
12 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
13 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
14 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
15 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
16 | | */ |
17 | | |
18 | | /* Helpers around bitmaps */ |
19 | | |
20 | | #include "lldpd.h" |
21 | | |
22 | | /* |
23 | | * Set vlan id in the bitmap |
24 | | */ |
25 | | void |
26 | | bitmap_set(uint32_t *bmap, uint16_t vlan_id) |
27 | 0 | { |
28 | 0 | if (vlan_id < MAX_VLAN) bmap[vlan_id / 32] |= (((uint32_t)1) << (vlan_id % 32)); |
29 | 0 | } |
30 | | |
31 | | /* |
32 | | * Checks if the bitmap is empty |
33 | | */ |
34 | | int |
35 | | bitmap_isempty(uint32_t *bmap) |
36 | 0 | { |
37 | 0 | int i; |
38 | |
|
39 | 0 | for (i = 0; i < VLAN_BITMAP_LEN; i++) { |
40 | 0 | if (bmap[i] != 0) return 0; |
41 | 0 | } |
42 | | |
43 | 0 | return 1; |
44 | 0 | } |
45 | | |
46 | | /* |
47 | | * Calculate the number of bits set in the bitmap to get total |
48 | | * number of VLANs |
49 | | */ |
50 | | unsigned int |
51 | | bitmap_numbits(uint32_t *bmap) |
52 | 0 | { |
53 | 0 | unsigned int num = 0; |
54 | |
|
55 | 0 | for (int i = 0; (i < VLAN_BITMAP_LEN); i++) { |
56 | 0 | uint32_t v = bmap[i]; |
57 | 0 | v = v - ((v >> 1) & 0x55555555); |
58 | 0 | v = (v & 0x33333333) + ((v >> 2) & 0x33333333); |
59 | 0 | num += (((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; |
60 | 0 | } |
61 | |
|
62 | 0 | return num; |
63 | 0 | } |