bitops.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 clear a bit with release
  114. *
  115. * This is like clear_bit_unlock, but the implementation uses a store
  116. * with release semantics. See also __raw_spin_unlock().
  117. */
  118. static __inline__ void
  119. __clear_bit_unlock(int nr, volatile void *addr)
  120. {
  121. __u32 mask, new;
  122. volatile __u32 *m;
  123. m = (volatile __u32 *)addr + (nr >> 5);
  124. mask = ~(1 << (nr & 31));
  125. new = *m & mask;
  126. barrier();
  127. ia64_st4_rel_nta(m, new);
  128. }
  129. /**
  130. * __clear_bit - Clears a bit in memory (non-atomic version)
  131. */
  132. static __inline__ void
  133. __clear_bit (int nr, volatile void *addr)
  134. {
  135. volatile __u32 *p = (__u32 *) addr + (nr >> 5);
  136. __u32 m = 1 << (nr & 31);
  137. *p &= ~m;
  138. }
  139. /**
  140. * change_bit - Toggle a bit in memory
  141. * @nr: Bit to clear
  142. * @addr: Address to start counting from
  143. *
  144. * change_bit() is atomic and may not be reordered.
  145. * Note that @nr may be almost arbitrarily large; this function is not
  146. * restricted to acting on a single-word quantity.
  147. */
  148. static __inline__ void
  149. change_bit (int nr, volatile void *addr)
  150. {
  151. __u32 bit, old, new;
  152. volatile __u32 *m;
  153. CMPXCHG_BUGCHECK_DECL
  154. m = (volatile __u32 *) addr + (nr >> 5);
  155. bit = (1 << (nr & 31));
  156. do {
  157. CMPXCHG_BUGCHECK(m);
  158. old = *m;
  159. new = old ^ bit;
  160. } while (cmpxchg_acq(m, old, new) != old);
  161. }
  162. /**
  163. * __change_bit - Toggle a bit in memory
  164. * @nr: the bit to set
  165. * @addr: the address to start counting from
  166. *
  167. * Unlike change_bit(), this function is non-atomic and may be reordered.
  168. * If it's called on the same region of memory simultaneously, the effect
  169. * may be that only one operation succeeds.
  170. */
  171. static __inline__ void
  172. __change_bit (int nr, volatile void *addr)
  173. {
  174. *((__u32 *) addr + (nr >> 5)) ^= (1 << (nr & 31));
  175. }
  176. /**
  177. * test_and_set_bit - Set 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_set_bit (int nr, volatile void *addr)
  186. {
  187. __u32 bit, old, new;
  188. volatile __u32 *m;
  189. CMPXCHG_BUGCHECK_DECL
  190. m = (volatile __u32 *) addr + (nr >> 5);
  191. bit = 1 << (nr & 31);
  192. do {
  193. CMPXCHG_BUGCHECK(m);
  194. old = *m;
  195. new = old | bit;
  196. } while (cmpxchg_acq(m, old, new) != old);
  197. return (old & bit) != 0;
  198. }
  199. /**
  200. * test_and_set_bit_lock - Set a bit and return its old value for lock
  201. * @nr: Bit to set
  202. * @addr: Address to count from
  203. *
  204. * This is the same as test_and_set_bit on ia64
  205. */
  206. #define test_and_set_bit_lock test_and_set_bit
  207. /**
  208. * __test_and_set_bit - Set a bit and return its old value
  209. * @nr: Bit to set
  210. * @addr: Address to count from
  211. *
  212. * This operation is non-atomic and can be reordered.
  213. * If two examples of this operation race, one can appear to succeed
  214. * but actually fail. You must protect multiple accesses with a lock.
  215. */
  216. static __inline__ int
  217. __test_and_set_bit (int nr, volatile void *addr)
  218. {
  219. __u32 *p = (__u32 *) addr + (nr >> 5);
  220. __u32 m = 1 << (nr & 31);
  221. int oldbitset = (*p & m) != 0;
  222. *p |= m;
  223. return oldbitset;
  224. }
  225. /**
  226. * test_and_clear_bit - Clear a bit and return its old value
  227. * @nr: Bit to set
  228. * @addr: Address to count from
  229. *
  230. * This operation is atomic and cannot be reordered.
  231. * It also implies a memory barrier.
  232. */
  233. static __inline__ int
  234. test_and_clear_bit (int nr, volatile void *addr)
  235. {
  236. __u32 mask, old, new;
  237. volatile __u32 *m;
  238. CMPXCHG_BUGCHECK_DECL
  239. m = (volatile __u32 *) addr + (nr >> 5);
  240. mask = ~(1 << (nr & 31));
  241. do {
  242. CMPXCHG_BUGCHECK(m);
  243. old = *m;
  244. new = old & mask;
  245. } while (cmpxchg_acq(m, old, new) != old);
  246. return (old & ~mask) != 0;
  247. }
  248. /**
  249. * __test_and_clear_bit - Clear a bit and return its old value
  250. * @nr: Bit to set
  251. * @addr: Address to count from
  252. *
  253. * This operation is non-atomic and can be reordered.
  254. * If two examples of this operation race, one can appear to succeed
  255. * but actually fail. You must protect multiple accesses with a lock.
  256. */
  257. static __inline__ int
  258. __test_and_clear_bit(int nr, volatile void * addr)
  259. {
  260. __u32 *p = (__u32 *) addr + (nr >> 5);
  261. __u32 m = 1 << (nr & 31);
  262. int oldbitset = *p & m;
  263. *p &= ~m;
  264. return oldbitset;
  265. }
  266. /**
  267. * test_and_change_bit - Change a bit and return its old value
  268. * @nr: Bit to set
  269. * @addr: Address to count from
  270. *
  271. * This operation is atomic and cannot be reordered.
  272. * It also implies a memory barrier.
  273. */
  274. static __inline__ int
  275. test_and_change_bit (int nr, volatile void *addr)
  276. {
  277. __u32 bit, old, new;
  278. volatile __u32 *m;
  279. CMPXCHG_BUGCHECK_DECL
  280. m = (volatile __u32 *) addr + (nr >> 5);
  281. bit = (1 << (nr & 31));
  282. do {
  283. CMPXCHG_BUGCHECK(m);
  284. old = *m;
  285. new = old ^ bit;
  286. } while (cmpxchg_acq(m, old, new) != old);
  287. return (old & bit) != 0;
  288. }
  289. /*
  290. * WARNING: non atomic version.
  291. */
  292. static __inline__ int
  293. __test_and_change_bit (int nr, void *addr)
  294. {
  295. __u32 old, bit = (1 << (nr & 31));
  296. __u32 *m = (__u32 *) addr + (nr >> 5);
  297. old = *m;
  298. *m = old ^ bit;
  299. return (old & bit) != 0;
  300. }
  301. static __inline__ int
  302. test_bit (int nr, const volatile void *addr)
  303. {
  304. return 1 & (((const volatile __u32 *) addr)[nr >> 5] >> (nr & 31));
  305. }
  306. /**
  307. * ffz - find the first zero bit in a long word
  308. * @x: The long word to find the bit in
  309. *
  310. * Returns the bit-number (0..63) of the first (least significant) zero bit.
  311. * Undefined if no zero exists, so code should check against ~0UL first...
  312. */
  313. static inline unsigned long
  314. ffz (unsigned long x)
  315. {
  316. unsigned long result;
  317. result = ia64_popcnt(x & (~x - 1));
  318. return result;
  319. }
  320. /**
  321. * __ffs - find first bit in word.
  322. * @x: The word to search
  323. *
  324. * Undefined if no bit exists, so code should check against 0 first.
  325. */
  326. static __inline__ unsigned long
  327. __ffs (unsigned long x)
  328. {
  329. unsigned long result;
  330. result = ia64_popcnt((x-1) & ~x);
  331. return result;
  332. }
  333. #ifdef __KERNEL__
  334. /*
  335. * Return bit number of last (most-significant) bit set. Undefined
  336. * for x==0. Bits are numbered from 0..63 (e.g., ia64_fls(9) == 3).
  337. */
  338. static inline unsigned long
  339. ia64_fls (unsigned long x)
  340. {
  341. long double d = x;
  342. long exp;
  343. exp = ia64_getf_exp(d);
  344. return exp - 0xffff;
  345. }
  346. /*
  347. * Find the last (most significant) bit set. Returns 0 for x==0 and
  348. * bits are numbered from 1..32 (e.g., fls(9) == 4).
  349. */
  350. static inline int
  351. fls (int t)
  352. {
  353. unsigned long x = t & 0xffffffffu;
  354. if (!x)
  355. return 0;
  356. x |= x >> 1;
  357. x |= x >> 2;
  358. x |= x >> 4;
  359. x |= x >> 8;
  360. x |= x >> 16;
  361. return ia64_popcnt(x);
  362. }
  363. #include <asm-generic/bitops/fls64.h>
  364. /*
  365. * ffs: find first bit set. This is defined the same way as the libc and
  366. * compiler builtin ffs routines, therefore differs in spirit from the above
  367. * ffz (man ffs): it operates on "int" values only and the result value is the
  368. * bit number + 1. ffs(0) is defined to return zero.
  369. */
  370. #define ffs(x) __builtin_ffs(x)
  371. /*
  372. * hweightN: returns the hamming weight (i.e. the number
  373. * of bits set) of a N-bit word
  374. */
  375. static __inline__ unsigned long
  376. hweight64 (unsigned long x)
  377. {
  378. unsigned long result;
  379. result = ia64_popcnt(x);
  380. return result;
  381. }
  382. #define hweight32(x) (unsigned int) hweight64((x) & 0xfffffffful)
  383. #define hweight16(x) (unsigned int) hweight64((x) & 0xfffful)
  384. #define hweight8(x) (unsigned int) hweight64((x) & 0xfful)
  385. #endif /* __KERNEL__ */
  386. #include <asm-generic/bitops/find.h>
  387. #ifdef __KERNEL__
  388. #include <asm-generic/bitops/ext2-non-atomic.h>
  389. #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a)
  390. #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a)
  391. #include <asm-generic/bitops/minix.h>
  392. #include <asm-generic/bitops/sched.h>
  393. #endif /* __KERNEL__ */
  394. #endif /* _ASM_IA64_BITOPS_H */