bitops.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <asm-generic/bitops/__ffs.h>
  17. #ifdef __KERNEL__
  18. #include <asm/proc-armv/system.h>
  19. #define smp_mb__before_clear_bit() do { } while (0)
  20. #define smp_mb__after_clear_bit() do { } while (0)
  21. /*
  22. * Function prototypes to keep gcc -Wall happy.
  23. */
  24. extern void set_bit(int nr, volatile void * addr);
  25. extern void clear_bit(int nr, volatile void * addr);
  26. extern void change_bit(int nr, volatile void * addr);
  27. static inline void __change_bit(int nr, volatile void *addr)
  28. {
  29. unsigned long mask = BIT_MASK(nr);
  30. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  31. *p ^= mask;
  32. }
  33. static inline int __test_and_set_bit(int nr, volatile void *addr)
  34. {
  35. unsigned long mask = BIT_MASK(nr);
  36. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  37. unsigned long old = *p;
  38. *p = old | mask;
  39. return (old & mask) != 0;
  40. }
  41. static inline int test_and_set_bit(int nr, volatile void * addr)
  42. {
  43. unsigned long flags = 0;
  44. int out;
  45. local_irq_save(flags);
  46. out = __test_and_set_bit(nr, addr);
  47. local_irq_restore(flags);
  48. return out;
  49. }
  50. static inline int __test_and_clear_bit(int nr, volatile void *addr)
  51. {
  52. unsigned long mask = BIT_MASK(nr);
  53. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  54. unsigned long old = *p;
  55. *p = old & ~mask;
  56. return (old & mask) != 0;
  57. }
  58. static inline int test_and_clear_bit(int nr, volatile void * addr)
  59. {
  60. unsigned long flags = 0;
  61. int out;
  62. local_irq_save(flags);
  63. out = __test_and_clear_bit(nr, addr);
  64. local_irq_restore(flags);
  65. return out;
  66. }
  67. extern int test_and_change_bit(int nr, volatile void * addr);
  68. static inline int __test_and_change_bit(int nr, volatile void *addr)
  69. {
  70. unsigned long mask = BIT_MASK(nr);
  71. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  72. unsigned long old = *p;
  73. *p = old ^ mask;
  74. return (old & mask) != 0;
  75. }
  76. /*
  77. * This routine doesn't need to be atomic.
  78. */
  79. static inline int test_bit(int nr, const void * addr)
  80. {
  81. return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
  82. }
  83. static inline int __ilog2(unsigned int x)
  84. {
  85. return generic_fls(x) - 1;
  86. }
  87. #define ffz(x) __ffs(~(x))
  88. static inline int find_next_zero_bit(void *addr, int size, int offset)
  89. {
  90. unsigned long *p = ((unsigned long *)addr) + (offset / BITS_PER_LONG);
  91. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  92. unsigned long tmp;
  93. if (offset >= size)
  94. return size;
  95. size -= result;
  96. offset &= (BITS_PER_LONG - 1);
  97. if (offset) {
  98. tmp = *(p++);
  99. tmp |= ~0UL >> (BITS_PER_LONG - offset);
  100. if (size < BITS_PER_LONG)
  101. goto found_first;
  102. if (~tmp)
  103. goto found_middle;
  104. size -= BITS_PER_LONG;
  105. result += BITS_PER_LONG;
  106. }
  107. while (size & ~(BITS_PER_LONG - 1)) {
  108. tmp = *(p++);
  109. if (~tmp)
  110. goto found_middle;
  111. result += BITS_PER_LONG;
  112. size -= BITS_PER_LONG;
  113. }
  114. if (!size)
  115. return result;
  116. tmp = *p;
  117. found_first:
  118. tmp |= ~0UL << size;
  119. found_middle:
  120. return result + ffz(tmp);
  121. }
  122. /*
  123. * hweightN: returns the hamming weight (i.e. the number
  124. * of bits set) of a N-bit word
  125. */
  126. #define hweight32(x) generic_hweight32(x)
  127. #define hweight16(x) generic_hweight16(x)
  128. #define hweight8(x) generic_hweight8(x)
  129. #define find_first_zero_bit(addr, size) \
  130. find_next_zero_bit((addr), (size), 0)
  131. #define ext2_set_bit test_and_set_bit
  132. #define ext2_clear_bit test_and_clear_bit
  133. #define ext2_test_bit test_bit
  134. #define ext2_find_first_zero_bit find_first_zero_bit
  135. #define ext2_find_next_zero_bit find_next_zero_bit
  136. /* Bitmap functions for the minix filesystem. */
  137. #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
  138. #define minix_set_bit(nr,addr) set_bit(nr,addr)
  139. #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
  140. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  141. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  142. #endif /* __KERNEL__ */
  143. #include <asm-generic/bitops/__fls.h>
  144. #include <asm-generic/bitops/fls.h>
  145. #include <asm-generic/bitops/fls64.h>
  146. #endif /* _ARM_BITOPS_H */