bitops.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 1995, Russell King.
  3. * Various bits and pieces copyrights include:
  4. * Linus Torvalds (test_bit).
  5. *
  6. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  7. *
  8. * Please note that the code in this file should never be included
  9. * from user space. Many of these are not implemented in assembler
  10. * since they would be too costly. Also, they require priviledged
  11. * instructions (which are not available from user mode) to ensure
  12. * that they are atomic.
  13. */
  14. #ifndef __ASM_ARM_BITOPS_H
  15. #define __ASM_ARM_BITOPS_H
  16. #ifdef __KERNEL__
  17. #include <asm/proc/system.h>
  18. #define smp_mb__before_clear_bit() do { } while (0)
  19. #define smp_mb__after_clear_bit() do { } while (0)
  20. /*
  21. * Function prototypes to keep gcc -Wall happy.
  22. */
  23. extern void set_bit(int nr, volatile void * addr);
  24. extern void clear_bit(int nr, volatile void * addr);
  25. extern void change_bit(int nr, volatile void * addr);
  26. static inline void __change_bit(int nr, volatile void *addr)
  27. {
  28. unsigned long mask = BIT_MASK(nr);
  29. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  30. *p ^= mask;
  31. }
  32. static inline int __test_and_set_bit(int nr, volatile void *addr)
  33. {
  34. unsigned long mask = BIT_MASK(nr);
  35. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  36. unsigned long old = *p;
  37. *p = old | mask;
  38. return (old & mask) != 0;
  39. }
  40. static inline int test_and_set_bit(int nr, volatile void * addr)
  41. {
  42. unsigned long flags;
  43. int out;
  44. local_irq_save(flags);
  45. out = __test_and_set_bit(nr, addr);
  46. local_irq_restore(flags);
  47. return out;
  48. }
  49. static inline int __test_and_clear_bit(int nr, volatile void *addr)
  50. {
  51. unsigned long mask = BIT_MASK(nr);
  52. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  53. unsigned long old = *p;
  54. *p = old & ~mask;
  55. return (old & mask) != 0;
  56. }
  57. static inline int test_and_clear_bit(int nr, volatile void * addr)
  58. {
  59. unsigned long flags;
  60. int out;
  61. local_irq_save(flags);
  62. out = __test_and_clear_bit(nr, addr);
  63. local_irq_restore(flags);
  64. return out;
  65. }
  66. extern int test_and_change_bit(int nr, volatile void * addr);
  67. static inline int __test_and_change_bit(int nr, volatile void *addr)
  68. {
  69. unsigned long mask = BIT_MASK(nr);
  70. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  71. unsigned long old = *p;
  72. *p = old ^ mask;
  73. return (old & mask) != 0;
  74. }
  75. extern int find_first_zero_bit(void * addr, unsigned size);
  76. extern int find_next_zero_bit(void * addr, int size, int offset);
  77. /*
  78. * This routine doesn't need to be atomic.
  79. */
  80. static inline int test_bit(int nr, const void * addr)
  81. {
  82. return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
  83. }
  84. /*
  85. * ffz = Find First Zero in word. Undefined if no zero exists,
  86. * so code should check against ~0UL first..
  87. */
  88. static inline unsigned long ffz(unsigned long word)
  89. {
  90. int k;
  91. word = ~word;
  92. k = 31;
  93. if (word & 0x0000ffff) { k -= 16; word <<= 16; }
  94. if (word & 0x00ff0000) { k -= 8; word <<= 8; }
  95. if (word & 0x0f000000) { k -= 4; word <<= 4; }
  96. if (word & 0x30000000) { k -= 2; word <<= 2; }
  97. if (word & 0x40000000) { k -= 1; }
  98. return k;
  99. }
  100. /*
  101. * hweightN: returns the hamming weight (i.e. the number
  102. * of bits set) of a N-bit word
  103. */
  104. #define hweight32(x) generic_hweight32(x)
  105. #define hweight16(x) generic_hweight16(x)
  106. #define hweight8(x) generic_hweight8(x)
  107. #define ext2_set_bit test_and_set_bit
  108. #define ext2_clear_bit test_and_clear_bit
  109. #define ext2_test_bit test_bit
  110. #define ext2_find_first_zero_bit find_first_zero_bit
  111. #define ext2_find_next_zero_bit find_next_zero_bit
  112. /* Bitmap functions for the minix filesystem. */
  113. #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
  114. #define minix_set_bit(nr,addr) set_bit(nr,addr)
  115. #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
  116. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  117. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  118. #endif /* __KERNEL__ */
  119. #endif /* _ARM_BITOPS_H */