bitops.h 11 KB

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