bitops.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #define PLATFORM__SET_BIT
  40. extern void clear_bit(int nr, void *addr);
  41. static inline void __clear_bit(int nr, void *addr)
  42. {
  43. int *a = (int *)addr;
  44. int mask;
  45. unsigned long flags;
  46. a += nr >> 5;
  47. mask = 1 << (nr & 0x1f);
  48. local_irq_save(flags);
  49. *a &= ~mask;
  50. local_irq_restore(flags);
  51. }
  52. #define PLATFORM__CLEAR_BIT
  53. extern void change_bit(int nr, void *addr);
  54. static inline void __change_bit(int nr, void *addr)
  55. {
  56. int mask;
  57. unsigned long *ADDR = (unsigned long *)addr;
  58. ADDR += nr >> 5;
  59. mask = 1 << (nr & 31);
  60. *ADDR ^= mask;
  61. }
  62. extern int test_and_set_bit(int nr, void *addr);
  63. static inline int __test_and_set_bit(int nr, void *addr)
  64. {
  65. int mask, retval;
  66. unsigned int *a = (unsigned int *)addr;
  67. a += nr >> 5;
  68. mask = 1 << (nr & 0x1f);
  69. retval = (mask & *a) != 0;
  70. *a |= mask;
  71. return retval;
  72. }
  73. extern int test_and_clear_bit(int nr, void *addr);
  74. static inline int __test_and_clear_bit(int nr, void *addr)
  75. {
  76. int mask, retval;
  77. unsigned int *a = (unsigned int *)addr;
  78. a += nr >> 5;
  79. mask = 1 << (nr & 0x1f);
  80. retval = (mask & *a) != 0;
  81. *a &= ~mask;
  82. return retval;
  83. }
  84. extern int test_and_change_bit(int nr, void *addr);
  85. static inline int __test_and_change_bit(int nr, void *addr)
  86. {
  87. int mask, retval;
  88. unsigned int *a = (unsigned int *)addr;
  89. a += nr >> 5;
  90. mask = 1 << (nr & 0x1f);
  91. retval = (mask & *a) != 0;
  92. *a ^= mask;
  93. return retval;
  94. }
  95. extern int find_first_zero_bit(void *addr, unsigned size);
  96. extern int find_next_zero_bit(void *addr, int size, int offset);
  97. /*
  98. * This routine doesn't need to be atomic.
  99. */
  100. static inline int test_bit(int nr, const void *addr)
  101. {
  102. return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
  103. }
  104. /*
  105. * ffz = Find First Zero in word. Undefined if no zero exists,
  106. * so code should check against ~0UL first..
  107. */
  108. static inline unsigned long ffz(unsigned long word)
  109. {
  110. int k;
  111. word = ~word;
  112. k = 31;
  113. if (word & 0x0000ffff) {
  114. k -= 16; word <<= 16;
  115. }
  116. if (word & 0x00ff0000) {
  117. k -= 8; word <<= 8;
  118. }
  119. if (word & 0x0f000000) {
  120. k -= 4; word <<= 4;
  121. }
  122. if (word & 0x30000000) {
  123. k -= 2; word <<= 2;
  124. }
  125. if (word & 0x40000000)
  126. k -= 1;
  127. return k;
  128. }
  129. /*
  130. * ffs: find first bit set. This is defined the same way as
  131. * the libc and compiler builtin ffs routines, therefore
  132. * differs in spirit from the above ffz (man ffs).
  133. */
  134. /*
  135. * redefined in include/linux/bitops.h
  136. * #define ffs(x) generic_ffs(x)
  137. */
  138. /*
  139. * hweightN: returns the hamming weight (i.e. the number
  140. * of bits set) of a N-bit word
  141. */
  142. #define hweight32(x) generic_hweight32(x)
  143. #define hweight16(x) generic_hweight16(x)
  144. #define hweight8(x) generic_hweight8(x)
  145. #define ext2_set_bit test_and_set_bit
  146. #define ext2_clear_bit test_and_clear_bit
  147. #define ext2_test_bit test_bit
  148. #define ext2_find_first_zero_bit find_first_zero_bit
  149. #define ext2_find_next_zero_bit find_next_zero_bit
  150. /* Bitmap functions for the minix filesystem. */
  151. #define minix_test_and_set_bit(nr, addr) test_and_set_bit(nr, addr)
  152. #define minix_set_bit(nr, addr) set_bit(nr, addr)
  153. #define minix_test_and_clear_bit(nr, addr) test_and_clear_bit(nr, addr)
  154. #define minix_test_bit(nr, addr) test_bit(nr, addr)
  155. #define minix_find_first_zero_bit(addr, size) find_first_zero_bit(addr, size)
  156. #endif /* __KERNEL__ */
  157. #endif /* __ASM_NDS_BITOPS_H */