bitops.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 1994 - 1997, 1999, 2000 Ralf Baechle (ralf@gnu.org)
  7. * Copyright (c) 1999, 2000 Silicon Graphics, Inc.
  8. */
  9. #ifndef _ASM_BITOPS_H
  10. #define _ASM_BITOPS_H
  11. #include <linux/config.h>
  12. #include <linux/compiler.h>
  13. #include <linux/types.h>
  14. #include <asm/byteorder.h> /* sigh ... */
  15. #include <asm/cpu-features.h>
  16. #if (_MIPS_SZLONG == 32)
  17. #define SZLONG_LOG 5
  18. #define SZLONG_MASK 31UL
  19. #define __LL "ll "
  20. #define __SC "sc "
  21. #define __SET_MIPS ".set mips2 "
  22. #define cpu_to_lelongp(x) cpu_to_le32p((__u32 *) (x))
  23. #elif (_MIPS_SZLONG == 64)
  24. #define SZLONG_LOG 6
  25. #define SZLONG_MASK 63UL
  26. #define __LL "lld "
  27. #define __SC "scd "
  28. #define __SET_MIPS ".set mips3 "
  29. #define cpu_to_lelongp(x) cpu_to_le64p((__u64 *) (x))
  30. #endif
  31. #ifdef __KERNEL__
  32. #include <asm/interrupt.h>
  33. #include <asm/sgidefs.h>
  34. #include <asm/war.h>
  35. /*
  36. * clear_bit() doesn't provide any barrier for the compiler.
  37. */
  38. #define smp_mb__before_clear_bit() smp_mb()
  39. #define smp_mb__after_clear_bit() smp_mb()
  40. /*
  41. * Only disable interrupt for kernel mode stuff to keep usermode stuff
  42. * that dares to use kernel include files alive.
  43. */
  44. #define __bi_flags unsigned long flags
  45. #define __bi_local_irq_save(x) local_irq_save(x)
  46. #define __bi_local_irq_restore(x) local_irq_restore(x)
  47. #else
  48. #define __bi_flags
  49. #define __bi_local_irq_save(x)
  50. #define __bi_local_irq_restore(x)
  51. #endif /* __KERNEL__ */
  52. /*
  53. * set_bit - Atomically set a bit in memory
  54. * @nr: the bit to set
  55. * @addr: the address to start counting from
  56. *
  57. * This function is atomic and may not be reordered. See __set_bit()
  58. * if you do not require the atomic guarantees.
  59. * Note that @nr may be almost arbitrarily large; this function is not
  60. * restricted to acting on a single-word quantity.
  61. */
  62. static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
  63. {
  64. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  65. unsigned long temp;
  66. if (cpu_has_llsc && R10000_LLSC_WAR) {
  67. __asm__ __volatile__(
  68. " " __SET_MIPS " \n"
  69. "1: " __LL "%0, %1 # set_bit \n"
  70. " or %0, %2 \n"
  71. " " __SC "%0, %1 \n"
  72. " beqzl %0, 1b \n"
  73. " .set mips0 \n"
  74. : "=&r" (temp), "=m" (*m)
  75. : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
  76. } else if (cpu_has_llsc) {
  77. __asm__ __volatile__(
  78. " " __SET_MIPS " \n"
  79. "1: " __LL "%0, %1 # set_bit \n"
  80. " or %0, %2 \n"
  81. " " __SC "%0, %1 \n"
  82. " beqz %0, 1b \n"
  83. " .set mips0 \n"
  84. : "=&r" (temp), "=m" (*m)
  85. : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
  86. } else {
  87. volatile unsigned long *a = addr;
  88. unsigned long mask;
  89. __bi_flags;
  90. a += nr >> SZLONG_LOG;
  91. mask = 1UL << (nr & SZLONG_MASK);
  92. __bi_local_irq_save(flags);
  93. *a |= mask;
  94. __bi_local_irq_restore(flags);
  95. }
  96. }
  97. /*
  98. * __set_bit - Set a bit in memory
  99. * @nr: the bit to set
  100. * @addr: the address to start counting from
  101. *
  102. * Unlike set_bit(), this function is non-atomic and may be reordered.
  103. * If it's called on the same region of memory simultaneously, the effect
  104. * may be that only one operation succeeds.
  105. */
  106. static inline void __set_bit(unsigned long nr, volatile unsigned long * addr)
  107. {
  108. unsigned long * m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  109. *m |= 1UL << (nr & SZLONG_MASK);
  110. }
  111. /*
  112. * clear_bit - Clears a bit in memory
  113. * @nr: Bit to clear
  114. * @addr: Address to start counting from
  115. *
  116. * clear_bit() is atomic and may not be reordered. However, it does
  117. * not contain a memory barrier, so if it is used for locking purposes,
  118. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  119. * in order to ensure changes are visible on other processors.
  120. */
  121. static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
  122. {
  123. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  124. unsigned long temp;
  125. if (cpu_has_llsc && R10000_LLSC_WAR) {
  126. __asm__ __volatile__(
  127. " " __SET_MIPS " \n"
  128. "1: " __LL "%0, %1 # clear_bit \n"
  129. " and %0, %2 \n"
  130. " " __SC "%0, %1 \n"
  131. " beqzl %0, 1b \n"
  132. " .set mips0 \n"
  133. : "=&r" (temp), "=m" (*m)
  134. : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
  135. } else if (cpu_has_llsc) {
  136. __asm__ __volatile__(
  137. " " __SET_MIPS " \n"
  138. "1: " __LL "%0, %1 # clear_bit \n"
  139. " and %0, %2 \n"
  140. " " __SC "%0, %1 \n"
  141. " beqz %0, 1b \n"
  142. " .set mips0 \n"
  143. : "=&r" (temp), "=m" (*m)
  144. : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
  145. } else {
  146. volatile unsigned long *a = addr;
  147. unsigned long mask;
  148. __bi_flags;
  149. a += nr >> SZLONG_LOG;
  150. mask = 1UL << (nr & SZLONG_MASK);
  151. __bi_local_irq_save(flags);
  152. *a &= ~mask;
  153. __bi_local_irq_restore(flags);
  154. }
  155. }
  156. /*
  157. * __clear_bit - Clears a bit in memory
  158. * @nr: Bit to clear
  159. * @addr: Address to start counting from
  160. *
  161. * Unlike clear_bit(), this function is non-atomic and may be reordered.
  162. * If it's called on the same region of memory simultaneously, the effect
  163. * may be that only one operation succeeds.
  164. */
  165. static inline void __clear_bit(unsigned long nr, volatile unsigned long * addr)
  166. {
  167. unsigned long * m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  168. *m &= ~(1UL << (nr & SZLONG_MASK));
  169. }
  170. /*
  171. * change_bit - Toggle a bit in memory
  172. * @nr: Bit to change
  173. * @addr: Address to start counting from
  174. *
  175. * change_bit() is atomic and may not be reordered.
  176. * Note that @nr may be almost arbitrarily large; this function is not
  177. * restricted to acting on a single-word quantity.
  178. */
  179. static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
  180. {
  181. if (cpu_has_llsc && R10000_LLSC_WAR) {
  182. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  183. unsigned long temp;
  184. __asm__ __volatile__(
  185. " " __SET_MIPS " \n"
  186. "1: " __LL "%0, %1 # change_bit \n"
  187. " xor %0, %2 \n"
  188. " " __SC "%0, %1 \n"
  189. " beqzl %0, 1b \n"
  190. " .set mips0 \n"
  191. : "=&r" (temp), "=m" (*m)
  192. : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
  193. } else if (cpu_has_llsc) {
  194. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  195. unsigned long temp;
  196. __asm__ __volatile__(
  197. " " __SET_MIPS " \n"
  198. "1: " __LL "%0, %1 # change_bit \n"
  199. " xor %0, %2 \n"
  200. " " __SC "%0, %1 \n"
  201. " beqz %0, 1b \n"
  202. " .set mips0 \n"
  203. : "=&r" (temp), "=m" (*m)
  204. : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
  205. } else {
  206. volatile unsigned long *a = addr;
  207. unsigned long mask;
  208. __bi_flags;
  209. a += nr >> SZLONG_LOG;
  210. mask = 1UL << (nr & SZLONG_MASK);
  211. __bi_local_irq_save(flags);
  212. *a ^= mask;
  213. __bi_local_irq_restore(flags);
  214. }
  215. }
  216. /*
  217. * __change_bit - Toggle a bit in memory
  218. * @nr: the bit to change
  219. * @addr: the address to start counting from
  220. *
  221. * Unlike change_bit(), this function is non-atomic and may be reordered.
  222. * If it's called on the same region of memory simultaneously, the effect
  223. * may be that only one operation succeeds.
  224. */
  225. static inline void __change_bit(unsigned long nr, volatile unsigned long * addr)
  226. {
  227. unsigned long * m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  228. *m ^= 1UL << (nr & SZLONG_MASK);
  229. }
  230. /*
  231. * test_and_set_bit - Set a bit and return its old value
  232. * @nr: Bit to set
  233. * @addr: Address to count from
  234. *
  235. * This operation is atomic and cannot be reordered.
  236. * It also implies a memory barrier.
  237. */
  238. static inline int test_and_set_bit(unsigned long nr,
  239. volatile unsigned long *addr)
  240. {
  241. if (cpu_has_llsc && R10000_LLSC_WAR) {
  242. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  243. unsigned long temp, res;
  244. __asm__ __volatile__(
  245. " " __SET_MIPS " \n"
  246. "1: " __LL "%0, %1 # test_and_set_bit \n"
  247. " or %2, %0, %3 \n"
  248. " " __SC "%2, %1 \n"
  249. " beqzl %2, 1b \n"
  250. " and %2, %0, %3 \n"
  251. #ifdef CONFIG_SMP
  252. " sync \n"
  253. #endif
  254. " .set mips0 \n"
  255. : "=&r" (temp), "=m" (*m), "=&r" (res)
  256. : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
  257. : "memory");
  258. return res != 0;
  259. } else if (cpu_has_llsc) {
  260. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  261. unsigned long temp, res;
  262. __asm__ __volatile__(
  263. " .set push \n"
  264. " .set noreorder \n"
  265. " " __SET_MIPS " \n"
  266. "1: " __LL "%0, %1 # test_and_set_bit \n"
  267. " or %2, %0, %3 \n"
  268. " " __SC "%2, %1 \n"
  269. " beqz %2, 1b \n"
  270. " and %2, %0, %3 \n"
  271. #ifdef CONFIG_SMP
  272. " sync \n"
  273. #endif
  274. " .set pop \n"
  275. : "=&r" (temp), "=m" (*m), "=&r" (res)
  276. : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
  277. : "memory");
  278. return res != 0;
  279. } else {
  280. volatile unsigned long *a = addr;
  281. unsigned long mask;
  282. int retval;
  283. __bi_flags;
  284. a += nr >> SZLONG_LOG;
  285. mask = 1UL << (nr & SZLONG_MASK);
  286. __bi_local_irq_save(flags);
  287. retval = (mask & *a) != 0;
  288. *a |= mask;
  289. __bi_local_irq_restore(flags);
  290. return retval;
  291. }
  292. }
  293. /*
  294. * __test_and_set_bit - Set a bit and return its old value
  295. * @nr: Bit to set
  296. * @addr: Address to count from
  297. *
  298. * This operation is non-atomic and can be reordered.
  299. * If two examples of this operation race, one can appear to succeed
  300. * but actually fail. You must protect multiple accesses with a lock.
  301. */
  302. static inline int __test_and_set_bit(unsigned long nr,
  303. volatile unsigned long *addr)
  304. {
  305. volatile unsigned long *a = addr;
  306. unsigned long mask;
  307. int retval;
  308. a += nr >> SZLONG_LOG;
  309. mask = 1UL << (nr & SZLONG_MASK);
  310. retval = (mask & *a) != 0;
  311. *a |= mask;
  312. return retval;
  313. }
  314. /*
  315. * test_and_clear_bit - Clear a bit and return its old value
  316. * @nr: Bit to clear
  317. * @addr: Address to count from
  318. *
  319. * This operation is atomic and cannot be reordered.
  320. * It also implies a memory barrier.
  321. */
  322. static inline int test_and_clear_bit(unsigned long nr,
  323. volatile unsigned long *addr)
  324. {
  325. if (cpu_has_llsc && R10000_LLSC_WAR) {
  326. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  327. unsigned long temp, res;
  328. __asm__ __volatile__(
  329. " " __SET_MIPS " \n"
  330. "1: " __LL "%0, %1 # test_and_clear_bit \n"
  331. " or %2, %0, %3 \n"
  332. " xor %2, %3 \n"
  333. " " __SC "%2, %1 \n"
  334. " beqzl %2, 1b \n"
  335. " and %2, %0, %3 \n"
  336. #ifdef CONFIG_SMP
  337. " sync \n"
  338. #endif
  339. " .set mips0 \n"
  340. : "=&r" (temp), "=m" (*m), "=&r" (res)
  341. : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
  342. : "memory");
  343. return res != 0;
  344. } else if (cpu_has_llsc) {
  345. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  346. unsigned long temp, res;
  347. __asm__ __volatile__(
  348. " .set push \n"
  349. " .set noreorder \n"
  350. " " __SET_MIPS " \n"
  351. "1: " __LL "%0, %1 # test_and_clear_bit \n"
  352. " or %2, %0, %3 \n"
  353. " xor %2, %3 \n"
  354. " " __SC "%2, %1 \n"
  355. " beqz %2, 1b \n"
  356. " and %2, %0, %3 \n"
  357. #ifdef CONFIG_SMP
  358. " sync \n"
  359. #endif
  360. " .set pop \n"
  361. : "=&r" (temp), "=m" (*m), "=&r" (res)
  362. : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
  363. : "memory");
  364. return res != 0;
  365. } else {
  366. volatile unsigned long *a = addr;
  367. unsigned long mask;
  368. int retval;
  369. __bi_flags;
  370. a += nr >> SZLONG_LOG;
  371. mask = 1UL << (nr & SZLONG_MASK);
  372. __bi_local_irq_save(flags);
  373. retval = (mask & *a) != 0;
  374. *a &= ~mask;
  375. __bi_local_irq_restore(flags);
  376. return retval;
  377. }
  378. }
  379. /*
  380. * __test_and_clear_bit - Clear a bit and return its old value
  381. * @nr: Bit to clear
  382. * @addr: Address to count from
  383. *
  384. * This operation is non-atomic and can be reordered.
  385. * If two examples of this operation race, one can appear to succeed
  386. * but actually fail. You must protect multiple accesses with a lock.
  387. */
  388. static inline int __test_and_clear_bit(unsigned long nr,
  389. volatile unsigned long * addr)
  390. {
  391. volatile unsigned long *a = addr;
  392. unsigned long mask;
  393. int retval;
  394. a += (nr >> SZLONG_LOG);
  395. mask = 1UL << (nr & SZLONG_MASK);
  396. retval = ((mask & *a) != 0);
  397. *a &= ~mask;
  398. return retval;
  399. }
  400. /*
  401. * test_and_change_bit - Change a bit and return its old value
  402. * @nr: Bit to change
  403. * @addr: Address to count from
  404. *
  405. * This operation is atomic and cannot be reordered.
  406. * It also implies a memory barrier.
  407. */
  408. static inline int test_and_change_bit(unsigned long nr,
  409. volatile unsigned long *addr)
  410. {
  411. if (cpu_has_llsc && R10000_LLSC_WAR) {
  412. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  413. unsigned long temp, res;
  414. __asm__ __volatile__(
  415. " " __SET_MIPS " \n"
  416. "1: " __LL "%0, %1 # test_and_change_bit \n"
  417. " xor %2, %0, %3 \n"
  418. " " __SC "%2, %1 \n"
  419. " beqzl %2, 1b \n"
  420. " and %2, %0, %3 \n"
  421. #ifdef CONFIG_SMP
  422. " sync \n"
  423. #endif
  424. " .set mips0 \n"
  425. : "=&r" (temp), "=m" (*m), "=&r" (res)
  426. : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
  427. : "memory");
  428. return res != 0;
  429. } else if (cpu_has_llsc) {
  430. unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
  431. unsigned long temp, res;
  432. __asm__ __volatile__(
  433. " .set push \n"
  434. " .set noreorder \n"
  435. " " __SET_MIPS " \n"
  436. "1: " __LL "%0, %1 # test_and_change_bit \n"
  437. " xor %2, %0, %3 \n"
  438. " " __SC "\t%2, %1 \n"
  439. " beqz %2, 1b \n"
  440. " and %2, %0, %3 \n"
  441. #ifdef CONFIG_SMP
  442. " sync \n"
  443. #endif
  444. " .set pop \n"
  445. : "=&r" (temp), "=m" (*m), "=&r" (res)
  446. : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
  447. : "memory");
  448. return res != 0;
  449. } else {
  450. volatile unsigned long *a = addr;
  451. unsigned long mask, retval;
  452. __bi_flags;
  453. a += nr >> SZLONG_LOG;
  454. mask = 1UL << (nr & SZLONG_MASK);
  455. __bi_local_irq_save(flags);
  456. retval = (mask & *a) != 0;
  457. *a ^= mask;
  458. __bi_local_irq_restore(flags);
  459. return retval;
  460. }
  461. }
  462. /*
  463. * __test_and_change_bit - Change a bit and return its old value
  464. * @nr: Bit to change
  465. * @addr: Address to count from
  466. *
  467. * This operation is non-atomic and can be reordered.
  468. * If two examples of this operation race, one can appear to succeed
  469. * but actually fail. You must protect multiple accesses with a lock.
  470. */
  471. static inline int __test_and_change_bit(unsigned long nr,
  472. volatile unsigned long *addr)
  473. {
  474. volatile unsigned long *a = addr;
  475. unsigned long mask;
  476. int retval;
  477. a += (nr >> SZLONG_LOG);
  478. mask = 1UL << (nr & SZLONG_MASK);
  479. retval = ((mask & *a) != 0);
  480. *a ^= mask;
  481. return retval;
  482. }
  483. #undef __bi_flags
  484. #undef __bi_local_irq_save
  485. #undef __bi_local_irq_restore
  486. /*
  487. * test_bit - Determine whether a bit is set
  488. * @nr: bit number to test
  489. * @addr: Address to start counting from
  490. */
  491. static inline int test_bit(unsigned long nr, const volatile unsigned long *addr)
  492. {
  493. return 1UL & (addr[nr >> SZLONG_LOG] >> (nr & SZLONG_MASK));
  494. }
  495. /*
  496. * ffz - find first zero in word.
  497. * @word: The word to search
  498. *
  499. * Undefined if no zero exists, so code should check against ~0UL first.
  500. */
  501. static inline unsigned long ffz(unsigned long word)
  502. {
  503. int b = 0, s;
  504. word = ~word;
  505. #ifdef CONFIG_32BIT
  506. s = 16; if (word << 16 != 0) s = 0; b += s; word >>= s;
  507. s = 8; if (word << 24 != 0) s = 0; b += s; word >>= s;
  508. s = 4; if (word << 28 != 0) s = 0; b += s; word >>= s;
  509. s = 2; if (word << 30 != 0) s = 0; b += s; word >>= s;
  510. s = 1; if (word << 31 != 0) s = 0; b += s;
  511. #endif
  512. #ifdef CONFIG_64BIT
  513. s = 32; if (word << 32 != 0) s = 0; b += s; word >>= s;
  514. s = 16; if (word << 48 != 0) s = 0; b += s; word >>= s;
  515. s = 8; if (word << 56 != 0) s = 0; b += s; word >>= s;
  516. s = 4; if (word << 60 != 0) s = 0; b += s; word >>= s;
  517. s = 2; if (word << 62 != 0) s = 0; b += s; word >>= s;
  518. s = 1; if (word << 63 != 0) s = 0; b += s;
  519. #endif
  520. return b;
  521. }
  522. /*
  523. * __ffs - find first bit in word.
  524. * @word: The word to search
  525. *
  526. * Undefined if no bit exists, so code should check against 0 first.
  527. */
  528. static inline unsigned long __ffs(unsigned long word)
  529. {
  530. return ffz(~word);
  531. }
  532. /*
  533. * fls: find last bit set.
  534. */
  535. #define fls(x) generic_fls(x)
  536. /*
  537. * find_next_zero_bit - find the first zero bit in a memory region
  538. * @addr: The address to base the search on
  539. * @offset: The bitnumber to start searching at
  540. * @size: The maximum size to search
  541. */
  542. static inline unsigned long find_next_zero_bit(const unsigned long *addr,
  543. unsigned long size, unsigned long offset)
  544. {
  545. const unsigned long *p = addr + (offset >> SZLONG_LOG);
  546. unsigned long result = offset & ~SZLONG_MASK;
  547. unsigned long tmp;
  548. if (offset >= size)
  549. return size;
  550. size -= result;
  551. offset &= SZLONG_MASK;
  552. if (offset) {
  553. tmp = *(p++);
  554. tmp |= ~0UL >> (_MIPS_SZLONG-offset);
  555. if (size < _MIPS_SZLONG)
  556. goto found_first;
  557. if (~tmp)
  558. goto found_middle;
  559. size -= _MIPS_SZLONG;
  560. result += _MIPS_SZLONG;
  561. }
  562. while (size & ~SZLONG_MASK) {
  563. if (~(tmp = *(p++)))
  564. goto found_middle;
  565. result += _MIPS_SZLONG;
  566. size -= _MIPS_SZLONG;
  567. }
  568. if (!size)
  569. return result;
  570. tmp = *p;
  571. found_first:
  572. tmp |= ~0UL << size;
  573. if (tmp == ~0UL) /* Are any bits zero? */
  574. return result + size; /* Nope. */
  575. found_middle:
  576. return result + ffz(tmp);
  577. }
  578. #define find_first_zero_bit(addr, size) \
  579. find_next_zero_bit((addr), (size), 0)
  580. /*
  581. * find_next_bit - find the next set bit in a memory region
  582. * @addr: The address to base the search on
  583. * @offset: The bitnumber to start searching at
  584. * @size: The maximum size to search
  585. */
  586. static inline unsigned long find_next_bit(const unsigned long *addr,
  587. unsigned long size, unsigned long offset)
  588. {
  589. const unsigned long *p = addr + (offset >> SZLONG_LOG);
  590. unsigned long result = offset & ~SZLONG_MASK;
  591. unsigned long tmp;
  592. if (offset >= size)
  593. return size;
  594. size -= result;
  595. offset &= SZLONG_MASK;
  596. if (offset) {
  597. tmp = *(p++);
  598. tmp &= ~0UL << offset;
  599. if (size < _MIPS_SZLONG)
  600. goto found_first;
  601. if (tmp)
  602. goto found_middle;
  603. size -= _MIPS_SZLONG;
  604. result += _MIPS_SZLONG;
  605. }
  606. while (size & ~SZLONG_MASK) {
  607. if ((tmp = *(p++)))
  608. goto found_middle;
  609. result += _MIPS_SZLONG;
  610. size -= _MIPS_SZLONG;
  611. }
  612. if (!size)
  613. return result;
  614. tmp = *p;
  615. found_first:
  616. tmp &= ~0UL >> (_MIPS_SZLONG - size);
  617. if (tmp == 0UL) /* Are any bits set? */
  618. return result + size; /* Nope. */
  619. found_middle:
  620. return result + __ffs(tmp);
  621. }
  622. /*
  623. * find_first_bit - find the first set bit in a memory region
  624. * @addr: The address to start the search at
  625. * @size: The maximum size to search
  626. *
  627. * Returns the bit-number of the first set bit, not the number of the byte
  628. * containing a bit.
  629. */
  630. #define find_first_bit(addr, size) \
  631. find_next_bit((addr), (size), 0)
  632. #ifdef __KERNEL__
  633. /*
  634. * Every architecture must define this function. It's the fastest
  635. * way of searching a 140-bit bitmap where the first 100 bits are
  636. * unlikely to be set. It's guaranteed that at least one of the 140
  637. * bits is cleared.
  638. */
  639. static inline int sched_find_first_bit(const unsigned long *b)
  640. {
  641. #ifdef CONFIG_32BIT
  642. if (unlikely(b[0]))
  643. return __ffs(b[0]);
  644. if (unlikely(b[1]))
  645. return __ffs(b[1]) + 32;
  646. if (unlikely(b[2]))
  647. return __ffs(b[2]) + 64;
  648. if (b[3])
  649. return __ffs(b[3]) + 96;
  650. return __ffs(b[4]) + 128;
  651. #endif
  652. #ifdef CONFIG_64BIT
  653. if (unlikely(b[0]))
  654. return __ffs(b[0]);
  655. if (unlikely(b[1]))
  656. return __ffs(b[1]) + 64;
  657. return __ffs(b[2]) + 128;
  658. #endif
  659. }
  660. /*
  661. * ffs - find first bit set
  662. * @x: the word to search
  663. *
  664. * This is defined the same way as
  665. * the libc and compiler builtin ffs routines, therefore
  666. * differs in spirit from the above ffz (man ffs).
  667. */
  668. #define ffs(x) generic_ffs(x)
  669. /*
  670. * hweightN - returns the hamming weight of a N-bit word
  671. * @x: the word to weigh
  672. *
  673. * The Hamming Weight of a number is the total number of bits set in it.
  674. */
  675. #define hweight64(x) generic_hweight64(x)
  676. #define hweight32(x) generic_hweight32(x)
  677. #define hweight16(x) generic_hweight16(x)
  678. #define hweight8(x) generic_hweight8(x)
  679. static inline int __test_and_set_le_bit(unsigned long nr, unsigned long *addr)
  680. {
  681. unsigned char *ADDR = (unsigned char *) addr;
  682. int mask, retval;
  683. ADDR += nr >> 3;
  684. mask = 1 << (nr & 0x07);
  685. retval = (mask & *ADDR) != 0;
  686. *ADDR |= mask;
  687. return retval;
  688. }
  689. static inline int __test_and_clear_le_bit(unsigned long nr, unsigned long *addr)
  690. {
  691. unsigned char *ADDR = (unsigned char *) addr;
  692. int mask, retval;
  693. ADDR += nr >> 3;
  694. mask = 1 << (nr & 0x07);
  695. retval = (mask & *ADDR) != 0;
  696. *ADDR &= ~mask;
  697. return retval;
  698. }
  699. static inline int test_le_bit(unsigned long nr, const unsigned long * addr)
  700. {
  701. const unsigned char *ADDR = (const unsigned char *) addr;
  702. int mask;
  703. ADDR += nr >> 3;
  704. mask = 1 << (nr & 0x07);
  705. return ((mask & *ADDR) != 0);
  706. }
  707. static inline unsigned long find_next_zero_le_bit(unsigned long *addr,
  708. unsigned long size, unsigned long offset)
  709. {
  710. unsigned long *p = ((unsigned long *) addr) + (offset >> SZLONG_LOG);
  711. unsigned long result = offset & ~SZLONG_MASK;
  712. unsigned long tmp;
  713. if (offset >= size)
  714. return size;
  715. size -= result;
  716. offset &= SZLONG_MASK;
  717. if (offset) {
  718. tmp = cpu_to_lelongp(p++);
  719. tmp |= ~0UL >> (_MIPS_SZLONG-offset); /* bug or feature ? */
  720. if (size < _MIPS_SZLONG)
  721. goto found_first;
  722. if (~tmp)
  723. goto found_middle;
  724. size -= _MIPS_SZLONG;
  725. result += _MIPS_SZLONG;
  726. }
  727. while (size & ~SZLONG_MASK) {
  728. if (~(tmp = cpu_to_lelongp(p++)))
  729. goto found_middle;
  730. result += _MIPS_SZLONG;
  731. size -= _MIPS_SZLONG;
  732. }
  733. if (!size)
  734. return result;
  735. tmp = cpu_to_lelongp(p);
  736. found_first:
  737. tmp |= ~0UL << size;
  738. if (tmp == ~0UL) /* Are any bits zero? */
  739. return result + size; /* Nope. */
  740. found_middle:
  741. return result + ffz(tmp);
  742. }
  743. #define find_first_zero_le_bit(addr, size) \
  744. find_next_zero_le_bit((addr), (size), 0)
  745. #define ext2_set_bit(nr,addr) \
  746. __test_and_set_le_bit((nr),(unsigned long*)addr)
  747. #define ext2_clear_bit(nr, addr) \
  748. __test_and_clear_le_bit((nr),(unsigned long*)addr)
  749. #define ext2_set_bit_atomic(lock, nr, addr) \
  750. ({ \
  751. int ret; \
  752. spin_lock(lock); \
  753. ret = ext2_set_bit((nr), (addr)); \
  754. spin_unlock(lock); \
  755. ret; \
  756. })
  757. #define ext2_clear_bit_atomic(lock, nr, addr) \
  758. ({ \
  759. int ret; \
  760. spin_lock(lock); \
  761. ret = ext2_clear_bit((nr), (addr)); \
  762. spin_unlock(lock); \
  763. ret; \
  764. })
  765. #define ext2_test_bit(nr, addr) test_le_bit((nr),(unsigned long*)addr)
  766. #define ext2_find_first_zero_bit(addr, size) \
  767. find_first_zero_le_bit((unsigned long*)addr, size)
  768. #define ext2_find_next_zero_bit(addr, size, off) \
  769. find_next_zero_le_bit((unsigned long*)addr, size, off)
  770. /*
  771. * Bitmap functions for the minix filesystem.
  772. *
  773. * FIXME: These assume that Minix uses the native byte/bitorder.
  774. * This limits the Minix filesystem's value for data exchange very much.
  775. */
  776. #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
  777. #define minix_set_bit(nr,addr) set_bit(nr,addr)
  778. #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
  779. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  780. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  781. #endif /* __KERNEL__ */
  782. #endif /* _ASM_BITOPS_H */