bitops.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #define smp_mb__before_clear_bit() do { } while (0)
  18. #define smp_mb__after_clear_bit() do { } while (0)
  19. /*
  20. * Function prototypes to keep gcc -Wall happy.
  21. */
  22. extern void set_bit(int nr, volatile void * addr);
  23. static inline void __set_bit(int nr, volatile void *addr)
  24. {
  25. ((unsigned char *) addr)[nr >> 3] |= (1U << (nr & 7));
  26. }
  27. extern void clear_bit(int nr, volatile void * addr);
  28. static inline void __clear_bit(int nr, volatile void *addr)
  29. {
  30. ((unsigned char *) addr)[nr >> 3] &= ~(1U << (nr & 7));
  31. }
  32. extern void change_bit(int nr, volatile void * addr);
  33. static inline void __change_bit(int nr, volatile void *addr)
  34. {
  35. ((unsigned char *) addr)[nr >> 3] ^= (1U << (nr & 7));
  36. }
  37. extern int test_and_set_bit(int nr, volatile void * addr);
  38. static inline int __test_and_set_bit(int nr, volatile void *addr)
  39. {
  40. unsigned int mask = 1 << (nr & 7);
  41. unsigned int oldval;
  42. oldval = ((unsigned char *) addr)[nr >> 3];
  43. ((unsigned char *) addr)[nr >> 3] = oldval | mask;
  44. return oldval & mask;
  45. }
  46. extern int test_and_clear_bit(int nr, volatile void * addr);
  47. static inline int __test_and_clear_bit(int nr, volatile void *addr)
  48. {
  49. unsigned int mask = 1 << (nr & 7);
  50. unsigned int oldval;
  51. oldval = ((unsigned char *) addr)[nr >> 3];
  52. ((unsigned char *) addr)[nr >> 3] = oldval & ~mask;
  53. return oldval & mask;
  54. }
  55. extern int test_and_change_bit(int nr, volatile void * addr);
  56. static inline int __test_and_change_bit(int nr, volatile void *addr)
  57. {
  58. unsigned int mask = 1 << (nr & 7);
  59. unsigned int oldval;
  60. oldval = ((unsigned char *) addr)[nr >> 3];
  61. ((unsigned char *) addr)[nr >> 3] = oldval ^ mask;
  62. return oldval & mask;
  63. }
  64. extern int find_first_zero_bit(void * addr, unsigned size);
  65. extern int find_next_zero_bit(void * addr, int size, int offset);
  66. /*
  67. * This routine doesn't need to be atomic.
  68. */
  69. static inline int test_bit(int nr, const void * addr)
  70. {
  71. return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
  72. }
  73. /*
  74. * ffz = Find First Zero in word. Undefined if no zero exists,
  75. * so code should check against ~0UL first..
  76. */
  77. static inline unsigned long ffz(unsigned long word)
  78. {
  79. int k;
  80. word = ~word;
  81. k = 31;
  82. if (word & 0x0000ffff) { k -= 16; word <<= 16; }
  83. if (word & 0x00ff0000) { k -= 8; word <<= 8; }
  84. if (word & 0x0f000000) { k -= 4; word <<= 4; }
  85. if (word & 0x30000000) { k -= 2; word <<= 2; }
  86. if (word & 0x40000000) { k -= 1; }
  87. return k;
  88. }
  89. /*
  90. * ffs: find first bit set. This is defined the same way as
  91. * the libc and compiler builtin ffs routines, therefore
  92. * differs in spirit from the above ffz (man ffs).
  93. */
  94. #define ffs(x) generic_ffs(x)
  95. /*
  96. * hweightN: returns the hamming weight (i.e. the number
  97. * of bits set) of a N-bit word
  98. */
  99. #define hweight32(x) generic_hweight32(x)
  100. #define hweight16(x) generic_hweight16(x)
  101. #define hweight8(x) generic_hweight8(x)
  102. #define ext2_set_bit test_and_set_bit
  103. #define ext2_clear_bit test_and_clear_bit
  104. #define ext2_test_bit test_bit
  105. #define ext2_find_first_zero_bit find_first_zero_bit
  106. #define ext2_find_next_zero_bit find_next_zero_bit
  107. /* Bitmap functions for the minix filesystem. */
  108. #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
  109. #define minix_set_bit(nr,addr) set_bit(nr,addr)
  110. #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
  111. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  112. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  113. #endif /* __KERNEL__ */
  114. #endif /* _ARM_BITOPS_H */