bitops.h 5.3 KB

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