bitops.h 10 KB

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