bitops.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999
  4. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  5. *
  6. * Derived from "include/asm-i386/bitops.h"
  7. * Copyright (C) 1992, Linus Torvalds
  8. *
  9. */
  10. #ifndef _S390_BITOPS_H
  11. #define _S390_BITOPS_H
  12. #ifndef _LINUX_BITOPS_H
  13. #error only <linux/bitops.h> can be included directly
  14. #endif
  15. #include <linux/compiler.h>
  16. /*
  17. * 32 bit bitops format:
  18. * bit 0 is the LSB of *addr; bit 31 is the MSB of *addr;
  19. * bit 32 is the LSB of *(addr+4). That combined with the
  20. * big endian byte order on S390 give the following bit
  21. * order in memory:
  22. * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 \
  23. * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
  24. * after that follows the next long with bit numbers
  25. * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
  26. * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
  27. * The reason for this bit ordering is the fact that
  28. * in the architecture independent code bits operations
  29. * of the form "flags |= (1 << bitnr)" are used INTERMIXED
  30. * with operation of the form "set_bit(bitnr, flags)".
  31. *
  32. * 64 bit bitops format:
  33. * bit 0 is the LSB of *addr; bit 63 is the MSB of *addr;
  34. * bit 64 is the LSB of *(addr+8). That combined with the
  35. * big endian byte order on S390 give the following bit
  36. * order in memory:
  37. * 3f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30
  38. * 2f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20
  39. * 1f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10
  40. * 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
  41. * after that follows the next long with bit numbers
  42. * 7f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70
  43. * 6f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60
  44. * 5f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50
  45. * 4f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40
  46. * The reason for this bit ordering is the fact that
  47. * in the architecture independent code bits operations
  48. * of the form "flags |= (1 << bitnr)" are used INTERMIXED
  49. * with operation of the form "set_bit(bitnr, flags)".
  50. */
  51. /* bitmap tables from arch/s390/kernel/bitmap.c */
  52. extern const char _oi_bitmap[];
  53. extern const char _ni_bitmap[];
  54. extern const char _zb_findmap[];
  55. extern const char _sb_findmap[];
  56. #ifndef CONFIG_64BIT
  57. #define __BITOPS_OR "or"
  58. #define __BITOPS_AND "nr"
  59. #define __BITOPS_XOR "xr"
  60. #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
  61. asm volatile( \
  62. " l %0,%2\n" \
  63. "0: lr %1,%0\n" \
  64. __op_string " %1,%3\n" \
  65. " cs %0,%1,%2\n" \
  66. " jl 0b" \
  67. : "=&d" (__old), "=&d" (__new), \
  68. "=Q" (*(unsigned long *) __addr) \
  69. : "d" (__val), "Q" (*(unsigned long *) __addr) \
  70. : "cc");
  71. #else /* CONFIG_64BIT */
  72. #define __BITOPS_OR "ogr"
  73. #define __BITOPS_AND "ngr"
  74. #define __BITOPS_XOR "xgr"
  75. #define __BITOPS_LOOP(__old, __new, __addr, __val, __op_string) \
  76. asm volatile( \
  77. " lg %0,%2\n" \
  78. "0: lgr %1,%0\n" \
  79. __op_string " %1,%3\n" \
  80. " csg %0,%1,%2\n" \
  81. " jl 0b" \
  82. : "=&d" (__old), "=&d" (__new), \
  83. "=Q" (*(unsigned long *) __addr) \
  84. : "d" (__val), "Q" (*(unsigned long *) __addr) \
  85. : "cc");
  86. #endif /* CONFIG_64BIT */
  87. #define __BITOPS_WORDS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
  88. #ifdef CONFIG_SMP
  89. /*
  90. * SMP safe set_bit routine based on compare and swap (CS)
  91. */
  92. static inline void set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  93. {
  94. unsigned long addr, old, new, mask;
  95. addr = (unsigned long) ptr;
  96. /* calculate address for CS */
  97. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  98. /* make OR mask */
  99. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  100. /* Do the atomic update. */
  101. __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
  102. }
  103. /*
  104. * SMP safe clear_bit routine based on compare and swap (CS)
  105. */
  106. static inline void clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  107. {
  108. unsigned long addr, old, new, mask;
  109. addr = (unsigned long) ptr;
  110. /* calculate address for CS */
  111. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  112. /* make AND mask */
  113. mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
  114. /* Do the atomic update. */
  115. __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
  116. }
  117. /*
  118. * SMP safe change_bit routine based on compare and swap (CS)
  119. */
  120. static inline void change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  121. {
  122. unsigned long addr, old, new, mask;
  123. addr = (unsigned long) ptr;
  124. /* calculate address for CS */
  125. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  126. /* make XOR mask */
  127. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  128. /* Do the atomic update. */
  129. __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
  130. }
  131. /*
  132. * SMP safe test_and_set_bit routine based on compare and swap (CS)
  133. */
  134. static inline int
  135. test_and_set_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  136. {
  137. unsigned long addr, old, new, mask;
  138. addr = (unsigned long) ptr;
  139. /* calculate address for CS */
  140. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  141. /* make OR/test mask */
  142. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  143. /* Do the atomic update. */
  144. __BITOPS_LOOP(old, new, addr, mask, __BITOPS_OR);
  145. barrier();
  146. return (old & mask) != 0;
  147. }
  148. /*
  149. * SMP safe test_and_clear_bit routine based on compare and swap (CS)
  150. */
  151. static inline int
  152. test_and_clear_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  153. {
  154. unsigned long addr, old, new, mask;
  155. addr = (unsigned long) ptr;
  156. /* calculate address for CS */
  157. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  158. /* make AND/test mask */
  159. mask = ~(1UL << (nr & (BITS_PER_LONG - 1)));
  160. /* Do the atomic update. */
  161. __BITOPS_LOOP(old, new, addr, mask, __BITOPS_AND);
  162. barrier();
  163. return (old ^ new) != 0;
  164. }
  165. /*
  166. * SMP safe test_and_change_bit routine based on compare and swap (CS)
  167. */
  168. static inline int
  169. test_and_change_bit_cs(unsigned long nr, volatile unsigned long *ptr)
  170. {
  171. unsigned long addr, old, new, mask;
  172. addr = (unsigned long) ptr;
  173. /* calculate address for CS */
  174. addr += (nr ^ (nr & (BITS_PER_LONG - 1))) >> 3;
  175. /* make XOR/test mask */
  176. mask = 1UL << (nr & (BITS_PER_LONG - 1));
  177. /* Do the atomic update. */
  178. __BITOPS_LOOP(old, new, addr, mask, __BITOPS_XOR);
  179. barrier();
  180. return (old & mask) != 0;
  181. }
  182. #endif /* CONFIG_SMP */
  183. /*
  184. * fast, non-SMP set_bit routine
  185. */
  186. static inline void __set_bit(unsigned long nr, volatile unsigned long *ptr)
  187. {
  188. unsigned long addr;
  189. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  190. asm volatile(
  191. " oc %O0(1,%R0),%1"
  192. : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc" );
  193. }
  194. static inline void
  195. __constant_set_bit(const unsigned long nr, volatile unsigned long *ptr)
  196. {
  197. unsigned long addr;
  198. addr = ((unsigned long) ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  199. *(unsigned char *) addr |= 1 << (nr & 7);
  200. }
  201. #define set_bit_simple(nr,addr) \
  202. (__builtin_constant_p((nr)) ? \
  203. __constant_set_bit((nr),(addr)) : \
  204. __set_bit((nr),(addr)) )
  205. /*
  206. * fast, non-SMP clear_bit routine
  207. */
  208. static inline void
  209. __clear_bit(unsigned long nr, volatile unsigned long *ptr)
  210. {
  211. unsigned long addr;
  212. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  213. asm volatile(
  214. " nc %O0(1,%R0),%1"
  215. : "=Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7]) : "cc" );
  216. }
  217. static inline void
  218. __constant_clear_bit(const unsigned long nr, volatile unsigned long *ptr)
  219. {
  220. unsigned long addr;
  221. addr = ((unsigned long) ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  222. *(unsigned char *) addr &= ~(1 << (nr & 7));
  223. }
  224. #define clear_bit_simple(nr,addr) \
  225. (__builtin_constant_p((nr)) ? \
  226. __constant_clear_bit((nr),(addr)) : \
  227. __clear_bit((nr),(addr)) )
  228. /*
  229. * fast, non-SMP change_bit routine
  230. */
  231. static inline void __change_bit(unsigned long nr, volatile unsigned long *ptr)
  232. {
  233. unsigned long addr;
  234. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  235. asm volatile(
  236. " xc %O0(1,%R0),%1"
  237. : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7]) : "cc" );
  238. }
  239. static inline void
  240. __constant_change_bit(const unsigned long nr, volatile unsigned long *ptr)
  241. {
  242. unsigned long addr;
  243. addr = ((unsigned long) ptr) + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  244. *(unsigned char *) addr ^= 1 << (nr & 7);
  245. }
  246. #define change_bit_simple(nr,addr) \
  247. (__builtin_constant_p((nr)) ? \
  248. __constant_change_bit((nr),(addr)) : \
  249. __change_bit((nr),(addr)) )
  250. /*
  251. * fast, non-SMP test_and_set_bit routine
  252. */
  253. static inline int
  254. test_and_set_bit_simple(unsigned long nr, volatile unsigned long *ptr)
  255. {
  256. unsigned long addr;
  257. unsigned char ch;
  258. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  259. ch = *(unsigned char *) addr;
  260. asm volatile(
  261. " oc %O0(1,%R0),%1"
  262. : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
  263. : "cc", "memory");
  264. return (ch >> (nr & 7)) & 1;
  265. }
  266. #define __test_and_set_bit(X,Y) test_and_set_bit_simple(X,Y)
  267. /*
  268. * fast, non-SMP test_and_clear_bit routine
  269. */
  270. static inline int
  271. test_and_clear_bit_simple(unsigned long nr, volatile unsigned long *ptr)
  272. {
  273. unsigned long addr;
  274. unsigned char ch;
  275. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  276. ch = *(unsigned char *) addr;
  277. asm volatile(
  278. " nc %O0(1,%R0),%1"
  279. : "=Q" (*(char *) addr) : "Q" (_ni_bitmap[nr & 7])
  280. : "cc", "memory");
  281. return (ch >> (nr & 7)) & 1;
  282. }
  283. #define __test_and_clear_bit(X,Y) test_and_clear_bit_simple(X,Y)
  284. /*
  285. * fast, non-SMP test_and_change_bit routine
  286. */
  287. static inline int
  288. test_and_change_bit_simple(unsigned long nr, volatile unsigned long *ptr)
  289. {
  290. unsigned long addr;
  291. unsigned char ch;
  292. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  293. ch = *(unsigned char *) addr;
  294. asm volatile(
  295. " xc %O0(1,%R0),%1"
  296. : "=Q" (*(char *) addr) : "Q" (_oi_bitmap[nr & 7])
  297. : "cc", "memory");
  298. return (ch >> (nr & 7)) & 1;
  299. }
  300. #define __test_and_change_bit(X,Y) test_and_change_bit_simple(X,Y)
  301. #ifdef CONFIG_SMP
  302. #define set_bit set_bit_cs
  303. #define clear_bit clear_bit_cs
  304. #define change_bit change_bit_cs
  305. #define test_and_set_bit test_and_set_bit_cs
  306. #define test_and_clear_bit test_and_clear_bit_cs
  307. #define test_and_change_bit test_and_change_bit_cs
  308. #else
  309. #define set_bit set_bit_simple
  310. #define clear_bit clear_bit_simple
  311. #define change_bit change_bit_simple
  312. #define test_and_set_bit test_and_set_bit_simple
  313. #define test_and_clear_bit test_and_clear_bit_simple
  314. #define test_and_change_bit test_and_change_bit_simple
  315. #endif
  316. /*
  317. * This routine doesn't need to be atomic.
  318. */
  319. static inline int __test_bit(unsigned long nr, const volatile unsigned long *ptr)
  320. {
  321. unsigned long addr;
  322. unsigned char ch;
  323. addr = (unsigned long) ptr + ((nr ^ (BITS_PER_LONG - 8)) >> 3);
  324. ch = *(volatile unsigned char *) addr;
  325. return (ch >> (nr & 7)) & 1;
  326. }
  327. static inline int
  328. __constant_test_bit(unsigned long nr, const volatile unsigned long *addr) {
  329. return (((volatile char *) addr)
  330. [(nr^(BITS_PER_LONG-8))>>3] & (1<<(nr&7))) != 0;
  331. }
  332. #define test_bit(nr,addr) \
  333. (__builtin_constant_p((nr)) ? \
  334. __constant_test_bit((nr),(addr)) : \
  335. __test_bit((nr),(addr)) )
  336. /*
  337. * Optimized find bit helper functions.
  338. */
  339. /**
  340. * __ffz_word_loop - find byte offset of first long != -1UL
  341. * @addr: pointer to array of unsigned long
  342. * @size: size of the array in bits
  343. */
  344. static inline unsigned long __ffz_word_loop(const unsigned long *addr,
  345. unsigned long size)
  346. {
  347. typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
  348. unsigned long bytes = 0;
  349. asm volatile(
  350. #ifndef CONFIG_64BIT
  351. " ahi %1,-1\n"
  352. " sra %1,5\n"
  353. " jz 1f\n"
  354. "0: c %2,0(%0,%3)\n"
  355. " jne 1f\n"
  356. " la %0,4(%0)\n"
  357. " brct %1,0b\n"
  358. "1:\n"
  359. #else
  360. " aghi %1,-1\n"
  361. " srag %1,%1,6\n"
  362. " jz 1f\n"
  363. "0: cg %2,0(%0,%3)\n"
  364. " jne 1f\n"
  365. " la %0,8(%0)\n"
  366. " brct %1,0b\n"
  367. "1:\n"
  368. #endif
  369. : "+&a" (bytes), "+&d" (size)
  370. : "d" (-1UL), "a" (addr), "m" (*(addrtype *) addr)
  371. : "cc" );
  372. return bytes;
  373. }
  374. /**
  375. * __ffs_word_loop - find byte offset of first long != 0UL
  376. * @addr: pointer to array of unsigned long
  377. * @size: size of the array in bits
  378. */
  379. static inline unsigned long __ffs_word_loop(const unsigned long *addr,
  380. unsigned long size)
  381. {
  382. typedef struct { long _[__BITOPS_WORDS(size)]; } addrtype;
  383. unsigned long bytes = 0;
  384. asm volatile(
  385. #ifndef CONFIG_64BIT
  386. " ahi %1,-1\n"
  387. " sra %1,5\n"
  388. " jz 1f\n"
  389. "0: c %2,0(%0,%3)\n"
  390. " jne 1f\n"
  391. " la %0,4(%0)\n"
  392. " brct %1,0b\n"
  393. "1:\n"
  394. #else
  395. " aghi %1,-1\n"
  396. " srag %1,%1,6\n"
  397. " jz 1f\n"
  398. "0: cg %2,0(%0,%3)\n"
  399. " jne 1f\n"
  400. " la %0,8(%0)\n"
  401. " brct %1,0b\n"
  402. "1:\n"
  403. #endif
  404. : "+&a" (bytes), "+&a" (size)
  405. : "d" (0UL), "a" (addr), "m" (*(addrtype *) addr)
  406. : "cc" );
  407. return bytes;
  408. }
  409. /**
  410. * __ffz_word - add number of the first unset bit
  411. * @nr: base value the bit number is added to
  412. * @word: the word that is searched for unset bits
  413. */
  414. static inline unsigned long __ffz_word(unsigned long nr, unsigned long word)
  415. {
  416. #ifdef CONFIG_64BIT
  417. if ((word & 0xffffffff) == 0xffffffff) {
  418. word >>= 32;
  419. nr += 32;
  420. }
  421. #endif
  422. if ((word & 0xffff) == 0xffff) {
  423. word >>= 16;
  424. nr += 16;
  425. }
  426. if ((word & 0xff) == 0xff) {
  427. word >>= 8;
  428. nr += 8;
  429. }
  430. return nr + _zb_findmap[(unsigned char) word];
  431. }
  432. /**
  433. * __ffs_word - add number of the first set bit
  434. * @nr: base value the bit number is added to
  435. * @word: the word that is searched for set bits
  436. */
  437. static inline unsigned long __ffs_word(unsigned long nr, unsigned long word)
  438. {
  439. #ifdef CONFIG_64BIT
  440. if ((word & 0xffffffff) == 0) {
  441. word >>= 32;
  442. nr += 32;
  443. }
  444. #endif
  445. if ((word & 0xffff) == 0) {
  446. word >>= 16;
  447. nr += 16;
  448. }
  449. if ((word & 0xff) == 0) {
  450. word >>= 8;
  451. nr += 8;
  452. }
  453. return nr + _sb_findmap[(unsigned char) word];
  454. }
  455. /**
  456. * __load_ulong_be - load big endian unsigned long
  457. * @p: pointer to array of unsigned long
  458. * @offset: byte offset of source value in the array
  459. */
  460. static inline unsigned long __load_ulong_be(const unsigned long *p,
  461. unsigned long offset)
  462. {
  463. p = (unsigned long *)((unsigned long) p + offset);
  464. return *p;
  465. }
  466. /**
  467. * __load_ulong_le - load little endian unsigned long
  468. * @p: pointer to array of unsigned long
  469. * @offset: byte offset of source value in the array
  470. */
  471. static inline unsigned long __load_ulong_le(const unsigned long *p,
  472. unsigned long offset)
  473. {
  474. unsigned long word;
  475. p = (unsigned long *)((unsigned long) p + offset);
  476. #ifndef CONFIG_64BIT
  477. asm volatile(
  478. " ic %0,%O1(%R1)\n"
  479. " icm %0,2,%O1+1(%R1)\n"
  480. " icm %0,4,%O1+2(%R1)\n"
  481. " icm %0,8,%O1+3(%R1)"
  482. : "=&d" (word) : "Q" (*p) : "cc");
  483. #else
  484. asm volatile(
  485. " lrvg %0,%1"
  486. : "=d" (word) : "m" (*p) );
  487. #endif
  488. return word;
  489. }
  490. /*
  491. * The various find bit functions.
  492. */
  493. /*
  494. * ffz - find first zero in word.
  495. * @word: The word to search
  496. *
  497. * Undefined if no zero exists, so code should check against ~0UL first.
  498. */
  499. static inline unsigned long ffz(unsigned long word)
  500. {
  501. return __ffz_word(0, word);
  502. }
  503. /**
  504. * __ffs - find first bit in word.
  505. * @word: The word to search
  506. *
  507. * Undefined if no bit exists, so code should check against 0 first.
  508. */
  509. static inline unsigned long __ffs (unsigned long word)
  510. {
  511. return __ffs_word(0, word);
  512. }
  513. /**
  514. * ffs - find first bit set
  515. * @x: the word to search
  516. *
  517. * This is defined the same way as
  518. * the libc and compiler builtin ffs routines, therefore
  519. * differs in spirit from the above ffz (man ffs).
  520. */
  521. static inline int ffs(int x)
  522. {
  523. if (!x)
  524. return 0;
  525. return __ffs_word(1, x);
  526. }
  527. /**
  528. * find_first_zero_bit - find the first zero bit in a memory region
  529. * @addr: The address to start the search at
  530. * @size: The maximum size to search
  531. *
  532. * Returns the bit-number of the first zero bit, not the number of the byte
  533. * containing a bit.
  534. */
  535. static inline unsigned long find_first_zero_bit(const unsigned long *addr,
  536. unsigned long size)
  537. {
  538. unsigned long bytes, bits;
  539. if (!size)
  540. return 0;
  541. bytes = __ffz_word_loop(addr, size);
  542. bits = __ffz_word(bytes*8, __load_ulong_be(addr, bytes));
  543. return (bits < size) ? bits : size;
  544. }
  545. #define find_first_zero_bit find_first_zero_bit
  546. /**
  547. * find_first_bit - find the first set bit in a memory region
  548. * @addr: The address to start the search at
  549. * @size: The maximum size to search
  550. *
  551. * Returns the bit-number of the first set bit, not the number of the byte
  552. * containing a bit.
  553. */
  554. static inline unsigned long find_first_bit(const unsigned long * addr,
  555. unsigned long size)
  556. {
  557. unsigned long bytes, bits;
  558. if (!size)
  559. return 0;
  560. bytes = __ffs_word_loop(addr, size);
  561. bits = __ffs_word(bytes*8, __load_ulong_be(addr, bytes));
  562. return (bits < size) ? bits : size;
  563. }
  564. #define find_first_bit find_first_bit
  565. /*
  566. * Big endian variant whichs starts bit counting from left using
  567. * the flogr (find leftmost one) instruction.
  568. */
  569. static inline unsigned long __flo_word(unsigned long nr, unsigned long val)
  570. {
  571. register unsigned long bit asm("2") = val;
  572. register unsigned long out asm("3");
  573. asm volatile (
  574. " .insn rre,0xb9830000,%[bit],%[bit]\n"
  575. : [bit] "+d" (bit), [out] "=d" (out) : : "cc");
  576. return nr + bit;
  577. }
  578. /*
  579. * 64 bit special left bitops format:
  580. * order in memory:
  581. * 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
  582. * 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
  583. * 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
  584. * 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
  585. * after that follows the next long with bit numbers
  586. * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
  587. * 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
  588. * 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
  589. * 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
  590. * The reason for this bit ordering is the fact that
  591. * the hardware sets bits in a bitmap starting at bit 0
  592. * and we don't want to scan the bitmap from the 'wrong
  593. * end'.
  594. */
  595. static inline unsigned long find_first_bit_left(const unsigned long *addr,
  596. unsigned long size)
  597. {
  598. unsigned long bytes, bits;
  599. if (!size)
  600. return 0;
  601. bytes = __ffs_word_loop(addr, size);
  602. bits = __flo_word(bytes * 8, __load_ulong_be(addr, bytes));
  603. return (bits < size) ? bits : size;
  604. }
  605. static inline int find_next_bit_left(const unsigned long *addr,
  606. unsigned long size,
  607. unsigned long offset)
  608. {
  609. const unsigned long *p;
  610. unsigned long bit, set;
  611. if (offset >= size)
  612. return size;
  613. bit = offset & (BITS_PER_LONG - 1);
  614. offset -= bit;
  615. size -= offset;
  616. p = addr + offset / BITS_PER_LONG;
  617. if (bit) {
  618. set = __flo_word(0, *p & (~0UL << bit));
  619. if (set >= size)
  620. return size + offset;
  621. if (set < BITS_PER_LONG)
  622. return set + offset;
  623. offset += BITS_PER_LONG;
  624. size -= BITS_PER_LONG;
  625. p++;
  626. }
  627. return offset + find_first_bit_left(p, size);
  628. }
  629. #define for_each_set_bit_left(bit, addr, size) \
  630. for ((bit) = find_first_bit_left((addr), (size)); \
  631. (bit) < (size); \
  632. (bit) = find_next_bit_left((addr), (size), (bit) + 1))
  633. /* same as for_each_set_bit() but use bit as value to start with */
  634. #define for_each_set_bit_left_cont(bit, addr, size) \
  635. for ((bit) = find_next_bit_left((addr), (size), (bit)); \
  636. (bit) < (size); \
  637. (bit) = find_next_bit_left((addr), (size), (bit) + 1))
  638. /**
  639. * find_next_zero_bit - find the first zero bit in a memory region
  640. * @addr: The address to base the search on
  641. * @offset: The bitnumber to start searching at
  642. * @size: The maximum size to search
  643. */
  644. static inline int find_next_zero_bit (const unsigned long * addr,
  645. unsigned long size,
  646. unsigned long offset)
  647. {
  648. const unsigned long *p;
  649. unsigned long bit, set;
  650. if (offset >= size)
  651. return size;
  652. bit = offset & (BITS_PER_LONG - 1);
  653. offset -= bit;
  654. size -= offset;
  655. p = addr + offset / BITS_PER_LONG;
  656. if (bit) {
  657. /*
  658. * __ffz_word returns BITS_PER_LONG
  659. * if no zero bit is present in the word.
  660. */
  661. set = __ffz_word(bit, *p >> bit);
  662. if (set >= size)
  663. return size + offset;
  664. if (set < BITS_PER_LONG)
  665. return set + offset;
  666. offset += BITS_PER_LONG;
  667. size -= BITS_PER_LONG;
  668. p++;
  669. }
  670. return offset + find_first_zero_bit(p, size);
  671. }
  672. #define find_next_zero_bit find_next_zero_bit
  673. /**
  674. * find_next_bit - find the first set bit in a memory region
  675. * @addr: The address to base the search on
  676. * @offset: The bitnumber to start searching at
  677. * @size: The maximum size to search
  678. */
  679. static inline int find_next_bit (const unsigned long * addr,
  680. unsigned long size,
  681. unsigned long offset)
  682. {
  683. const unsigned long *p;
  684. unsigned long bit, set;
  685. if (offset >= size)
  686. return size;
  687. bit = offset & (BITS_PER_LONG - 1);
  688. offset -= bit;
  689. size -= offset;
  690. p = addr + offset / BITS_PER_LONG;
  691. if (bit) {
  692. /*
  693. * __ffs_word returns BITS_PER_LONG
  694. * if no one bit is present in the word.
  695. */
  696. set = __ffs_word(0, *p & (~0UL << bit));
  697. if (set >= size)
  698. return size + offset;
  699. if (set < BITS_PER_LONG)
  700. return set + offset;
  701. offset += BITS_PER_LONG;
  702. size -= BITS_PER_LONG;
  703. p++;
  704. }
  705. return offset + find_first_bit(p, size);
  706. }
  707. #define find_next_bit find_next_bit
  708. /*
  709. * Every architecture must define this function. It's the fastest
  710. * way of searching a 140-bit bitmap where the first 100 bits are
  711. * unlikely to be set. It's guaranteed that at least one of the 140
  712. * bits is cleared.
  713. */
  714. static inline int sched_find_first_bit(unsigned long *b)
  715. {
  716. return find_first_bit(b, 140);
  717. }
  718. #include <asm-generic/bitops/fls.h>
  719. #include <asm-generic/bitops/__fls.h>
  720. #include <asm-generic/bitops/fls64.h>
  721. #include <asm-generic/bitops/hweight.h>
  722. #include <asm-generic/bitops/lock.h>
  723. /*
  724. * ATTENTION: intel byte ordering convention for ext2 and minix !!
  725. * bit 0 is the LSB of addr; bit 31 is the MSB of addr;
  726. * bit 32 is the LSB of (addr+4).
  727. * That combined with the little endian byte order of Intel gives the
  728. * following bit order in memory:
  729. * 07 06 05 04 03 02 01 00 15 14 13 12 11 10 09 08 \
  730. * 23 22 21 20 19 18 17 16 31 30 29 28 27 26 25 24
  731. */
  732. static inline int find_first_zero_bit_le(void *vaddr, unsigned int size)
  733. {
  734. unsigned long bytes, bits;
  735. if (!size)
  736. return 0;
  737. bytes = __ffz_word_loop(vaddr, size);
  738. bits = __ffz_word(bytes*8, __load_ulong_le(vaddr, bytes));
  739. return (bits < size) ? bits : size;
  740. }
  741. #define find_first_zero_bit_le find_first_zero_bit_le
  742. static inline int find_next_zero_bit_le(void *vaddr, unsigned long size,
  743. unsigned long offset)
  744. {
  745. unsigned long *addr = vaddr, *p;
  746. unsigned long bit, set;
  747. if (offset >= size)
  748. return size;
  749. bit = offset & (BITS_PER_LONG - 1);
  750. offset -= bit;
  751. size -= offset;
  752. p = addr + offset / BITS_PER_LONG;
  753. if (bit) {
  754. /*
  755. * s390 version of ffz returns BITS_PER_LONG
  756. * if no zero bit is present in the word.
  757. */
  758. set = __ffz_word(bit, __load_ulong_le(p, 0) >> bit);
  759. if (set >= size)
  760. return size + offset;
  761. if (set < BITS_PER_LONG)
  762. return set + offset;
  763. offset += BITS_PER_LONG;
  764. size -= BITS_PER_LONG;
  765. p++;
  766. }
  767. return offset + find_first_zero_bit_le(p, size);
  768. }
  769. #define find_next_zero_bit_le find_next_zero_bit_le
  770. static inline unsigned long find_first_bit_le(void *vaddr, unsigned long size)
  771. {
  772. unsigned long bytes, bits;
  773. if (!size)
  774. return 0;
  775. bytes = __ffs_word_loop(vaddr, size);
  776. bits = __ffs_word(bytes*8, __load_ulong_le(vaddr, bytes));
  777. return (bits < size) ? bits : size;
  778. }
  779. #define find_first_bit_le find_first_bit_le
  780. static inline int find_next_bit_le(void *vaddr, unsigned long size,
  781. unsigned long offset)
  782. {
  783. unsigned long *addr = vaddr, *p;
  784. unsigned long bit, set;
  785. if (offset >= size)
  786. return size;
  787. bit = offset & (BITS_PER_LONG - 1);
  788. offset -= bit;
  789. size -= offset;
  790. p = addr + offset / BITS_PER_LONG;
  791. if (bit) {
  792. /*
  793. * s390 version of ffz returns BITS_PER_LONG
  794. * if no zero bit is present in the word.
  795. */
  796. set = __ffs_word(0, __load_ulong_le(p, 0) & (~0UL << bit));
  797. if (set >= size)
  798. return size + offset;
  799. if (set < BITS_PER_LONG)
  800. return set + offset;
  801. offset += BITS_PER_LONG;
  802. size -= BITS_PER_LONG;
  803. p++;
  804. }
  805. return offset + find_first_bit_le(p, size);
  806. }
  807. #define find_next_bit_le find_next_bit_le
  808. #include <asm-generic/bitops/le.h>
  809. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  810. #endif /* _S390_BITOPS_H */