bitops.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef _TOOLS_LINUX_BITOPS_H_
  2. #define _TOOLS_LINUX_BITOPS_H_
  3. #include <asm/types.h>
  4. #include <linux/kernel.h>
  5. #include <linux/compiler.h>
  6. #ifndef __WORDSIZE
  7. #define __WORDSIZE (__SIZEOF_LONG__ * 8)
  8. #endif
  9. #define BITS_PER_LONG __WORDSIZE
  10. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  11. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  12. #define BITS_PER_BYTE 8
  13. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  14. #define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64))
  15. #define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32))
  16. #define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE)
  17. extern unsigned int __sw_hweight8(unsigned int w);
  18. extern unsigned int __sw_hweight16(unsigned int w);
  19. extern unsigned int __sw_hweight32(unsigned int w);
  20. extern unsigned long __sw_hweight64(__u64 w);
  21. /*
  22. * Include this here because some architectures need generic_ffs/fls in
  23. * scope
  24. *
  25. * XXX: this needs to be asm/bitops.h, when we get to per arch optimizations
  26. */
  27. #include <asm-generic/bitops.h>
  28. #define for_each_set_bit(bit, addr, size) \
  29. for ((bit) = find_first_bit((addr), (size)); \
  30. (bit) < (size); \
  31. (bit) = find_next_bit((addr), (size), (bit) + 1))
  32. /* same as for_each_set_bit() but use bit as value to start with */
  33. #define for_each_set_bit_from(bit, addr, size) \
  34. for ((bit) = find_next_bit((addr), (size), (bit)); \
  35. (bit) < (size); \
  36. (bit) = find_next_bit((addr), (size), (bit) + 1))
  37. static inline unsigned long hweight_long(unsigned long w)
  38. {
  39. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  40. }
  41. static inline unsigned fls_long(unsigned long l)
  42. {
  43. if (sizeof(l) == 4)
  44. return fls(l);
  45. return fls64(l);
  46. }
  47. #endif