blob: db466ef7be9d13dd27a03619c3e8bac834c89b32 [file] [log] [blame]
Thomas Gleixner40b0b3f2019-06-03 07:44:46 +02001// SPDX-License-Identifier: GPL-2.0-only
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -03002/*
3 * From lib/bitmap.c
4 * Helper functions for bitmap.h.
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -03005 */
6#include <linux/bitmap.h>
7
8int __bitmap_weight(const unsigned long *bitmap, int bits)
9{
10 int k, w = 0, lim = bits/BITS_PER_LONG;
11
12 for (k = 0; k < lim; k++)
13 w += hweight_long(bitmap[k]);
14
15 if (bits % BITS_PER_LONG)
16 w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
17
18 return w;
19}
Jiri Olsa850f8122012-01-27 15:34:23 +010020
21void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
22 const unsigned long *bitmap2, int bits)
23{
24 int k;
25 int nr = BITS_TO_LONGS(bits);
26
27 for (k = 0; k < nr; k++)
28 dst[k] = bitmap1[k] | bitmap2[k];
29}
Jiri Olsa820d12b2016-08-01 20:02:30 +020030
Yury Norove5b92522021-05-06 18:02:46 -070031size_t bitmap_scnprintf(unsigned long *bitmap, unsigned int nbits,
Jiri Olsa820d12b2016-08-01 20:02:30 +020032 char *buf, size_t size)
33{
34 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
Yury Norove5b92522021-05-06 18:02:46 -070035 unsigned int cur, rbot, rtop;
Jiri Olsa820d12b2016-08-01 20:02:30 +020036 bool first = true;
37 size_t ret = 0;
38
39 rbot = cur = find_first_bit(bitmap, nbits);
40 while (cur < nbits) {
41 rtop = cur;
42 cur = find_next_bit(bitmap, nbits, cur + 1);
43 if (cur < nbits && cur <= rtop + 1)
44 continue;
45
46 if (!first)
47 ret += scnprintf(buf + ret, size - ret, ",");
48
49 first = false;
50
51 ret += scnprintf(buf + ret, size - ret, "%d", rbot);
52 if (rbot < rtop)
53 ret += scnprintf(buf + ret, size - ret, "-%d", rtop);
54
55 rbot = cur;
56 }
57 return ret;
58}
Jiri Olsa741c74f2016-08-01 20:02:31 +020059
60int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
61 const unsigned long *bitmap2, unsigned int bits)
62{
63 unsigned int k;
64 unsigned int lim = bits/BITS_PER_LONG;
65 unsigned long result = 0;
66
67 for (k = 0; k < lim; k++)
68 result |= (dst[k] = bitmap1[k] & bitmap2[k]);
69 if (bits % BITS_PER_LONG)
70 result |= (dst[k] = bitmap1[k] & bitmap2[k] &
71 BITMAP_LAST_WORD_MASK(bits));
72 return result != 0;
73}
Alexey Budankov8812ad42019-12-03 14:43:33 +030074
75int __bitmap_equal(const unsigned long *bitmap1,
76 const unsigned long *bitmap2, unsigned int bits)
77{
78 unsigned int k, lim = bits/BITS_PER_LONG;
79 for (k = 0; k < lim; ++k)
80 if (bitmap1[k] != bitmap2[k])
81 return 0;
82
83 if (bits % BITS_PER_LONG)
84 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
85 return 0;
86
87 return 1;
88}
Alexey Bayduraevf20510d2021-06-30 18:54:48 +030089
90int __bitmap_intersects(const unsigned long *bitmap1,
91 const unsigned long *bitmap2, unsigned int bits)
92{
93 unsigned int k, lim = bits/BITS_PER_LONG;
94 for (k = 0; k < lim; ++k)
95 if (bitmap1[k] & bitmap2[k])
96 return 1;
97
98 if (bits % BITS_PER_LONG)
99 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
100 return 1;
101 return 0;
102}