bitops.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef __ASM_SH_BITOPS_H
  2. #define __ASM_SH_BITOPS_H
  3. #ifdef __KERNEL__
  4. //#include <asm/system.h>
  5. #include <asm/irqflags.h>
  6. /* For __swab32 */
  7. #include <asm/byteorder.h>
  8. static inline void set_bit(int nr, volatile void * addr)
  9. {
  10. int mask;
  11. volatile unsigned int *a = addr;
  12. unsigned long flags;
  13. a += nr >> 5;
  14. mask = 1 << (nr & 0x1f);
  15. local_irq_save(flags);
  16. *a |= mask;
  17. local_irq_restore(flags);
  18. }
  19. /*
  20. * clear_bit() doesn't provide any barrier for the compiler.
  21. */
  22. #define smp_mb__before_clear_bit() barrier()
  23. #define smp_mb__after_clear_bit() barrier()
  24. static inline void clear_bit(int nr, volatile void * addr)
  25. {
  26. int mask;
  27. volatile unsigned int *a = addr;
  28. unsigned long flags;
  29. a += nr >> 5;
  30. mask = 1 << (nr & 0x1f);
  31. local_irq_save(flags);
  32. *a &= ~mask;
  33. local_irq_restore(flags);
  34. }
  35. static inline void change_bit(int nr, volatile void * addr)
  36. {
  37. int mask;
  38. volatile unsigned int *a = addr;
  39. unsigned long flags;
  40. a += nr >> 5;
  41. mask = 1 << (nr & 0x1f);
  42. local_irq_save(flags);
  43. *a ^= mask;
  44. local_irq_restore(flags);
  45. }
  46. static inline int test_and_set_bit(int nr, volatile void * addr)
  47. {
  48. int mask, retval;
  49. volatile unsigned int *a = addr;
  50. unsigned long flags;
  51. a += nr >> 5;
  52. mask = 1 << (nr & 0x1f);
  53. local_irq_save(flags);
  54. retval = (mask & *a) != 0;
  55. *a |= mask;
  56. local_irq_restore(flags);
  57. return retval;
  58. }
  59. static inline int test_and_clear_bit(int nr, volatile void * addr)
  60. {
  61. int mask, retval;
  62. volatile unsigned int *a = addr;
  63. unsigned long flags;
  64. a += nr >> 5;
  65. mask = 1 << (nr & 0x1f);
  66. local_irq_save(flags);
  67. retval = (mask & *a) != 0;
  68. *a &= ~mask;
  69. local_irq_restore(flags);
  70. return retval;
  71. }
  72. static inline int test_and_change_bit(int nr, volatile void * addr)
  73. {
  74. int mask, retval;
  75. volatile unsigned int *a = addr;
  76. unsigned long flags;
  77. a += nr >> 5;
  78. mask = 1 << (nr & 0x1f);
  79. local_irq_save(flags);
  80. retval = (mask & *a) != 0;
  81. *a ^= mask;
  82. local_irq_restore(flags);
  83. return retval;
  84. }
  85. //#include <asm-generic/bitops/non-atomic.h>
  86. static inline unsigned long ffz(unsigned long word)
  87. {
  88. unsigned long result;
  89. __asm__("1:\n\t"
  90. "shlr %1\n\t"
  91. "bt/s 1b\n\t"
  92. " add #1, %0"
  93. : "=r" (result), "=r" (word)
  94. : "0" (~0L), "1" (word)
  95. : "t");
  96. return result;
  97. }
  98. /**
  99. * ffs - find first bit in word.
  100. * @word: The word to search
  101. *
  102. * Undefined if no bit exists, so code should check against 0 first.
  103. */
  104. static inline int ffs(int x)
  105. {
  106. int r = 1;
  107. if (!x)
  108. return 0;
  109. if (!(x & 0xffff)) {
  110. x >>= 16;
  111. r += 16;
  112. }
  113. if (!(x & 0xff)) {
  114. x >>= 8;
  115. r += 8;
  116. }
  117. if (!(x & 0xf)) {
  118. x >>= 4;
  119. r += 4;
  120. }
  121. if (!(x & 3)) {
  122. x >>= 2;
  123. r += 2;
  124. }
  125. if (!(x & 1)) {
  126. x >>= 1;
  127. r += 1;
  128. }
  129. return r;
  130. }
  131. #if 0
  132. #include <asm-generic/bitops/find.h>
  133. #include <asm-generic/bitops/ffs.h>
  134. #include <asm-generic/bitops/hweight.h>
  135. #include <asm-generic/bitops/sched.h>
  136. #include <asm-generic/bitops/ext2-non-atomic.h>
  137. #include <asm-generic/bitops/ext2-atomic.h>
  138. #include <asm-generic/bitops/minix.h>
  139. #include <asm-generic/bitops/fls.h>
  140. #include <asm-generic/bitops/fls64.h>
  141. #endif
  142. #endif /* __KERNEL__ */
  143. #endif /* __ASM_SH_BITOPS_H */