bitops.h 9.4 KB

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