bitops.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /*
  78. * hweightN: returns the hamming weight (i.e. the number
  79. * of bits set) of a N-bit word
  80. */
  81. static inline unsigned int generic_hweight32(unsigned int w)
  82. {
  83. unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
  84. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  85. res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
  86. res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
  87. return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
  88. }
  89. static inline unsigned int generic_hweight16(unsigned int w)
  90. {
  91. unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
  92. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  93. res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
  94. return (res & 0x00FF) + ((res >> 8) & 0x00FF);
  95. }
  96. static inline unsigned int generic_hweight8(unsigned int w)
  97. {
  98. unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
  99. res = (res & 0x33) + ((res >> 2) & 0x33);
  100. return (res & 0x0F) + ((res >> 4) & 0x0F);
  101. }
  102. static inline unsigned long generic_hweight64(__u64 w)
  103. {
  104. #if BITS_PER_LONG < 64
  105. return generic_hweight32((unsigned int)(w >> 32)) +
  106. generic_hweight32((unsigned int)w);
  107. #else
  108. u64 res;
  109. res = (w & 0x5555555555555555ul) + ((w >> 1) & 0x5555555555555555ul);
  110. res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
  111. res = (res & 0x0F0F0F0F0F0F0F0Ful) + ((res >> 4) & 0x0F0F0F0F0F0F0F0Ful);
  112. res = (res & 0x00FF00FF00FF00FFul) + ((res >> 8) & 0x00FF00FF00FF00FFul);
  113. res = (res & 0x0000FFFF0000FFFFul) + ((res >> 16) & 0x0000FFFF0000FFFFul);
  114. return (res & 0x00000000FFFFFFFFul) + ((res >> 32) & 0x00000000FFFFFFFFul);
  115. #endif
  116. }
  117. static inline unsigned long hweight_long(unsigned long w)
  118. {
  119. return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
  120. }
  121. /*
  122. * rol32 - rotate a 32-bit value left
  123. *
  124. * @word: value to rotate
  125. * @shift: bits to roll
  126. */
  127. static inline __u32 rol32(__u32 word, unsigned int shift)
  128. {
  129. return (word << shift) | (word >> (32 - shift));
  130. }
  131. /*
  132. * ror32 - rotate a 32-bit value right
  133. *
  134. * @word: value to rotate
  135. * @shift: bits to roll
  136. */
  137. static inline __u32 ror32(__u32 word, unsigned int shift)
  138. {
  139. return (word >> shift) | (word << (32 - shift));
  140. }
  141. #endif