bitmap.h 3.3 KB

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