bitops.h 13 KB

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