bitops.h 21 KB

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