bitops.h 3.8 KB

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