bitops.h 9.2 KB

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