Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * From lib/bitmap.c
0004  * Helper functions for bitmap.h.
0005  */
0006 #include <linux/bitmap.h>
0007 
0008 unsigned int __bitmap_weight(const unsigned long *bitmap, int bits)
0009 {
0010     unsigned int k, w = 0, lim = bits/BITS_PER_LONG;
0011 
0012     for (k = 0; k < lim; k++)
0013         w += hweight_long(bitmap[k]);
0014 
0015     if (bits % BITS_PER_LONG)
0016         w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
0017 
0018     return w;
0019 }
0020 
0021 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
0022          const unsigned long *bitmap2, int bits)
0023 {
0024     int k;
0025     int nr = BITS_TO_LONGS(bits);
0026 
0027     for (k = 0; k < nr; k++)
0028         dst[k] = bitmap1[k] | bitmap2[k];
0029 }
0030 
0031 size_t bitmap_scnprintf(unsigned long *bitmap, unsigned int nbits,
0032             char *buf, size_t size)
0033 {
0034     /* current bit is 'cur', most recently seen range is [rbot, rtop] */
0035     unsigned int cur, rbot, rtop;
0036     bool first = true;
0037     size_t ret = 0;
0038 
0039     rbot = cur = find_first_bit(bitmap, nbits);
0040     while (cur < nbits) {
0041         rtop = cur;
0042         cur = find_next_bit(bitmap, nbits, cur + 1);
0043         if (cur < nbits && cur <= rtop + 1)
0044             continue;
0045 
0046         if (!first)
0047             ret += scnprintf(buf + ret, size - ret, ",");
0048 
0049         first = false;
0050 
0051         ret += scnprintf(buf + ret, size - ret, "%d", rbot);
0052         if (rbot < rtop)
0053             ret += scnprintf(buf + ret, size - ret, "-%d", rtop);
0054 
0055         rbot = cur;
0056     }
0057     return ret;
0058 }
0059 
0060 bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
0061          const unsigned long *bitmap2, unsigned int bits)
0062 {
0063     unsigned int k;
0064     unsigned int lim = bits/BITS_PER_LONG;
0065     unsigned long result = 0;
0066 
0067     for (k = 0; k < lim; k++)
0068         result |= (dst[k] = bitmap1[k] & bitmap2[k]);
0069     if (bits % BITS_PER_LONG)
0070         result |= (dst[k] = bitmap1[k] & bitmap2[k] &
0071                BITMAP_LAST_WORD_MASK(bits));
0072     return result != 0;
0073 }
0074 
0075 bool __bitmap_equal(const unsigned long *bitmap1,
0076             const unsigned long *bitmap2, unsigned int bits)
0077 {
0078     unsigned int k, lim = bits/BITS_PER_LONG;
0079     for (k = 0; k < lim; ++k)
0080         if (bitmap1[k] != bitmap2[k])
0081             return false;
0082 
0083     if (bits % BITS_PER_LONG)
0084         if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
0085             return false;
0086 
0087     return true;
0088 }
0089 
0090 bool __bitmap_intersects(const unsigned long *bitmap1,
0091              const unsigned long *bitmap2, unsigned int bits)
0092 {
0093     unsigned int k, lim = bits/BITS_PER_LONG;
0094     for (k = 0; k < lim; ++k)
0095         if (bitmap1[k] & bitmap2[k])
0096             return true;
0097 
0098     if (bits % BITS_PER_LONG)
0099         if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
0100             return true;
0101     return false;
0102 }