bitops.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. asm volatile(LOCK_PREFIX "btc %1,%0" : ADDR : "Ir" (nr));
  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 atomic and cannot be reordered.
  163. * It also implies a memory barrier.
  164. */
  165. static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
  166. {
  167. int oldbit;
  168. asm volatile(LOCK_PREFIX "bts %2,%1\n\t"
  169. "sbb %0,%0" : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
  170. return oldbit;
  171. }
  172. /**
  173. * test_and_set_bit_lock - Set a bit and return its old value for lock
  174. * @nr: Bit to set
  175. * @addr: Address to count from
  176. *
  177. * This is the same as test_and_set_bit on x86.
  178. */
  179. static inline int test_and_set_bit_lock(int nr, volatile unsigned long *addr)
  180. {
  181. return test_and_set_bit(nr, addr);
  182. }
  183. /**
  184. * __test_and_set_bit - Set a bit and return its old value
  185. * @nr: Bit to set
  186. * @addr: Address to count from
  187. *
  188. * This operation is non-atomic and can be reordered.
  189. * If two examples of this operation race, one can appear to succeed
  190. * but actually fail. You must protect multiple accesses with a lock.
  191. */
  192. static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
  193. {
  194. int oldbit;
  195. asm("bts %2,%1\n\t"
  196. "sbb %0,%0"
  197. : "=r" (oldbit), ADDR
  198. : "Ir" (nr));
  199. return oldbit;
  200. }
  201. /**
  202. * test_and_clear_bit - Clear a bit and return its old value
  203. * @nr: Bit to clear
  204. * @addr: Address to count from
  205. *
  206. * This operation is atomic and cannot be reordered.
  207. * It also implies a memory barrier.
  208. */
  209. static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
  210. {
  211. int oldbit;
  212. asm volatile(LOCK_PREFIX "btr %2,%1\n\t"
  213. "sbb %0,%0"
  214. : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
  215. return oldbit;
  216. }
  217. /**
  218. * __test_and_clear_bit - Clear a bit and return its old value
  219. * @nr: Bit to clear
  220. * @addr: Address to count from
  221. *
  222. * This operation is non-atomic and can be reordered.
  223. * If two examples of this operation race, one can appear to succeed
  224. * but actually fail. You must protect multiple accesses with a lock.
  225. */
  226. static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
  227. {
  228. int oldbit;
  229. asm volatile("btr %2,%1\n\t"
  230. "sbb %0,%0"
  231. : "=r" (oldbit), ADDR
  232. : "Ir" (nr));
  233. return oldbit;
  234. }
  235. /* WARNING: non atomic and it can be reordered! */
  236. static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)
  237. {
  238. int oldbit;
  239. asm volatile("btc %2,%1\n\t"
  240. "sbb %0,%0"
  241. : "=r" (oldbit), ADDR
  242. : "Ir" (nr) : "memory");
  243. return oldbit;
  244. }
  245. /**
  246. * test_and_change_bit - Change a bit and return its old value
  247. * @nr: Bit to change
  248. * @addr: Address to count from
  249. *
  250. * This operation is atomic and cannot be reordered.
  251. * It also implies a memory barrier.
  252. */
  253. static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
  254. {
  255. int oldbit;
  256. asm volatile(LOCK_PREFIX "btc %2,%1\n\t"
  257. "sbb %0,%0"
  258. : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
  259. return oldbit;
  260. }
  261. static inline int constant_test_bit(int nr, const volatile unsigned long *addr)
  262. {
  263. return ((1UL << (nr % BITS_PER_LONG)) &
  264. (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
  265. }
  266. static inline int variable_test_bit(int nr, volatile const unsigned long *addr)
  267. {
  268. int oldbit;
  269. asm volatile("bt %2,%1\n\t"
  270. "sbb %0,%0"
  271. : "=r" (oldbit)
  272. : "m" (*(unsigned long *)addr), "Ir" (nr));
  273. return oldbit;
  274. }
  275. #if 0 /* Fool kernel-doc since it doesn't do macros yet */
  276. /**
  277. * test_bit - Determine whether a bit is set
  278. * @nr: bit number to test
  279. * @addr: Address to start counting from
  280. */
  281. static int test_bit(int nr, const volatile unsigned long *addr);
  282. #endif
  283. #define test_bit(nr, addr) \
  284. (__builtin_constant_p((nr)) \
  285. ? constant_test_bit((nr), (addr)) \
  286. : variable_test_bit((nr), (addr)))
  287. /**
  288. * __ffs - find first set bit in word
  289. * @word: The word to search
  290. *
  291. * Undefined if no bit exists, so code should check against 0 first.
  292. */
  293. static inline unsigned long __ffs(unsigned long word)
  294. {
  295. asm("bsf %1,%0"
  296. : "=r" (word)
  297. : "rm" (word));
  298. return word;
  299. }
  300. /**
  301. * ffz - find first zero bit in word
  302. * @word: The word to search
  303. *
  304. * Undefined if no zero exists, so code should check against ~0UL first.
  305. */
  306. static inline unsigned long ffz(unsigned long word)
  307. {
  308. asm("bsf %1,%0"
  309. : "=r" (word)
  310. : "r" (~word));
  311. return word;
  312. }
  313. /*
  314. * __fls: find last set bit in word
  315. * @word: The word to search
  316. *
  317. * Undefined if no set bit exists, so code should check against 0 first.
  318. */
  319. static inline unsigned long __fls(unsigned long word)
  320. {
  321. asm("bsr %1,%0"
  322. : "=r" (word)
  323. : "rm" (word));
  324. return word;
  325. }
  326. #ifdef __KERNEL__
  327. /**
  328. * ffs - find first set bit in word
  329. * @x: the word to search
  330. *
  331. * This is defined the same way as the libc and compiler builtin ffs
  332. * routines, therefore differs in spirit from the other bitops.
  333. *
  334. * ffs(value) returns 0 if value is 0 or the position of the first
  335. * set bit if value is nonzero. The first (least significant) bit
  336. * is at position 1.
  337. */
  338. static inline int ffs(int x)
  339. {
  340. int r;
  341. #ifdef CONFIG_X86_CMOV
  342. asm("bsfl %1,%0\n\t"
  343. "cmovzl %2,%0"
  344. : "=r" (r) : "rm" (x), "r" (-1));
  345. #else
  346. asm("bsfl %1,%0\n\t"
  347. "jnz 1f\n\t"
  348. "movl $-1,%0\n"
  349. "1:" : "=r" (r) : "rm" (x));
  350. #endif
  351. return r + 1;
  352. }
  353. /**
  354. * fls - find last set bit in word
  355. * @x: the word to search
  356. *
  357. * This is defined in a similar way as the libc and compiler builtin
  358. * ffs, but returns the position of the most significant set bit.
  359. *
  360. * fls(value) returns 0 if value is 0 or the position of the last
  361. * set bit if value is nonzero. The last (most significant) bit is
  362. * at position 32.
  363. */
  364. static inline int fls(int x)
  365. {
  366. int r;
  367. #ifdef CONFIG_X86_CMOV
  368. asm("bsrl %1,%0\n\t"
  369. "cmovzl %2,%0"
  370. : "=&r" (r) : "rm" (x), "rm" (-1));
  371. #else
  372. asm("bsrl %1,%0\n\t"
  373. "jnz 1f\n\t"
  374. "movl $-1,%0\n"
  375. "1:" : "=r" (r) : "rm" (x));
  376. #endif
  377. return r + 1;
  378. }
  379. #endif /* __KERNEL__ */
  380. #undef ADDR
  381. #ifdef __KERNEL__
  382. #include <asm-generic/bitops/sched.h>
  383. #define ARCH_HAS_FAST_MULTIPLIER 1
  384. #include <asm-generic/bitops/hweight.h>
  385. #endif /* __KERNEL__ */
  386. #include <asm-generic/bitops/fls64.h>
  387. #ifdef __KERNEL__
  388. #include <asm-generic/bitops/ext2-non-atomic.h>
  389. #define ext2_set_bit_atomic(lock, nr, addr) \
  390. test_and_set_bit((nr), (unsigned long *)(addr))
  391. #define ext2_clear_bit_atomic(lock, nr, addr) \
  392. test_and_clear_bit((nr), (unsigned long *)(addr))
  393. #include <asm-generic/bitops/minix.h>
  394. #endif /* __KERNEL__ */
  395. #endif /* ASM_X86__BITOPS_H */