bitops_32.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. static inline int test_and_set_bit_lock(int nr, volatile unsigned long *addr)
  172. {
  173. return test_and_set_bit(nr, addr);
  174. }
  175. /**
  176. * __test_and_set_bit - Set a bit and return its old value
  177. * @nr: Bit to set
  178. * @addr: Address to count from
  179. *
  180. * This operation is non-atomic and can be reordered.
  181. * If two examples of this operation race, one can appear to succeed
  182. * but actually fail. You must protect multiple accesses with a lock.
  183. */
  184. static inline int __test_and_set_bit(int nr, volatile unsigned long * addr)
  185. {
  186. int oldbit;
  187. __asm__(
  188. "btsl %2,%1\n\tsbbl %0,%0"
  189. :"=r" (oldbit),"+m" (ADDR)
  190. :"Ir" (nr));
  191. return oldbit;
  192. }
  193. /**
  194. * test_and_clear_bit - Clear a bit and return its old value
  195. * @nr: Bit to clear
  196. * @addr: Address to count from
  197. *
  198. * This operation is atomic and cannot be reordered.
  199. * It can be reorderdered on other architectures other than x86.
  200. * It also implies a memory barrier.
  201. */
  202. static inline int test_and_clear_bit(int nr, volatile unsigned long * addr)
  203. {
  204. int oldbit;
  205. __asm__ __volatile__( LOCK_PREFIX
  206. "btrl %2,%1\n\tsbbl %0,%0"
  207. :"=r" (oldbit),"+m" (ADDR)
  208. :"Ir" (nr) : "memory");
  209. return oldbit;
  210. }
  211. /**
  212. * __test_and_clear_bit - Clear a bit and return its old value
  213. * @nr: Bit to clear
  214. * @addr: Address to count from
  215. *
  216. * This operation is non-atomic and can be reordered.
  217. * If two examples of this operation race, one can appear to succeed
  218. * but actually fail. You must protect multiple accesses with a lock.
  219. */
  220. static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
  221. {
  222. int oldbit;
  223. __asm__(
  224. "btrl %2,%1\n\tsbbl %0,%0"
  225. :"=r" (oldbit),"+m" (ADDR)
  226. :"Ir" (nr));
  227. return oldbit;
  228. }
  229. /* WARNING: non atomic and it can be reordered! */
  230. static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)
  231. {
  232. int oldbit;
  233. __asm__ __volatile__(
  234. "btcl %2,%1\n\tsbbl %0,%0"
  235. :"=r" (oldbit),"+m" (ADDR)
  236. :"Ir" (nr) : "memory");
  237. return oldbit;
  238. }
  239. /**
  240. * test_and_change_bit - Change a bit and return its old value
  241. * @nr: Bit to change
  242. * @addr: Address to count from
  243. *
  244. * This operation is atomic and cannot be reordered.
  245. * It also implies a memory barrier.
  246. */
  247. static inline int test_and_change_bit(int nr, volatile unsigned long* addr)
  248. {
  249. int oldbit;
  250. __asm__ __volatile__( LOCK_PREFIX
  251. "btcl %2,%1\n\tsbbl %0,%0"
  252. :"=r" (oldbit),"+m" (ADDR)
  253. :"Ir" (nr) : "memory");
  254. return oldbit;
  255. }
  256. #if 0 /* Fool kernel-doc since it doesn't do macros yet */
  257. /**
  258. * test_bit - Determine whether a bit is set
  259. * @nr: bit number to test
  260. * @addr: Address to start counting from
  261. */
  262. static int test_bit(int nr, const volatile void * addr);
  263. #endif
  264. static __always_inline int constant_test_bit(int nr, const volatile unsigned long *addr)
  265. {
  266. return ((1UL << (nr & 31)) & (addr[nr >> 5])) != 0;
  267. }
  268. static inline int variable_test_bit(int nr, const volatile unsigned long * addr)
  269. {
  270. int oldbit;
  271. __asm__ __volatile__(
  272. "btl %2,%1\n\tsbbl %0,%0"
  273. :"=r" (oldbit)
  274. :"m" (ADDR),"Ir" (nr));
  275. return oldbit;
  276. }
  277. #define test_bit(nr,addr) \
  278. (__builtin_constant_p(nr) ? \
  279. constant_test_bit((nr),(addr)) : \
  280. variable_test_bit((nr),(addr)))
  281. #undef ADDR
  282. /**
  283. * find_first_zero_bit - find the first zero bit in a memory region
  284. * @addr: The address to start the search at
  285. * @size: The maximum size to search
  286. *
  287. * Returns the bit-number of the first zero bit, not the number of the byte
  288. * containing a bit.
  289. */
  290. static inline int find_first_zero_bit(const unsigned long *addr, unsigned size)
  291. {
  292. int d0, d1, d2;
  293. int res;
  294. if (!size)
  295. return 0;
  296. /* This looks at memory. Mark it volatile to tell gcc not to move it around */
  297. __asm__ __volatile__(
  298. "movl $-1,%%eax\n\t"
  299. "xorl %%edx,%%edx\n\t"
  300. "repe; scasl\n\t"
  301. "je 1f\n\t"
  302. "xorl -4(%%edi),%%eax\n\t"
  303. "subl $4,%%edi\n\t"
  304. "bsfl %%eax,%%edx\n"
  305. "1:\tsubl %%ebx,%%edi\n\t"
  306. "shll $3,%%edi\n\t"
  307. "addl %%edi,%%edx"
  308. :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
  309. :"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory");
  310. return res;
  311. }
  312. /**
  313. * find_next_zero_bit - find the first zero 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_zero_bit(const unsigned long *addr, int size, int offset);
  319. /**
  320. * __ffs - find first bit in word.
  321. * @word: The word to search
  322. *
  323. * Undefined if no bit exists, so code should check against 0 first.
  324. */
  325. static inline unsigned long __ffs(unsigned long word)
  326. {
  327. __asm__("bsfl %1,%0"
  328. :"=r" (word)
  329. :"rm" (word));
  330. return word;
  331. }
  332. /**
  333. * find_first_bit - find the first set bit in a memory region
  334. * @addr: The address to start the search at
  335. * @size: The maximum size to search
  336. *
  337. * Returns the bit-number of the first set bit, not the number of the byte
  338. * containing a bit.
  339. */
  340. static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)
  341. {
  342. unsigned x = 0;
  343. while (x < size) {
  344. unsigned long val = *addr++;
  345. if (val)
  346. return __ffs(val) + x;
  347. x += (sizeof(*addr)<<3);
  348. }
  349. return x;
  350. }
  351. /**
  352. * find_next_bit - find the first set bit in a memory region
  353. * @addr: The address to base the search on
  354. * @offset: The bitnumber to start searching at
  355. * @size: The maximum size to search
  356. */
  357. int find_next_bit(const unsigned long *addr, int size, int offset);
  358. /**
  359. * ffz - find first zero in word.
  360. * @word: The word to search
  361. *
  362. * Undefined if no zero exists, so code should check against ~0UL first.
  363. */
  364. static inline unsigned long ffz(unsigned long word)
  365. {
  366. __asm__("bsfl %1,%0"
  367. :"=r" (word)
  368. :"r" (~word));
  369. return word;
  370. }
  371. #ifdef __KERNEL__
  372. #include <asm-generic/bitops/sched.h>
  373. /**
  374. * ffs - find first bit set
  375. * @x: the word to search
  376. *
  377. * This is defined the same way as
  378. * the libc and compiler builtin ffs routines, therefore
  379. * differs in spirit from the above ffz() (man ffs).
  380. */
  381. static inline int ffs(int x)
  382. {
  383. int r;
  384. __asm__("bsfl %1,%0\n\t"
  385. "jnz 1f\n\t"
  386. "movl $-1,%0\n"
  387. "1:" : "=r" (r) : "rm" (x));
  388. return r+1;
  389. }
  390. /**
  391. * fls - find last bit set
  392. * @x: the word to search
  393. *
  394. * This is defined the same way as ffs().
  395. */
  396. static inline int fls(int x)
  397. {
  398. int r;
  399. __asm__("bsrl %1,%0\n\t"
  400. "jnz 1f\n\t"
  401. "movl $-1,%0\n"
  402. "1:" : "=r" (r) : "rm" (x));
  403. return r+1;
  404. }
  405. #include <asm-generic/bitops/hweight.h>
  406. #endif /* __KERNEL__ */
  407. #include <asm-generic/bitops/fls64.h>
  408. #ifdef __KERNEL__
  409. #include <asm-generic/bitops/ext2-non-atomic.h>
  410. #define ext2_set_bit_atomic(lock,nr,addr) \
  411. test_and_set_bit((nr),(unsigned long*)addr)
  412. #define ext2_clear_bit_atomic(lock,nr, addr) \
  413. test_and_clear_bit((nr),(unsigned long*)addr)
  414. #include <asm-generic/bitops/minix.h>
  415. #endif /* __KERNEL__ */
  416. #endif /* _I386_BITOPS_H */