bitops.h 12 KB

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