bitops.h 10 KB

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