bitops.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef _LINUX_BITOPS_H
  2. #define _LINUX_BITOPS_H
  3. #include <asm/types.h>
  4. #ifdef __KERNEL__
  5. #define BIT(nr) (1UL << (nr))
  6. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  7. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  8. #define BITS_PER_BYTE 8
  9. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  10. #endif
  11. extern unsigned int __sw_hweight8(unsigned int w);
  12. extern unsigned int __sw_hweight16(unsigned int w);
  13. extern unsigned int __sw_hweight32(unsigned int w);
  14. extern unsigned long __sw_hweight64(__u64 w);
  15. /*
  16. * Include this here because some architectures need generic_ffs/fls in
  17. * scope
  18. */
  19. #include <asm/bitops.h>
  20. #define for_each_set_bit(bit, addr, size) \
  21. for ((bit) = find_first_bit((addr), (size)); \
  22. (bit) < (size); \
  23. (bit) = find_next_bit((addr), (size), (bit) + 1))
  24. static __inline__ int get_bitmask_order(unsigned int count)
  25. {
  26. int order;
  27. order = fls(count);
  28. return order; /* We could be slightly more clever with -1 here... */
  29. }
  30. static __inline__ int get_count_order(unsigned int count)
  31. {
  32. int order;
  33. order = fls(count) - 1;
  34. if (count & (count - 1))
  35. order++;
  36. return order;
  37. }
  38. static inline unsigned long hweight_long(unsigned long w)
  39. {
  40. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  41. }
  42. /**
  43. * rol32 - rotate a 32-bit value left
  44. * @word: value to rotate
  45. * @shift: bits to roll
  46. */
  47. static inline __u32 rol32(__u32 word, unsigned int shift)
  48. {
  49. return (word << shift) | (word >> (32 - shift));
  50. }
  51. /**
  52. * ror32 - rotate a 32-bit value right
  53. * @word: value to rotate
  54. * @shift: bits to roll
  55. */
  56. static inline __u32 ror32(__u32 word, unsigned int shift)
  57. {
  58. return (word >> shift) | (word << (32 - shift));
  59. }
  60. /**
  61. * rol16 - rotate a 16-bit value left
  62. * @word: value to rotate
  63. * @shift: bits to roll
  64. */
  65. static inline __u16 rol16(__u16 word, unsigned int shift)
  66. {
  67. return (word << shift) | (word >> (16 - shift));
  68. }
  69. /**
  70. * ror16 - rotate a 16-bit value right
  71. * @word: value to rotate
  72. * @shift: bits to roll
  73. */
  74. static inline __u16 ror16(__u16 word, unsigned int shift)
  75. {
  76. return (word >> shift) | (word << (16 - shift));
  77. }
  78. /**
  79. * rol8 - rotate an 8-bit value left
  80. * @word: value to rotate
  81. * @shift: bits to roll
  82. */
  83. static inline __u8 rol8(__u8 word, unsigned int shift)
  84. {
  85. return (word << shift) | (word >> (8 - shift));
  86. }
  87. /**
  88. * ror8 - rotate an 8-bit value right
  89. * @word: value to rotate
  90. * @shift: bits to roll
  91. */
  92. static inline __u8 ror8(__u8 word, unsigned int shift)
  93. {
  94. return (word >> shift) | (word << (8 - shift));
  95. }
  96. static inline unsigned fls_long(unsigned long l)
  97. {
  98. if (sizeof(l) == 4)
  99. return fls(l);
  100. return fls64(l);
  101. }
  102. /**
  103. * __ffs64 - find first set bit in a 64 bit word
  104. * @word: The 64 bit word
  105. *
  106. * On 64 bit arches this is a synomyn for __ffs
  107. * The result is not defined if no bits are set, so check that @word
  108. * is non-zero before calling this.
  109. */
  110. static inline unsigned long __ffs64(u64 word)
  111. {
  112. #if BITS_PER_LONG == 32
  113. if (((u32)word) == 0UL)
  114. return __ffs((u32)(word >> 32)) + 32;
  115. #elif BITS_PER_LONG != 64
  116. #error BITS_PER_LONG not 32 or 64
  117. #endif
  118. return __ffs((unsigned long)word);
  119. }
  120. #ifdef __KERNEL__
  121. #ifdef CONFIG_GENERIC_FIND_LAST_BIT
  122. /**
  123. * find_last_bit - find the last set bit in a memory region
  124. * @addr: The address to start the search at
  125. * @size: The maximum size to search
  126. *
  127. * Returns the bit number of the first set bit, or size.
  128. */
  129. extern unsigned long find_last_bit(const unsigned long *addr,
  130. unsigned long size);
  131. #endif /* CONFIG_GENERIC_FIND_LAST_BIT */
  132. #endif /* __KERNEL__ */
  133. #endif