bitops.h 4.4 KB

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