bitops.h 11 KB

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