bitmap.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef _PERF_BITOPS_H
  2. #define _PERF_BITOPS_H
  3. #include <string.h>
  4. #include <linux/bitops.h>
  5. #define DECLARE_BITMAP(name,bits) \
  6. unsigned long name[BITS_TO_LONGS(bits)]
  7. int __bitmap_weight(const unsigned long *bitmap, int bits);
  8. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  9. const unsigned long *bitmap2, int bits);
  10. #define BITMAP_LAST_WORD_MASK(nbits) \
  11. ( \
  12. ((nbits) % BITS_PER_LONG) ? \
  13. (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
  14. )
  15. #define small_const_nbits(nbits) \
  16. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
  17. static inline void bitmap_zero(unsigned long *dst, int nbits)
  18. {
  19. if (small_const_nbits(nbits))
  20. *dst = 0UL;
  21. else {
  22. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  23. memset(dst, 0, len);
  24. }
  25. }
  26. static inline int bitmap_weight(const unsigned long *src, int nbits)
  27. {
  28. if (small_const_nbits(nbits))
  29. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  30. return __bitmap_weight(src, nbits);
  31. }
  32. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  33. const unsigned long *src2, int nbits)
  34. {
  35. if (small_const_nbits(nbits))
  36. *dst = *src1 | *src2;
  37. else
  38. __bitmap_or(dst, src1, src2, nbits);
  39. }
  40. /**
  41. * test_and_set_bit - Set a bit and return its old value
  42. * @nr: Bit to set
  43. * @addr: Address to count from
  44. */
  45. static inline int test_and_set_bit(int nr, unsigned long *addr)
  46. {
  47. unsigned long mask = BIT_MASK(nr);
  48. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  49. unsigned long old;
  50. old = *p;
  51. *p = old | mask;
  52. return (old & mask) != 0;
  53. }
  54. #endif /* _PERF_BITOPS_H */