bitops.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/ext2-atomic.h>
  23. #ifndef CONFIG_SMP
  24. #include <linux/irqflags.h>
  25. /*
  26. * clear_bit may not imply a memory barrier
  27. */
  28. #ifndef smp_mb__before_clear_bit
  29. #define smp_mb__before_clear_bit() smp_mb()
  30. #define smp_mb__after_clear_bit() smp_mb()
  31. #endif
  32. #include <asm-generic/bitops/atomic.h>
  33. #include <asm-generic/bitops/non-atomic.h>
  34. #else
  35. #include <asm/byteorder.h> /* swab32 */
  36. #include <linux/linkage.h>
  37. asmlinkage int __raw_bit_set_asm(volatile unsigned long *addr, int nr);
  38. asmlinkage int __raw_bit_clear_asm(volatile unsigned long *addr, int nr);
  39. asmlinkage int __raw_bit_toggle_asm(volatile unsigned long *addr, int nr);
  40. asmlinkage int __raw_bit_test_set_asm(volatile unsigned long *addr, int nr);
  41. asmlinkage int __raw_bit_test_clear_asm(volatile unsigned long *addr, int nr);
  42. asmlinkage int __raw_bit_test_toggle_asm(volatile unsigned long *addr, int nr);
  43. asmlinkage int __raw_bit_test_asm(const volatile unsigned long *addr, int nr);
  44. static inline void set_bit(int nr, volatile unsigned long *addr)
  45. {
  46. volatile unsigned long *a = addr + (nr >> 5);
  47. __raw_bit_set_asm(a, nr & 0x1f);
  48. }
  49. static inline void clear_bit(int nr, volatile unsigned long *addr)
  50. {
  51. volatile unsigned long *a = addr + (nr >> 5);
  52. __raw_bit_clear_asm(a, nr & 0x1f);
  53. }
  54. static inline void change_bit(int nr, volatile unsigned long *addr)
  55. {
  56. volatile unsigned long *a = addr + (nr >> 5);
  57. __raw_bit_toggle_asm(a, nr & 0x1f);
  58. }
  59. static inline int test_bit(int nr, const volatile unsigned long *addr)
  60. {
  61. volatile const unsigned long *a = addr + (nr >> 5);
  62. return __raw_bit_test_asm(a, nr & 0x1f) != 0;
  63. }
  64. static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
  65. {
  66. volatile unsigned long *a = addr + (nr >> 5);
  67. return __raw_bit_test_set_asm(a, nr & 0x1f);
  68. }
  69. static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
  70. {
  71. volatile unsigned long *a = addr + (nr >> 5);
  72. return __raw_bit_test_clear_asm(a, nr & 0x1f);
  73. }
  74. static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
  75. {
  76. volatile unsigned long *a = addr + (nr >> 5);
  77. return __raw_bit_test_toggle_asm(a, nr & 0x1f);
  78. }
  79. /*
  80. * clear_bit() doesn't provide any barrier for the compiler.
  81. */
  82. #define smp_mb__before_clear_bit() barrier()
  83. #define smp_mb__after_clear_bit() barrier()
  84. #define test_bit __skip_test_bit
  85. #include <asm-generic/bitops/non-atomic.h>
  86. #undef test_bit
  87. #endif /* CONFIG_SMP */
  88. /* Needs to be after test_bit and friends */
  89. #include <asm-generic/bitops/le.h>
  90. /*
  91. * hweightN: returns the hamming weight (i.e. the number
  92. * of bits set) of a N-bit word
  93. */
  94. static inline unsigned int __arch_hweight32(unsigned int w)
  95. {
  96. unsigned int res;
  97. __asm__ ("%0.l = ONES %1;"
  98. "%0 = %0.l (Z);"
  99. : "=d" (res) : "d" (w));
  100. return res;
  101. }
  102. static inline unsigned int __arch_hweight64(__u64 w)
  103. {
  104. return __arch_hweight32((unsigned int)(w >> 32)) +
  105. __arch_hweight32((unsigned int)w);
  106. }
  107. static inline unsigned int __arch_hweight16(unsigned int w)
  108. {
  109. return __arch_hweight32(w & 0xffff);
  110. }
  111. static inline unsigned int __arch_hweight8(unsigned int w)
  112. {
  113. return __arch_hweight32(w & 0xff);
  114. }
  115. #endif /* _BLACKFIN_BITOPS_H */