bitops.h 12 KB

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