bitops.h 12 KB

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