bitops.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include <asm/barrier.h>
  21. /*
  22. * set bit
  23. */
  24. #define __set_bit(nr, addr) \
  25. ({ \
  26. volatile unsigned char *_a = (unsigned char *)(addr); \
  27. const unsigned shift = (nr) & 7; \
  28. _a += (nr) >> 3; \
  29. \
  30. asm volatile("bset %2,(%1) # set_bit reg" \
  31. : "=m"(*_a) \
  32. : "a"(_a), "d"(1 << shift), "m"(*_a) \
  33. : "memory", "cc"); \
  34. })
  35. #define set_bit(nr, addr) __set_bit((nr), (addr))
  36. /*
  37. * clear bit
  38. */
  39. #define ___clear_bit(nr, addr) \
  40. ({ \
  41. volatile unsigned char *_a = (unsigned char *)(addr); \
  42. const unsigned shift = (nr) & 7; \
  43. _a += (nr) >> 3; \
  44. \
  45. asm volatile("bclr %2,(%1) # clear_bit reg" \
  46. : "=m"(*_a) \
  47. : "a"(_a), "d"(1 << shift), "m"(*_a) \
  48. : "memory", "cc"); \
  49. })
  50. #define clear_bit(nr, addr) ___clear_bit((nr), (addr))
  51. static inline void __clear_bit(unsigned long nr, volatile void *addr)
  52. {
  53. unsigned int *a = (unsigned int *) addr;
  54. int mask;
  55. a += nr >> 5;
  56. mask = 1 << (nr & 0x1f);
  57. *a &= ~mask;
  58. }
  59. /*
  60. * test bit
  61. */
  62. static inline int test_bit(unsigned long nr, const volatile void *addr)
  63. {
  64. return 1UL & (((const volatile unsigned int *) addr)[nr >> 5] >> (nr & 31));
  65. }
  66. /*
  67. * change bit
  68. */
  69. static inline void __change_bit(unsigned long nr, volatile void *addr)
  70. {
  71. int mask;
  72. unsigned int *a = (unsigned int *) addr;
  73. a += nr >> 5;
  74. mask = 1 << (nr & 0x1f);
  75. *a ^= mask;
  76. }
  77. extern void change_bit(unsigned long nr, volatile void *addr);
  78. /*
  79. * test and set bit
  80. */
  81. #define __test_and_set_bit(nr,addr) \
  82. ({ \
  83. volatile unsigned char *_a = (unsigned char *)(addr); \
  84. const unsigned shift = (nr) & 7; \
  85. unsigned epsw; \
  86. _a += (nr) >> 3; \
  87. \
  88. asm volatile("bset %3,(%2) # test_set_bit reg\n" \
  89. "mov epsw,%1" \
  90. : "=m"(*_a), "=d"(epsw) \
  91. : "a"(_a), "d"(1 << shift), "m"(*_a) \
  92. : "memory", "cc"); \
  93. \
  94. !(epsw & EPSW_FLAG_Z); \
  95. })
  96. #define test_and_set_bit(nr, addr) __test_and_set_bit((nr), (addr))
  97. /*
  98. * test and clear bit
  99. */
  100. #define __test_and_clear_bit(nr, addr) \
  101. ({ \
  102. volatile unsigned char *_a = (unsigned char *)(addr); \
  103. const unsigned shift = (nr) & 7; \
  104. unsigned epsw; \
  105. _a += (nr) >> 3; \
  106. \
  107. asm volatile("bclr %3,(%2) # test_clear_bit reg\n" \
  108. "mov epsw,%1" \
  109. : "=m"(*_a), "=d"(epsw) \
  110. : "a"(_a), "d"(1 << shift), "m"(*_a) \
  111. : "memory", "cc"); \
  112. \
  113. !(epsw & EPSW_FLAG_Z); \
  114. })
  115. #define test_and_clear_bit(nr, addr) __test_and_clear_bit((nr), (addr))
  116. /*
  117. * test and change bit
  118. */
  119. static inline int __test_and_change_bit(unsigned long nr, volatile void *addr)
  120. {
  121. int mask, retval;
  122. unsigned int *a = (unsigned int *)addr;
  123. a += nr >> 5;
  124. mask = 1 << (nr & 0x1f);
  125. retval = (mask & *a) != 0;
  126. *a ^= mask;
  127. return retval;
  128. }
  129. extern int test_and_change_bit(unsigned long nr, volatile void *addr);
  130. #include <asm-generic/bitops/lock.h>
  131. #ifdef __KERNEL__
  132. /**
  133. * __ffs - find first bit set
  134. * @x: the word to search
  135. *
  136. * - return 31..0 to indicate bit 31..0 most least significant bit set
  137. * - if no bits are set in x, the result is undefined
  138. */
  139. static inline __attribute__((const))
  140. unsigned long __ffs(unsigned long x)
  141. {
  142. int bit;
  143. asm("bsch %2,%0" : "=r"(bit) : "0"(0), "r"(x & -x) : "cc");
  144. return bit;
  145. }
  146. /*
  147. * special slimline version of fls() for calculating ilog2_u32()
  148. * - note: no protection against n == 0
  149. */
  150. static inline __attribute__((const))
  151. int __ilog2_u32(u32 n)
  152. {
  153. int bit;
  154. asm("bsch %2,%0" : "=r"(bit) : "0"(0), "r"(n) : "cc");
  155. return bit;
  156. }
  157. /**
  158. * fls - find last bit set
  159. * @x: the word to search
  160. *
  161. * This is defined the same way as ffs:
  162. * - return 32..1 to indicate bit 31..0 most significant bit set
  163. * - return 0 to indicate no bits set
  164. */
  165. static inline __attribute__((const))
  166. int fls(int x)
  167. {
  168. return (x != 0) ? __ilog2_u32(x) + 1 : 0;
  169. }
  170. /**
  171. * __fls - find last (most-significant) set bit in a long word
  172. * @word: the word to search
  173. *
  174. * Undefined if no set bit exists, so code should check against 0 first.
  175. */
  176. static inline unsigned long __fls(unsigned long word)
  177. {
  178. return __ilog2_u32(word);
  179. }
  180. /**
  181. * ffs - find first bit set
  182. * @x: the word to search
  183. *
  184. * - return 32..1 to indicate bit 31..0 most least significant bit set
  185. * - return 0 to indicate no bits set
  186. */
  187. static inline __attribute__((const))
  188. int ffs(int x)
  189. {
  190. /* Note: (x & -x) gives us a mask that is the least significant
  191. * (rightmost) 1-bit of the value in x.
  192. */
  193. return fls(x & -x);
  194. }
  195. #include <asm-generic/bitops/ffz.h>
  196. #include <asm-generic/bitops/fls64.h>
  197. #include <asm-generic/bitops/find.h>
  198. #include <asm-generic/bitops/sched.h>
  199. #include <asm-generic/bitops/hweight.h>
  200. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  201. #include <asm-generic/bitops/le.h>
  202. #endif /* __KERNEL__ */
  203. #endif /* __ASM_BITOPS_H */