bitops.h 10.0 KB

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