bitops.h 2.6 KB

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