bitops.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #ifndef _ASM_X86_BITOPS_H
  2. #define _ASM_X86_BITOPS_H
  3. /*
  4. * Copyright 1992, Linus Torvalds.
  5. */
  6. #ifndef _LINUX_BITOPS_H
  7. #error only <linux/bitops.h> can be included directly
  8. #endif
  9. #include <linux/compiler.h>
  10. #include <asm/alternative.h>
  11. /*
  12. * These have to be done with inline assembly: that way the bit-setting
  13. * is guaranteed to be atomic. All bit operations return 0 if the bit
  14. * was cleared before the operation and != 0 if it was not.
  15. *
  16. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  17. */
  18. #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
  19. /* Technically wrong, but this avoids compilation errors on some gcc
  20. versions. */
  21. #define BITOP_ADDR(x) "=m" (*(volatile long *) (x))
  22. #else
  23. #define BITOP_ADDR(x) "+m" (*(volatile long *) (x))
  24. #endif
  25. #define ADDR BITOP_ADDR(addr)
  26. /*
  27. * We do the locked ops that don't return the old value as
  28. * a mask operation on a byte.
  29. */
  30. #define IS_IMMEDIATE(nr) (__builtin_constant_p(nr))
  31. #define CONST_MASK_ADDR(nr, addr) BITOP_ADDR((void *)(addr) + ((nr)>>3))
  32. #define CONST_MASK(nr) (1 << ((nr) & 7))
  33. /**
  34. * set_bit - Atomically set a bit in memory
  35. * @nr: the bit to set
  36. * @addr: the address to start counting from
  37. *
  38. * This function is atomic and may not be reordered. See __set_bit()
  39. * if you do not require the atomic guarantees.
  40. *
  41. * Note: there are no guarantees that this function will not be reordered
  42. * on non x86 architectures, so if you are writing portable code,
  43. * make sure not to rely on its reordering guarantees.
  44. *
  45. * Note that @nr may be almost arbitrarily large; this function is not
  46. * restricted to acting on a single-word quantity.
  47. */
  48. static inline void set_bit(unsigned int nr, volatile unsigned long *addr)
  49. {
  50. if (IS_IMMEDIATE(nr)) {
  51. asm volatile(LOCK_PREFIX "orb %1,%0"
  52. : CONST_MASK_ADDR(nr, addr)
  53. : "iq" ((u8)CONST_MASK(nr))
  54. : "memory");
  55. } else {
  56. asm volatile(LOCK_PREFIX "bts %1,%0"
  57. : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
  58. }
  59. }
  60. /**
  61. * __set_bit - Set a bit in memory
  62. * @nr: the bit to set
  63. * @addr: the address to start counting from
  64. *
  65. * Unlike set_bit(), this function is non-atomic and may be reordered.
  66. * If it's called on the same region of memory simultaneously, the effect
  67. * may be that only one operation succeeds.
  68. */
  69. static inline void __set_bit(int nr, volatile unsigned long *addr)
  70. {
  71. asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
  72. }
  73. /**
  74. * clear_bit - Clears a bit in memory
  75. * @nr: Bit to clear
  76. * @addr: Address to start counting from
  77. *
  78. * clear_bit() is atomic and may not be reordered. However, it does
  79. * not contain a memory barrier, so if it is used for locking purposes,
  80. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  81. * in order to ensure changes are visible on other processors.
  82. */
  83. static inline void clear_bit(int nr, volatile unsigned long *addr)
  84. {
  85. if (IS_IMMEDIATE(nr)) {
  86. asm volatile(LOCK_PREFIX "andb %1,%0"
  87. : CONST_MASK_ADDR(nr, addr)
  88. : "iq" ((u8)~CONST_MASK(nr)));
  89. } else {
  90. asm volatile(LOCK_PREFIX "btr %1,%0"
  91. : BITOP_ADDR(addr)
  92. : "Ir" (nr));
  93. }
  94. }
  95. /*
  96. * clear_bit_unlock - Clears a bit in memory
  97. * @nr: Bit to clear
  98. * @addr: Address to start counting from
  99. *
  100. * clear_bit() is atomic and implies release semantics before the memory
  101. * operation. It can be used for an unlock.
  102. */
  103. static inline void clear_bit_unlock(unsigned nr, volatile unsigned long *addr)
  104. {
  105. barrier();
  106. clear_bit(nr, addr);
  107. }
  108. static inline void __clear_bit(int nr, volatile unsigned long *addr)
  109. {
  110. asm volatile("btr %1,%0" : ADDR : "Ir" (nr));
  111. }
  112. /*
  113. * __clear_bit_unlock - Clears a bit in memory
  114. * @nr: Bit to clear
  115. * @addr: Address to start counting from
  116. *
  117. * __clear_bit() is non-atomic and implies release semantics before the memory
  118. * operation. It can be used for an unlock if no other CPUs can concurrently
  119. * modify other bits in the word.
  120. *
  121. * No memory barrier is required here, because x86 cannot reorder stores past
  122. * older loads. Same principle as spin_unlock.
  123. */
  124. static inline void __clear_bit_unlock(unsigned nr, volatile unsigned long *addr)
  125. {
  126. barrier();
  127. __clear_bit(nr, addr);
  128. }
  129. #define smp_mb__before_clear_bit() barrier()
  130. #define smp_mb__after_clear_bit() barrier()
  131. /**
  132. * __change_bit - Toggle a bit in memory
  133. * @nr: the bit to change
  134. * @addr: the address to start counting from
  135. *
  136. * Unlike change_bit(), this function is non-atomic and may be reordered.
  137. * If it's called on the same region of memory simultaneously, the effect
  138. * may be that only one operation succeeds.
  139. */
  140. static inline void __change_bit(int nr, volatile unsigned long *addr)
  141. {
  142. asm volatile("btc %1,%0" : ADDR : "Ir" (nr));
  143. }
  144. /**
  145. * change_bit - Toggle a bit in memory
  146. * @nr: Bit to change
  147. * @addr: Address to start counting from
  148. *
  149. * change_bit() is atomic and may not be reordered.
  150. * Note that @nr may be almost arbitrarily large; this function is not
  151. * restricted to acting on a single-word quantity.
  152. */
  153. static inline void change_bit(int nr, volatile unsigned long *addr)
  154. {
  155. if (IS_IMMEDIATE(nr)) {
  156. asm volatile(LOCK_PREFIX "xorb %1,%0"
  157. : CONST_MASK_ADDR(nr, addr)
  158. : "iq" ((u8)CONST_MASK(nr)));
  159. } else {
  160. asm volatile(LOCK_PREFIX "btc %1,%0"
  161. : BITOP_ADDR(addr)
  162. : "Ir" (nr));
  163. }
  164. }
  165. /**
  166. * test_and_set_bit - Set a bit and return its old value
  167. * @nr: Bit to set
  168. * @addr: Address to count from
  169. *
  170. * This operation is atomic and cannot be reordered.
  171. * It also implies a memory barrier.
  172. */
  173. static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
  174. {
  175. int oldbit;
  176. asm volatile(LOCK_PREFIX "bts %2,%1\n\t"
  177. "sbb %0,%0" : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
  178. return oldbit;
  179. }
  180. /**
  181. * test_and_set_bit_lock - Set a bit and return its old value for lock
  182. * @nr: Bit to set
  183. * @addr: Address to count from
  184. *
  185. * This is the same as test_and_set_bit on x86.
  186. */
  187. static inline int test_and_set_bit_lock(int nr, volatile unsigned long *addr)
  188. {
  189. return test_and_set_bit(nr, addr);
  190. }
  191. /**
  192. * __test_and_set_bit - Set a bit and return its old value
  193. * @nr: Bit to set
  194. * @addr: Address to count from
  195. *
  196. * This operation is non-atomic and can be reordered.
  197. * If two examples of this operation race, one can appear to succeed
  198. * but actually fail. You must protect multiple accesses with a lock.
  199. */
  200. static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
  201. {
  202. int oldbit;
  203. asm("bts %2,%1\n\t"
  204. "sbb %0,%0"
  205. : "=r" (oldbit), ADDR
  206. : "Ir" (nr));
  207. return oldbit;
  208. }
  209. /**
  210. * test_and_clear_bit - Clear a bit and return its old value
  211. * @nr: Bit to clear
  212. * @addr: Address to count from
  213. *
  214. * This operation is atomic and cannot be reordered.
  215. * It also implies a memory barrier.
  216. */
  217. static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
  218. {
  219. int oldbit;
  220. asm volatile(LOCK_PREFIX "btr %2,%1\n\t"
  221. "sbb %0,%0"
  222. : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
  223. return oldbit;
  224. }
  225. /**
  226. * __test_and_clear_bit - Clear a bit and return its old value
  227. * @nr: Bit to clear
  228. * @addr: Address to count from
  229. *
  230. * This operation is non-atomic and can be reordered.
  231. * If two examples of this operation race, one can appear to succeed
  232. * but actually fail. You must protect multiple accesses with a lock.
  233. */
  234. static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
  235. {
  236. int oldbit;
  237. asm volatile("btr %2,%1\n\t"
  238. "sbb %0,%0"
  239. : "=r" (oldbit), ADDR
  240. : "Ir" (nr));
  241. return oldbit;
  242. }
  243. /* WARNING: non atomic and it can be reordered! */
  244. static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)
  245. {
  246. int oldbit;
  247. asm volatile("btc %2,%1\n\t"
  248. "sbb %0,%0"
  249. : "=r" (oldbit), ADDR
  250. : "Ir" (nr) : "memory");
  251. return oldbit;
  252. }
  253. /**
  254. * test_and_change_bit - Change a bit and return its old value
  255. * @nr: Bit to change
  256. * @addr: Address to count from
  257. *
  258. * This operation is atomic and cannot be reordered.
  259. * It also implies a memory barrier.
  260. */
  261. static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
  262. {
  263. int oldbit;
  264. asm volatile(LOCK_PREFIX "btc %2,%1\n\t"
  265. "sbb %0,%0"
  266. : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
  267. return oldbit;
  268. }
  269. static inline int constant_test_bit(unsigned int nr, const volatile unsigned long *addr)
  270. {
  271. return ((1UL << (nr % BITS_PER_LONG)) &
  272. (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
  273. }
  274. static inline int variable_test_bit(int nr, volatile const unsigned long *addr)
  275. {
  276. int oldbit;
  277. asm volatile("bt %2,%1\n\t"
  278. "sbb %0,%0"
  279. : "=r" (oldbit)
  280. : "m" (*(unsigned long *)addr), "Ir" (nr));
  281. return oldbit;
  282. }
  283. #if 0 /* Fool kernel-doc since it doesn't do macros yet */
  284. /**
  285. * test_bit - Determine whether a bit is set
  286. * @nr: bit number to test
  287. * @addr: Address to start counting from
  288. */
  289. static int test_bit(int nr, const volatile unsigned long *addr);
  290. #endif
  291. #define test_bit(nr, addr) \
  292. (__builtin_constant_p((nr)) \
  293. ? constant_test_bit((nr), (addr)) \
  294. : variable_test_bit((nr), (addr)))
  295. /**
  296. * __ffs - find first set bit in word
  297. * @word: The word to search
  298. *
  299. * Undefined if no bit exists, so code should check against 0 first.
  300. */
  301. static inline unsigned long __ffs(unsigned long word)
  302. {
  303. asm("bsf %1,%0"
  304. : "=r" (word)
  305. : "rm" (word));
  306. return word;
  307. }
  308. /**
  309. * ffz - find first zero bit in word
  310. * @word: The word to search
  311. *
  312. * Undefined if no zero exists, so code should check against ~0UL first.
  313. */
  314. static inline unsigned long ffz(unsigned long word)
  315. {
  316. asm("bsf %1,%0"
  317. : "=r" (word)
  318. : "r" (~word));
  319. return word;
  320. }
  321. /*
  322. * __fls: find last set bit in word
  323. * @word: The word to search
  324. *
  325. * Undefined if no set bit exists, so code should check against 0 first.
  326. */
  327. static inline unsigned long __fls(unsigned long word)
  328. {
  329. asm("bsr %1,%0"
  330. : "=r" (word)
  331. : "rm" (word));
  332. return word;
  333. }
  334. #ifdef __KERNEL__
  335. /**
  336. * ffs - find first set bit in word
  337. * @x: the word to search
  338. *
  339. * This is defined the same way as the libc and compiler builtin ffs
  340. * routines, therefore differs in spirit from the other bitops.
  341. *
  342. * ffs(value) returns 0 if value is 0 or the position of the first
  343. * set bit if value is nonzero. The first (least significant) bit
  344. * is at position 1.
  345. */
  346. static inline int ffs(int x)
  347. {
  348. int r;
  349. #ifdef CONFIG_X86_CMOV
  350. asm("bsfl %1,%0\n\t"
  351. "cmovzl %2,%0"
  352. : "=r" (r) : "rm" (x), "r" (-1));
  353. #else
  354. asm("bsfl %1,%0\n\t"
  355. "jnz 1f\n\t"
  356. "movl $-1,%0\n"
  357. "1:" : "=r" (r) : "rm" (x));
  358. #endif
  359. return r + 1;
  360. }
  361. /**
  362. * fls - find last set bit in word
  363. * @x: the word to search
  364. *
  365. * This is defined in a similar way as the libc and compiler builtin
  366. * ffs, but returns the position of the most significant set bit.
  367. *
  368. * fls(value) returns 0 if value is 0 or the position of the last
  369. * set bit if value is nonzero. The last (most significant) bit is
  370. * at position 32.
  371. */
  372. static inline int fls(int x)
  373. {
  374. int r;
  375. #ifdef CONFIG_X86_CMOV
  376. asm("bsrl %1,%0\n\t"
  377. "cmovzl %2,%0"
  378. : "=&r" (r) : "rm" (x), "rm" (-1));
  379. #else
  380. asm("bsrl %1,%0\n\t"
  381. "jnz 1f\n\t"
  382. "movl $-1,%0\n"
  383. "1:" : "=r" (r) : "rm" (x));
  384. #endif
  385. return r + 1;
  386. }
  387. #endif /* __KERNEL__ */
  388. #undef ADDR
  389. #ifdef __KERNEL__
  390. #include <asm-generic/bitops/sched.h>
  391. #define ARCH_HAS_FAST_MULTIPLIER 1
  392. #include <asm-generic/bitops/hweight.h>
  393. #endif /* __KERNEL__ */
  394. #include <asm-generic/bitops/fls64.h>
  395. #ifdef __KERNEL__
  396. #include <asm-generic/bitops/ext2-non-atomic.h>
  397. #define ext2_set_bit_atomic(lock, nr, addr) \
  398. test_and_set_bit((nr), (unsigned long *)(addr))
  399. #define ext2_clear_bit_atomic(lock, nr, addr) \
  400. test_and_clear_bit((nr), (unsigned long *)(addr))
  401. #include <asm-generic/bitops/minix.h>
  402. #endif /* __KERNEL__ */
  403. #endif /* _ASM_X86_BITOPS_H */