bitops.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* bitops.h: bit operations for the Fujitsu FR-V CPUs
  2. *
  3. * For an explanation of how atomic ops work in this arch, see:
  4. * Documentation/fujitsu/frv/atomic-ops.txt
  5. *
  6. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  7. * Written by David Howells (dhowells@redhat.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #ifndef _ASM_BITOPS_H
  15. #define _ASM_BITOPS_H
  16. #include <linux/compiler.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/system.h>
  19. #include <asm/atomic.h>
  20. #ifdef __KERNEL__
  21. #ifndef _LINUX_BITOPS_H
  22. #error only <linux/bitops.h> can be included directly
  23. #endif
  24. #include <asm-generic/bitops/ffz.h>
  25. /*
  26. * clear_bit() doesn't provide any barrier for the compiler.
  27. */
  28. #define smp_mb__before_clear_bit() barrier()
  29. #define smp_mb__after_clear_bit() barrier()
  30. static inline int test_and_clear_bit(int nr, volatile void *addr)
  31. {
  32. volatile unsigned long *ptr = addr;
  33. unsigned long mask = 1UL << (nr & 31);
  34. ptr += nr >> 5;
  35. return (atomic_test_and_ANDNOT_mask(mask, ptr) & mask) != 0;
  36. }
  37. static inline int test_and_set_bit(int nr, volatile void *addr)
  38. {
  39. volatile unsigned long *ptr = addr;
  40. unsigned long mask = 1UL << (nr & 31);
  41. ptr += nr >> 5;
  42. return (atomic_test_and_OR_mask(mask, ptr) & mask) != 0;
  43. }
  44. static inline int test_and_change_bit(int nr, volatile void *addr)
  45. {
  46. volatile unsigned long *ptr = addr;
  47. unsigned long mask = 1UL << (nr & 31);
  48. ptr += nr >> 5;
  49. return (atomic_test_and_XOR_mask(mask, ptr) & mask) != 0;
  50. }
  51. static inline void clear_bit(int nr, volatile void *addr)
  52. {
  53. test_and_clear_bit(nr, addr);
  54. }
  55. static inline void set_bit(int nr, volatile void *addr)
  56. {
  57. test_and_set_bit(nr, addr);
  58. }
  59. static inline void change_bit(int nr, volatile void * addr)
  60. {
  61. test_and_change_bit(nr, addr);
  62. }
  63. static inline void __clear_bit(int nr, volatile void * addr)
  64. {
  65. volatile unsigned long *a = addr;
  66. int mask;
  67. a += nr >> 5;
  68. mask = 1 << (nr & 31);
  69. *a &= ~mask;
  70. }
  71. static inline void __set_bit(int nr, volatile void * addr)
  72. {
  73. volatile unsigned long *a = addr;
  74. int mask;
  75. a += nr >> 5;
  76. mask = 1 << (nr & 31);
  77. *a |= mask;
  78. }
  79. static inline void __change_bit(int nr, volatile void *addr)
  80. {
  81. volatile unsigned long *a = addr;
  82. int mask;
  83. a += nr >> 5;
  84. mask = 1 << (nr & 31);
  85. *a ^= mask;
  86. }
  87. static inline int __test_and_clear_bit(int nr, volatile void * addr)
  88. {
  89. volatile unsigned long *a = addr;
  90. int mask, retval;
  91. a += nr >> 5;
  92. mask = 1 << (nr & 31);
  93. retval = (mask & *a) != 0;
  94. *a &= ~mask;
  95. return retval;
  96. }
  97. static inline int __test_and_set_bit(int nr, volatile void * addr)
  98. {
  99. volatile unsigned long *a = addr;
  100. int mask, retval;
  101. a += nr >> 5;
  102. mask = 1 << (nr & 31);
  103. retval = (mask & *a) != 0;
  104. *a |= mask;
  105. return retval;
  106. }
  107. static inline int __test_and_change_bit(int nr, volatile void * addr)
  108. {
  109. volatile unsigned long *a = addr;
  110. int mask, retval;
  111. a += nr >> 5;
  112. mask = 1 << (nr & 31);
  113. retval = (mask & *a) != 0;
  114. *a ^= mask;
  115. return retval;
  116. }
  117. /*
  118. * This routine doesn't need to be atomic.
  119. */
  120. static inline int __constant_test_bit(int nr, const volatile void * addr)
  121. {
  122. return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
  123. }
  124. static inline int __test_bit(int nr, const volatile void * addr)
  125. {
  126. int * a = (int *) addr;
  127. int mask;
  128. a += nr >> 5;
  129. mask = 1 << (nr & 0x1f);
  130. return ((mask & *a) != 0);
  131. }
  132. #define test_bit(nr,addr) \
  133. (__builtin_constant_p(nr) ? \
  134. __constant_test_bit((nr),(addr)) : \
  135. __test_bit((nr),(addr)))
  136. #include <asm-generic/bitops/find.h>
  137. /**
  138. * fls - find last bit set
  139. * @x: the word to search
  140. *
  141. * This is defined the same way as ffs:
  142. * - return 32..1 to indicate bit 31..0 most significant bit set
  143. * - return 0 to indicate no bits set
  144. */
  145. #define fls(x) \
  146. ({ \
  147. int bit; \
  148. \
  149. asm(" subcc %1,gr0,gr0,icc0 \n" \
  150. " ckne icc0,cc4 \n" \
  151. " cscan.p %1,gr0,%0 ,cc4,#1 \n" \
  152. " csub %0,%0,%0 ,cc4,#0 \n" \
  153. " csub %2,%0,%0 ,cc4,#1 \n" \
  154. : "=&r"(bit) \
  155. : "r"(x), "r"(32) \
  156. : "icc0", "cc4" \
  157. ); \
  158. \
  159. bit; \
  160. })
  161. /**
  162. * fls64 - find last bit set in a 64-bit value
  163. * @n: the value to search
  164. *
  165. * This is defined the same way as ffs:
  166. * - return 64..1 to indicate bit 63..0 most significant bit set
  167. * - return 0 to indicate no bits set
  168. */
  169. static inline __attribute__((const))
  170. int fls64(u64 n)
  171. {
  172. union {
  173. u64 ll;
  174. struct { u32 h, l; };
  175. } _;
  176. int bit, x, y;
  177. _.ll = n;
  178. asm(" subcc.p %3,gr0,gr0,icc0 \n"
  179. " subcc %4,gr0,gr0,icc1 \n"
  180. " ckne icc0,cc4 \n"
  181. " ckne icc1,cc5 \n"
  182. " norcr cc4,cc5,cc6 \n"
  183. " csub.p %0,%0,%0 ,cc6,1 \n"
  184. " orcr cc5,cc4,cc4 \n"
  185. " andcr cc4,cc5,cc4 \n"
  186. " cscan.p %3,gr0,%0 ,cc4,0 \n"
  187. " setlos #64,%1 \n"
  188. " cscan.p %4,gr0,%0 ,cc4,1 \n"
  189. " setlos #32,%2 \n"
  190. " csub.p %1,%0,%0 ,cc4,0 \n"
  191. " csub %2,%0,%0 ,cc4,1 \n"
  192. : "=&r"(bit), "=r"(x), "=r"(y)
  193. : "0r"(_.h), "r"(_.l)
  194. : "icc0", "icc1", "cc4", "cc5", "cc6"
  195. );
  196. return bit;
  197. }
  198. /**
  199. * ffs - find first bit set
  200. * @x: the word to search
  201. *
  202. * - return 32..1 to indicate bit 31..0 most least significant bit set
  203. * - return 0 to indicate no bits set
  204. */
  205. static inline __attribute__((const))
  206. int ffs(int x)
  207. {
  208. /* Note: (x & -x) gives us a mask that is the least significant
  209. * (rightmost) 1-bit of the value in x.
  210. */
  211. return fls(x & -x);
  212. }
  213. /**
  214. * __ffs - find first bit set
  215. * @x: the word to search
  216. *
  217. * - return 31..0 to indicate bit 31..0 most least significant bit set
  218. * - if no bits are set in x, the result is undefined
  219. */
  220. static inline __attribute__((const))
  221. int __ffs(unsigned long x)
  222. {
  223. int bit;
  224. asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x & -x));
  225. return 31 - bit;
  226. }
  227. /*
  228. * special slimline version of fls() for calculating ilog2_u32()
  229. * - note: no protection against n == 0
  230. */
  231. #define ARCH_HAS_ILOG2_U32
  232. static inline __attribute__((const))
  233. int __ilog2_u32(u32 n)
  234. {
  235. int bit;
  236. asm("scan %1,gr0,%0" : "=r"(bit) : "r"(n));
  237. return 31 - bit;
  238. }
  239. /*
  240. * special slimline version of fls64() for calculating ilog2_u64()
  241. * - note: no protection against n == 0
  242. */
  243. #define ARCH_HAS_ILOG2_U64
  244. static inline __attribute__((const))
  245. int __ilog2_u64(u64 n)
  246. {
  247. union {
  248. u64 ll;
  249. struct { u32 h, l; };
  250. } _;
  251. int bit, x, y;
  252. _.ll = n;
  253. asm(" subcc %3,gr0,gr0,icc0 \n"
  254. " ckeq icc0,cc4 \n"
  255. " cscan.p %3,gr0,%0 ,cc4,0 \n"
  256. " setlos #63,%1 \n"
  257. " cscan.p %4,gr0,%0 ,cc4,1 \n"
  258. " setlos #31,%2 \n"
  259. " csub.p %1,%0,%0 ,cc4,0 \n"
  260. " csub %2,%0,%0 ,cc4,1 \n"
  261. : "=&r"(bit), "=r"(x), "=r"(y)
  262. : "0r"(_.h), "r"(_.l)
  263. : "icc0", "cc4"
  264. );
  265. return bit;
  266. }
  267. #include <asm-generic/bitops/sched.h>
  268. #include <asm-generic/bitops/hweight.h>
  269. #include <asm-generic/bitops/lock.h>
  270. #include <asm-generic/bitops/ext2-non-atomic.h>
  271. #define ext2_set_bit_atomic(lock,nr,addr) test_and_set_bit ((nr) ^ 0x18, (addr))
  272. #define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_bit((nr) ^ 0x18, (addr))
  273. #include <asm-generic/bitops/minix-le.h>
  274. #endif /* __KERNEL__ */
  275. #endif /* _ASM_BITOPS_H */