bitops.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #ifndef _I386_BITOPS_H
  2. #define _I386_BITOPS_H
  3. /*
  4. * Copyright 1992, Linus Torvalds.
  5. */
  6. /*
  7. * These have to be done with inline assembly: that way the bit-setting
  8. * is guaranteed to be atomic. All bit operations return 0 if the bit
  9. * was cleared before the operation and != 0 if it was not.
  10. *
  11. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  12. */
  13. #include <asm-generic/bitops/fls.h>
  14. #include <asm-generic/bitops/__fls.h>
  15. #include <asm-generic/bitops/fls64.h>
  16. #ifdef CONFIG_SMP
  17. #define LOCK_PREFIX "lock ; "
  18. #else
  19. #define LOCK_PREFIX ""
  20. #endif
  21. #define ADDR (*(volatile long *) addr)
  22. /**
  23. * set_bit - Atomically set a bit in memory
  24. * @nr: the bit to set
  25. * @addr: the address to start counting from
  26. *
  27. * This function is atomic and may not be reordered. See __set_bit()
  28. * if you do not require the atomic guarantees.
  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 void * 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 void * 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 void * addr)
  66. {
  67. __asm__ __volatile__( LOCK_PREFIX
  68. "btrl %1,%0"
  69. :"=m" (ADDR)
  70. :"Ir" (nr));
  71. }
  72. #define smp_mb__before_clear_bit() barrier()
  73. #define smp_mb__after_clear_bit() barrier()
  74. /**
  75. * __change_bit - Toggle a bit in memory
  76. * @nr: the bit to set
  77. * @addr: the address to start counting from
  78. *
  79. * Unlike change_bit(), this function is non-atomic and may be reordered.
  80. * If it's called on the same region of memory simultaneously, the effect
  81. * may be that only one operation succeeds.
  82. */
  83. static __inline__ void __change_bit(int nr, volatile void * addr)
  84. {
  85. __asm__ __volatile__(
  86. "btcl %1,%0"
  87. :"=m" (ADDR)
  88. :"Ir" (nr));
  89. }
  90. /**
  91. * change_bit - Toggle a bit in memory
  92. * @nr: Bit to clear
  93. * @addr: Address to start counting from
  94. *
  95. * change_bit() is atomic and may not be reordered.
  96. * Note that @nr may be almost arbitrarily large; this function is not
  97. * restricted to acting on a single-word quantity.
  98. */
  99. static __inline__ void change_bit(int nr, volatile void * addr)
  100. {
  101. __asm__ __volatile__( LOCK_PREFIX
  102. "btcl %1,%0"
  103. :"=m" (ADDR)
  104. :"Ir" (nr));
  105. }
  106. /**
  107. * test_and_set_bit - Set a bit and return its old value
  108. * @nr: Bit to set
  109. * @addr: Address to count from
  110. *
  111. * This operation is atomic and cannot be reordered.
  112. * It also implies a memory barrier.
  113. */
  114. static __inline__ int test_and_set_bit(int nr, volatile void * addr)
  115. {
  116. int oldbit;
  117. __asm__ __volatile__( LOCK_PREFIX
  118. "btsl %2,%1\n\tsbbl %0,%0"
  119. :"=r" (oldbit),"=m" (ADDR)
  120. :"Ir" (nr) : "memory");
  121. return oldbit;
  122. }
  123. /**
  124. * __test_and_set_bit - Set a bit and return its old value
  125. * @nr: Bit to set
  126. * @addr: Address to count from
  127. *
  128. * This operation is non-atomic and can be reordered.
  129. * If two examples of this operation race, one can appear to succeed
  130. * but actually fail. You must protect multiple accesses with a lock.
  131. */
  132. static __inline__ int __test_and_set_bit(int nr, volatile void * addr)
  133. {
  134. int oldbit;
  135. __asm__(
  136. "btsl %2,%1\n\tsbbl %0,%0"
  137. :"=r" (oldbit),"=m" (ADDR)
  138. :"Ir" (nr));
  139. return oldbit;
  140. }
  141. /**
  142. * test_and_clear_bit - Clear a bit and return its old value
  143. * @nr: Bit to set
  144. * @addr: Address to count from
  145. *
  146. * This operation is atomic and cannot be reordered.
  147. * It also implies a memory barrier.
  148. */
  149. static __inline__ int test_and_clear_bit(int nr, volatile void * addr)
  150. {
  151. int oldbit;
  152. __asm__ __volatile__( LOCK_PREFIX
  153. "btrl %2,%1\n\tsbbl %0,%0"
  154. :"=r" (oldbit),"=m" (ADDR)
  155. :"Ir" (nr) : "memory");
  156. return oldbit;
  157. }
  158. /**
  159. * __test_and_clear_bit - Clear a bit and return its old value
  160. * @nr: Bit to set
  161. * @addr: Address to count from
  162. *
  163. * This operation is non-atomic and can be reordered.
  164. * If two examples of this operation race, one can appear to succeed
  165. * but actually fail. You must protect multiple accesses with a lock.
  166. */
  167. static __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
  168. {
  169. int oldbit;
  170. __asm__(
  171. "btrl %2,%1\n\tsbbl %0,%0"
  172. :"=r" (oldbit),"=m" (ADDR)
  173. :"Ir" (nr));
  174. return oldbit;
  175. }
  176. /* WARNING: non atomic and it can be reordered! */
  177. static __inline__ int __test_and_change_bit(int nr, volatile void * addr)
  178. {
  179. int oldbit;
  180. __asm__ __volatile__(
  181. "btcl %2,%1\n\tsbbl %0,%0"
  182. :"=r" (oldbit),"=m" (ADDR)
  183. :"Ir" (nr) : "memory");
  184. return oldbit;
  185. }
  186. /**
  187. * test_and_change_bit - Change a bit and return its new value
  188. * @nr: Bit to set
  189. * @addr: Address to count from
  190. *
  191. * This operation is atomic and cannot be reordered.
  192. * It also implies a memory barrier.
  193. */
  194. static __inline__ int test_and_change_bit(int nr, volatile void * addr)
  195. {
  196. int oldbit;
  197. __asm__ __volatile__( LOCK_PREFIX
  198. "btcl %2,%1\n\tsbbl %0,%0"
  199. :"=r" (oldbit),"=m" (ADDR)
  200. :"Ir" (nr) : "memory");
  201. return oldbit;
  202. }
  203. #if 0 /* Fool kernel-doc since it doesn't do macros yet */
  204. /**
  205. * test_bit - Determine whether a bit is set
  206. * @nr: bit number to test
  207. * @addr: Address to start counting from
  208. */
  209. static int test_bit(int nr, const volatile void * addr);
  210. #endif
  211. static __inline__ int constant_test_bit(int nr, const volatile void * addr)
  212. {
  213. return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
  214. }
  215. static __inline__ int variable_test_bit(int nr, volatile void * addr)
  216. {
  217. int oldbit;
  218. __asm__ __volatile__(
  219. "btl %2,%1\n\tsbbl %0,%0"
  220. :"=r" (oldbit)
  221. :"m" (ADDR),"Ir" (nr));
  222. return oldbit;
  223. }
  224. #define test_bit(nr,addr) \
  225. (__builtin_constant_p(nr) ? \
  226. constant_test_bit((nr),(addr)) : \
  227. variable_test_bit((nr),(addr)))
  228. /**
  229. * find_first_zero_bit - find the first zero bit in a memory region
  230. * @addr: The address to start the search at
  231. * @size: The maximum size to search
  232. *
  233. * Returns the bit-number of the first zero bit, not the number of the byte
  234. * containing a bit.
  235. */
  236. static __inline__ int find_first_zero_bit(void * addr, unsigned size)
  237. {
  238. int d0, d1, d2;
  239. int res;
  240. if (!size)
  241. return 0;
  242. /* This looks at memory. Mark it volatile to tell gcc not to move it around */
  243. __asm__ __volatile__(
  244. "movl $-1,%%eax\n\t"
  245. "xorl %%edx,%%edx\n\t"
  246. "repe; scasl\n\t"
  247. "je 1f\n\t"
  248. "xorl -4(%%edi),%%eax\n\t"
  249. "subl $4,%%edi\n\t"
  250. "bsfl %%eax,%%edx\n"
  251. "1:\tsubl %%ebx,%%edi\n\t"
  252. "shll $3,%%edi\n\t"
  253. "addl %%edi,%%edx"
  254. :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
  255. :"1" ((size + 31) >> 5), "2" (addr), "b" (addr));
  256. return res;
  257. }
  258. /**
  259. * find_next_zero_bit - find the first zero bit in a memory region
  260. * @addr: The address to base the search on
  261. * @offset: The bitnumber to start searching at
  262. * @size: The maximum size to search
  263. */
  264. static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
  265. {
  266. unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
  267. int set = 0, bit = offset & 31, res;
  268. if (bit) {
  269. /*
  270. * Look for zero in first byte
  271. */
  272. __asm__("bsfl %1,%0\n\t"
  273. "jne 1f\n\t"
  274. "movl $32, %0\n"
  275. "1:"
  276. : "=r" (set)
  277. : "r" (~(*p >> bit)));
  278. if (set < (32 - bit))
  279. return set + offset;
  280. set = 32 - bit;
  281. p++;
  282. }
  283. /*
  284. * No zero yet, search remaining full bytes for a zero
  285. */
  286. res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
  287. return (offset + set + res);
  288. }
  289. /**
  290. * ffz - find first zero in word.
  291. * @word: The word to search
  292. *
  293. * Undefined if no zero exists, so code should check against ~0UL first.
  294. */
  295. static __inline__ unsigned long ffz(unsigned long word)
  296. {
  297. __asm__("bsfl %1,%0"
  298. :"=r" (word)
  299. :"r" (~word));
  300. return word;
  301. }
  302. #ifdef __KERNEL__
  303. /**
  304. * __ffs - find first set bit in word
  305. * @word: The word to search
  306. *
  307. * Undefined if no bit exists, so code should check against 0 first.
  308. */
  309. static inline unsigned long __ffs(unsigned long word)
  310. {
  311. __asm__("rep; bsf %1,%0"
  312. : "=r" (word)
  313. : "rm" (word));
  314. return word;
  315. }
  316. /**
  317. * ffs - find first bit set
  318. * @x: the word to search
  319. *
  320. * This is defined the same way as
  321. * the libc and compiler builtin ffs routines, therefore
  322. * differs in spirit from the above ffz (man ffs).
  323. */
  324. static __inline__ int ffs(int x)
  325. {
  326. int r;
  327. __asm__("bsfl %1,%0\n\t"
  328. "jnz 1f\n\t"
  329. "movl $-1,%0\n"
  330. "1:" : "=r" (r) : "rm" (x));
  331. return r+1;
  332. }
  333. #define PLATFORM_FFS
  334. static inline int __ilog2(unsigned int x)
  335. {
  336. return generic_fls(x) - 1;
  337. }
  338. /**
  339. * hweightN - returns the hamming weight of a N-bit word
  340. * @x: the word to weigh
  341. *
  342. * The Hamming Weight of a number is the total number of bits set in it.
  343. */
  344. #define hweight32(x) generic_hweight32(x)
  345. #define hweight16(x) generic_hweight16(x)
  346. #define hweight8(x) generic_hweight8(x)
  347. #endif /* __KERNEL__ */
  348. #ifdef __KERNEL__
  349. #define ext2_set_bit __test_and_set_bit
  350. #define ext2_clear_bit __test_and_clear_bit
  351. #define ext2_test_bit test_bit
  352. #define ext2_find_first_zero_bit find_first_zero_bit
  353. #define ext2_find_next_zero_bit find_next_zero_bit
  354. /* Bitmap functions for the minix filesystem. */
  355. #define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,addr)
  356. #define minix_set_bit(nr,addr) __set_bit(nr,addr)
  357. #define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,addr)
  358. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  359. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  360. #endif /* __KERNEL__ */
  361. #endif /* _I386_BITOPS_H */