bitops.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright 1995, Russell King.
  3. *
  4. * Based on the arm32 version by RMK (and others). Their copyrights apply to
  5. * Those parts.
  6. * Modified for arm26 by Ian Molton on 25/11/04
  7. *
  8. * bit 0 is the LSB of an "unsigned long" quantity.
  9. *
  10. * Please note that the code in this file should never be included
  11. * from user space. Many of these are not implemented in assembler
  12. * since they would be too costly. Also, they require privileged
  13. * instructions (which are not available from user mode) to ensure
  14. * that they are atomic.
  15. */
  16. #ifndef __ASM_ARM_BITOPS_H
  17. #define __ASM_ARM_BITOPS_H
  18. #ifdef __KERNEL__
  19. #include <linux/compiler.h>
  20. #include <asm/system.h>
  21. #define smp_mb__before_clear_bit() do { } while (0)
  22. #define smp_mb__after_clear_bit() do { } while (0)
  23. /*
  24. * These functions are the basis of our bit ops.
  25. *
  26. * First, the atomic bitops. These use native endian.
  27. */
  28. static inline void ____atomic_set_bit(unsigned int bit, volatile unsigned long *p)
  29. {
  30. unsigned long flags;
  31. unsigned long mask = 1UL << (bit & 31);
  32. p += bit >> 5;
  33. local_irq_save(flags);
  34. *p |= mask;
  35. local_irq_restore(flags);
  36. }
  37. static inline void ____atomic_clear_bit(unsigned int bit, volatile unsigned long *p)
  38. {
  39. unsigned long flags;
  40. unsigned long mask = 1UL << (bit & 31);
  41. p += bit >> 5;
  42. local_irq_save(flags);
  43. *p &= ~mask;
  44. local_irq_restore(flags);
  45. }
  46. static inline void ____atomic_change_bit(unsigned int bit, volatile unsigned long *p)
  47. {
  48. unsigned long flags;
  49. unsigned long mask = 1UL << (bit & 31);
  50. p += bit >> 5;
  51. local_irq_save(flags);
  52. *p ^= mask;
  53. local_irq_restore(flags);
  54. }
  55. static inline int
  56. ____atomic_test_and_set_bit(unsigned int bit, volatile unsigned long *p)
  57. {
  58. unsigned long flags;
  59. unsigned int res;
  60. unsigned long mask = 1UL << (bit & 31);
  61. p += bit >> 5;
  62. local_irq_save(flags);
  63. res = *p;
  64. *p = res | mask;
  65. local_irq_restore(flags);
  66. return res & mask;
  67. }
  68. static inline int
  69. ____atomic_test_and_clear_bit(unsigned int bit, volatile unsigned long *p)
  70. {
  71. unsigned long flags;
  72. unsigned int res;
  73. unsigned long mask = 1UL << (bit & 31);
  74. p += bit >> 5;
  75. local_irq_save(flags);
  76. res = *p;
  77. *p = res & ~mask;
  78. local_irq_restore(flags);
  79. return res & mask;
  80. }
  81. static inline int
  82. ____atomic_test_and_change_bit(unsigned int bit, volatile unsigned long *p)
  83. {
  84. unsigned long flags;
  85. unsigned int res;
  86. unsigned long mask = 1UL << (bit & 31);
  87. p += bit >> 5;
  88. local_irq_save(flags);
  89. res = *p;
  90. *p = res ^ mask;
  91. local_irq_restore(flags);
  92. return res & mask;
  93. }
  94. /*
  95. * Now the non-atomic variants. We let the compiler handle all
  96. * optimisations for these. These are all _native_ endian.
  97. */
  98. static inline void __set_bit(int nr, volatile unsigned long *p)
  99. {
  100. p[nr >> 5] |= (1UL << (nr & 31));
  101. }
  102. static inline void __clear_bit(int nr, volatile unsigned long *p)
  103. {
  104. p[nr >> 5] &= ~(1UL << (nr & 31));
  105. }
  106. static inline void __change_bit(int nr, volatile unsigned long *p)
  107. {
  108. p[nr >> 5] ^= (1UL << (nr & 31));
  109. }
  110. static inline int __test_and_set_bit(int nr, volatile unsigned long *p)
  111. {
  112. unsigned long oldval, mask = 1UL << (nr & 31);
  113. p += nr >> 5;
  114. oldval = *p;
  115. *p = oldval | mask;
  116. return oldval & mask;
  117. }
  118. static inline int __test_and_clear_bit(int nr, volatile unsigned long *p)
  119. {
  120. unsigned long oldval, mask = 1UL << (nr & 31);
  121. p += nr >> 5;
  122. oldval = *p;
  123. *p = oldval & ~mask;
  124. return oldval & mask;
  125. }
  126. static inline int __test_and_change_bit(int nr, volatile unsigned long *p)
  127. {
  128. unsigned long oldval, mask = 1UL << (nr & 31);
  129. p += nr >> 5;
  130. oldval = *p;
  131. *p = oldval ^ mask;
  132. return oldval & mask;
  133. }
  134. /*
  135. * This routine doesn't need to be atomic.
  136. */
  137. static inline int __test_bit(int nr, const volatile unsigned long * p)
  138. {
  139. return (p[nr >> 5] >> (nr & 31)) & 1UL;
  140. }
  141. /*
  142. * Little endian assembly bitops. nr = 0 -> byte 0 bit 0.
  143. */
  144. extern void _set_bit_le(int nr, volatile unsigned long * p);
  145. extern void _clear_bit_le(int nr, volatile unsigned long * p);
  146. extern void _change_bit_le(int nr, volatile unsigned long * p);
  147. extern int _test_and_set_bit_le(int nr, volatile unsigned long * p);
  148. extern int _test_and_clear_bit_le(int nr, volatile unsigned long * p);
  149. extern int _test_and_change_bit_le(int nr, volatile unsigned long * p);
  150. extern int _find_first_zero_bit_le(void * p, unsigned size);
  151. extern int _find_next_zero_bit_le(void * p, int size, int offset);
  152. extern int _find_first_bit_le(const unsigned long *p, unsigned size);
  153. extern int _find_next_bit_le(const unsigned long *p, int size, int offset);
  154. /*
  155. * The __* form of bitops are non-atomic and may be reordered.
  156. */
  157. #define ATOMIC_BITOP_LE(name,nr,p) \
  158. (__builtin_constant_p(nr) ? \
  159. ____atomic_##name(nr, p) : \
  160. _##name##_le(nr,p))
  161. #define NONATOMIC_BITOP(name,nr,p) \
  162. (____nonatomic_##name(nr, p))
  163. /*
  164. * These are the little endian, atomic definitions.
  165. */
  166. #define set_bit(nr,p) ATOMIC_BITOP_LE(set_bit,nr,p)
  167. #define clear_bit(nr,p) ATOMIC_BITOP_LE(clear_bit,nr,p)
  168. #define change_bit(nr,p) ATOMIC_BITOP_LE(change_bit,nr,p)
  169. #define test_and_set_bit(nr,p) ATOMIC_BITOP_LE(test_and_set_bit,nr,p)
  170. #define test_and_clear_bit(nr,p) ATOMIC_BITOP_LE(test_and_clear_bit,nr,p)
  171. #define test_and_change_bit(nr,p) ATOMIC_BITOP_LE(test_and_change_bit,nr,p)
  172. #define test_bit(nr,p) __test_bit(nr,p)
  173. #define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz)
  174. #define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off)
  175. #define find_first_bit(p,sz) _find_first_bit_le(p,sz)
  176. #define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off)
  177. #define WORD_BITOFF_TO_LE(x) ((x))
  178. /*
  179. * ffz = Find First Zero in word. Undefined if no zero exists,
  180. * so code should check against ~0UL first..
  181. */
  182. static inline unsigned long ffz(unsigned long word)
  183. {
  184. int k;
  185. word = ~word;
  186. k = 31;
  187. if (word & 0x0000ffff) { k -= 16; word <<= 16; }
  188. if (word & 0x00ff0000) { k -= 8; word <<= 8; }
  189. if (word & 0x0f000000) { k -= 4; word <<= 4; }
  190. if (word & 0x30000000) { k -= 2; word <<= 2; }
  191. if (word & 0x40000000) { k -= 1; }
  192. return k;
  193. }
  194. /*
  195. * ffz = Find First Zero in word. Undefined if no zero exists,
  196. * so code should check against ~0UL first..
  197. */
  198. static inline unsigned long __ffs(unsigned long word)
  199. {
  200. int k;
  201. k = 31;
  202. if (word & 0x0000ffff) { k -= 16; word <<= 16; }
  203. if (word & 0x00ff0000) { k -= 8; word <<= 8; }
  204. if (word & 0x0f000000) { k -= 4; word <<= 4; }
  205. if (word & 0x30000000) { k -= 2; word <<= 2; }
  206. if (word & 0x40000000) { k -= 1; }
  207. return k;
  208. }
  209. /*
  210. * fls: find last bit set.
  211. */
  212. #define fls(x) generic_fls(x)
  213. /*
  214. * ffs: find first bit set. This is defined the same way as
  215. * the libc and compiler builtin ffs routines, therefore
  216. * differs in spirit from the above ffz (man ffs).
  217. */
  218. #define ffs(x) generic_ffs(x)
  219. /*
  220. * Find first bit set in a 168-bit bitmap, where the first
  221. * 128 bits are unlikely to be set.
  222. */
  223. static inline int sched_find_first_bit(unsigned long *b)
  224. {
  225. unsigned long v;
  226. unsigned int off;
  227. for (off = 0; v = b[off], off < 4; off++) {
  228. if (unlikely(v))
  229. break;
  230. }
  231. return __ffs(v) + off * 32;
  232. }
  233. /*
  234. * hweightN: returns the hamming weight (i.e. the number
  235. * of bits set) of a N-bit word
  236. */
  237. #define hweight32(x) generic_hweight32(x)
  238. #define hweight16(x) generic_hweight16(x)
  239. #define hweight8(x) generic_hweight8(x)
  240. /*
  241. * Ext2 is defined to use little-endian byte ordering.
  242. * These do not need to be atomic.
  243. */
  244. #define ext2_set_bit(nr,p) \
  245. __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  246. #define ext2_set_bit_atomic(lock,nr,p) \
  247. test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  248. #define ext2_clear_bit(nr,p) \
  249. __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  250. #define ext2_clear_bit_atomic(lock,nr,p) \
  251. test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  252. #define ext2_test_bit(nr,p) \
  253. __test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  254. #define ext2_find_first_zero_bit(p,sz) \
  255. _find_first_zero_bit_le(p,sz)
  256. #define ext2_find_next_zero_bit(p,sz,off) \
  257. _find_next_zero_bit_le(p,sz,off)
  258. /*
  259. * Minix is defined to use little-endian byte ordering.
  260. * These do not need to be atomic.
  261. */
  262. #define minix_set_bit(nr,p) \
  263. __set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  264. #define minix_test_bit(nr,p) \
  265. __test_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  266. #define minix_test_and_set_bit(nr,p) \
  267. __test_and_set_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  268. #define minix_test_and_clear_bit(nr,p) \
  269. __test_and_clear_bit(WORD_BITOFF_TO_LE(nr), (unsigned long *)(p))
  270. #define minix_find_first_zero_bit(p,sz) \
  271. _find_first_zero_bit_le(p,sz)
  272. #endif /* __KERNEL__ */
  273. #endif /* _ARM_BITOPS_H */