treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / lib / bitmap.c
blob5043747ef6c5f9f2d662f26add8736ddc275b461
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * From lib/bitmap.c
4 * Helper functions for bitmap.h.
5 */
6 #include <linux/bitmap.h>
8 int __bitmap_weight(const unsigned long *bitmap, int bits)
10 int k, w = 0, lim = bits/BITS_PER_LONG;
12 for (k = 0; k < lim; k++)
13 w += hweight_long(bitmap[k]);
15 if (bits % BITS_PER_LONG)
16 w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
18 return w;
21 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
22 const unsigned long *bitmap2, int bits)
24 int k;
25 int nr = BITS_TO_LONGS(bits);
27 for (k = 0; k < nr; k++)
28 dst[k] = bitmap1[k] | bitmap2[k];
31 size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
32 char *buf, size_t size)
34 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
35 int cur, rbot, rtop;
36 bool first = true;
37 size_t ret = 0;
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;
46 if (!first)
47 ret += scnprintf(buf + ret, size - ret, ",");
49 first = false;
51 ret += scnprintf(buf + ret, size - ret, "%d", rbot);
52 if (rbot < rtop)
53 ret += scnprintf(buf + ret, size - ret, "-%d", rtop);
55 rbot = cur;
57 return ret;
60 int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
61 const unsigned long *bitmap2, unsigned int bits)
63 unsigned int k;
64 unsigned int lim = bits/BITS_PER_LONG;
65 unsigned long result = 0;
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;
75 int __bitmap_equal(const unsigned long *bitmap1,
76 const unsigned long *bitmap2, unsigned int bits)
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;
83 if (bits % BITS_PER_LONG)
84 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
85 return 0;
87 return 1;