bitops.h 10.0 KB

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