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