bitops_32.h 11 KB

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