bitmap.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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>
  7. unsigned int __bitmap_weight(const unsigned long *bitmap, int bits)
  8. {
  9. unsigned int k, w = 0, lim = bits/BITS_PER_LONG;
  10. for (k = 0; k < lim; k++)
  11. w += hweight_long(bitmap[k]);
  12. if (bits % BITS_PER_LONG)
  13. w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
  14. return w;
  15. }
  16. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  17. const unsigned long *bitmap2, int bits)
  18. {
  19. int k;
  20. int nr = BITS_TO_LONGS(bits);
  21. for (k = 0; k < nr; k++)
  22. dst[k] = bitmap1[k] | bitmap2[k];
  23. }
  24. size_t bitmap_scnprintf(unsigned long *bitmap, unsigned int nbits,
  25. char *buf, size_t size)
  26. {
  27. /* current bit is 'cur', most recently seen range is [rbot, rtop] */
  28. unsigned int cur, rbot, rtop;
  29. bool first = true;
  30. size_t ret = 0;
  31. rbot = cur = find_first_bit(bitmap, nbits);
  32. while (cur < nbits) {
  33. rtop = cur;
  34. cur = find_next_bit(bitmap, nbits, cur + 1);
  35. if (cur < nbits && cur <= rtop + 1)
  36. continue;
  37. if (!first)
  38. ret += scnprintf(buf + ret, size - ret, ",");
  39. first = false;
  40. ret += scnprintf(buf + ret, size - ret, "%d", rbot);
  41. if (rbot < rtop)
  42. ret += scnprintf(buf + ret, size - ret, "-%d", rtop);
  43. rbot = cur;
  44. }
  45. return ret;
  46. }
  47. bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  48. const unsigned long *bitmap2, unsigned int bits)
  49. {
  50. unsigned int k;
  51. unsigned int lim = bits/BITS_PER_LONG;
  52. unsigned long result = 0;
  53. for (k = 0; k < lim; k++)
  54. result |= (dst[k] = bitmap1[k] & bitmap2[k]);
  55. if (bits % BITS_PER_LONG)
  56. result |= (dst[k] = bitmap1[k] & bitmap2[k] &
  57. BITMAP_LAST_WORD_MASK(bits));
  58. return result != 0;
  59. }
  60. bool __bitmap_equal(const unsigned long *bitmap1,
  61. const unsigned long *bitmap2, unsigned int bits)
  62. {
  63. unsigned int k, lim = bits/BITS_PER_LONG;
  64. for (k = 0; k < lim; ++k)
  65. if (bitmap1[k] != bitmap2[k])
  66. return false;
  67. if (bits % BITS_PER_LONG)
  68. if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  69. return false;
  70. return true;
  71. }
  72. bool __bitmap_intersects(const unsigned long *bitmap1,
  73. const unsigned long *bitmap2, unsigned int bits)
  74. {
  75. unsigned int k, lim = bits/BITS_PER_LONG;
  76. for (k = 0; k < lim; ++k)
  77. if (bitmap1[k] & bitmap2[k])
  78. return true;
  79. if (bits % BITS_PER_LONG)
  80. if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  81. return true;
  82. return false;
  83. }
  84. void __bitmap_set(unsigned long *map, unsigned int start, int len)
  85. {
  86. unsigned long *p = map + BIT_WORD(start);
  87. const unsigned int size = start + len;
  88. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  89. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  90. while (len - bits_to_set >= 0) {
  91. *p |= mask_to_set;
  92. len -= bits_to_set;
  93. bits_to_set = BITS_PER_LONG;
  94. mask_to_set = ~0UL;
  95. p++;
  96. }
  97. if (len) {
  98. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  99. *p |= mask_to_set;
  100. }
  101. }
  102. void __bitmap_clear(unsigned long *map, unsigned int start, int len)
  103. {
  104. unsigned long *p = map + BIT_WORD(start);
  105. const unsigned int size = start + len;
  106. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  107. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  108. while (len - bits_to_clear >= 0) {
  109. *p &= ~mask_to_clear;
  110. len -= bits_to_clear;
  111. bits_to_clear = BITS_PER_LONG;
  112. mask_to_clear = ~0UL;
  113. p++;
  114. }
  115. if (len) {
  116. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  117. *p &= ~mask_to_clear;
  118. }
  119. }
  120. bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  121. const unsigned long *bitmap2, unsigned int bits)
  122. {
  123. unsigned int k;
  124. unsigned int lim = bits/BITS_PER_LONG;
  125. unsigned long result = 0;
  126. for (k = 0; k < lim; k++)
  127. result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
  128. if (bits % BITS_PER_LONG)
  129. result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
  130. BITMAP_LAST_WORD_MASK(bits));
  131. return result != 0;
  132. }
  133. bool __bitmap_subset(const unsigned long *bitmap1,
  134. const unsigned long *bitmap2, unsigned int bits)
  135. {
  136. unsigned int k, lim = bits/BITS_PER_LONG;
  137. for (k = 0; k < lim; ++k)
  138. if (bitmap1[k] & ~bitmap2[k])
  139. return false;
  140. if (bits % BITS_PER_LONG)
  141. if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  142. return false;
  143. return true;
  144. }