bitops.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:22k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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