bitops.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * bitops.h: Bit string operations on the ppc
  3. */
  4. #ifndef _PPC_BITOPS_H
  5. #define _PPC_BITOPS_H
  6. #include <asm/byteorder.h>
  7. #include <asm-generic/bitops/__ffs.h>
  8. /*
  9. * Arguably these bit operations don't imply any memory barrier or
  10. * SMP ordering, but in fact a lot of drivers expect them to imply
  11. * both, since they do on x86 cpus.
  12. */
  13. #ifdef CONFIG_SMP
  14. #define SMP_WMB "eieio\n"
  15. #define SMP_MB "\nsync"
  16. #else
  17. #define SMP_WMB
  18. #define SMP_MB
  19. #endif /* CONFIG_SMP */
  20. #define __INLINE_BITOPS 1
  21. #if __INLINE_BITOPS
  22. /*
  23. * These used to be if'd out here because using : "cc" as a constraint
  24. * resulted in errors from egcs. Things may be OK with gcc-2.95.
  25. */
  26. static __inline__ void set_bit(int nr, volatile void * addr)
  27. {
  28. unsigned long old;
  29. unsigned long mask = 1 << (nr & 0x1f);
  30. unsigned long *p = ((unsigned long *)addr) + (nr >> 5);
  31. __asm__ __volatile__(SMP_WMB "\
  32. 1: lwarx %0,0,%3\n\
  33. or %0,%0,%2\n\
  34. stwcx. %0,0,%3\n\
  35. bne 1b"
  36. SMP_MB
  37. : "=&r" (old), "=m" (*p)
  38. : "r" (mask), "r" (p), "m" (*p)
  39. : "cc" );
  40. }
  41. static __inline__ void clear_bit(int nr, volatile void *addr)
  42. {
  43. unsigned long old;
  44. unsigned long mask = 1 << (nr & 0x1f);
  45. unsigned long *p = ((unsigned long *)addr) + (nr >> 5);
  46. __asm__ __volatile__(SMP_WMB "\
  47. 1: lwarx %0,0,%3\n\
  48. andc %0,%0,%2\n\
  49. stwcx. %0,0,%3\n\
  50. bne 1b"
  51. SMP_MB
  52. : "=&r" (old), "=m" (*p)
  53. : "r" (mask), "r" (p), "m" (*p)
  54. : "cc");
  55. }
  56. static __inline__ void change_bit(int nr, volatile void *addr)
  57. {
  58. unsigned long old;
  59. unsigned long mask = 1 << (nr & 0x1f);
  60. unsigned long *p = ((unsigned long *)addr) + (nr >> 5);
  61. __asm__ __volatile__(SMP_WMB "\
  62. 1: lwarx %0,0,%3\n\
  63. xor %0,%0,%2\n\
  64. stwcx. %0,0,%3\n\
  65. bne 1b"
  66. SMP_MB
  67. : "=&r" (old), "=m" (*p)
  68. : "r" (mask), "r" (p), "m" (*p)
  69. : "cc");
  70. }
  71. static __inline__ int test_and_set_bit(int nr, volatile void *addr)
  72. {
  73. unsigned int old, t;
  74. unsigned int mask = 1 << (nr & 0x1f);
  75. volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);
  76. __asm__ __volatile__(SMP_WMB "\
  77. 1: lwarx %0,0,%4\n\
  78. or %1,%0,%3\n\
  79. stwcx. %1,0,%4\n\
  80. bne 1b"
  81. SMP_MB
  82. : "=&r" (old), "=&r" (t), "=m" (*p)
  83. : "r" (mask), "r" (p), "m" (*p)
  84. : "cc");
  85. return (old & mask) != 0;
  86. }
  87. static __inline__ int test_and_clear_bit(int nr, volatile void *addr)
  88. {
  89. unsigned int old, t;
  90. unsigned int mask = 1 << (nr & 0x1f);
  91. volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);
  92. __asm__ __volatile__(SMP_WMB "\
  93. 1: lwarx %0,0,%4\n\
  94. andc %1,%0,%3\n\
  95. stwcx. %1,0,%4\n\
  96. bne 1b"
  97. SMP_MB
  98. : "=&r" (old), "=&r" (t), "=m" (*p)
  99. : "r" (mask), "r" (p), "m" (*p)
  100. : "cc");
  101. return (old & mask) != 0;
  102. }
  103. static __inline__ int test_and_change_bit(int nr, volatile void *addr)
  104. {
  105. unsigned int old, t;
  106. unsigned int mask = 1 << (nr & 0x1f);
  107. volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);
  108. __asm__ __volatile__(SMP_WMB "\
  109. 1: lwarx %0,0,%4\n\
  110. xor %1,%0,%3\n\
  111. stwcx. %1,0,%4\n\
  112. bne 1b"
  113. SMP_MB
  114. : "=&r" (old), "=&r" (t), "=m" (*p)
  115. : "r" (mask), "r" (p), "m" (*p)
  116. : "cc");
  117. return (old & mask) != 0;
  118. }
  119. #endif /* __INLINE_BITOPS */
  120. static __inline__ int test_bit(int nr, __const__ volatile void *addr)
  121. {
  122. __const__ unsigned int *p = (__const__ unsigned int *) addr;
  123. return ((p[nr >> 5] >> (nr & 0x1f)) & 1) != 0;
  124. }
  125. /* Return the bit position of the most significant 1 bit in a word */
  126. /* - the result is undefined when x == 0 */
  127. static __inline__ int __ilog2(unsigned int x)
  128. {
  129. int lz;
  130. asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
  131. return 31 - lz;
  132. }
  133. static __inline__ int ffz(unsigned int x)
  134. {
  135. if ((x = ~x) == 0)
  136. return 32;
  137. return __ilog2(x & -x);
  138. }
  139. /*
  140. * fls: find last (most-significant) bit set.
  141. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  142. *
  143. * On powerpc, __ilog2(0) returns -1, but this is not safe in general
  144. */
  145. static __inline__ int fls(unsigned int x)
  146. {
  147. return __ilog2(x) + 1;
  148. }
  149. #define PLATFORM_FLS
  150. /**
  151. * fls64 - find last set bit in a 64-bit word
  152. * @x: the word to search
  153. *
  154. * This is defined in a similar way as the libc and compiler builtin
  155. * ffsll, but returns the position of the most significant set bit.
  156. *
  157. * fls64(value) returns 0 if value is 0 or the position of the last
  158. * set bit if value is nonzero. The last (most significant) bit is
  159. * at position 64.
  160. */
  161. #if BITS_PER_LONG == 32
  162. static inline int fls64(__u64 x)
  163. {
  164. __u32 h = x >> 32;
  165. if (h)
  166. return fls(h) + 32;
  167. return fls(x);
  168. }
  169. #elif BITS_PER_LONG == 64
  170. static inline int fls64(__u64 x)
  171. {
  172. if (x == 0)
  173. return 0;
  174. return __ilog2(x) + 1;
  175. }
  176. #else
  177. #error BITS_PER_LONG not 32 or 64
  178. #endif
  179. #ifdef __KERNEL__
  180. /*
  181. * ffs: find first bit set. This is defined the same way as
  182. * the libc and compiler builtin ffs routines, therefore
  183. * differs in spirit from the above ffz (man ffs).
  184. */
  185. static __inline__ int ffs(int x)
  186. {
  187. return __ilog2(x & -x) + 1;
  188. }
  189. #define PLATFORM_FFS
  190. /*
  191. * hweightN: returns the hamming weight (i.e. the number
  192. * of bits set) of a N-bit word
  193. */
  194. #define hweight32(x) generic_hweight32(x)
  195. #define hweight16(x) generic_hweight16(x)
  196. #define hweight8(x) generic_hweight8(x)
  197. #endif /* __KERNEL__ */
  198. /*
  199. * This implementation of find_{first,next}_zero_bit was stolen from
  200. * Linus' asm-alpha/bitops.h.
  201. */
  202. #define find_first_zero_bit(addr, size) \
  203. find_next_zero_bit((addr), (size), 0)
  204. static __inline__ unsigned long find_next_zero_bit(void * addr,
  205. unsigned long size, unsigned long offset)
  206. {
  207. unsigned int * p = ((unsigned int *) addr) + (offset >> 5);
  208. unsigned int result = offset & ~31UL;
  209. unsigned int tmp;
  210. if (offset >= size)
  211. return size;
  212. size -= result;
  213. offset &= 31UL;
  214. if (offset) {
  215. tmp = *p++;
  216. tmp |= ~0UL >> (32-offset);
  217. if (size < 32)
  218. goto found_first;
  219. if (tmp != ~0U)
  220. goto found_middle;
  221. size -= 32;
  222. result += 32;
  223. }
  224. while (size >= 32) {
  225. if ((tmp = *p++) != ~0U)
  226. goto found_middle;
  227. result += 32;
  228. size -= 32;
  229. }
  230. if (!size)
  231. return result;
  232. tmp = *p;
  233. found_first:
  234. tmp |= ~0UL << size;
  235. found_middle:
  236. return result + ffz(tmp);
  237. }
  238. #define _EXT2_HAVE_ASM_BITOPS_
  239. #ifdef __KERNEL__
  240. /*
  241. * test_and_{set,clear}_bit guarantee atomicity without
  242. * disabling interrupts.
  243. */
  244. #define ext2_set_bit(nr, addr) test_and_set_bit((nr) ^ 0x18, addr)
  245. #define ext2_clear_bit(nr, addr) test_and_clear_bit((nr) ^ 0x18, addr)
  246. #else
  247. static __inline__ int ext2_set_bit(int nr, void * addr)
  248. {
  249. int mask;
  250. unsigned char *ADDR = (unsigned char *) addr;
  251. int oldbit;
  252. ADDR += nr >> 3;
  253. mask = 1 << (nr & 0x07);
  254. oldbit = (*ADDR & mask) ? 1 : 0;
  255. *ADDR |= mask;
  256. return oldbit;
  257. }
  258. static __inline__ int ext2_clear_bit(int nr, void * addr)
  259. {
  260. int mask;
  261. unsigned char *ADDR = (unsigned char *) addr;
  262. int oldbit;
  263. ADDR += nr >> 3;
  264. mask = 1 << (nr & 0x07);
  265. oldbit = (*ADDR & mask) ? 1 : 0;
  266. *ADDR = *ADDR & ~mask;
  267. return oldbit;
  268. }
  269. #endif /* __KERNEL__ */
  270. static __inline__ int ext2_test_bit(int nr, __const__ void * addr)
  271. {
  272. __const__ unsigned char *ADDR = (__const__ unsigned char *) addr;
  273. return (ADDR[nr >> 3] >> (nr & 7)) & 1;
  274. }
  275. /*
  276. * This implementation of ext2_find_{first,next}_zero_bit was stolen from
  277. * Linus' asm-alpha/bitops.h and modified for a big-endian machine.
  278. */
  279. #define ext2_find_first_zero_bit(addr, size) \
  280. ext2_find_next_zero_bit((addr), (size), 0)
  281. static __inline__ unsigned long ext2_find_next_zero_bit(void *addr,
  282. unsigned long size, unsigned long offset)
  283. {
  284. unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
  285. unsigned int result = offset & ~31UL;
  286. unsigned int tmp;
  287. if (offset >= size)
  288. return size;
  289. size -= result;
  290. offset &= 31UL;
  291. if (offset) {
  292. tmp = cpu_to_le32p(p++);
  293. tmp |= ~0UL >> (32-offset);
  294. if (size < 32)
  295. goto found_first;
  296. if (tmp != ~0U)
  297. goto found_middle;
  298. size -= 32;
  299. result += 32;
  300. }
  301. while (size >= 32) {
  302. if ((tmp = cpu_to_le32p(p++)) != ~0U)
  303. goto found_middle;
  304. result += 32;
  305. size -= 32;
  306. }
  307. if (!size)
  308. return result;
  309. tmp = cpu_to_le32p(p);
  310. found_first:
  311. tmp |= ~0U << size;
  312. found_middle:
  313. return result + ffz(tmp);
  314. }
  315. /* Bitmap functions for the minix filesystem. */
  316. #define minix_test_and_set_bit(nr,addr) ext2_set_bit(nr,addr)
  317. #define minix_set_bit(nr,addr) ((void)ext2_set_bit(nr,addr))
  318. #define minix_test_and_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
  319. #define minix_test_bit(nr,addr) ext2_test_bit(nr,addr)
  320. #define minix_find_first_zero_bit(addr,size) ext2_find_first_zero_bit(addr,size)
  321. #endif /* _PPC_BITOPS_H */