bitops.h 9.9 KB

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