bitops.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef _LINUX_BITOPS_H
  2. #define _LINUX_BITOPS_H
  3. #include <asm/types.h>
  4. #ifdef __KERNEL__
  5. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  6. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  7. #define BITS_TO_TYPE(nr, t) (((nr)+(t)-1)/(t))
  8. #define BITS_TO_LONGS(nr) BITS_TO_TYPE(nr, BITS_PER_LONG)
  9. #define BITS_PER_BYTE 8
  10. #endif
  11. /*
  12. * Include this here because some architectures need generic_ffs/fls in
  13. * scope
  14. */
  15. #include <asm/bitops.h>
  16. #define for_each_bit(bit, addr, size) \
  17. for ((bit) = find_first_bit((addr), (size)); \
  18. (bit) < (size); \
  19. (bit) = find_next_bit((addr), (size), (bit) + 1))
  20. static __inline__ int get_bitmask_order(unsigned int count)
  21. {
  22. int order;
  23. order = fls(count);
  24. return order; /* We could be slightly more clever with -1 here... */
  25. }
  26. static __inline__ int get_count_order(unsigned int count)
  27. {
  28. int order;
  29. order = fls(count) - 1;
  30. if (count & (count - 1))
  31. order++;
  32. return order;
  33. }
  34. static inline unsigned long hweight_long(unsigned long w)
  35. {
  36. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  37. }
  38. /**
  39. * rol32 - rotate a 32-bit value left
  40. * @word: value to rotate
  41. * @shift: bits to roll
  42. */
  43. static inline __u32 rol32(__u32 word, unsigned int shift)
  44. {
  45. return (word << shift) | (word >> (32 - shift));
  46. }
  47. /**
  48. * ror32 - rotate a 32-bit value right
  49. * @word: value to rotate
  50. * @shift: bits to roll
  51. */
  52. static inline __u32 ror32(__u32 word, unsigned int shift)
  53. {
  54. return (word >> shift) | (word << (32 - shift));
  55. }
  56. static inline unsigned fls_long(unsigned long l)
  57. {
  58. if (sizeof(l) == 4)
  59. return fls(l);
  60. return fls64(l);
  61. }
  62. #endif