bitops.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* MN10300 bit operations
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. *
  11. * These have to be done with inline assembly: that way the bit-setting
  12. * is guaranteed to be atomic. All bit operations return 0 if the bit
  13. * was cleared before the operation and != 0 if it was not.
  14. *
  15. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  16. */
  17. #ifndef __ASM_BITOPS_H
  18. #define __ASM_BITOPS_H
  19. #include <asm/cpu-regs.h>
  20. #define smp_mb__before_clear_bit() barrier()
  21. #define smp_mb__after_clear_bit() barrier()
  22. /*
  23. * set bit
  24. */
  25. #define __set_bit(nr, addr) \
  26. ({ \
  27. volatile unsigned char *_a = (unsigned char *)(addr); \
  28. const unsigned shift = (nr) & 7; \
  29. _a += (nr) >> 3; \
  30. \
  31. asm volatile("bset %2,(%1) # set_bit reg" \
  32. : "=m"(*_a) \
  33. : "a"(_a), "d"(1 << shift), "m"(*_a) \
  34. : "memory", "cc"); \
  35. })
  36. #define set_bit(nr, addr) __set_bit((nr), (addr))
  37. /*
  38. * clear bit
  39. */
  40. #define ___clear_bit(nr, addr) \
  41. ({ \
  42. volatile unsigned char *_a = (unsigned char *)(addr); \
  43. const unsigned shift = (nr) & 7; \
  44. _a += (nr) >> 3; \
  45. \
  46. asm volatile("bclr %2,(%1) # clear_bit reg" \
  47. : "=m"(*_a) \
  48. : "a"(_a), "d"(1 << shift), "m"(*_a) \
  49. : "memory", "cc"); \
  50. })
  51. #define clear_bit(nr, addr) ___clear_bit((nr), (addr))
  52. static inline void __clear_bit(int nr, volatile void *addr)
  53. {
  54. unsigned int *a = (unsigned int *) addr;
  55. int mask;
  56. a += nr >> 5;
  57. mask = 1 << (nr & 0x1f);
  58. *a &= ~mask;
  59. }
  60. /*
  61. * test bit
  62. */
  63. static inline int test_bit(int nr, const volatile void *addr)
  64. {
  65. return 1UL & (((const unsigned int *) addr)[nr >> 5] >> (nr & 31));
  66. }
  67. /*
  68. * change bit
  69. */
  70. static inline void __change_bit(int nr, volatile void *addr)
  71. {
  72. int mask;
  73. unsigned int *a = (unsigned int *) addr;
  74. a += nr >> 5;
  75. mask = 1 << (nr & 0x1f);
  76. *a ^= mask;
  77. }
  78. extern void change_bit(int nr, volatile void *addr);
  79. /*
  80. * test and set bit
  81. */
  82. #define __test_and_set_bit(nr,addr) \
  83. ({ \
  84. volatile unsigned char *_a = (unsigned char *)(addr); \
  85. const unsigned shift = (nr) & 7; \
  86. unsigned epsw; \
  87. _a += (nr) >> 3; \
  88. \
  89. asm volatile("bset %3,(%2) # test_set_bit reg\n" \
  90. "mov epsw,%1" \
  91. : "=m"(*_a), "=d"(epsw) \
  92. : "a"(_a), "d"(1 << shift), "m"(*_a) \
  93. : "memory", "cc"); \
  94. \
  95. !(epsw & EPSW_FLAG_Z); \
  96. })
  97. #define test_and_set_bit(nr, addr) __test_and_set_bit((nr), (addr))
  98. /*
  99. * test and clear bit
  100. */
  101. #define __test_and_clear_bit(nr, addr) \
  102. ({ \
  103. volatile unsigned char *_a = (unsigned char *)(addr); \
  104. const unsigned shift = (nr) & 7; \
  105. unsigned epsw; \
  106. _a += (nr) >> 3; \
  107. \
  108. asm volatile("bclr %3,(%2) # test_clear_bit reg\n" \
  109. "mov epsw,%1" \
  110. : "=m"(*_a), "=d"(epsw) \
  111. : "a"(_a), "d"(1 << shift), "m"(*_a) \
  112. : "memory", "cc"); \
  113. \
  114. !(epsw & EPSW_FLAG_Z); \
  115. })
  116. #define test_and_clear_bit(nr, addr) __test_and_clear_bit((nr), (addr))
  117. /*
  118. * test and change bit
  119. */
  120. static inline int __test_and_change_bit(int nr, volatile void *addr)
  121. {
  122. int mask, retval;
  123. unsigned int *a = (unsigned int *)addr;
  124. a += nr >> 5;
  125. mask = 1 << (nr & 0x1f);
  126. retval = (mask & *a) != 0;
  127. *a ^= mask;
  128. return retval;
  129. }
  130. extern int test_and_change_bit(int nr, volatile void *addr);
  131. #include <asm-generic/bitops/lock.h>
  132. #ifdef __KERNEL__
  133. /**
  134. * __ffs - find first bit set
  135. * @x: the word to search
  136. *
  137. * - return 31..0 to indicate bit 31..0 most least significant bit set
  138. * - if no bits are set in x, the result is undefined
  139. */
  140. static inline __attribute__((const))
  141. unsigned long __ffs(unsigned long x)
  142. {
  143. int bit;
  144. asm("bsch %2,%0" : "=r"(bit) : "0"(0), "r"(x & -x));
  145. return bit;
  146. }
  147. /*
  148. * special slimline version of fls() for calculating ilog2_u32()
  149. * - note: no protection against n == 0
  150. */
  151. static inline __attribute__((const))
  152. int __ilog2_u32(u32 n)
  153. {
  154. int bit;
  155. asm("bsch %2,%0" : "=r"(bit) : "0"(0), "r"(n));
  156. return bit;
  157. }
  158. /**
  159. * fls - find last bit set
  160. * @x: the word to search
  161. *
  162. * This is defined the same way as ffs:
  163. * - return 32..1 to indicate bit 31..0 most significant bit set
  164. * - return 0 to indicate no bits set
  165. */
  166. static inline __attribute__((const))
  167. int fls(int x)
  168. {
  169. return (x != 0) ? __ilog2_u32(x) + 1 : 0;
  170. }
  171. /**
  172. * ffs - find first bit set
  173. * @x: the word to search
  174. *
  175. * - return 32..1 to indicate bit 31..0 most least significant bit set
  176. * - return 0 to indicate no bits set
  177. */
  178. static inline __attribute__((const))
  179. int ffs(int x)
  180. {
  181. /* Note: (x & -x) gives us a mask that is the least significant
  182. * (rightmost) 1-bit of the value in x.
  183. */
  184. return fls(x & -x);
  185. }
  186. #include <asm-generic/bitops/ffz.h>
  187. #include <asm-generic/bitops/fls64.h>
  188. #include <asm-generic/bitops/find.h>
  189. #include <asm-generic/bitops/sched.h>
  190. #include <asm-generic/bitops/hweight.h>
  191. #define ext2_set_bit_atomic(lock, nr, addr) \
  192. test_and_set_bit((nr) ^ 0x18, (addr))
  193. #define ext2_clear_bit_atomic(lock, nr, addr) \
  194. test_and_clear_bit((nr) ^ 0x18, (addr))
  195. #include <asm-generic/bitops/ext2-non-atomic.h>
  196. #include <asm-generic/bitops/minix-le.h>
  197. #endif /* __KERNEL__ */
  198. #endif /* __ASM_BITOPS_H */