bitops.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 1995, Russell King.
  3. * Various bits and pieces copyrights include:
  4. * Linus Torvalds (test_bit).
  5. *
  6. * Copyright (C) 2011 Andes Technology Corporation
  7. * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
  8. *
  9. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  10. *
  11. * Please note that the code in this file should never be included
  12. * from user space. Many of these are not implemented in assembler
  13. * since they would be too costly. Also, they require priviledged
  14. * instructions (which are not available from user mode) to ensure
  15. * that they are atomic.
  16. */
  17. #ifndef __ASM_NDS_BITOPS_H
  18. #define __ASM_NDS_BITOPS_H
  19. #ifdef __KERNEL__
  20. #include <asm/system.h>
  21. #include <asm-generic/bitops/fls.h>
  22. #include <asm-generic/bitops/__fls.h>
  23. #include <asm-generic/bitops/fls64.h>
  24. #include <asm-generic/bitops/__ffs.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, void *addr);
  31. static inline void __set_bit(int nr, void *addr)
  32. {
  33. int *a = (int *)addr;
  34. int mask;
  35. a += nr >> 5;
  36. mask = 1 << (nr & 0x1f);
  37. *a |= mask;
  38. }
  39. extern void clear_bit(int nr, void *addr);
  40. static inline void __clear_bit(int nr, void *addr)
  41. {
  42. int *a = (int *)addr;
  43. int mask;
  44. unsigned long flags;
  45. a += nr >> 5;
  46. mask = 1 << (nr & 0x1f);
  47. local_irq_save(flags);
  48. *a &= ~mask;
  49. local_irq_restore(flags);
  50. }
  51. extern void change_bit(int nr, void *addr);
  52. static inline void __change_bit(int nr, void *addr)
  53. {
  54. int mask;
  55. unsigned long *ADDR = (unsigned long *)addr;
  56. ADDR += nr >> 5;
  57. mask = 1 << (nr & 31);
  58. *ADDR ^= mask;
  59. }
  60. extern int test_and_set_bit(int nr, void *addr);
  61. static inline int __test_and_set_bit(int nr, void *addr)
  62. {
  63. int mask, retval;
  64. unsigned int *a = (unsigned int *)addr;
  65. a += nr >> 5;
  66. mask = 1 << (nr & 0x1f);
  67. retval = (mask & *a) != 0;
  68. *a |= mask;
  69. return retval;
  70. }
  71. extern int test_and_clear_bit(int nr, void *addr);
  72. static inline int __test_and_clear_bit(int nr, void *addr)
  73. {
  74. int mask, retval;
  75. unsigned int *a = (unsigned int *)addr;
  76. a += nr >> 5;
  77. mask = 1 << (nr & 0x1f);
  78. retval = (mask & *a) != 0;
  79. *a &= ~mask;
  80. return retval;
  81. }
  82. extern int test_and_change_bit(int nr, void *addr);
  83. static inline int __test_and_change_bit(int nr, void *addr)
  84. {
  85. int mask, retval;
  86. unsigned int *a = (unsigned int *)addr;
  87. a += nr >> 5;
  88. mask = 1 << (nr & 0x1f);
  89. retval = (mask & *a) != 0;
  90. *a ^= mask;
  91. return retval;
  92. }
  93. extern int find_first_zero_bit(void *addr, unsigned size);
  94. extern int find_next_zero_bit(void *addr, int size, int offset);
  95. /*
  96. * This routine doesn't need to be atomic.
  97. */
  98. static inline int test_bit(int nr, const void *addr)
  99. {
  100. return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
  101. }
  102. /*
  103. * ffz = Find First Zero in word. Undefined if no zero exists,
  104. * so code should check against ~0UL first..
  105. */
  106. static inline unsigned long ffz(unsigned long word)
  107. {
  108. int k;
  109. word = ~word;
  110. k = 31;
  111. if (word & 0x0000ffff) {
  112. k -= 16; word <<= 16;
  113. }
  114. if (word & 0x00ff0000) {
  115. k -= 8; word <<= 8;
  116. }
  117. if (word & 0x0f000000) {
  118. k -= 4; word <<= 4;
  119. }
  120. if (word & 0x30000000) {
  121. k -= 2; word <<= 2;
  122. }
  123. if (word & 0x40000000)
  124. k -= 1;
  125. return k;
  126. }
  127. /*
  128. * ffs: find first bit set. This is defined the same way as
  129. * the libc and compiler builtin ffs routines, therefore
  130. * differs in spirit from the above ffz (man ffs).
  131. */
  132. /*
  133. * redefined in include/linux/bitops.h
  134. * #define ffs(x) generic_ffs(x)
  135. */
  136. /*
  137. * hweightN: returns the hamming weight (i.e. the number
  138. * of bits set) of a N-bit word
  139. */
  140. #define hweight32(x) generic_hweight32(x)
  141. #define hweight16(x) generic_hweight16(x)
  142. #define hweight8(x) generic_hweight8(x)
  143. #define ext2_set_bit test_and_set_bit
  144. #define ext2_clear_bit test_and_clear_bit
  145. #define ext2_test_bit test_bit
  146. #define ext2_find_first_zero_bit find_first_zero_bit
  147. #define ext2_find_next_zero_bit find_next_zero_bit
  148. /* Bitmap functions for the minix filesystem. */
  149. #define minix_test_and_set_bit(nr, addr) test_and_set_bit(nr, addr)
  150. #define minix_set_bit(nr, addr) set_bit(nr, addr)
  151. #define minix_test_and_clear_bit(nr, addr) test_and_clear_bit(nr, addr)
  152. #define minix_test_bit(nr, addr) test_bit(nr, addr)
  153. #define minix_find_first_zero_bit(addr, size) find_first_zero_bit(addr, size)
  154. #endif /* __KERNEL__ */
  155. #endif /* __ASM_NDS_BITOPS_H */