bitops.h 9.4 KB

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