bitops.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 __always_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. * __ffs - find first bit in word.
  282. * @word: The word to search
  283. *
  284. * Undefined if no bit exists, so code should check against 0 first.
  285. */
  286. static inline unsigned long __ffs(unsigned long word)
  287. {
  288. __asm__("bsfl %1,%0"
  289. :"=r" (word)
  290. :"rm" (word));
  291. return word;
  292. }
  293. /**
  294. * find_first_bit - find the first set bit in a memory region
  295. * @addr: The address to start the search at
  296. * @size: The maximum size to search
  297. *
  298. * Returns the bit-number of the first set bit, not the number of the byte
  299. * containing a bit.
  300. */
  301. static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)
  302. {
  303. unsigned x = 0;
  304. while (x < size) {
  305. unsigned long val = *addr++;
  306. if (val)
  307. return __ffs(val) + x;
  308. x += (sizeof(*addr)<<3);
  309. }
  310. return x;
  311. }
  312. /**
  313. * find_next_bit - find the first set bit in a memory region
  314. * @addr: The address to base the search on
  315. * @offset: The bitnumber to start searching at
  316. * @size: The maximum size to search
  317. */
  318. int find_next_bit(const unsigned long *addr, int size, int offset);
  319. /**
  320. * ffz - find first zero in word.
  321. * @word: The word to search
  322. *
  323. * Undefined if no zero exists, so code should check against ~0UL first.
  324. */
  325. static inline unsigned long ffz(unsigned long word)
  326. {
  327. __asm__("bsfl %1,%0"
  328. :"=r" (word)
  329. :"r" (~word));
  330. return word;
  331. }
  332. #define fls64(x) generic_fls64(x)
  333. #ifdef __KERNEL__
  334. /*
  335. * Every architecture must define this function. It's the fastest
  336. * way of searching a 140-bit bitmap where the first 100 bits are
  337. * unlikely to be set. It's guaranteed that at least one of the 140
  338. * bits is cleared.
  339. */
  340. static inline int sched_find_first_bit(const unsigned long *b)
  341. {
  342. if (unlikely(b[0]))
  343. return __ffs(b[0]);
  344. if (unlikely(b[1]))
  345. return __ffs(b[1]) + 32;
  346. if (unlikely(b[2]))
  347. return __ffs(b[2]) + 64;
  348. if (b[3])
  349. return __ffs(b[3]) + 96;
  350. return __ffs(b[4]) + 128;
  351. }
  352. /**
  353. * ffs - find first bit set
  354. * @x: the word to search
  355. *
  356. * This is defined the same way as
  357. * the libc and compiler builtin ffs routines, therefore
  358. * differs in spirit from the above ffz (man ffs).
  359. */
  360. static inline int ffs(int x)
  361. {
  362. int r;
  363. __asm__("bsfl %1,%0\n\t"
  364. "jnz 1f\n\t"
  365. "movl $-1,%0\n"
  366. "1:" : "=r" (r) : "rm" (x));
  367. return r+1;
  368. }
  369. /**
  370. * fls - find last bit set
  371. * @x: the word to search
  372. *
  373. * This is defined the same way as ffs.
  374. */
  375. static inline int fls(int x)
  376. {
  377. int r;
  378. __asm__("bsrl %1,%0\n\t"
  379. "jnz 1f\n\t"
  380. "movl $-1,%0\n"
  381. "1:" : "=r" (r) : "rm" (x));
  382. return r+1;
  383. }
  384. /**
  385. * hweightN - returns the hamming weight of a N-bit word
  386. * @x: the word to weigh
  387. *
  388. * The Hamming Weight of a number is the total number of bits set in it.
  389. */
  390. #define hweight32(x) generic_hweight32(x)
  391. #define hweight16(x) generic_hweight16(x)
  392. #define hweight8(x) generic_hweight8(x)
  393. #endif /* __KERNEL__ */
  394. #ifdef __KERNEL__
  395. #define ext2_set_bit(nr,addr) \
  396. __test_and_set_bit((nr),(unsigned long*)addr)
  397. #define ext2_set_bit_atomic(lock,nr,addr) \
  398. test_and_set_bit((nr),(unsigned long*)addr)
  399. #define ext2_clear_bit(nr, addr) \
  400. __test_and_clear_bit((nr),(unsigned long*)addr)
  401. #define ext2_clear_bit_atomic(lock,nr, addr) \
  402. test_and_clear_bit((nr),(unsigned long*)addr)
  403. #define ext2_test_bit(nr, addr) test_bit((nr),(unsigned long*)addr)
  404. #define ext2_find_first_zero_bit(addr, size) \
  405. find_first_zero_bit((unsigned long*)addr, size)
  406. #define ext2_find_next_zero_bit(addr, size, off) \
  407. find_next_zero_bit((unsigned long*)addr, size, off)
  408. /* Bitmap functions for the minix filesystem. */
  409. #define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,(void*)addr)
  410. #define minix_set_bit(nr,addr) __set_bit(nr,(void*)addr)
  411. #define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,(void*)addr)
  412. #define minix_test_bit(nr,addr) test_bit(nr,(void*)addr)
  413. #define minix_find_first_zero_bit(addr,size) \
  414. find_first_zero_bit((void*)addr,size)
  415. #endif /* __KERNEL__ */
  416. #endif /* _I386_BITOPS_H */