bitops.h 5.8 KB

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