]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/daemon/bitmap.c
interfaces: move bitmaps function to a dedicated file
[thirdparty/lldpd.git] / src / daemon / bitmap.c
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 {
28 if (vlan_id < MAX_VLAN)
29 bmap[vlan_id / 32] |= (((uint32_t) 1) << (vlan_id % 32));
30 }
31
32 /*
33 * Checks if the bitmap is empty
34 */
35 int
36 bitmap_isempty(uint32_t *bmap)
37 {
38 int i;
39
40 for (i = 0; i < VLAN_BITMAP_LEN; i++) {
41 if (bmap[i] != 0)
42 return 0;
43 }
44
45 return 1;
46 }
47
48 /*
49 * Calculate the number of bits set in the bitmap to get total
50 * number of VLANs
51 */
52 unsigned int
53 bitmap_numbits(uint32_t *bmap)
54 {
55 unsigned int num = 0;
56
57 for (int i = 0; (i < VLAN_BITMAP_LEN); i++) {
58 uint32_t v = bmap[i];
59 v = v - ((v >> 1) & 0x55555555);
60 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
61 num += (((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
62 }
63
64 return num;
65 }