bitops.h 10 KB

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