bitmap.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifndef __LINUX_BITMAP_H
  2. #define __LINUX_BITMAP_H
  3. #ifndef __ASSEMBLY__
  4. #include <linux/types.h>
  5. #include <linux/bitops.h>
  6. #include <linux/string.h>
  7. /*
  8. * bitmaps provide bit arrays that consume one or more unsigned
  9. * longs. The bitmap interface and available operations are listed
  10. * here, in bitmap.h
  11. *
  12. * Function implementations generic to all architectures are in
  13. * lib/bitmap.c. Functions implementations that are architecture
  14. * specific are in various include/asm-<arch>/bitops.h headers
  15. * and other arch/<arch> specific files.
  16. *
  17. * See lib/bitmap.c for more details.
  18. */
  19. /*
  20. * The available bitmap operations and their rough meaning in the
  21. * case that the bitmap is a single unsigned long are thus:
  22. *
  23. * bitmap_zero(dst, nbits) *dst = 0UL
  24. * bitmap_fill(dst, nbits) *dst = ~0UL
  25. * bitmap_copy(dst, src, nbits) *dst = *src
  26. * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
  27. * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
  28. * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
  29. * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
  30. * bitmap_complement(dst, src, nbits) *dst = ~(*src)
  31. * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
  32. * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
  33. * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
  34. * bitmap_empty(src, nbits) Are all bits zero in *src?
  35. * bitmap_full(src, nbits) Are all bits set in *src?
  36. * bitmap_weight(src, nbits) Hamming Weight: number set bits
  37. * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
  38. * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
  39. * bitmap_scnprintf(buf, len, src, nbits) Print bitmap src to buf
  40. * bitmap_parse(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
  41. * bitmap_scnlistprintf(buf, len, src, nbits) Print bitmap src as list to buf
  42. * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from list
  43. */
  44. /*
  45. * Also the following operations in asm/bitops.h apply to bitmaps.
  46. *
  47. * set_bit(bit, addr) *addr |= bit
  48. * clear_bit(bit, addr) *addr &= ~bit
  49. * change_bit(bit, addr) *addr ^= bit
  50. * test_bit(bit, addr) Is bit set in *addr?
  51. * test_and_set_bit(bit, addr) Set bit and return old value
  52. * test_and_clear_bit(bit, addr) Clear bit and return old value
  53. * test_and_change_bit(bit, addr) Change bit and return old value
  54. * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
  55. * find_first_bit(addr, nbits) Position first set bit in *addr
  56. * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
  57. * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
  58. */
  59. /*
  60. * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
  61. * to declare an array named 'name' of just enough unsigned longs to
  62. * contain all bit positions from 0 to 'bits' - 1.
  63. */
  64. /*
  65. * lib/bitmap.c provides these functions:
  66. */
  67. extern int __bitmap_empty(const unsigned long *bitmap, int bits);
  68. extern int __bitmap_full(const unsigned long *bitmap, int bits);
  69. extern int __bitmap_equal(const unsigned long *bitmap1,
  70. const unsigned long *bitmap2, int bits);
  71. extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
  72. int bits);
  73. extern void __bitmap_shift_right(unsigned long *dst,
  74. const unsigned long *src, int shift, int bits);
  75. extern void __bitmap_shift_left(unsigned long *dst,
  76. const unsigned long *src, int shift, int bits);
  77. extern void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  78. const unsigned long *bitmap2, int bits);
  79. extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  80. const unsigned long *bitmap2, int bits);
  81. extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  82. const unsigned long *bitmap2, int bits);
  83. extern void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  84. const unsigned long *bitmap2, int bits);
  85. extern int __bitmap_intersects(const unsigned long *bitmap1,
  86. const unsigned long *bitmap2, int bits);
  87. extern int __bitmap_subset(const unsigned long *bitmap1,
  88. const unsigned long *bitmap2, int bits);
  89. extern int __bitmap_weight(const unsigned long *bitmap, int bits);
  90. extern int bitmap_scnprintf(char *buf, unsigned int len,
  91. const unsigned long *src, int nbits);
  92. extern int bitmap_parse(const char __user *ubuf, unsigned int ulen,
  93. unsigned long *dst, int nbits);
  94. extern int bitmap_scnlistprintf(char *buf, unsigned int len,
  95. const unsigned long *src, int nbits);
  96. extern int bitmap_parselist(const char *buf, unsigned long *maskp,
  97. int nmaskbits);
  98. extern int bitmap_find_free_region(unsigned long *bitmap, int bits, int order);
  99. extern void bitmap_release_region(unsigned long *bitmap, int pos, int order);
  100. extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
  101. #define BITMAP_LAST_WORD_MASK(nbits) \
  102. ( \
  103. ((nbits) % BITS_PER_LONG) ? \
  104. (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
  105. )
  106. static inline void bitmap_zero(unsigned long *dst, int nbits)
  107. {
  108. if (nbits <= BITS_PER_LONG)
  109. *dst = 0UL;
  110. else {
  111. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  112. memset(dst, 0, len);
  113. }
  114. }
  115. static inline void bitmap_fill(unsigned long *dst, int nbits)
  116. {
  117. size_t nlongs = BITS_TO_LONGS(nbits);
  118. if (nlongs > 1) {
  119. int len = (nlongs - 1) * sizeof(unsigned long);
  120. memset(dst, 0xff, len);
  121. }
  122. dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
  123. }
  124. static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
  125. int nbits)
  126. {
  127. if (nbits <= BITS_PER_LONG)
  128. *dst = *src;
  129. else {
  130. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  131. memcpy(dst, src, len);
  132. }
  133. }
  134. static inline void bitmap_and(unsigned long *dst, const unsigned long *src1,
  135. const unsigned long *src2, int nbits)
  136. {
  137. if (nbits <= BITS_PER_LONG)
  138. *dst = *src1 & *src2;
  139. else
  140. __bitmap_and(dst, src1, src2, nbits);
  141. }
  142. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  143. const unsigned long *src2, int nbits)
  144. {
  145. if (nbits <= BITS_PER_LONG)
  146. *dst = *src1 | *src2;
  147. else
  148. __bitmap_or(dst, src1, src2, nbits);
  149. }
  150. static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
  151. const unsigned long *src2, int nbits)
  152. {
  153. if (nbits <= BITS_PER_LONG)
  154. *dst = *src1 ^ *src2;
  155. else
  156. __bitmap_xor(dst, src1, src2, nbits);
  157. }
  158. static inline void bitmap_andnot(unsigned long *dst, const unsigned long *src1,
  159. const unsigned long *src2, int nbits)
  160. {
  161. if (nbits <= BITS_PER_LONG)
  162. *dst = *src1 & ~(*src2);
  163. else
  164. __bitmap_andnot(dst, src1, src2, nbits);
  165. }
  166. static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
  167. int nbits)
  168. {
  169. if (nbits <= BITS_PER_LONG)
  170. *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
  171. else
  172. __bitmap_complement(dst, src, nbits);
  173. }
  174. static inline int bitmap_equal(const unsigned long *src1,
  175. const unsigned long *src2, int nbits)
  176. {
  177. if (nbits <= BITS_PER_LONG)
  178. return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
  179. else
  180. return __bitmap_equal(src1, src2, nbits);
  181. }
  182. static inline int bitmap_intersects(const unsigned long *src1,
  183. const unsigned long *src2, int nbits)
  184. {
  185. if (nbits <= BITS_PER_LONG)
  186. return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  187. else
  188. return __bitmap_intersects(src1, src2, nbits);
  189. }
  190. static inline int bitmap_subset(const unsigned long *src1,
  191. const unsigned long *src2, int nbits)
  192. {
  193. if (nbits <= BITS_PER_LONG)
  194. return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
  195. else
  196. return __bitmap_subset(src1, src2, nbits);
  197. }
  198. static inline int bitmap_empty(const unsigned long *src, int nbits)
  199. {
  200. if (nbits <= BITS_PER_LONG)
  201. return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
  202. else
  203. return __bitmap_empty(src, nbits);
  204. }
  205. static inline int bitmap_full(const unsigned long *src, int nbits)
  206. {
  207. if (nbits <= BITS_PER_LONG)
  208. return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
  209. else
  210. return __bitmap_full(src, nbits);
  211. }
  212. static inline int bitmap_weight(const unsigned long *src, int nbits)
  213. {
  214. return __bitmap_weight(src, nbits);
  215. }
  216. static inline void bitmap_shift_right(unsigned long *dst,
  217. const unsigned long *src, int n, int nbits)
  218. {
  219. if (nbits <= BITS_PER_LONG)
  220. *dst = *src >> n;
  221. else
  222. __bitmap_shift_right(dst, src, n, nbits);
  223. }
  224. static inline void bitmap_shift_left(unsigned long *dst,
  225. const unsigned long *src, int n, int nbits)
  226. {
  227. if (nbits <= BITS_PER_LONG)
  228. *dst = (*src << n) & BITMAP_LAST_WORD_MASK(nbits);
  229. else
  230. __bitmap_shift_left(dst, src, n, nbits);
  231. }
  232. #endif /* __ASSEMBLY__ */
  233. #endif /* __LINUX_BITMAP_H */