bitops.h 13 KB

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