bitops.h 2.7 KB

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