bitops.h 3.9 KB

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