bitops.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2004-2009 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2 or later.
  5. */
  6. #ifndef _BLACKFIN_BITOPS_H
  7. #define _BLACKFIN_BITOPS_H
  8. #include <linux/compiler.h>
  9. #include <asm-generic/bitops/__ffs.h>
  10. #include <asm-generic/bitops/ffz.h>
  11. #include <asm-generic/bitops/fls.h>
  12. #include <asm-generic/bitops/__fls.h>
  13. #include <asm-generic/bitops/fls64.h>
  14. #include <asm-generic/bitops/find.h>
  15. #ifndef _LINUX_BITOPS_H
  16. #error only <linux/bitops.h> can be included directly
  17. #endif
  18. #include <asm-generic/bitops/sched.h>
  19. #include <asm-generic/bitops/ffs.h>
  20. #include <asm-generic/bitops/const_hweight.h>
  21. #include <asm-generic/bitops/lock.h>
  22. #include <asm-generic/bitops/le.h>
  23. #include <asm-generic/bitops/ext2-atomic.h>
  24. #ifndef CONFIG_SMP
  25. #include <linux/irqflags.h>
  26. /*
  27. * clear_bit may not imply a memory barrier
  28. */
  29. #ifndef smp_mb__before_clear_bit
  30. #define smp_mb__before_clear_bit() smp_mb()
  31. #define smp_mb__after_clear_bit() smp_mb()
  32. #endif
  33. #include <asm-generic/bitops/atomic.h>
  34. #include <asm-generic/bitops/non-atomic.h>
  35. #else
  36. #include <asm/byteorder.h> /* swab32 */
  37. #include <linux/linkage.h>
  38. asmlinkage int __raw_bit_set_asm(volatile unsigned long *addr, int nr);
  39. asmlinkage int __raw_bit_clear_asm(volatile unsigned long *addr, int nr);
  40. asmlinkage int __raw_bit_toggle_asm(volatile unsigned long *addr, int nr);
  41. asmlinkage int __raw_bit_test_set_asm(volatile unsigned long *addr, int nr);
  42. asmlinkage int __raw_bit_test_clear_asm(volatile unsigned long *addr, int nr);
  43. asmlinkage int __raw_bit_test_toggle_asm(volatile unsigned long *addr, int nr);
  44. asmlinkage int __raw_bit_test_asm(const volatile unsigned long *addr, int nr);
  45. static inline void set_bit(int nr, volatile unsigned long *addr)
  46. {
  47. volatile unsigned long *a = addr + (nr >> 5);
  48. __raw_bit_set_asm(a, nr & 0x1f);
  49. }
  50. static inline void clear_bit(int nr, volatile unsigned long *addr)
  51. {
  52. volatile unsigned long *a = addr + (nr >> 5);
  53. __raw_bit_clear_asm(a, nr & 0x1f);
  54. }
  55. static inline void change_bit(int nr, volatile unsigned long *addr)
  56. {
  57. volatile unsigned long *a = addr + (nr >> 5);
  58. __raw_bit_toggle_asm(a, nr & 0x1f);
  59. }
  60. static inline int test_bit(int nr, const volatile unsigned long *addr)
  61. {
  62. volatile const unsigned long *a = addr + (nr >> 5);
  63. return __raw_bit_test_asm(a, nr & 0x1f) != 0;
  64. }
  65. static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
  66. {
  67. volatile unsigned long *a = addr + (nr >> 5);
  68. return __raw_bit_test_set_asm(a, nr & 0x1f);
  69. }
  70. static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
  71. {
  72. volatile unsigned long *a = addr + (nr >> 5);
  73. return __raw_bit_test_clear_asm(a, nr & 0x1f);
  74. }
  75. static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
  76. {
  77. volatile unsigned long *a = addr + (nr >> 5);
  78. return __raw_bit_test_toggle_asm(a, nr & 0x1f);
  79. }
  80. /*
  81. * clear_bit() doesn't provide any barrier for the compiler.
  82. */
  83. #define smp_mb__before_clear_bit() barrier()
  84. #define smp_mb__after_clear_bit() barrier()
  85. #define test_bit __skip_test_bit
  86. #include <asm-generic/bitops/non-atomic.h>
  87. #undef test_bit
  88. #endif /* CONFIG_SMP */
  89. /*
  90. * hweightN: returns the hamming weight (i.e. the number
  91. * of bits set) of a N-bit word
  92. */
  93. static inline unsigned int __arch_hweight32(unsigned int w)
  94. {
  95. unsigned int res;
  96. __asm__ ("%0.l = ONES %1;"
  97. "%0 = %0.l (Z);"
  98. : "=d" (res) : "d" (w));
  99. return res;
  100. }
  101. static inline unsigned int __arch_hweight64(__u64 w)
  102. {
  103. return __arch_hweight32((unsigned int)(w >> 32)) +
  104. __arch_hweight32((unsigned int)w);
  105. }
  106. static inline unsigned int __arch_hweight16(unsigned int w)
  107. {
  108. return __arch_hweight32(w & 0xffff);
  109. }
  110. static inline unsigned int __arch_hweight8(unsigned int w)
  111. {
  112. return __arch_hweight32(w & 0xff);
  113. }
  114. #endif /* _BLACKFIN_BITOPS_H */