bitops.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #ifndef _ASM_IA64_BITOPS_H
  2. #define _ASM_IA64_BITOPS_H
  3. /*
  4. * Copyright (C) 1998-2003 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. *
  7. * 02/06/02 find_next_bit() and find_first_bit() added from Erich Focht's ia64 O(1)
  8. * scheduler patch
  9. */
  10. #include <linux/compiler.h>
  11. #include <linux/types.h>
  12. #include <asm/bitops.h>
  13. #include <asm/intrinsics.h>
  14. /**
  15. * set_bit - Atomically set a bit in memory
  16. * @nr: the bit to set
  17. * @addr: the address to start counting from
  18. *
  19. * This function is atomic and may not be reordered. See __set_bit()
  20. * if you do not require the atomic guarantees.
  21. * Note that @nr may be almost arbitrarily large; this function is not
  22. * restricted to acting on a single-word quantity.
  23. *
  24. * The address must be (at least) "long" aligned.
  25. * Note that there are driver (e.g., eepro100) which use these operations to operate on
  26. * hw-defined data-structures, so we can't easily change these operations to force a
  27. * bigger alignment.
  28. *
  29. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  30. */
  31. static __inline__ void
  32. set_bit (int nr, volatile void *addr)
  33. {
  34. __u32 bit, old, new;
  35. volatile __u32 *m;
  36. CMPXCHG_BUGCHECK_DECL
  37. m = (volatile __u32 *) addr + (nr >> 5);
  38. bit = 1 << (nr & 31);
  39. do {
  40. CMPXCHG_BUGCHECK(m);
  41. old = *m;
  42. new = old | bit;
  43. } while (cmpxchg_acq(m, old, new) != old);
  44. }
  45. /**
  46. * __set_bit - Set a bit in memory
  47. * @nr: the bit to set
  48. * @addr: the address to start counting from
  49. *
  50. * Unlike set_bit(), this function is non-atomic and may be reordered.
  51. * If it's called on the same region of memory simultaneously, the effect
  52. * may be that only one operation succeeds.
  53. */
  54. static __inline__ void
  55. __set_bit (int nr, volatile void *addr)
  56. {
  57. *((__u32 *) addr + (nr >> 5)) |= (1 << (nr & 31));
  58. }
  59. /*
  60. * clear_bit() has "acquire" semantics.
  61. */
  62. #define smp_mb__before_clear_bit() smp_mb()
  63. #define smp_mb__after_clear_bit() do { /* skip */; } while (0)
  64. /**
  65. * clear_bit - Clears a bit in memory
  66. * @nr: Bit to clear
  67. * @addr: Address to start counting from
  68. *
  69. * clear_bit() is atomic and may not be reordered. However, it does
  70. * not contain a memory barrier, so if it is used for locking purposes,
  71. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  72. * in order to ensure changes are visible on other processors.
  73. */
  74. static __inline__ void
  75. clear_bit (int nr, volatile void *addr)
  76. {
  77. __u32 mask, old, new;
  78. volatile __u32 *m;
  79. CMPXCHG_BUGCHECK_DECL
  80. m = (volatile __u32 *) addr + (nr >> 5);
  81. mask = ~(1 << (nr & 31));
  82. do {
  83. CMPXCHG_BUGCHECK(m);
  84. old = *m;
  85. new = old & mask;
  86. } while (cmpxchg_acq(m, old, new) != old);
  87. }
  88. /**
  89. * __clear_bit - Clears a bit in memory (non-atomic version)
  90. */
  91. static __inline__ void
  92. __clear_bit (int nr, volatile void *addr)
  93. {
  94. volatile __u32 *p = (__u32 *) addr + (nr >> 5);
  95. __u32 m = 1 << (nr & 31);
  96. *p &= ~m;
  97. }
  98. /**
  99. * change_bit - Toggle a bit in memory
  100. * @nr: Bit to clear
  101. * @addr: Address to start counting from
  102. *
  103. * change_bit() is atomic and may not be reordered.
  104. * Note that @nr may be almost arbitrarily large; this function is not
  105. * restricted to acting on a single-word quantity.
  106. */
  107. static __inline__ void
  108. change_bit (int nr, volatile void *addr)
  109. {
  110. __u32 bit, old, new;
  111. volatile __u32 *m;
  112. CMPXCHG_BUGCHECK_DECL
  113. m = (volatile __u32 *) addr + (nr >> 5);
  114. bit = (1 << (nr & 31));
  115. do {
  116. CMPXCHG_BUGCHECK(m);
  117. old = *m;
  118. new = old ^ bit;
  119. } while (cmpxchg_acq(m, old, new) != old);
  120. }
  121. /**
  122. * __change_bit - Toggle a bit in memory
  123. * @nr: the bit to set
  124. * @addr: the address to start counting from
  125. *
  126. * Unlike change_bit(), this function is non-atomic and may be reordered.
  127. * If it's called on the same region of memory simultaneously, the effect
  128. * may be that only one operation succeeds.
  129. */
  130. static __inline__ void
  131. __change_bit (int nr, volatile void *addr)
  132. {
  133. *((__u32 *) addr + (nr >> 5)) ^= (1 << (nr & 31));
  134. }
  135. /**
  136. * test_and_set_bit - Set a bit and return its old value
  137. * @nr: Bit to set
  138. * @addr: Address to count from
  139. *
  140. * This operation is atomic and cannot be reordered.
  141. * It also implies a memory barrier.
  142. */
  143. static __inline__ int
  144. test_and_set_bit (int nr, volatile void *addr)
  145. {
  146. __u32 bit, old, new;
  147. volatile __u32 *m;
  148. CMPXCHG_BUGCHECK_DECL
  149. m = (volatile __u32 *) addr + (nr >> 5);
  150. bit = 1 << (nr & 31);
  151. do {
  152. CMPXCHG_BUGCHECK(m);
  153. old = *m;
  154. new = old | bit;
  155. } while (cmpxchg_acq(m, old, new) != old);
  156. return (old & bit) != 0;
  157. }
  158. /**
  159. * __test_and_set_bit - Set a bit and return its old value
  160. * @nr: Bit to set
  161. * @addr: Address to count from
  162. *
  163. * This operation is non-atomic and can be reordered.
  164. * If two examples of this operation race, one can appear to succeed
  165. * but actually fail. You must protect multiple accesses with a lock.
  166. */
  167. static __inline__ int
  168. __test_and_set_bit (int nr, volatile void *addr)
  169. {
  170. __u32 *p = (__u32 *) addr + (nr >> 5);
  171. __u32 m = 1 << (nr & 31);
  172. int oldbitset = (*p & m) != 0;
  173. *p |= m;
  174. return oldbitset;
  175. }
  176. /**
  177. * test_and_clear_bit - Clear a bit and return its old value
  178. * @nr: Bit to set
  179. * @addr: Address to count from
  180. *
  181. * This operation is atomic and cannot be reordered.
  182. * It also implies a memory barrier.
  183. */
  184. static __inline__ int
  185. test_and_clear_bit (int nr, volatile void *addr)
  186. {
  187. __u32 mask, old, new;
  188. volatile __u32 *m;
  189. CMPXCHG_BUGCHECK_DECL
  190. m = (volatile __u32 *) addr + (nr >> 5);
  191. mask = ~(1 << (nr & 31));
  192. do {
  193. CMPXCHG_BUGCHECK(m);
  194. old = *m;
  195. new = old & mask;
  196. } while (cmpxchg_acq(m, old, new) != old);
  197. return (old & ~mask) != 0;
  198. }
  199. /**
  200. * __test_and_clear_bit - Clear a bit and return its old value
  201. * @nr: Bit to set
  202. * @addr: Address to count from
  203. *
  204. * This operation is non-atomic and can be reordered.
  205. * If two examples of this operation race, one can appear to succeed
  206. * but actually fail. You must protect multiple accesses with a lock.
  207. */
  208. static __inline__ int
  209. __test_and_clear_bit(int nr, volatile void * addr)
  210. {
  211. __u32 *p = (__u32 *) addr + (nr >> 5);
  212. __u32 m = 1 << (nr & 31);
  213. int oldbitset = *p & m;
  214. *p &= ~m;
  215. return oldbitset;
  216. }
  217. /**
  218. * test_and_change_bit - Change a bit and return its old value
  219. * @nr: Bit to set
  220. * @addr: Address to count from
  221. *
  222. * This operation is atomic and cannot be reordered.
  223. * It also implies a memory barrier.
  224. */
  225. static __inline__ int
  226. test_and_change_bit (int nr, volatile void *addr)
  227. {
  228. __u32 bit, old, new;
  229. volatile __u32 *m;
  230. CMPXCHG_BUGCHECK_DECL
  231. m = (volatile __u32 *) addr + (nr >> 5);
  232. bit = (1 << (nr & 31));
  233. do {
  234. CMPXCHG_BUGCHECK(m);
  235. old = *m;
  236. new = old ^ bit;
  237. } while (cmpxchg_acq(m, old, new) != old);
  238. return (old & bit) != 0;
  239. }
  240. /*
  241. * WARNING: non atomic version.
  242. */
  243. static __inline__ int
  244. __test_and_change_bit (int nr, void *addr)
  245. {
  246. __u32 old, bit = (1 << (nr & 31));
  247. __u32 *m = (__u32 *) addr + (nr >> 5);
  248. old = *m;
  249. *m = old ^ bit;
  250. return (old & bit) != 0;
  251. }
  252. static __inline__ int
  253. test_bit (int nr, const volatile void *addr)
  254. {
  255. return 1 & (((const volatile __u32 *) addr)[nr >> 5] >> (nr & 31));
  256. }
  257. /**
  258. * ffz - find the first zero bit in a long word
  259. * @x: The long word to find the bit in
  260. *
  261. * Returns the bit-number (0..63) of the first (least significant) zero bit. Undefined if
  262. * no zero exists, so code should check against ~0UL first...
  263. */
  264. static inline unsigned long
  265. ffz (unsigned long x)
  266. {
  267. unsigned long result;
  268. result = ia64_popcnt(x & (~x - 1));
  269. return result;
  270. }
  271. /**
  272. * __ffs - find first bit in word.
  273. * @x: The word to search
  274. *
  275. * Undefined if no bit exists, so code should check against 0 first.
  276. */
  277. static __inline__ unsigned long
  278. __ffs (unsigned long x)
  279. {
  280. unsigned long result;
  281. result = ia64_popcnt((x-1) & ~x);
  282. return result;
  283. }
  284. #ifdef __KERNEL__
  285. /*
  286. * Return bit number of last (most-significant) bit set. Undefined
  287. * for x==0. Bits are numbered from 0..63 (e.g., ia64_fls(9) == 3).
  288. */
  289. static inline unsigned long
  290. ia64_fls (unsigned long x)
  291. {
  292. long double d = x;
  293. long exp;
  294. exp = ia64_getf_exp(d);
  295. return exp - 0xffff;
  296. }
  297. /*
  298. * Find the last (most significant) bit set. Returns 0 for x==0 and
  299. * bits are numbered from 1..32 (e.g., fls(9) == 4).
  300. */
  301. static inline int
  302. fls (int t)
  303. {
  304. unsigned long x = t & 0xffffffffu;
  305. if (!x)
  306. return 0;
  307. x |= x >> 1;
  308. x |= x >> 2;
  309. x |= x >> 4;
  310. x |= x >> 8;
  311. x |= x >> 16;
  312. return ia64_popcnt(x);
  313. }
  314. #define fls64(x) generic_fls64(x)
  315. /*
  316. * ffs: find first bit set. This is defined the same way as the libc and compiler builtin
  317. * ffs routines, therefore differs in spirit from the above ffz (man ffs): it operates on
  318. * "int" values only and the result value is the bit number + 1. ffs(0) is defined to
  319. * return zero.
  320. */
  321. #define ffs(x) __builtin_ffs(x)
  322. /*
  323. * hweightN: returns the hamming weight (i.e. the number
  324. * of bits set) of a N-bit word
  325. */
  326. static __inline__ unsigned long
  327. hweight64 (unsigned long x)
  328. {
  329. unsigned long result;
  330. result = ia64_popcnt(x);
  331. return result;
  332. }
  333. #define hweight32(x) (unsigned int) hweight64((x) & 0xfffffffful)
  334. #define hweight16(x) (unsigned int) hweight64((x) & 0xfffful)
  335. #define hweight8(x) (unsigned int) hweight64((x) & 0xfful)
  336. #endif /* __KERNEL__ */
  337. extern int __find_next_zero_bit (const void *addr, unsigned long size,
  338. unsigned long offset);
  339. extern int __find_next_bit(const void *addr, unsigned long size,
  340. unsigned long offset);
  341. #define find_next_zero_bit(addr, size, offset) \
  342. __find_next_zero_bit((addr), (size), (offset))
  343. #define find_next_bit(addr, size, offset) \
  344. __find_next_bit((addr), (size), (offset))
  345. /*
  346. * The optimizer actually does good code for this case..
  347. */
  348. #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
  349. #define find_first_bit(addr, size) find_next_bit((addr), (size), 0)
  350. #ifdef __KERNEL__
  351. #define __clear_bit(nr, addr) clear_bit(nr, addr)
  352. #define ext2_set_bit test_and_set_bit
  353. #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a)
  354. #define ext2_clear_bit test_and_clear_bit
  355. #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a)
  356. #define ext2_test_bit test_bit
  357. #define ext2_find_first_zero_bit find_first_zero_bit
  358. #define ext2_find_next_zero_bit find_next_zero_bit
  359. /* Bitmap functions for the minix filesystem. */
  360. #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
  361. #define minix_set_bit(nr,addr) set_bit(nr,addr)
  362. #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
  363. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  364. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  365. static inline int
  366. sched_find_first_bit (unsigned long *b)
  367. {
  368. if (unlikely(b[0]))
  369. return __ffs(b[0]);
  370. if (unlikely(b[1]))
  371. return 64 + __ffs(b[1]);
  372. return __ffs(b[2]) + 128;
  373. }
  374. #endif /* __KERNEL__ */
  375. #endif /* _ASM_IA64_BITOPS_H */