bitmap.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef _PERF_BITOPS_H
  2. #define _PERF_BITOPS_H
  3. #include <string.h>
  4. #include <linux/bitops.h>
  5. int __bitmap_weight(const unsigned long *bitmap, int bits);
  6. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  7. const unsigned long *bitmap2, int bits);
  8. #define BITMAP_LAST_WORD_MASK(nbits) \
  9. ( \
  10. ((nbits) % BITS_PER_LONG) ? \
  11. (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
  12. )
  13. #define small_const_nbits(nbits) \
  14. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
  15. static inline void bitmap_zero(unsigned long *dst, int nbits)
  16. {
  17. if (small_const_nbits(nbits))
  18. *dst = 0UL;
  19. else {
  20. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  21. memset(dst, 0, len);
  22. }
  23. }
  24. static inline int bitmap_weight(const unsigned long *src, int nbits)
  25. {
  26. if (small_const_nbits(nbits))
  27. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  28. return __bitmap_weight(src, nbits);
  29. }
  30. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  31. const unsigned long *src2, int nbits)
  32. {
  33. if (small_const_nbits(nbits))
  34. *dst = *src1 | *src2;
  35. else
  36. __bitmap_or(dst, src1, src2, nbits);
  37. }
  38. #endif /* _PERF_BITOPS_H */