bitops.h 12 KB

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