bitmap.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Bitmap Module
  3. *
  4. * Stolen from linux/src/lib/bitmap.c
  5. *
  6. * Copyright (C) 2010 Corentin Chary
  7. *
  8. * This source code is licensed under the GNU General Public License,
  9. * Version 2.
  10. */
  11. #include "bitops.h"
  12. #include "bitmap.h"
  13. /*
  14. * bitmaps provide an array of bits, implemented using an an
  15. * array of unsigned longs. The number of valid bits in a
  16. * given bitmap does _not_ need to be an exact multiple of
  17. * BITS_PER_LONG.
  18. *
  19. * The possible unused bits in the last, partially used word
  20. * of a bitmap are 'don't care'. The implementation makes
  21. * no particular effort to keep them zero. It ensures that
  22. * their value will not affect the results of any operation.
  23. * The bitmap operations that return Boolean (bitmap_empty,
  24. * for example) or scalar (bitmap_weight, for example) results
  25. * carefully filter out these unused bits from impacting their
  26. * results.
  27. *
  28. * These operations actually hold to a slightly stronger rule:
  29. * if you don't input any bitmaps to these ops that have some
  30. * unused bits set, then they won't output any set unused bits
  31. * in output bitmaps.
  32. *
  33. * The byte ordering of bitmaps is more natural on little
  34. * endian architectures.
  35. */
  36. int slow_bitmap_empty(const unsigned long *bitmap, int bits)
  37. {
  38. int k, lim = bits/BITS_PER_LONG;
  39. for (k = 0; k < lim; ++k) {
  40. if (bitmap[k]) {
  41. return 0;
  42. }
  43. }
  44. if (bits % BITS_PER_LONG) {
  45. if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) {
  46. return 0;
  47. }
  48. }
  49. return 1;
  50. }
  51. int slow_bitmap_full(const unsigned long *bitmap, int bits)
  52. {
  53. int k, lim = bits/BITS_PER_LONG;
  54. for (k = 0; k < lim; ++k) {
  55. if (~bitmap[k]) {
  56. return 0;
  57. }
  58. }
  59. if (bits % BITS_PER_LONG) {
  60. if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) {
  61. return 0;
  62. }
  63. }
  64. return 1;
  65. }
  66. int slow_bitmap_equal(const unsigned long *bitmap1,
  67. const unsigned long *bitmap2, int bits)
  68. {
  69. int k, lim = bits/BITS_PER_LONG;
  70. for (k = 0; k < lim; ++k) {
  71. if (bitmap1[k] != bitmap2[k]) {
  72. return 0;
  73. }
  74. }
  75. if (bits % BITS_PER_LONG) {
  76. if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) {
  77. return 0;
  78. }
  79. }
  80. return 1;
  81. }
  82. void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
  83. int bits)
  84. {
  85. int k, lim = bits/BITS_PER_LONG;
  86. for (k = 0; k < lim; ++k) {
  87. dst[k] = ~src[k];
  88. }
  89. if (bits % BITS_PER_LONG) {
  90. dst[k] = ~src[k] & BITMAP_LAST_WORD_MASK(bits);
  91. }
  92. }
  93. int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  94. const unsigned long *bitmap2, int bits)
  95. {
  96. int k;
  97. int nr = BITS_TO_LONGS(bits);
  98. unsigned long result = 0;
  99. for (k = 0; k < nr; k++) {
  100. result |= (dst[k] = bitmap1[k] & bitmap2[k]);
  101. }
  102. return result != 0;
  103. }
  104. void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  105. const unsigned long *bitmap2, int bits)
  106. {
  107. int k;
  108. int nr = BITS_TO_LONGS(bits);
  109. for (k = 0; k < nr; k++) {
  110. dst[k] = bitmap1[k] | bitmap2[k];
  111. }
  112. }
  113. void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  114. const unsigned long *bitmap2, int bits)
  115. {
  116. int k;
  117. int nr = BITS_TO_LONGS(bits);
  118. for (k = 0; k < nr; k++) {
  119. dst[k] = bitmap1[k] ^ bitmap2[k];
  120. }
  121. }
  122. int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  123. const unsigned long *bitmap2, int bits)
  124. {
  125. int k;
  126. int nr = BITS_TO_LONGS(bits);
  127. unsigned long result = 0;
  128. for (k = 0; k < nr; k++) {
  129. result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
  130. }
  131. return result != 0;
  132. }
  133. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
  134. void bitmap_set(unsigned long *map, int start, int nr)
  135. {
  136. unsigned long *p = map + BIT_WORD(start);
  137. const int size = start + nr;
  138. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  139. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  140. while (nr - bits_to_set >= 0) {
  141. *p |= mask_to_set;
  142. nr -= bits_to_set;
  143. bits_to_set = BITS_PER_LONG;
  144. mask_to_set = ~0UL;
  145. p++;
  146. }
  147. if (nr) {
  148. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  149. *p |= mask_to_set;
  150. }
  151. }
  152. void bitmap_clear(unsigned long *map, int start, int nr)
  153. {
  154. unsigned long *p = map + BIT_WORD(start);
  155. const int size = start + nr;
  156. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  157. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  158. while (nr - bits_to_clear >= 0) {
  159. *p &= ~mask_to_clear;
  160. nr -= bits_to_clear;
  161. bits_to_clear = BITS_PER_LONG;
  162. mask_to_clear = ~0UL;
  163. p++;
  164. }
  165. if (nr) {
  166. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  167. *p &= ~mask_to_clear;
  168. }
  169. }
  170. #define ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
  171. /**
  172. * bitmap_find_next_zero_area - find a contiguous aligned zero area
  173. * @map: The address to base the search on
  174. * @size: The bitmap size in bits
  175. * @start: The bitnumber to start searching at
  176. * @nr: The number of zeroed bits we're looking for
  177. * @align_mask: Alignment mask for zero area
  178. *
  179. * The @align_mask should be one less than a power of 2; the effect is that
  180. * the bit offset of all zero areas this function finds is multiples of that
  181. * power of 2. A @align_mask of 0 means no alignment is required.
  182. */
  183. unsigned long bitmap_find_next_zero_area(unsigned long *map,
  184. unsigned long size,
  185. unsigned long start,
  186. unsigned int nr,
  187. unsigned long align_mask)
  188. {
  189. unsigned long index, end, i;
  190. again:
  191. index = find_next_zero_bit(map, size, start);
  192. /* Align allocation */
  193. index = ALIGN_MASK(index, align_mask);
  194. end = index + nr;
  195. if (end > size) {
  196. return end;
  197. }
  198. i = find_next_bit(map, end, index);
  199. if (i < end) {
  200. start = i + 1;
  201. goto again;
  202. }
  203. return index;
  204. }
  205. int slow_bitmap_intersects(const unsigned long *bitmap1,
  206. const unsigned long *bitmap2, int bits)
  207. {
  208. int k, lim = bits/BITS_PER_LONG;
  209. for (k = 0; k < lim; ++k) {
  210. if (bitmap1[k] & bitmap2[k]) {
  211. return 1;
  212. }
  213. }
  214. if (bits % BITS_PER_LONG) {
  215. if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) {
  216. return 1;
  217. }
  218. }
  219. return 0;
  220. }