bitops.h 3.8 KB

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