bitops.h 5.9 KB

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