bitops.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * hweightN: returns the hamming weight (i.e. the number
  38. * of bits set) of a N-bit word
  39. */
  40. static inline unsigned int generic_hweight32(unsigned int w)
  41. {
  42. unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
  43. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  44. res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
  45. res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
  46. return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
  47. }
  48. static inline unsigned int generic_hweight16(unsigned int w)
  49. {
  50. unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
  51. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  52. res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
  53. return (res & 0x00FF) + ((res >> 8) & 0x00FF);
  54. }
  55. static inline unsigned int generic_hweight8(unsigned int w)
  56. {
  57. unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
  58. res = (res & 0x33) + ((res >> 2) & 0x33);
  59. return (res & 0x0F) + ((res >> 4) & 0x0F);
  60. }
  61. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  62. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  63. #include <asm/bitops.h>
  64. /* linux/include/asm-generic/bitops/non-atomic.h */
  65. #ifndef __set_bit
  66. # define __set_bit generic_set_bit
  67. #endif
  68. #ifndef __clear_bit
  69. # define __clear_bit generic_clear_bit
  70. #endif
  71. /**
  72. * __set_bit - Set a bit in memory
  73. * @nr: the bit to set
  74. * @addr: the address to start counting from
  75. *
  76. * Unlike set_bit(), this function is non-atomic and may be reordered.
  77. * If it's called on the same region of memory simultaneously, the effect
  78. * may be that only one operation succeeds.
  79. */
  80. static inline void generic_set_bit(int nr, volatile unsigned long *addr)
  81. {
  82. unsigned long mask = BIT_MASK(nr);
  83. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  84. *p |= mask;
  85. }
  86. static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
  87. {
  88. unsigned long mask = BIT_MASK(nr);
  89. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  90. *p &= ~mask;
  91. }
  92. #endif