bitmap.h 1.6 KB

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