bitops.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* asm/bitops.h for Linux/CRIS
  2. *
  3. * TODO: asm versions if speed is needed
  4. *
  5. * All bit operations return 0 if the bit was cleared before the
  6. * operation and != 0 if it was not.
  7. *
  8. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  9. */
  10. #ifndef _CRIS_BITOPS_H
  11. #define _CRIS_BITOPS_H
  12. /* Currently this is unsuitable for consumption outside the kernel. */
  13. #ifdef __KERNEL__
  14. #include <asm/arch/bitops.h>
  15. #include <asm/system.h>
  16. #include <linux/compiler.h>
  17. /*
  18. * Some hacks to defeat gcc over-optimizations..
  19. */
  20. struct __dummy { unsigned long a[100]; };
  21. #define ADDR (*(struct __dummy *) addr)
  22. #define CONST_ADDR (*(const struct __dummy *) addr)
  23. /*
  24. * set_bit - Atomically set a bit in memory
  25. * @nr: the bit to set
  26. * @addr: the address to start counting from
  27. *
  28. * This function is atomic and may not be reordered. See __set_bit()
  29. * if you do not require the atomic guarantees.
  30. * Note that @nr may be almost arbitrarily large; this function is not
  31. * restricted to acting on a single-word quantity.
  32. */
  33. #define set_bit(nr, addr) (void)test_and_set_bit(nr, addr)
  34. #define __set_bit(nr, addr) (void)__test_and_set_bit(nr, addr)
  35. /*
  36. * clear_bit - Clears a bit in memory
  37. * @nr: Bit to clear
  38. * @addr: Address to start counting from
  39. *
  40. * clear_bit() is atomic and may not be reordered. However, it does
  41. * not contain a memory barrier, so if it is used for locking purposes,
  42. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  43. * in order to ensure changes are visible on other processors.
  44. */
  45. #define clear_bit(nr, addr) (void)test_and_clear_bit(nr, addr)
  46. #define __clear_bit(nr, addr) (void)__test_and_clear_bit(nr, addr)
  47. /*
  48. * change_bit - Toggle a bit in memory
  49. * @nr: Bit to change
  50. * @addr: Address to start counting from
  51. *
  52. * change_bit() is atomic and may not be reordered.
  53. * Note that @nr may be almost arbitrarily large; this function is not
  54. * restricted to acting on a single-word quantity.
  55. */
  56. #define change_bit(nr, addr) (void)test_and_change_bit(nr, addr)
  57. /*
  58. * __change_bit - Toggle a bit in memory
  59. * @nr: the bit to change
  60. * @addr: the address to start counting from
  61. *
  62. * Unlike change_bit(), this function is non-atomic and may be reordered.
  63. * If it's called on the same region of memory simultaneously, the effect
  64. * may be that only one operation succeeds.
  65. */
  66. #define __change_bit(nr, addr) (void)__test_and_change_bit(nr, addr)
  67. /**
  68. * test_and_set_bit - Set a bit and return its old value
  69. * @nr: Bit to set
  70. * @addr: Address to count from
  71. *
  72. * This operation is atomic and cannot be reordered.
  73. * It also implies a memory barrier.
  74. */
  75. extern inline int test_and_set_bit(int nr, void *addr)
  76. {
  77. unsigned int mask, retval;
  78. unsigned long flags;
  79. unsigned int *adr = (unsigned int *)addr;
  80. adr += nr >> 5;
  81. mask = 1 << (nr & 0x1f);
  82. local_save_flags(flags);
  83. local_irq_disable();
  84. retval = (mask & *adr) != 0;
  85. *adr |= mask;
  86. local_irq_restore(flags);
  87. return retval;
  88. }
  89. extern inline int __test_and_set_bit(int nr, void *addr)
  90. {
  91. unsigned int mask, retval;
  92. unsigned int *adr = (unsigned int *)addr;
  93. adr += nr >> 5;
  94. mask = 1 << (nr & 0x1f);
  95. retval = (mask & *adr) != 0;
  96. *adr |= mask;
  97. return retval;
  98. }
  99. /*
  100. * clear_bit() doesn't provide any barrier for the compiler.
  101. */
  102. #define smp_mb__before_clear_bit() barrier()
  103. #define smp_mb__after_clear_bit() barrier()
  104. /**
  105. * test_and_clear_bit - Clear a bit and return its old value
  106. * @nr: Bit to clear
  107. * @addr: Address to count from
  108. *
  109. * This operation is atomic and cannot be reordered.
  110. * It also implies a memory barrier.
  111. */
  112. extern inline int test_and_clear_bit(int nr, void *addr)
  113. {
  114. unsigned int mask, retval;
  115. unsigned long flags;
  116. unsigned int *adr = (unsigned int *)addr;
  117. adr += nr >> 5;
  118. mask = 1 << (nr & 0x1f);
  119. local_save_flags(flags);
  120. local_irq_disable();
  121. retval = (mask & *adr) != 0;
  122. *adr &= ~mask;
  123. local_irq_restore(flags);
  124. return retval;
  125. }
  126. /**
  127. * __test_and_clear_bit - Clear a bit and return its old value
  128. * @nr: Bit to clear
  129. * @addr: Address to count from
  130. *
  131. * This operation is non-atomic and can be reordered.
  132. * If two examples of this operation race, one can appear to succeed
  133. * but actually fail. You must protect multiple accesses with a lock.
  134. */
  135. extern inline int __test_and_clear_bit(int nr, void *addr)
  136. {
  137. unsigned int mask, retval;
  138. unsigned int *adr = (unsigned int *)addr;
  139. adr += nr >> 5;
  140. mask = 1 << (nr & 0x1f);
  141. retval = (mask & *adr) != 0;
  142. *adr &= ~mask;
  143. return retval;
  144. }
  145. /**
  146. * test_and_change_bit - Change a bit and return its old value
  147. * @nr: Bit to change
  148. * @addr: Address to count from
  149. *
  150. * This operation is atomic and cannot be reordered.
  151. * It also implies a memory barrier.
  152. */
  153. extern inline int test_and_change_bit(int nr, void *addr)
  154. {
  155. unsigned int mask, retval;
  156. unsigned long flags;
  157. unsigned int *adr = (unsigned int *)addr;
  158. adr += nr >> 5;
  159. mask = 1 << (nr & 0x1f);
  160. local_save_flags(flags);
  161. local_irq_disable();
  162. retval = (mask & *adr) != 0;
  163. *adr ^= mask;
  164. local_irq_restore(flags);
  165. return retval;
  166. }
  167. /* WARNING: non atomic and it can be reordered! */
  168. extern inline int __test_and_change_bit(int nr, void *addr)
  169. {
  170. unsigned int mask, retval;
  171. unsigned int *adr = (unsigned int *)addr;
  172. adr += nr >> 5;
  173. mask = 1 << (nr & 0x1f);
  174. retval = (mask & *adr) != 0;
  175. *adr ^= mask;
  176. return retval;
  177. }
  178. /**
  179. * test_bit - Determine whether a bit is set
  180. * @nr: bit number to test
  181. * @addr: Address to start counting from
  182. *
  183. * This routine doesn't need to be atomic.
  184. */
  185. extern inline int test_bit(int nr, const void *addr)
  186. {
  187. unsigned int mask;
  188. unsigned int *adr = (unsigned int *)addr;
  189. adr += nr >> 5;
  190. mask = 1 << (nr & 0x1f);
  191. return ((mask & *adr) != 0);
  192. }
  193. /*
  194. * Find-bit routines..
  195. */
  196. /*
  197. * Since we define it "external", it collides with the built-in
  198. * definition, which doesn't have the same semantics. We don't want to
  199. * use -fno-builtin, so just hide the name ffs.
  200. */
  201. #define ffs kernel_ffs
  202. /*
  203. * fls: find last bit set.
  204. */
  205. #define fls(x) generic_fls(x)
  206. /*
  207. * hweightN - returns the hamming weight of a N-bit word
  208. * @x: the word to weigh
  209. *
  210. * The Hamming Weight of a number is the total number of bits set in it.
  211. */
  212. #define hweight32(x) generic_hweight32(x)
  213. #define hweight16(x) generic_hweight16(x)
  214. #define hweight8(x) generic_hweight8(x)
  215. /**
  216. * find_next_zero_bit - find the first zero bit in a memory region
  217. * @addr: The address to base the search on
  218. * @offset: The bitnumber to start searching at
  219. * @size: The maximum size to search
  220. */
  221. extern inline int find_next_zero_bit (void * addr, int size, int offset)
  222. {
  223. unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
  224. unsigned long result = offset & ~31UL;
  225. unsigned long tmp;
  226. if (offset >= size)
  227. return size;
  228. size -= result;
  229. offset &= 31UL;
  230. if (offset) {
  231. tmp = *(p++);
  232. tmp |= ~0UL >> (32-offset);
  233. if (size < 32)
  234. goto found_first;
  235. if (~tmp)
  236. goto found_middle;
  237. size -= 32;
  238. result += 32;
  239. }
  240. while (size & ~31UL) {
  241. if (~(tmp = *(p++)))
  242. goto found_middle;
  243. result += 32;
  244. size -= 32;
  245. }
  246. if (!size)
  247. return result;
  248. tmp = *p;
  249. found_first:
  250. tmp |= ~0UL >> size;
  251. found_middle:
  252. return result + ffz(tmp);
  253. }
  254. /**
  255. * find_next_bit - find the first set bit in a memory region
  256. * @addr: The address to base the search on
  257. * @offset: The bitnumber to start searching at
  258. * @size: The maximum size to search
  259. */
  260. static __inline__ int find_next_bit(void *addr, int size, int offset)
  261. {
  262. unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
  263. unsigned long result = offset & ~31UL;
  264. unsigned long tmp;
  265. if (offset >= size)
  266. return size;
  267. size -= result;
  268. offset &= 31UL;
  269. if (offset) {
  270. tmp = *(p++);
  271. tmp &= (~0UL << offset);
  272. if (size < 32)
  273. goto found_first;
  274. if (tmp)
  275. goto found_middle;
  276. size -= 32;
  277. result += 32;
  278. }
  279. while (size & ~31UL) {
  280. if ((tmp = *(p++)))
  281. goto found_middle;
  282. result += 32;
  283. size -= 32;
  284. }
  285. if (!size)
  286. return result;
  287. tmp = *p;
  288. found_first:
  289. tmp &= (~0UL >> (32 - size));
  290. if (tmp == 0UL) /* Are any bits set? */
  291. return result + size; /* Nope. */
  292. found_middle:
  293. return result + __ffs(tmp);
  294. }
  295. /**
  296. * find_first_zero_bit - find the first zero bit in a memory region
  297. * @addr: The address to start the search at
  298. * @size: The maximum size to search
  299. *
  300. * Returns the bit-number of the first zero bit, not the number of the byte
  301. * containing a bit.
  302. */
  303. #define find_first_zero_bit(addr, size) \
  304. find_next_zero_bit((addr), (size), 0)
  305. #define find_first_bit(addr, size) \
  306. find_next_bit((addr), (size), 0)
  307. #define ext2_set_bit test_and_set_bit
  308. #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a)
  309. #define ext2_clear_bit test_and_clear_bit
  310. #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a)
  311. #define ext2_test_bit test_bit
  312. #define ext2_find_first_zero_bit find_first_zero_bit
  313. #define ext2_find_next_zero_bit find_next_zero_bit
  314. /* Bitmap functions for the minix filesystem. */
  315. #define minix_set_bit(nr,addr) test_and_set_bit(nr,addr)
  316. #define minix_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
  317. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  318. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  319. extern inline int sched_find_first_bit(unsigned long *b)
  320. {
  321. if (unlikely(b[0]))
  322. return __ffs(b[0]);
  323. if (unlikely(b[1]))
  324. return __ffs(b[1]) + 32;
  325. if (unlikely(b[2]))
  326. return __ffs(b[2]) + 64;
  327. if (unlikely(b[3]))
  328. return __ffs(b[3]) + 96;
  329. if (b[4])
  330. return __ffs(b[4]) + 128;
  331. return __ffs(b[5]) + 32 + 128;
  332. }
  333. #endif /* __KERNEL__ */
  334. #endif /* _CRIS_BITOPS_H */