bitops.h 3.8 KB

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