bitops.h 22 KB

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