bitops.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright IBM Corp. 1999,2013
  3. *
  4. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  5. *
  6. * The description below was taken in large parts from the powerpc
  7. * bitops header file:
  8. * Within a word, bits are numbered LSB first. Lot's of places make
  9. * this assumption by directly testing bits with (val & (1<<nr)).
  10. * This can cause confusion for large (> 1 word) bitmaps on a
  11. * big-endian system because, unlike little endian, the number of each
  12. * bit depends on the word size.
  13. *
  14. * The bitop functions are defined to work on unsigned longs, so the bits
  15. * end up numbered:
  16. * |63..............0|127............64|191...........128|255...........192|
  17. *
  18. * We also have special functions which work with an MSB0 encoding.
  19. * The bits are numbered:
  20. * |0..............63|64............127|128...........191|192...........255|
  21. *
  22. * The main difference is that bit 0-63 in the bit number field needs to be
  23. * reversed compared to the LSB0 encoded bit fields. This can be achieved by
  24. * XOR with 0x3f.
  25. *
  26. */
  27. #ifndef _S390_BITOPS_H
  28. #define _S390_BITOPS_H
  29. #ifndef _LINUX_BITOPS_H
  30. #error only <linux/bitops.h> can be included directly
  31. #endif
  32. #include <linux/typecheck.h>
  33. #include <linux/compiler.h>
  34. #include <asm/atomic_ops.h>
  35. #include <asm/barrier.h>
  36. #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
  37. static inline unsigned long *
  38. __bitops_word(unsigned long nr, volatile unsigned long *ptr)
  39. {
  40. unsigned long addr;
  41. addr = (unsigned long)ptr + ((nr ^ (nr & (BITS_PER_LONG - 1))) >> 3);
  42. return (unsigned long *)addr;
  43. }
  44. static inline unsigned char *
  45. __bitops_byte(unsigned long nr, volatile unsigned long *ptr)
  46. {
  47. return ((unsigned char *)ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  48. }
  49. static inline void set_bit(unsigned long nr, volatile unsigned long *ptr)
  50. {
  51. unsigned long *addr = __bitops_word(nr, ptr);
  52. unsigned long mask;
  53. #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
  54. if (__builtin_constant_p(nr)) {
  55. unsigned char *caddr = __bitops_byte(nr, ptr);
  56. asm volatile(
  57. "oi %0,%b1\n"
  58. : "+Q" (*caddr)
  59. : "i" (1 << (nr & 7))
  60. : "cc", "memory");
  61. return;
  62. }
  63. #endif
  64. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  65. __atomic64_or(mask, addr);
  66. }
  67. static inline void clear_bit(unsigned long nr, volatile unsigned long *ptr)
  68. {
  69. unsigned long *addr = __bitops_word(nr, ptr);
  70. unsigned long mask;
  71. #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
  72. if (__builtin_constant_p(nr)) {
  73. unsigned char *caddr = __bitops_byte(nr, ptr);
  74. asm volatile(
  75. "ni %0,%b1\n"
  76. : "+Q" (*caddr)
  77. : "i" (~(1 << (nr & 7)))
  78. : "cc", "memory");
  79. return;
  80. }
  81. #endif
  82. mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
  83. __atomic64_and(mask, addr);
  84. }
  85. static inline void change_bit(unsigned long nr, volatile unsigned long *ptr)
  86. {
  87. unsigned long *addr = __bitops_word(nr, ptr);
  88. unsigned long mask;
  89. #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
  90. if (__builtin_constant_p(nr)) {
  91. unsigned char *caddr = __bitops_byte(nr, ptr);
  92. asm volatile(
  93. "xi %0,%b1\n"
  94. : "+Q" (*caddr)
  95. : "i" (1 << (nr & 7))
  96. : "cc", "memory");
  97. return;
  98. }
  99. #endif
  100. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  101. __atomic64_xor(mask, addr);
  102. }
  103. static inline int
  104. test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
  105. {
  106. unsigned long *addr = __bitops_word(nr, ptr);
  107. unsigned long old, mask;
  108. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  109. old = __atomic64_or_barrier(mask, addr);
  110. return (old & mask) != 0;
  111. }
  112. static inline int
  113. test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
  114. {
  115. unsigned long *addr = __bitops_word(nr, ptr);
  116. unsigned long old, mask;
  117. mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
  118. old = __atomic64_and_barrier(mask, addr);
  119. return (old & ~mask) != 0;
  120. }
  121. static inline int
  122. test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
  123. {
  124. unsigned long *addr = __bitops_word(nr, ptr);
  125. unsigned long old, mask;
  126. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  127. old = __atomic64_xor_barrier(mask, addr);
  128. return (old & mask) != 0;
  129. }
  130. static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
  131. {
  132. unsigned char *addr = __bitops_byte(nr, ptr);
  133. *addr |= 1 << (nr & 7);
  134. }
  135. static inline void
  136. __clear_bit(unsigned long nr, volatile unsigned long *ptr)
  137. {
  138. unsigned char *addr = __bitops_byte(nr, ptr);
  139. *addr &= ~(1 << (nr & 7));
  140. }
  141. static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
  142. {
  143. unsigned char *addr = __bitops_byte(nr, ptr);
  144. *addr ^= 1 << (nr & 7);
  145. }
  146. static inline int
  147. __test_and_set_bit(unsigned long nr, volatile unsigned long *ptr)
  148. {
  149. unsigned char *addr = __bitops_byte(nr, ptr);
  150. unsigned char ch;
  151. ch = *addr;
  152. *addr |= 1 << (nr & 7);
  153. return (ch >> (nr & 7)) & 1;
  154. }
  155. static inline int
  156. __test_and_clear_bit(unsigned long nr, volatile unsigned long *ptr)
  157. {
  158. unsigned char *addr = __bitops_byte(nr, ptr);
  159. unsigned char ch;
  160. ch = *addr;
  161. *addr &= ~(1 << (nr & 7));
  162. return (ch >> (nr & 7)) & 1;
  163. }
  164. static inline int
  165. __test_and_change_bit(unsigned long nr, volatile unsigned long *ptr)
  166. {
  167. unsigned char *addr = __bitops_byte(nr, ptr);
  168. unsigned char ch;
  169. ch = *addr;
  170. *addr ^= 1 << (nr & 7);
  171. return (ch >> (nr & 7)) & 1;
  172. }
  173. static inline int test_bit(unsigned long nr, const volatile unsigned long *ptr)
  174. {
  175. const volatile unsigned char *addr;
  176. addr = ((const volatile unsigned char *)ptr);
  177. addr += (nr ^ (BITS_PER_LONG - 8)) >> 3;
  178. return (*addr >> (nr & 7)) & 1;
  179. }
  180. static inline int test_and_set_bit_lock(unsigned long nr,
  181. volatile unsigned long *ptr)
  182. {
  183. if (test_bit(nr, ptr))
  184. return 1;
  185. return test_and_set_bit(nr, ptr);
  186. }
  187. static inline void clear_bit_unlock(unsigned long nr,
  188. volatile unsigned long *ptr)
  189. {
  190. smp_mb__before_atomic();
  191. clear_bit(nr, ptr);
  192. }
  193. static inline void __clear_bit_unlock(unsigned long nr,
  194. volatile unsigned long *ptr)
  195. {
  196. smp_mb();
  197. __clear_bit(nr, ptr);
  198. }
  199. /*
  200. * Functions which use MSB0 bit numbering.
  201. * The bits are numbered:
  202. * |0..............63|64............127|128...........191|192...........255|
  203. */
  204. unsigned long find_first_bit_inv(const unsigned long *addr, unsigned long size);
  205. unsigned long find_next_bit_inv(const unsigned long *addr, unsigned long size,
  206. unsigned long offset);
  207. #define for_each_set_bit_inv(bit, addr, size) \
  208. for ((bit) = find_first_bit_inv((addr), (size)); \
  209. (bit) < (size); \
  210. (bit) = find_next_bit_inv((addr), (size), (bit) + 1))
  211. static inline void set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  212. {
  213. return set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  214. }
  215. static inline void clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  216. {
  217. return clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  218. }
  219. static inline void __set_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  220. {
  221. return __set_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  222. }
  223. static inline void __clear_bit_inv(unsigned long nr, volatile unsigned long *ptr)
  224. {
  225. return __clear_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  226. }
  227. static inline int test_bit_inv(unsigned long nr,
  228. const volatile unsigned long *ptr)
  229. {
  230. return test_bit(nr ^ (BITS_PER_LONG - 1), ptr);
  231. }
  232. #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
  233. /**
  234. * __flogr - find leftmost one
  235. * @word - The word to search
  236. *
  237. * Returns the bit number of the most significant bit set,
  238. * where the most significant bit has bit number 0.
  239. * If no bit is set this function returns 64.
  240. */
  241. static inline unsigned char __flogr(unsigned long word)
  242. {
  243. if (__builtin_constant_p(word)) {
  244. unsigned long bit = 0;
  245. if (!word)
  246. return 64;
  247. if (!(word & 0xffffffff00000000UL)) {
  248. word <<= 32;
  249. bit += 32;
  250. }
  251. if (!(word & 0xffff000000000000UL)) {
  252. word <<= 16;
  253. bit += 16;
  254. }
  255. if (!(word & 0xff00000000000000UL)) {
  256. word <<= 8;
  257. bit += 8;
  258. }
  259. if (!(word & 0xf000000000000000UL)) {
  260. word <<= 4;
  261. bit += 4;
  262. }
  263. if (!(word & 0xc000000000000000UL)) {
  264. word <<= 2;
  265. bit += 2;
  266. }
  267. if (!(word & 0x8000000000000000UL)) {
  268. word <<= 1;
  269. bit += 1;
  270. }
  271. return bit;
  272. } else {
  273. register unsigned long bit asm("4") = word;
  274. register unsigned long out asm("5");
  275. asm volatile(
  276. " flogr %[bit],%[bit]\n"
  277. : [bit] "+d" (bit), [out] "=d" (out) : : "cc");
  278. return bit;
  279. }
  280. }
  281. /**
  282. * __ffs - find first bit in word.
  283. * @word: The word to search
  284. *
  285. * Undefined if no bit exists, so code should check against 0 first.
  286. */
  287. static inline unsigned long __ffs(unsigned long word)
  288. {
  289. return __flogr(-word & word) ^ (BITS_PER_LONG - 1);
  290. }
  291. /**
  292. * ffs - find first bit set
  293. * @word: the word to search
  294. *
  295. * This is defined the same way as the libc and
  296. * compiler builtin ffs routines (man ffs).
  297. */
  298. static inline int ffs(int word)
  299. {
  300. unsigned long mask = 2 * BITS_PER_LONG - 1;
  301. unsigned int val = (unsigned int)word;
  302. return (1 + (__flogr(-val & val) ^ (BITS_PER_LONG - 1))) & mask;
  303. }
  304. /**
  305. * __fls - find last (most-significant) set bit in a long word
  306. * @word: the word to search
  307. *
  308. * Undefined if no set bit exists, so code should check against 0 first.
  309. */
  310. static inline unsigned long __fls(unsigned long word)
  311. {
  312. return __flogr(word) ^ (BITS_PER_LONG - 1);
  313. }
  314. /**
  315. * fls64 - find last set bit in a 64-bit word
  316. * @word: the word to search
  317. *
  318. * This is defined in a similar way as the libc and compiler builtin
  319. * ffsll, but returns the position of the most significant set bit.
  320. *
  321. * fls64(value) returns 0 if value is 0 or the position of the last
  322. * set bit if value is nonzero. The last (most significant) bit is
  323. * at position 64.
  324. */
  325. static inline int fls64(unsigned long word)
  326. {
  327. unsigned long mask = 2 * BITS_PER_LONG - 1;
  328. return (1 + (__flogr(word) ^ (BITS_PER_LONG - 1))) & mask;
  329. }
  330. /**
  331. * fls - find last (most-significant) bit set
  332. * @word: the word to search
  333. *
  334. * This is defined the same way as ffs.
  335. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  336. */
  337. static inline int fls(int word)
  338. {
  339. return fls64((unsigned int)word);
  340. }
  341. #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
  342. #include <asm-generic/bitops/__ffs.h>
  343. #include <asm-generic/bitops/ffs.h>
  344. #include <asm-generic/bitops/__fls.h>
  345. #include <asm-generic/bitops/fls.h>
  346. #include <asm-generic/bitops/fls64.h>
  347. #endif /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
  348. #include <asm-generic/bitops/ffz.h>
  349. #include <asm-generic/bitops/find.h>
  350. #include <asm-generic/bitops/hweight.h>
  351. #include <asm-generic/bitops/sched.h>
  352. #include <asm-generic/bitops/le.h>
  353. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  354. #endif /* _S390_BITOPS_H */