bitops.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 (most-significant) bit set
  38. * @x: the word to search
  39. *
  40. * This is defined the same way as ffs.
  41. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  42. */
  43. static inline int generic_fls(int x)
  44. {
  45. int r = 32;
  46. if (!x)
  47. return 0;
  48. if (!(x & 0xffff0000u)) {
  49. x <<= 16;
  50. r -= 16;
  51. }
  52. if (!(x & 0xff000000u)) {
  53. x <<= 8;
  54. r -= 8;
  55. }
  56. if (!(x & 0xf0000000u)) {
  57. x <<= 4;
  58. r -= 4;
  59. }
  60. if (!(x & 0xc0000000u)) {
  61. x <<= 2;
  62. r -= 2;
  63. }
  64. if (!(x & 0x80000000u)) {
  65. x <<= 1;
  66. r -= 1;
  67. }
  68. return r;
  69. }
  70. /*
  71. * hweightN: returns the hamming weight (i.e. the number
  72. * of bits set) of a N-bit word
  73. */
  74. static inline unsigned int generic_hweight32(unsigned int w)
  75. {
  76. unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
  77. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  78. res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
  79. res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
  80. return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
  81. }
  82. static inline unsigned int generic_hweight16(unsigned int w)
  83. {
  84. unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
  85. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  86. res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
  87. return (res & 0x00FF) + ((res >> 8) & 0x00FF);
  88. }
  89. static inline unsigned int generic_hweight8(unsigned int w)
  90. {
  91. unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
  92. res = (res & 0x33) + ((res >> 2) & 0x33);
  93. return (res & 0x0F) + ((res >> 4) & 0x0F);
  94. }
  95. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  96. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  97. #include <asm/bitops.h>
  98. /* linux/include/asm-generic/bitops/non-atomic.h */
  99. #ifndef PLATFORM__SET_BIT
  100. # define __set_bit generic_set_bit
  101. #endif
  102. #ifndef PLATFORM__CLEAR_BIT
  103. # define __clear_bit generic_clear_bit
  104. #endif
  105. #ifndef PLATFORM_FFS
  106. # define ffs generic_ffs
  107. #endif
  108. #ifndef PLATFORM_FLS
  109. # define fls generic_fls
  110. #endif
  111. /**
  112. * __set_bit - Set a bit in memory
  113. * @nr: the bit to set
  114. * @addr: the address to start counting from
  115. *
  116. * Unlike set_bit(), this function is non-atomic and may be reordered.
  117. * If it's called on the same region of memory simultaneously, the effect
  118. * may be that only one operation succeeds.
  119. */
  120. static inline void generic_set_bit(int nr, volatile unsigned long *addr)
  121. {
  122. unsigned long mask = BIT_MASK(nr);
  123. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  124. *p |= mask;
  125. }
  126. static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
  127. {
  128. unsigned long mask = BIT_MASK(nr);
  129. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  130. *p &= ~mask;
  131. }
  132. #endif