bitops.h 7.1 KB

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