bitops.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #ifndef _PARISC_BITOPS_H
  2. #define _PARISC_BITOPS_H
  3. #include <linux/compiler.h>
  4. #include <asm/system.h>
  5. #include <asm/byteorder.h>
  6. #include <asm/atomic.h>
  7. /*
  8. * HP-PARISC specific bit operations
  9. * for a detailed description of the functions please refer
  10. * to include/asm-i386/bitops.h or kerneldoc
  11. */
  12. #ifdef __LP64__
  13. # define SHIFT_PER_LONG 6
  14. #ifndef BITS_PER_LONG
  15. # define BITS_PER_LONG 64
  16. #endif
  17. #else
  18. # define SHIFT_PER_LONG 5
  19. #ifndef BITS_PER_LONG
  20. # define BITS_PER_LONG 32
  21. #endif
  22. #endif
  23. #define CHOP_SHIFTCOUNT(x) ((x) & (BITS_PER_LONG - 1))
  24. #define smp_mb__before_clear_bit() smp_mb()
  25. #define smp_mb__after_clear_bit() smp_mb()
  26. static __inline__ void set_bit(int nr, volatile unsigned long * address)
  27. {
  28. unsigned long mask;
  29. unsigned long *addr = (unsigned long *) address;
  30. unsigned long flags;
  31. addr += (nr >> SHIFT_PER_LONG);
  32. mask = 1L << CHOP_SHIFTCOUNT(nr);
  33. _atomic_spin_lock_irqsave(addr, flags);
  34. *addr |= mask;
  35. _atomic_spin_unlock_irqrestore(addr, flags);
  36. }
  37. static __inline__ void __set_bit(int nr, volatile unsigned long * address)
  38. {
  39. unsigned long mask;
  40. unsigned long *addr = (unsigned long *) address;
  41. addr += (nr >> SHIFT_PER_LONG);
  42. mask = 1L << CHOP_SHIFTCOUNT(nr);
  43. *addr |= mask;
  44. }
  45. static __inline__ void clear_bit(int nr, volatile unsigned long * address)
  46. {
  47. unsigned long mask;
  48. unsigned long *addr = (unsigned long *) address;
  49. unsigned long flags;
  50. addr += (nr >> SHIFT_PER_LONG);
  51. mask = 1L << CHOP_SHIFTCOUNT(nr);
  52. _atomic_spin_lock_irqsave(addr, flags);
  53. *addr &= ~mask;
  54. _atomic_spin_unlock_irqrestore(addr, flags);
  55. }
  56. static __inline__ void __clear_bit(unsigned long nr, volatile unsigned long * address)
  57. {
  58. unsigned long mask;
  59. unsigned long *addr = (unsigned long *) address;
  60. addr += (nr >> SHIFT_PER_LONG);
  61. mask = 1L << CHOP_SHIFTCOUNT(nr);
  62. *addr &= ~mask;
  63. }
  64. static __inline__ void change_bit(int nr, volatile unsigned long * address)
  65. {
  66. unsigned long mask;
  67. unsigned long *addr = (unsigned long *) address;
  68. unsigned long flags;
  69. addr += (nr >> SHIFT_PER_LONG);
  70. mask = 1L << CHOP_SHIFTCOUNT(nr);
  71. _atomic_spin_lock_irqsave(addr, flags);
  72. *addr ^= mask;
  73. _atomic_spin_unlock_irqrestore(addr, flags);
  74. }
  75. static __inline__ void __change_bit(int nr, volatile unsigned long * address)
  76. {
  77. unsigned long mask;
  78. unsigned long *addr = (unsigned long *) address;
  79. addr += (nr >> SHIFT_PER_LONG);
  80. mask = 1L << CHOP_SHIFTCOUNT(nr);
  81. *addr ^= mask;
  82. }
  83. static __inline__ int test_and_set_bit(int nr, volatile unsigned long * address)
  84. {
  85. unsigned long mask;
  86. unsigned long *addr = (unsigned long *) address;
  87. int oldbit;
  88. unsigned long flags;
  89. addr += (nr >> SHIFT_PER_LONG);
  90. mask = 1L << CHOP_SHIFTCOUNT(nr);
  91. _atomic_spin_lock_irqsave(addr, flags);
  92. oldbit = (*addr & mask) ? 1 : 0;
  93. *addr |= mask;
  94. _atomic_spin_unlock_irqrestore(addr, flags);
  95. return oldbit;
  96. }
  97. static __inline__ int __test_and_set_bit(int nr, volatile unsigned long * address)
  98. {
  99. unsigned long mask;
  100. unsigned long *addr = (unsigned long *) address;
  101. int oldbit;
  102. addr += (nr >> SHIFT_PER_LONG);
  103. mask = 1L << CHOP_SHIFTCOUNT(nr);
  104. oldbit = (*addr & mask) ? 1 : 0;
  105. *addr |= mask;
  106. return oldbit;
  107. }
  108. static __inline__ int test_and_clear_bit(int nr, volatile unsigned long * address)
  109. {
  110. unsigned long mask;
  111. unsigned long *addr = (unsigned long *) address;
  112. int oldbit;
  113. unsigned long flags;
  114. addr += (nr >> SHIFT_PER_LONG);
  115. mask = 1L << CHOP_SHIFTCOUNT(nr);
  116. _atomic_spin_lock_irqsave(addr, flags);
  117. oldbit = (*addr & mask) ? 1 : 0;
  118. *addr &= ~mask;
  119. _atomic_spin_unlock_irqrestore(addr, flags);
  120. return oldbit;
  121. }
  122. static __inline__ int __test_and_clear_bit(int nr, volatile unsigned long * address)
  123. {
  124. unsigned long mask;
  125. unsigned long *addr = (unsigned long *) address;
  126. int oldbit;
  127. addr += (nr >> SHIFT_PER_LONG);
  128. mask = 1L << CHOP_SHIFTCOUNT(nr);
  129. oldbit = (*addr & mask) ? 1 : 0;
  130. *addr &= ~mask;
  131. return oldbit;
  132. }
  133. static __inline__ int test_and_change_bit(int nr, volatile unsigned long * address)
  134. {
  135. unsigned long mask;
  136. unsigned long *addr = (unsigned long *) address;
  137. int oldbit;
  138. unsigned long flags;
  139. addr += (nr >> SHIFT_PER_LONG);
  140. mask = 1L << CHOP_SHIFTCOUNT(nr);
  141. _atomic_spin_lock_irqsave(addr, flags);
  142. oldbit = (*addr & mask) ? 1 : 0;
  143. *addr ^= mask;
  144. _atomic_spin_unlock_irqrestore(addr, flags);
  145. return oldbit;
  146. }
  147. static __inline__ int __test_and_change_bit(int nr, volatile unsigned long * address)
  148. {
  149. unsigned long mask;
  150. unsigned long *addr = (unsigned long *) address;
  151. int oldbit;
  152. addr += (nr >> SHIFT_PER_LONG);
  153. mask = 1L << CHOP_SHIFTCOUNT(nr);
  154. oldbit = (*addr & mask) ? 1 : 0;
  155. *addr ^= mask;
  156. return oldbit;
  157. }
  158. static __inline__ int test_bit(int nr, const volatile unsigned long *address)
  159. {
  160. unsigned long mask;
  161. const unsigned long *addr = (const unsigned long *)address;
  162. addr += (nr >> SHIFT_PER_LONG);
  163. mask = 1L << CHOP_SHIFTCOUNT(nr);
  164. return !!(*addr & mask);
  165. }
  166. #ifdef __KERNEL__
  167. /**
  168. * __ffs - find first bit in word. returns 0 to "BITS_PER_LONG-1".
  169. * @word: The word to search
  170. *
  171. * __ffs() return is undefined if no bit is set.
  172. *
  173. * 32-bit fast __ffs by LaMont Jones "lamont At hp com".
  174. * 64-bit enhancement by Grant Grundler "grundler At parisc-linux org".
  175. * (with help from willy/jejb to get the semantics right)
  176. *
  177. * This algorithm avoids branches by making use of nullification.
  178. * One side effect of "extr" instructions is it sets PSW[N] bit.
  179. * How PSW[N] (nullify next insn) gets set is determined by the
  180. * "condition" field (eg "<>" or "TR" below) in the extr* insn.
  181. * Only the 1st and one of either the 2cd or 3rd insn will get executed.
  182. * Each set of 3 insn will get executed in 2 cycles on PA8x00 vs 16 or so
  183. * cycles for each mispredicted branch.
  184. */
  185. static __inline__ unsigned long __ffs(unsigned long x)
  186. {
  187. unsigned long ret;
  188. __asm__(
  189. #if BITS_PER_LONG > 32
  190. " ldi 63,%1\n"
  191. " extrd,u,*<> %0,63,32,%%r0\n"
  192. " extrd,u,*TR %0,31,32,%0\n" /* move top 32-bits down */
  193. " addi -32,%1,%1\n"
  194. #else
  195. " ldi 31,%1\n"
  196. #endif
  197. " extru,<> %0,31,16,%%r0\n"
  198. " extru,TR %0,15,16,%0\n" /* xxxx0000 -> 0000xxxx */
  199. " addi -16,%1,%1\n"
  200. " extru,<> %0,31,8,%%r0\n"
  201. " extru,TR %0,23,8,%0\n" /* 0000xx00 -> 000000xx */
  202. " addi -8,%1,%1\n"
  203. " extru,<> %0,31,4,%%r0\n"
  204. " extru,TR %0,27,4,%0\n" /* 000000x0 -> 0000000x */
  205. " addi -4,%1,%1\n"
  206. " extru,<> %0,31,2,%%r0\n"
  207. " extru,TR %0,29,2,%0\n" /* 0000000y, 1100b -> 0011b */
  208. " addi -2,%1,%1\n"
  209. " extru,= %0,31,1,%%r0\n" /* check last bit */
  210. " addi -1,%1,%1\n"
  211. : "+r" (x), "=r" (ret) );
  212. return ret;
  213. }
  214. /* Undefined if no bit is zero. */
  215. #define ffz(x) __ffs(~x)
  216. /*
  217. * ffs: find first bit set. returns 1 to BITS_PER_LONG or 0 (if none set)
  218. * This is defined the same way as the libc and compiler builtin
  219. * ffs routines, therefore differs in spirit from the above ffz (man ffs).
  220. */
  221. static __inline__ int ffs(int x)
  222. {
  223. return x ? (__ffs((unsigned long)x) + 1) : 0;
  224. }
  225. /*
  226. * fls: find last (most significant) bit set.
  227. * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  228. */
  229. static __inline__ int fls(int x)
  230. {
  231. int ret;
  232. if (!x)
  233. return 0;
  234. __asm__(
  235. " ldi 1,%1\n"
  236. " extru,<> %0,15,16,%%r0\n"
  237. " zdep,TR %0,15,16,%0\n" /* xxxx0000 */
  238. " addi 16,%1,%1\n"
  239. " extru,<> %0,7,8,%%r0\n"
  240. " zdep,TR %0,23,24,%0\n" /* xx000000 */
  241. " addi 8,%1,%1\n"
  242. " extru,<> %0,3,4,%%r0\n"
  243. " zdep,TR %0,27,28,%0\n" /* x0000000 */
  244. " addi 4,%1,%1\n"
  245. " extru,<> %0,1,2,%%r0\n"
  246. " zdep,TR %0,29,30,%0\n" /* y0000000 (y&3 = 0) */
  247. " addi 2,%1,%1\n"
  248. " extru,= %0,0,1,%%r0\n"
  249. " addi 1,%1,%1\n" /* if y & 8, add 1 */
  250. : "+r" (x), "=r" (ret) );
  251. return ret;
  252. }
  253. /*
  254. * hweightN: returns the hamming weight (i.e. the number
  255. * of bits set) of a N-bit word
  256. */
  257. #define hweight64(x) \
  258. ({ \
  259. unsigned long __x = (x); \
  260. unsigned int __w; \
  261. __w = generic_hweight32((unsigned int) __x); \
  262. __w += generic_hweight32((unsigned int) (__x>>32)); \
  263. __w; \
  264. })
  265. #define hweight32(x) generic_hweight32(x)
  266. #define hweight16(x) generic_hweight16(x)
  267. #define hweight8(x) generic_hweight8(x)
  268. /*
  269. * Every architecture must define this function. It's the fastest
  270. * way of searching a 140-bit bitmap where the first 100 bits are
  271. * unlikely to be set. It's guaranteed that at least one of the 140
  272. * bits is cleared.
  273. */
  274. static inline int sched_find_first_bit(const unsigned long *b)
  275. {
  276. #ifndef __LP64__
  277. if (unlikely(b[0]))
  278. return __ffs(b[0]);
  279. if (unlikely(b[1]))
  280. return __ffs(b[1]) + 32;
  281. if (unlikely(b[2]))
  282. return __ffs(b[2]) + 64;
  283. if (b[3])
  284. return __ffs(b[3]) + 96;
  285. return __ffs(b[4]) + 128;
  286. #else
  287. if (unlikely(b[0]))
  288. return __ffs(b[0]);
  289. if (unlikely(((unsigned int)b[1])))
  290. return __ffs(b[1]) + 64;
  291. if (b[1] >> 32)
  292. return __ffs(b[1] >> 32) + 96;
  293. return __ffs(b[2]) + 128;
  294. #endif
  295. }
  296. #endif /* __KERNEL__ */
  297. /*
  298. * This implementation of find_{first,next}_zero_bit was stolen from
  299. * Linus' asm-alpha/bitops.h.
  300. */
  301. #define find_first_zero_bit(addr, size) \
  302. find_next_zero_bit((addr), (size), 0)
  303. static __inline__ unsigned long find_next_zero_bit(const void * addr, unsigned long size, unsigned long offset)
  304. {
  305. const unsigned long * p = ((unsigned long *) addr) + (offset >> SHIFT_PER_LONG);
  306. unsigned long result = offset & ~(BITS_PER_LONG-1);
  307. unsigned long tmp;
  308. if (offset >= size)
  309. return size;
  310. size -= result;
  311. offset &= (BITS_PER_LONG-1);
  312. if (offset) {
  313. tmp = *(p++);
  314. tmp |= ~0UL >> (BITS_PER_LONG-offset);
  315. if (size < BITS_PER_LONG)
  316. goto found_first;
  317. if (~tmp)
  318. goto found_middle;
  319. size -= BITS_PER_LONG;
  320. result += BITS_PER_LONG;
  321. }
  322. while (size & ~(BITS_PER_LONG -1)) {
  323. if (~(tmp = *(p++)))
  324. goto found_middle;
  325. result += BITS_PER_LONG;
  326. size -= BITS_PER_LONG;
  327. }
  328. if (!size)
  329. return result;
  330. tmp = *p;
  331. found_first:
  332. tmp |= ~0UL << size;
  333. found_middle:
  334. return result + ffz(tmp);
  335. }
  336. static __inline__ unsigned long find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
  337. {
  338. const unsigned long *p = addr + (offset >> 6);
  339. unsigned long result = offset & ~(BITS_PER_LONG-1);
  340. unsigned long tmp;
  341. if (offset >= size)
  342. return size;
  343. size -= result;
  344. offset &= (BITS_PER_LONG-1);
  345. if (offset) {
  346. tmp = *(p++);
  347. tmp &= (~0UL << offset);
  348. if (size < BITS_PER_LONG)
  349. goto found_first;
  350. if (tmp)
  351. goto found_middle;
  352. size -= BITS_PER_LONG;
  353. result += BITS_PER_LONG;
  354. }
  355. while (size & ~(BITS_PER_LONG-1)) {
  356. if ((tmp = *(p++)))
  357. goto found_middle;
  358. result += BITS_PER_LONG;
  359. size -= BITS_PER_LONG;
  360. }
  361. if (!size)
  362. return result;
  363. tmp = *p;
  364. found_first:
  365. tmp &= (~0UL >> (BITS_PER_LONG - size));
  366. if (tmp == 0UL) /* Are any bits set? */
  367. return result + size; /* Nope. */
  368. found_middle:
  369. return result + __ffs(tmp);
  370. }
  371. /**
  372. * find_first_bit - find the first set bit in a memory region
  373. * @addr: The address to start the search at
  374. * @size: The maximum size to search
  375. *
  376. * Returns the bit-number of the first set bit, not the number of the byte
  377. * containing a bit.
  378. */
  379. #define find_first_bit(addr, size) \
  380. find_next_bit((addr), (size), 0)
  381. #define _EXT2_HAVE_ASM_BITOPS_
  382. #ifdef __KERNEL__
  383. /*
  384. * test_and_{set,clear}_bit guarantee atomicity without
  385. * disabling interrupts.
  386. */
  387. #ifdef __LP64__
  388. #define ext2_set_bit(nr, addr) __test_and_set_bit((nr) ^ 0x38, (unsigned long *)addr)
  389. #define ext2_set_bit_atomic(l,nr,addr) test_and_set_bit((nr) ^ 0x38, (unsigned long *)addr)
  390. #define ext2_clear_bit(nr, addr) __test_and_clear_bit((nr) ^ 0x38, (unsigned long *)addr)
  391. #define ext2_clear_bit_atomic(l,nr,addr) test_and_clear_bit((nr) ^ 0x38, (unsigned long *)addr)
  392. #else
  393. #define ext2_set_bit(nr, addr) __test_and_set_bit((nr) ^ 0x18, (unsigned long *)addr)
  394. #define ext2_set_bit_atomic(l,nr,addr) test_and_set_bit((nr) ^ 0x18, (unsigned long *)addr)
  395. #define ext2_clear_bit(nr, addr) __test_and_clear_bit((nr) ^ 0x18, (unsigned long *)addr)
  396. #define ext2_clear_bit_atomic(l,nr,addr) test_and_clear_bit((nr) ^ 0x18, (unsigned long *)addr)
  397. #endif
  398. #endif /* __KERNEL__ */
  399. static __inline__ int ext2_test_bit(int nr, __const__ void * addr)
  400. {
  401. __const__ unsigned char *ADDR = (__const__ unsigned char *) addr;
  402. return (ADDR[nr >> 3] >> (nr & 7)) & 1;
  403. }
  404. /*
  405. * This implementation of ext2_find_{first,next}_zero_bit was stolen from
  406. * Linus' asm-alpha/bitops.h and modified for a big-endian machine.
  407. */
  408. #define ext2_find_first_zero_bit(addr, size) \
  409. ext2_find_next_zero_bit((addr), (size), 0)
  410. extern __inline__ unsigned long ext2_find_next_zero_bit(void *addr,
  411. unsigned long size, unsigned long offset)
  412. {
  413. unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
  414. unsigned int result = offset & ~31UL;
  415. unsigned int tmp;
  416. if (offset >= size)
  417. return size;
  418. size -= result;
  419. offset &= 31UL;
  420. if (offset) {
  421. tmp = cpu_to_le32p(p++);
  422. tmp |= ~0UL >> (32-offset);
  423. if (size < 32)
  424. goto found_first;
  425. if (tmp != ~0U)
  426. goto found_middle;
  427. size -= 32;
  428. result += 32;
  429. }
  430. while (size >= 32) {
  431. if ((tmp = cpu_to_le32p(p++)) != ~0U)
  432. goto found_middle;
  433. result += 32;
  434. size -= 32;
  435. }
  436. if (!size)
  437. return result;
  438. tmp = cpu_to_le32p(p);
  439. found_first:
  440. tmp |= ~0U << size;
  441. found_middle:
  442. return result + ffz(tmp);
  443. }
  444. /* Bitmap functions for the minix filesystem. */
  445. #define minix_test_and_set_bit(nr,addr) ext2_set_bit(nr,addr)
  446. #define minix_set_bit(nr,addr) ((void)ext2_set_bit(nr,addr))
  447. #define minix_test_and_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
  448. #define minix_test_bit(nr,addr) ext2_test_bit(nr,addr)
  449. #define minix_find_first_zero_bit(addr,size) ext2_find_first_zero_bit(addr,size)
  450. #endif /* _PARISC_BITOPS_H */