bitmap.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef _PERF_BITOPS_H
  2. #define _PERF_BITOPS_H
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "bitops.h"
  6. #include "findbit.h"
  7. #define DECLARE_BITMAP(name,bits) \
  8. unsigned long name[BITS_TO_LONGS(bits)]
  9. int __bitmap_weight(const unsigned long *bitmap, int bits);
  10. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  11. const unsigned long *bitmap2, int bits);
  12. int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  13. const unsigned long *bitmap2, unsigned int bits);
  14. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
  15. #define BITMAP_LAST_WORD_MASK(nbits) \
  16. ( \
  17. ((nbits) % BITS_PER_LONG) ? \
  18. (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
  19. )
  20. #define small_const_nbits(nbits) \
  21. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
  22. static inline void bitmap_zero(unsigned long *dst, int nbits)
  23. {
  24. if (small_const_nbits(nbits))
  25. *dst = 0UL;
  26. else {
  27. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  28. memset(dst, 0, len);
  29. }
  30. }
  31. static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
  32. {
  33. unsigned int nlongs = BITS_TO_LONGS(nbits);
  34. if (!small_const_nbits(nbits)) {
  35. unsigned int len = (nlongs - 1) * sizeof(unsigned long);
  36. memset(dst, 0xff, len);
  37. }
  38. dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
  39. }
  40. static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
  41. {
  42. if (small_const_nbits(nbits))
  43. return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
  44. return find_first_bit(src, nbits) == nbits;
  45. }
  46. static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
  47. {
  48. if (small_const_nbits(nbits))
  49. return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
  50. return find_first_zero_bit(src, nbits) == nbits;
  51. }
  52. static inline int bitmap_weight(const unsigned long *src, int nbits)
  53. {
  54. if (small_const_nbits(nbits))
  55. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  56. return __bitmap_weight(src, nbits);
  57. }
  58. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  59. const unsigned long *src2, int nbits)
  60. {
  61. if (small_const_nbits(nbits))
  62. *dst = *src1 | *src2;
  63. else
  64. __bitmap_or(dst, src1, src2, nbits);
  65. }
  66. /**
  67. * test_and_set_bit - Set a bit and return its old value
  68. * @nr: Bit to set
  69. * @addr: Address to count from
  70. */
  71. static inline int test_and_set_bit(int nr, unsigned long *addr)
  72. {
  73. unsigned long mask = BIT_MASK(nr);
  74. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  75. unsigned long old;
  76. old = *p;
  77. *p = old | mask;
  78. return (old & mask) != 0;
  79. }
  80. /**
  81. * bitmap_alloc - Allocate bitmap
  82. * @nbits: Number of bits
  83. */
  84. static inline unsigned long *bitmap_alloc(int nbits)
  85. {
  86. return (unsigned long *)calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
  87. }
  88. /*
  89. * bitmap_scnprintf - print bitmap list into buffer
  90. * @bitmap: bitmap
  91. * @nbits: size of bitmap
  92. * @buf: buffer to store output
  93. * @size: size of @buf
  94. */
  95. size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
  96. char *buf, size_t size);
  97. /**
  98. * bitmap_and - Do logical and on bitmaps
  99. * @dst: resulting bitmap
  100. * @src1: operand 1
  101. * @src2: operand 2
  102. * @nbits: size of bitmap
  103. */
  104. static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
  105. const unsigned long *src2, unsigned int nbits)
  106. {
  107. if (small_const_nbits(nbits))
  108. return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  109. return __bitmap_and(dst, src1, src2, nbits);
  110. }
  111. #endif /* _PERF_BITOPS_H */