bitops.h 3.7 KB

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