bitops.h 1.7 KB

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