bitops.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef _LINUX_BITOPS_H
  2. #define _LINUX_BITOPS_H
  3. #include <asm/types.h>
  4. /*
  5. * ffs: find first bit set. This is defined the same way as
  6. * the libc and compiler builtin ffs routines, therefore
  7. * differs in spirit from the above ffz (man ffs).
  8. */
  9. static inline int generic_ffs(int x)
  10. {
  11. int r = 1;
  12. if (!x)
  13. return 0;
  14. if (!(x & 0xffff)) {
  15. x >>= 16;
  16. r += 16;
  17. }
  18. if (!(x & 0xff)) {
  19. x >>= 8;
  20. r += 8;
  21. }
  22. if (!(x & 0xf)) {
  23. x >>= 4;
  24. r += 4;
  25. }
  26. if (!(x & 3)) {
  27. x >>= 2;
  28. r += 2;
  29. }
  30. if (!(x & 1)) {
  31. x >>= 1;
  32. r += 1;
  33. }
  34. return r;
  35. }
  36. /*
  37. * fls: find last bit set.
  38. */
  39. static __inline__ int generic_fls(int x)
  40. {
  41. int r = 32;
  42. if (!x)
  43. return 0;
  44. if (!(x & 0xffff0000u)) {
  45. x <<= 16;
  46. r -= 16;
  47. }
  48. if (!(x & 0xff000000u)) {
  49. x <<= 8;
  50. r -= 8;
  51. }
  52. if (!(x & 0xf0000000u)) {
  53. x <<= 4;
  54. r -= 4;
  55. }
  56. if (!(x & 0xc0000000u)) {
  57. x <<= 2;
  58. r -= 2;
  59. }
  60. if (!(x & 0x80000000u)) {
  61. x <<= 1;
  62. r -= 1;
  63. }
  64. return r;
  65. }
  66. /*
  67. * Include this here because some architectures need generic_ffs/fls in
  68. * scope
  69. */
  70. #include <asm/bitops.h>
  71. static __inline__ int get_bitmask_order(unsigned int count)
  72. {
  73. int order;
  74. order = fls(count);
  75. return order; /* We could be slightly more clever with -1 here... */
  76. }
  77. static __inline__ int get_count_order(unsigned int count)
  78. {
  79. int order;
  80. order = fls(count) - 1;
  81. if (count & (count - 1))
  82. order++;
  83. return order;
  84. }
  85. /*
  86. * hweightN: returns the hamming weight (i.e. the number
  87. * of bits set) of a N-bit word
  88. */
  89. static inline unsigned int generic_hweight32(unsigned int w)
  90. {
  91. unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
  92. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  93. res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
  94. res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
  95. return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
  96. }
  97. static inline unsigned int generic_hweight16(unsigned int w)
  98. {
  99. unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
  100. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  101. res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
  102. return (res & 0x00FF) + ((res >> 8) & 0x00FF);
  103. }
  104. static inline unsigned int generic_hweight8(unsigned int w)
  105. {
  106. unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
  107. res = (res & 0x33) + ((res >> 2) & 0x33);
  108. return (res & 0x0F) + ((res >> 4) & 0x0F);
  109. }
  110. static inline unsigned long generic_hweight64(__u64 w)
  111. {
  112. #if BITS_PER_LONG < 64
  113. return generic_hweight32((unsigned int)(w >> 32)) +
  114. generic_hweight32((unsigned int)w);
  115. #else
  116. u64 res;
  117. res = (w & 0x5555555555555555ul) + ((w >> 1) & 0x5555555555555555ul);
  118. res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
  119. res = (res & 0x0F0F0F0F0F0F0F0Ful) + ((res >> 4) & 0x0F0F0F0F0F0F0F0Ful);
  120. res = (res & 0x00FF00FF00FF00FFul) + ((res >> 8) & 0x00FF00FF00FF00FFul);
  121. res = (res & 0x0000FFFF0000FFFFul) + ((res >> 16) & 0x0000FFFF0000FFFFul);
  122. return (res & 0x00000000FFFFFFFFul) + ((res >> 32) & 0x00000000FFFFFFFFul);
  123. #endif
  124. }
  125. static inline unsigned long hweight_long(unsigned long w)
  126. {
  127. return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
  128. }
  129. /*
  130. * rol32 - rotate a 32-bit value left
  131. *
  132. * @word: value to rotate
  133. * @shift: bits to roll
  134. */
  135. static inline __u32 rol32(__u32 word, unsigned int shift)
  136. {
  137. return (word << shift) | (word >> (32 - shift));
  138. }
  139. /*
  140. * ror32 - rotate a 32-bit value right
  141. *
  142. * @word: value to rotate
  143. * @shift: bits to roll
  144. */
  145. static inline __u32 ror32(__u32 word, unsigned int shift)
  146. {
  147. return (word >> shift) | (word << (32 - shift));
  148. }
  149. #endif