bitops.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #ifndef _PARISC_BITOPS_H
  2. #define _PARISC_BITOPS_H
  3. #ifndef _LINUX_BITOPS_H
  4. #error only <linux/bitops.h> can be included directly
  5. #endif
  6. #include <linux/compiler.h>
  7. #include <asm/types.h> /* for BITS_PER_LONG/SHIFT_PER_LONG */
  8. #include <asm/byteorder.h>
  9. #include <asm/atomic.h>
  10. /*
  11. * HP-PARISC specific bit operations
  12. * for a detailed description of the functions please refer
  13. * to include/asm-i386/bitops.h or kerneldoc
  14. */
  15. #define CHOP_SHIFTCOUNT(x) (((unsigned long) (x)) & (BITS_PER_LONG - 1))
  16. #define smp_mb__before_clear_bit() smp_mb()
  17. #define smp_mb__after_clear_bit() smp_mb()
  18. /* See http://marc.theaimsgroup.com/?t=108826637900003 for discussion
  19. * on use of volatile and __*_bit() (set/clear/change):
  20. * *_bit() want use of volatile.
  21. * __*_bit() are "relaxed" and don't use spinlock or volatile.
  22. */
  23. static __inline__ void set_bit(int nr, volatile unsigned long * addr)
  24. {
  25. unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
  26. unsigned long flags;
  27. addr += (nr >> SHIFT_PER_LONG);
  28. _atomic_spin_lock_irqsave(addr, flags);
  29. *addr |= mask;
  30. _atomic_spin_unlock_irqrestore(addr, flags);
  31. }
  32. static __inline__ void clear_bit(int nr, volatile unsigned long * addr)
  33. {
  34. unsigned long mask = ~(1UL << CHOP_SHIFTCOUNT(nr));
  35. unsigned long flags;
  36. addr += (nr >> SHIFT_PER_LONG);
  37. _atomic_spin_lock_irqsave(addr, flags);
  38. *addr &= mask;
  39. _atomic_spin_unlock_irqrestore(addr, flags);
  40. }
  41. static __inline__ void change_bit(int nr, volatile unsigned long * addr)
  42. {
  43. unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
  44. unsigned long flags;
  45. addr += (nr >> SHIFT_PER_LONG);
  46. _atomic_spin_lock_irqsave(addr, flags);
  47. *addr ^= mask;
  48. _atomic_spin_unlock_irqrestore(addr, flags);
  49. }
  50. static __inline__ int test_and_set_bit(int nr, volatile unsigned long * addr)
  51. {
  52. unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
  53. unsigned long old;
  54. unsigned long flags;
  55. int set;
  56. addr += (nr >> SHIFT_PER_LONG);
  57. _atomic_spin_lock_irqsave(addr, flags);
  58. old = *addr;
  59. set = (old & mask) ? 1 : 0;
  60. if (!set)
  61. *addr = old | mask;
  62. _atomic_spin_unlock_irqrestore(addr, flags);
  63. return set;
  64. }
  65. static __inline__ int test_and_clear_bit(int nr, volatile unsigned long * addr)
  66. {
  67. unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
  68. unsigned long old;
  69. unsigned long flags;
  70. int set;
  71. addr += (nr >> SHIFT_PER_LONG);
  72. _atomic_spin_lock_irqsave(addr, flags);
  73. old = *addr;
  74. set = (old & mask) ? 1 : 0;
  75. if (set)
  76. *addr = old & ~mask;
  77. _atomic_spin_unlock_irqrestore(addr, flags);
  78. return set;
  79. }
  80. static __inline__ int test_and_change_bit(int nr, volatile unsigned long * addr)
  81. {
  82. unsigned long mask = 1UL << CHOP_SHIFTCOUNT(nr);
  83. unsigned long oldbit;
  84. unsigned long flags;
  85. addr += (nr >> SHIFT_PER_LONG);
  86. _atomic_spin_lock_irqsave(addr, flags);
  87. oldbit = *addr;
  88. *addr = oldbit ^ mask;
  89. _atomic_spin_unlock_irqrestore(addr, flags);
  90. return (oldbit & mask) ? 1 : 0;
  91. }
  92. #include <asm-generic/bitops/non-atomic.h>
  93. #ifdef __KERNEL__
  94. /**
  95. * __ffs - find first bit in word. returns 0 to "BITS_PER_LONG-1".
  96. * @word: The word to search
  97. *
  98. * __ffs() return is undefined if no bit is set.
  99. *
  100. * 32-bit fast __ffs by LaMont Jones "lamont At hp com".
  101. * 64-bit enhancement by Grant Grundler "grundler At parisc-linux org".
  102. * (with help from willy/jejb to get the semantics right)
  103. *
  104. * This algorithm avoids branches by making use of nullification.
  105. * One side effect of "extr" instructions is it sets PSW[N] bit.
  106. * How PSW[N] (nullify next insn) gets set is determined by the
  107. * "condition" field (eg "<>" or "TR" below) in the extr* insn.
  108. * Only the 1st and one of either the 2cd or 3rd insn will get executed.
  109. * Each set of 3 insn will get executed in 2 cycles on PA8x00 vs 16 or so
  110. * cycles for each mispredicted branch.
  111. */
  112. static __inline__ unsigned long __ffs(unsigned long x)
  113. {
  114. unsigned long ret;
  115. __asm__(
  116. #ifdef CONFIG_64BIT
  117. " ldi 63,%1\n"
  118. " extrd,u,*<> %0,63,32,%%r0\n"
  119. " extrd,u,*TR %0,31,32,%0\n" /* move top 32-bits down */
  120. " addi -32,%1,%1\n"
  121. #else
  122. " ldi 31,%1\n"
  123. #endif
  124. " extru,<> %0,31,16,%%r0\n"
  125. " extru,TR %0,15,16,%0\n" /* xxxx0000 -> 0000xxxx */
  126. " addi -16,%1,%1\n"
  127. " extru,<> %0,31,8,%%r0\n"
  128. " extru,TR %0,23,8,%0\n" /* 0000xx00 -> 000000xx */
  129. " addi -8,%1,%1\n"
  130. " extru,<> %0,31,4,%%r0\n"
  131. " extru,TR %0,27,4,%0\n" /* 000000x0 -> 0000000x */
  132. " addi -4,%1,%1\n"
  133. " extru,<> %0,31,2,%%r0\n"
  134. " extru,TR %0,29,2,%0\n" /* 0000000y, 1100b -> 0011b */
  135. " addi -2,%1,%1\n"
  136. " extru,= %0,31,1,%%r0\n" /* check last bit */
  137. " addi -1,%1,%1\n"
  138. : "+r" (x), "=r" (ret) );
  139. return ret;
  140. }
  141. #include <asm-generic/bitops/ffz.h>
  142. /*
  143. * ffs: find first bit set. returns 1 to BITS_PER_LONG or 0 (if none set)
  144. * This is defined the same way as the libc and compiler builtin
  145. * ffs routines, therefore differs in spirit from the above ffz (man ffs).
  146. */
  147. static __inline__ int ffs(int x)
  148. {
  149. return x ? (__ffs((unsigned long)x) + 1) : 0;
  150. }
  151. /*
  152. * fls: find last (most significant) bit set.
  153. * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  154. */
  155. static __inline__ int fls(int x)
  156. {
  157. int ret;
  158. if (!x)
  159. return 0;
  160. __asm__(
  161. " ldi 1,%1\n"
  162. " extru,<> %0,15,16,%%r0\n"
  163. " zdep,TR %0,15,16,%0\n" /* xxxx0000 */
  164. " addi 16,%1,%1\n"
  165. " extru,<> %0,7,8,%%r0\n"
  166. " zdep,TR %0,23,24,%0\n" /* xx000000 */
  167. " addi 8,%1,%1\n"
  168. " extru,<> %0,3,4,%%r0\n"
  169. " zdep,TR %0,27,28,%0\n" /* x0000000 */
  170. " addi 4,%1,%1\n"
  171. " extru,<> %0,1,2,%%r0\n"
  172. " zdep,TR %0,29,30,%0\n" /* y0000000 (y&3 = 0) */
  173. " addi 2,%1,%1\n"
  174. " extru,= %0,0,1,%%r0\n"
  175. " addi 1,%1,%1\n" /* if y & 8, add 1 */
  176. : "+r" (x), "=r" (ret) );
  177. return ret;
  178. }
  179. #include <asm-generic/bitops/__fls.h>
  180. #include <asm-generic/bitops/fls64.h>
  181. #include <asm-generic/bitops/hweight.h>
  182. #include <asm-generic/bitops/lock.h>
  183. #include <asm-generic/bitops/sched.h>
  184. #endif /* __KERNEL__ */
  185. #include <asm-generic/bitops/find.h>
  186. #ifdef __KERNEL__
  187. #include <asm-generic/bitops/ext2-non-atomic.h>
  188. /* '3' is bits per byte */
  189. #define LE_BYTE_ADDR ((sizeof(unsigned long) - 1) << 3)
  190. #define ext2_set_bit_atomic(l,nr,addr) \
  191. test_and_set_bit((nr) ^ LE_BYTE_ADDR, (unsigned long *)addr)
  192. #define ext2_clear_bit_atomic(l,nr,addr) \
  193. test_and_clear_bit( (nr) ^ LE_BYTE_ADDR, (unsigned long *)addr)
  194. #endif /* __KERNEL__ */
  195. #include <asm-generic/bitops/minix-le.h>
  196. #endif /* _PARISC_BITOPS_H */