bitops.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Bitops Module
  3. *
  4. * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
  5. *
  6. * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
  7. *
  8. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  9. * See the COPYING.LIB file in the top-level directory.
  10. */
  11. #ifndef BITOPS_H
  12. #define BITOPS_H
  13. #include "qemu-common.h"
  14. #define BITS_PER_BYTE CHAR_BIT
  15. #define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
  16. #define BIT(nr) (1UL << (nr))
  17. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  18. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  19. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  20. /**
  21. * bitops_ffs - find first bit in word.
  22. * @word: The word to search
  23. *
  24. * Undefined if no bit exists, so code should check against 0 first.
  25. */
  26. static unsigned long bitops_ffsl(unsigned long word)
  27. {
  28. int num = 0;
  29. #if LONG_MAX > 0x7FFFFFFF
  30. if ((word & 0xffffffff) == 0) {
  31. num += 32;
  32. word >>= 32;
  33. }
  34. #endif
  35. if ((word & 0xffff) == 0) {
  36. num += 16;
  37. word >>= 16;
  38. }
  39. if ((word & 0xff) == 0) {
  40. num += 8;
  41. word >>= 8;
  42. }
  43. if ((word & 0xf) == 0) {
  44. num += 4;
  45. word >>= 4;
  46. }
  47. if ((word & 0x3) == 0) {
  48. num += 2;
  49. word >>= 2;
  50. }
  51. if ((word & 0x1) == 0) {
  52. num += 1;
  53. }
  54. return num;
  55. }
  56. /**
  57. * bitops_fls - find last (most-significant) set bit in a long word
  58. * @word: the word to search
  59. *
  60. * Undefined if no set bit exists, so code should check against 0 first.
  61. */
  62. static inline unsigned long bitops_flsl(unsigned long word)
  63. {
  64. int num = BITS_PER_LONG - 1;
  65. #if LONG_MAX > 0x7FFFFFFF
  66. if (!(word & (~0ul << 32))) {
  67. num -= 32;
  68. word <<= 32;
  69. }
  70. #endif
  71. if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
  72. num -= 16;
  73. word <<= 16;
  74. }
  75. if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
  76. num -= 8;
  77. word <<= 8;
  78. }
  79. if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
  80. num -= 4;
  81. word <<= 4;
  82. }
  83. if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
  84. num -= 2;
  85. word <<= 2;
  86. }
  87. if (!(word & (~0ul << (BITS_PER_LONG-1))))
  88. num -= 1;
  89. return num;
  90. }
  91. /**
  92. * ffz - find first zero in word.
  93. * @word: The word to search
  94. *
  95. * Undefined if no zero exists, so code should check against ~0UL first.
  96. */
  97. static inline unsigned long ffz(unsigned long word)
  98. {
  99. return bitops_ffsl(~word);
  100. }
  101. /**
  102. * set_bit - Set a bit in memory
  103. * @nr: the bit to set
  104. * @addr: the address to start counting from
  105. */
  106. static inline void set_bit(int nr, volatile unsigned long *addr)
  107. {
  108. unsigned long mask = BIT_MASK(nr);
  109. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  110. *p |= mask;
  111. }
  112. /**
  113. * clear_bit - Clears a bit in memory
  114. * @nr: Bit to clear
  115. * @addr: Address to start counting from
  116. */
  117. static inline void clear_bit(int nr, volatile unsigned long *addr)
  118. {
  119. unsigned long mask = BIT_MASK(nr);
  120. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  121. *p &= ~mask;
  122. }
  123. /**
  124. * change_bit - Toggle a bit in memory
  125. * @nr: Bit to change
  126. * @addr: Address to start counting from
  127. */
  128. static inline void change_bit(int nr, volatile unsigned long *addr)
  129. {
  130. unsigned long mask = BIT_MASK(nr);
  131. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  132. *p ^= mask;
  133. }
  134. /**
  135. * test_and_set_bit - Set a bit and return its old value
  136. * @nr: Bit to set
  137. * @addr: Address to count from
  138. */
  139. static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
  140. {
  141. unsigned long mask = BIT_MASK(nr);
  142. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  143. unsigned long old = *p;
  144. *p = old | mask;
  145. return (old & mask) != 0;
  146. }
  147. /**
  148. * test_and_clear_bit - Clear a bit and return its old value
  149. * @nr: Bit to clear
  150. * @addr: Address to count from
  151. */
  152. static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
  153. {
  154. unsigned long mask = BIT_MASK(nr);
  155. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  156. unsigned long old = *p;
  157. *p = old & ~mask;
  158. return (old & mask) != 0;
  159. }
  160. /**
  161. * test_and_change_bit - Change a bit and return its old value
  162. * @nr: Bit to change
  163. * @addr: Address to count from
  164. */
  165. static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
  166. {
  167. unsigned long mask = BIT_MASK(nr);
  168. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  169. unsigned long old = *p;
  170. *p = old ^ mask;
  171. return (old & mask) != 0;
  172. }
  173. /**
  174. * test_bit - Determine whether a bit is set
  175. * @nr: bit number to test
  176. * @addr: Address to start counting from
  177. */
  178. static inline int test_bit(int nr, const volatile unsigned long *addr)
  179. {
  180. return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
  181. }
  182. /**
  183. * find_last_bit - find the last set bit in a memory region
  184. * @addr: The address to start the search at
  185. * @size: The maximum size to search
  186. *
  187. * Returns the bit number of the first set bit, or size.
  188. */
  189. unsigned long find_last_bit(const unsigned long *addr,
  190. unsigned long size);
  191. /**
  192. * find_next_bit - find the next set bit in a memory region
  193. * @addr: The address to base the search on
  194. * @offset: The bitnumber to start searching at
  195. * @size: The bitmap size in bits
  196. */
  197. unsigned long find_next_bit(const unsigned long *addr,
  198. unsigned long size, unsigned long offset);
  199. /**
  200. * find_next_zero_bit - find the next cleared bit in a memory region
  201. * @addr: The address to base the search on
  202. * @offset: The bitnumber to start searching at
  203. * @size: The bitmap size in bits
  204. */
  205. unsigned long find_next_zero_bit(const unsigned long *addr,
  206. unsigned long size,
  207. unsigned long offset);
  208. /**
  209. * find_first_bit - find the first set bit in a memory region
  210. * @addr: The address to start the search at
  211. * @size: The maximum size to search
  212. *
  213. * Returns the bit number of the first set bit.
  214. */
  215. static inline unsigned long find_first_bit(const unsigned long *addr,
  216. unsigned long size)
  217. {
  218. return find_next_bit(addr, size, 0);
  219. }
  220. /**
  221. * find_first_zero_bit - find the first cleared bit in a memory region
  222. * @addr: The address to start the search at
  223. * @size: The maximum size to search
  224. *
  225. * Returns the bit number of the first cleared bit.
  226. */
  227. static inline unsigned long find_first_zero_bit(const unsigned long *addr,
  228. unsigned long size)
  229. {
  230. return find_next_zero_bit(addr, size, 0);
  231. }
  232. static inline unsigned long hweight_long(unsigned long w)
  233. {
  234. unsigned long count;
  235. for (count = 0; w; w >>= 1) {
  236. count += w & 1;
  237. }
  238. return count;
  239. }
  240. #endif