bitops_64.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #ifndef _X86_64_BITOPS_H
  2. #define _X86_64_BITOPS_H
  3. /*
  4. * Copyright 1992, Linus Torvalds.
  5. */
  6. #include <asm/alternative.h>
  7. #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
  8. /* Technically wrong, but this avoids compilation errors on some gcc
  9. versions. */
  10. #define ADDR "=m" (*(volatile long *) addr)
  11. #else
  12. #define ADDR "+m" (*(volatile long *) addr)
  13. #endif
  14. /**
  15. * set_bit - Atomically set a bit in memory
  16. * @nr: the bit to set
  17. * @addr: the address to start counting from
  18. *
  19. * This function is atomic and may not be reordered. See __set_bit()
  20. * if you do not require the atomic guarantees.
  21. * Note that @nr may be almost arbitrarily large; this function is not
  22. * restricted to acting on a single-word quantity.
  23. */
  24. static __inline__ void set_bit(int nr, volatile void * addr)
  25. {
  26. __asm__ __volatile__( LOCK_PREFIX
  27. "btsl %1,%0"
  28. :ADDR
  29. :"dIr" (nr) : "memory");
  30. }
  31. /**
  32. * __set_bit - Set a bit in memory
  33. * @nr: the bit to set
  34. * @addr: the address to start counting from
  35. *
  36. * Unlike set_bit(), this function is non-atomic and may be reordered.
  37. * If it's called on the same region of memory simultaneously, the effect
  38. * may be that only one operation succeeds.
  39. */
  40. static __inline__ void __set_bit(int nr, volatile void * addr)
  41. {
  42. __asm__ volatile(
  43. "btsl %1,%0"
  44. :ADDR
  45. :"dIr" (nr) : "memory");
  46. }
  47. /**
  48. * clear_bit - Clears a bit in memory
  49. * @nr: Bit to clear
  50. * @addr: Address to start counting from
  51. *
  52. * clear_bit() is atomic and may not be reordered. However, it does
  53. * not contain a memory barrier, so if it is used for locking purposes,
  54. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  55. * in order to ensure changes are visible on other processors.
  56. */
  57. static __inline__ void clear_bit(int nr, volatile void * addr)
  58. {
  59. __asm__ __volatile__( LOCK_PREFIX
  60. "btrl %1,%0"
  61. :ADDR
  62. :"dIr" (nr));
  63. }
  64. static __inline__ void __clear_bit(int nr, volatile void * addr)
  65. {
  66. __asm__ __volatile__(
  67. "btrl %1,%0"
  68. :ADDR
  69. :"dIr" (nr));
  70. }
  71. #define smp_mb__before_clear_bit() barrier()
  72. #define smp_mb__after_clear_bit() barrier()
  73. /**
  74. * __change_bit - Toggle a bit in memory
  75. * @nr: the bit to change
  76. * @addr: the address to start counting from
  77. *
  78. * Unlike change_bit(), this function is non-atomic and may be reordered.
  79. * If it's called on the same region of memory simultaneously, the effect
  80. * may be that only one operation succeeds.
  81. */
  82. static __inline__ void __change_bit(int nr, volatile void * addr)
  83. {
  84. __asm__ __volatile__(
  85. "btcl %1,%0"
  86. :ADDR
  87. :"dIr" (nr));
  88. }
  89. /**
  90. * change_bit - Toggle a bit in memory
  91. * @nr: Bit to change
  92. * @addr: Address to start counting from
  93. *
  94. * change_bit() is atomic and may not be reordered.
  95. * Note that @nr may be almost arbitrarily large; this function is not
  96. * restricted to acting on a single-word quantity.
  97. */
  98. static __inline__ void change_bit(int nr, volatile void * addr)
  99. {
  100. __asm__ __volatile__( LOCK_PREFIX
  101. "btcl %1,%0"
  102. :ADDR
  103. :"dIr" (nr));
  104. }
  105. /**
  106. * test_and_set_bit - Set a bit and return its old value
  107. * @nr: Bit to set
  108. * @addr: Address to count from
  109. *
  110. * This operation is atomic and cannot be reordered.
  111. * It also implies a memory barrier.
  112. */
  113. static __inline__ int test_and_set_bit(int nr, volatile void * addr)
  114. {
  115. int oldbit;
  116. __asm__ __volatile__( LOCK_PREFIX
  117. "btsl %2,%1\n\tsbbl %0,%0"
  118. :"=r" (oldbit),ADDR
  119. :"dIr" (nr) : "memory");
  120. return oldbit;
  121. }
  122. /**
  123. * __test_and_set_bit - Set a bit and return its old value
  124. * @nr: Bit to set
  125. * @addr: Address to count from
  126. *
  127. * This operation is non-atomic and can be reordered.
  128. * If two examples of this operation race, one can appear to succeed
  129. * but actually fail. You must protect multiple accesses with a lock.
  130. */
  131. static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
  132. {
  133. int oldbit;
  134. __asm__(
  135. "btsl %2,%1\n\tsbbl %0,%0"
  136. :"=r" (oldbit),ADDR
  137. :"dIr" (nr));
  138. return oldbit;
  139. }
  140. /**
  141. * test_and_clear_bit - Clear a bit and return its old value
  142. * @nr: Bit to clear
  143. * @addr: Address to count from
  144. *
  145. * This operation is atomic and cannot be reordered.
  146. * It also implies a memory barrier.
  147. */
  148. static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
  149. {
  150. int oldbit;
  151. __asm__ __volatile__( LOCK_PREFIX
  152. "btrl %2,%1\n\tsbbl %0,%0"
  153. :"=r" (oldbit),ADDR
  154. :"dIr" (nr) : "memory");
  155. return oldbit;
  156. }
  157. /**
  158. * __test_and_clear_bit - Clear a bit and return its old value
  159. * @nr: Bit to clear
  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 __test_and_clear_bit(int nr, volatile void * addr)
  167. {
  168. int oldbit;
  169. __asm__(
  170. "btrl %2,%1\n\tsbbl %0,%0"
  171. :"=r" (oldbit),ADDR
  172. :"dIr" (nr));
  173. return oldbit;
  174. }
  175. /* WARNING: non atomic and it can be reordered! */
  176. static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
  177. {
  178. int oldbit;
  179. __asm__ __volatile__(
  180. "btcl %2,%1\n\tsbbl %0,%0"
  181. :"=r" (oldbit),ADDR
  182. :"dIr" (nr) : "memory");
  183. return oldbit;
  184. }
  185. /**
  186. * test_and_change_bit - Change a bit and return its old value
  187. * @nr: Bit to change
  188. * @addr: Address to count from
  189. *
  190. * This operation is atomic and cannot be reordered.
  191. * It also implies a memory barrier.
  192. */
  193. static __inline__ int test_and_change_bit(int nr, volatile void * addr)
  194. {
  195. int oldbit;
  196. __asm__ __volatile__( LOCK_PREFIX
  197. "btcl %2,%1\n\tsbbl %0,%0"
  198. :"=r" (oldbit),ADDR
  199. :"dIr" (nr) : "memory");
  200. return oldbit;
  201. }
  202. #if 0 /* Fool kernel-doc since it doesn't do macros yet */
  203. /**
  204. * test_bit - Determine whether a bit is set
  205. * @nr: bit number to test
  206. * @addr: Address to start counting from
  207. */
  208. static int test_bit(int nr, const volatile void * addr);
  209. #endif
  210. static __inline__ int constant_test_bit(int nr, const volatile void * addr)
  211. {
  212. return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
  213. }
  214. static __inline__ int variable_test_bit(int nr, volatile const void * addr)
  215. {
  216. int oldbit;
  217. __asm__ __volatile__(
  218. "btl %2,%1\n\tsbbl %0,%0"
  219. :"=r" (oldbit)
  220. :"m" (*(volatile long *)addr),"dIr" (nr));
  221. return oldbit;
  222. }
  223. #define test_bit(nr,addr) \
  224. (__builtin_constant_p(nr) ? \
  225. constant_test_bit((nr),(addr)) : \
  226. variable_test_bit((nr),(addr)))
  227. #undef ADDR
  228. extern long find_first_zero_bit(const unsigned long * addr, unsigned long size);
  229. extern long find_next_zero_bit (const unsigned long * addr, long size, long offset);
  230. extern long find_first_bit(const unsigned long * addr, unsigned long size);
  231. extern long find_next_bit(const unsigned long * addr, long size, long offset);
  232. /* return index of first bet set in val or max when no bit is set */
  233. static inline unsigned long __scanbit(unsigned long val, unsigned long max)
  234. {
  235. asm("bsfq %1,%0 ; cmovz %2,%0" : "=&r" (val) : "r" (val), "r" (max));
  236. return val;
  237. }
  238. #define find_first_bit(addr,size) \
  239. ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
  240. (__scanbit(*(unsigned long *)addr,(size))) : \
  241. find_first_bit(addr,size)))
  242. #define find_next_bit(addr,size,off) \
  243. ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
  244. ((off) + (__scanbit((*(unsigned long *)addr) >> (off),(size)-(off)))) : \
  245. find_next_bit(addr,size,off)))
  246. #define find_first_zero_bit(addr,size) \
  247. ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
  248. (__scanbit(~*(unsigned long *)addr,(size))) : \
  249. find_first_zero_bit(addr,size)))
  250. #define find_next_zero_bit(addr,size,off) \
  251. ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
  252. ((off)+(__scanbit(~(((*(unsigned long *)addr)) >> (off)),(size)-(off)))) : \
  253. find_next_zero_bit(addr,size,off)))
  254. /*
  255. * Find string of zero bits in a bitmap. -1 when not found.
  256. */
  257. extern unsigned long
  258. find_next_zero_string(unsigned long *bitmap, long start, long nbits, int len);
  259. static inline void set_bit_string(unsigned long *bitmap, unsigned long i,
  260. int len)
  261. {
  262. unsigned long end = i + len;
  263. while (i < end) {
  264. __set_bit(i, bitmap);
  265. i++;
  266. }
  267. }
  268. static inline void __clear_bit_string(unsigned long *bitmap, unsigned long i,
  269. int len)
  270. {
  271. unsigned long end = i + len;
  272. while (i < end) {
  273. __clear_bit(i, bitmap);
  274. i++;
  275. }
  276. }
  277. /**
  278. * ffz - find first zero in word.
  279. * @word: The word to search
  280. *
  281. * Undefined if no zero exists, so code should check against ~0UL first.
  282. */
  283. static __inline__ unsigned long ffz(unsigned long word)
  284. {
  285. __asm__("bsfq %1,%0"
  286. :"=r" (word)
  287. :"r" (~word));
  288. return word;
  289. }
  290. /**
  291. * __ffs - find first bit in word.
  292. * @word: The word to search
  293. *
  294. * Undefined if no bit exists, so code should check against 0 first.
  295. */
  296. static __inline__ unsigned long __ffs(unsigned long word)
  297. {
  298. __asm__("bsfq %1,%0"
  299. :"=r" (word)
  300. :"rm" (word));
  301. return word;
  302. }
  303. /*
  304. * __fls: find last bit set.
  305. * @word: The word to search
  306. *
  307. * Undefined if no zero exists, so code should check against ~0UL first.
  308. */
  309. static __inline__ unsigned long __fls(unsigned long word)
  310. {
  311. __asm__("bsrq %1,%0"
  312. :"=r" (word)
  313. :"rm" (word));
  314. return word;
  315. }
  316. #ifdef __KERNEL__
  317. #include <asm-generic/bitops/sched.h>
  318. /**
  319. * ffs - find first bit set
  320. * @x: the word to search
  321. *
  322. * This is defined the same way as
  323. * the libc and compiler builtin ffs routines, therefore
  324. * differs in spirit from the above ffz (man ffs).
  325. */
  326. static __inline__ int ffs(int x)
  327. {
  328. int r;
  329. __asm__("bsfl %1,%0\n\t"
  330. "cmovzl %2,%0"
  331. : "=r" (r) : "rm" (x), "r" (-1));
  332. return r+1;
  333. }
  334. /**
  335. * fls64 - find last bit set in 64 bit word
  336. * @x: the word to search
  337. *
  338. * This is defined the same way as fls.
  339. */
  340. static __inline__ int fls64(__u64 x)
  341. {
  342. if (x == 0)
  343. return 0;
  344. return __fls(x) + 1;
  345. }
  346. /**
  347. * fls - find last bit set
  348. * @x: the word to search
  349. *
  350. * This is defined the same way as ffs.
  351. */
  352. static __inline__ int fls(int x)
  353. {
  354. int r;
  355. __asm__("bsrl %1,%0\n\t"
  356. "cmovzl %2,%0"
  357. : "=&r" (r) : "rm" (x), "rm" (-1));
  358. return r+1;
  359. }
  360. #define ARCH_HAS_FAST_MULTIPLIER 1
  361. #include <asm-generic/bitops/hweight.h>
  362. #endif /* __KERNEL__ */
  363. #ifdef __KERNEL__
  364. #include <asm-generic/bitops/ext2-non-atomic.h>
  365. #define ext2_set_bit_atomic(lock,nr,addr) \
  366. test_and_set_bit((nr),(unsigned long*)addr)
  367. #define ext2_clear_bit_atomic(lock,nr,addr) \
  368. test_and_clear_bit((nr),(unsigned long*)addr)
  369. #include <asm-generic/bitops/minix.h>
  370. #endif /* __KERNEL__ */
  371. #endif /* _X86_64_BITOPS_H */