bitops.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
  2. #define _ASM_GENERIC_BITOPS_NON_ATOMIC_H_
  3. #include <linux/types.h>
  4. #include <linux/bitops/find.h>
  5. #include <linux/bitops/hweight.h>
  6. #include <linux/kernel.h>
  7. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  8. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  9. #define BITS_PER_BYTE 8
  10. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  11. /**
  12. * __set_bit - Set a bit in memory
  13. * @nr: the bit to set
  14. * @addr: the address to start counting from
  15. *
  16. * Unlike set_bit(), this function is non-atomic and may be reordered.
  17. * If it's called on the same region of memory simultaneously, the effect
  18. * may be that only one operation succeeds.
  19. */
  20. static inline void __set_bit(int nr, volatile unsigned long *addr)
  21. {
  22. unsigned long mask = BIT_MASK(nr);
  23. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  24. *p |= mask;
  25. }
  26. static inline void __clear_bit(int nr, volatile unsigned long *addr)
  27. {
  28. unsigned long mask = BIT_MASK(nr);
  29. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  30. *p &= ~mask;
  31. }
  32. /**
  33. * __change_bit - Toggle a bit in memory
  34. * @nr: the bit to change
  35. * @addr: the address to start counting from
  36. *
  37. * Unlike change_bit(), this function is non-atomic and may be reordered.
  38. * If it's called on the same region of memory simultaneously, the effect
  39. * may be that only one operation succeeds.
  40. */
  41. static inline void __change_bit(int nr, volatile unsigned long *addr)
  42. {
  43. unsigned long mask = BIT_MASK(nr);
  44. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  45. *p ^= mask;
  46. }
  47. /**
  48. * __test_and_set_bit - Set a bit and return its old value
  49. * @nr: Bit to set
  50. * @addr: Address to count from
  51. *
  52. * This operation is non-atomic and can be reordered.
  53. * If two examples of this operation race, one can appear to succeed
  54. * but actually fail. You must protect multiple accesses with a lock.
  55. */
  56. static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
  57. {
  58. unsigned long mask = BIT_MASK(nr);
  59. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  60. unsigned long old = *p;
  61. *p = old | mask;
  62. return (old & mask) != 0;
  63. }
  64. /**
  65. * __test_and_clear_bit - Clear a bit and return its old value
  66. * @nr: Bit to clear
  67. * @addr: Address to count from
  68. *
  69. * This operation is non-atomic and can be reordered.
  70. * If two examples of this operation race, one can appear to succeed
  71. * but actually fail. You must protect multiple accesses with a lock.
  72. */
  73. static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
  74. {
  75. unsigned long mask = BIT_MASK(nr);
  76. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  77. unsigned long old = *p;
  78. *p = old & ~mask;
  79. return (old & mask) != 0;
  80. }
  81. /* WARNING: non atomic and it can be reordered! */
  82. static inline int __test_and_change_bit(int nr,
  83. volatile unsigned long *addr)
  84. {
  85. unsigned long mask = BIT_MASK(nr);
  86. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  87. unsigned long old = *p;
  88. *p = old ^ mask;
  89. return (old & mask) != 0;
  90. }
  91. /**
  92. * test_bit - Determine whether a bit is set
  93. * @nr: bit number to test
  94. * @addr: Address to start counting from
  95. */
  96. static inline int test_bit(int nr, const volatile unsigned long *addr)
  97. {
  98. return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
  99. }
  100. /**
  101. * __ffs - find first bit in word.
  102. * @word: The word to search
  103. *
  104. * Undefined if no bit exists, so code should check against 0 first.
  105. */
  106. static inline unsigned long __ffs(unsigned long word)
  107. {
  108. int num = 0;
  109. if ((word & 0xffffffff) == 0) {
  110. num += 32;
  111. word >>= 32;
  112. }
  113. if ((word & 0xffff) == 0) {
  114. num += 16;
  115. word >>= 16;
  116. }
  117. if ((word & 0xff) == 0) {
  118. num += 8;
  119. word >>= 8;
  120. }
  121. if ((word & 0xf) == 0) {
  122. num += 4;
  123. word >>= 4;
  124. }
  125. if ((word & 0x3) == 0) {
  126. num += 2;
  127. word >>= 2;
  128. }
  129. if ((word & 0x1) == 0)
  130. num += 1;
  131. return num;
  132. }
  133. unsigned long find_next_bit(const unsigned long *addr,
  134. unsigned long size,
  135. unsigned long offset);
  136. static inline unsigned long hweight_long(unsigned long w)
  137. {
  138. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  139. }
  140. #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */