bitops.h 4.1 KB

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