bitmap.h 3.7 KB

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