bitops_64.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef _X86_64_BITOPS_H
  2. #define _X86_64_BITOPS_H
  3. /*
  4. * Copyright 1992, Linus Torvalds.
  5. */
  6. extern long find_first_zero_bit(const unsigned long *addr, unsigned long size);
  7. extern long find_next_zero_bit(const unsigned long *addr, long size, long offset);
  8. extern long find_first_bit(const unsigned long *addr, unsigned long size);
  9. extern long find_next_bit(const unsigned long *addr, long size, long offset);
  10. /* return index of first bet set in val or max when no bit is set */
  11. static inline long __scanbit(unsigned long val, unsigned long max)
  12. {
  13. asm("bsfq %1,%0 ; cmovz %2,%0" : "=&r" (val) : "r" (val), "r" (max));
  14. return val;
  15. }
  16. #define find_next_bit(addr,size,off) \
  17. ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
  18. ((off) + (__scanbit((*(unsigned long *)addr) >> (off),(size)-(off)))) : \
  19. find_next_bit(addr,size,off)))
  20. #define find_next_zero_bit(addr,size,off) \
  21. ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
  22. ((off)+(__scanbit(~(((*(unsigned long *)addr)) >> (off)),(size)-(off)))) : \
  23. find_next_zero_bit(addr,size,off)))
  24. #define find_first_bit(addr, size) \
  25. ((__builtin_constant_p((size)) && (size) <= BITS_PER_LONG \
  26. ? (__scanbit(*(unsigned long *)(addr), (size))) \
  27. : find_first_bit((addr), (size))))
  28. #define find_first_zero_bit(addr, size) \
  29. ((__builtin_constant_p((size)) && (size) <= BITS_PER_LONG \
  30. ? (__scanbit(~*(unsigned long *)(addr), (size))) \
  31. : find_first_zero_bit((addr), (size))))
  32. static inline void set_bit_string(unsigned long *bitmap, unsigned long i,
  33. int len)
  34. {
  35. unsigned long end = i + len;
  36. while (i < end) {
  37. __set_bit(i, bitmap);
  38. i++;
  39. }
  40. }
  41. /**
  42. * ffz - find first zero in word.
  43. * @word: The word to search
  44. *
  45. * Undefined if no zero exists, so code should check against ~0UL first.
  46. */
  47. static inline unsigned long ffz(unsigned long word)
  48. {
  49. __asm__("bsfq %1,%0"
  50. :"=r" (word)
  51. :"r" (~word));
  52. return word;
  53. }
  54. /**
  55. * __ffs - find first bit in word.
  56. * @word: The word to search
  57. *
  58. * Undefined if no bit exists, so code should check against 0 first.
  59. */
  60. static inline unsigned long __ffs(unsigned long word)
  61. {
  62. __asm__("bsfq %1,%0"
  63. :"=r" (word)
  64. :"rm" (word));
  65. return word;
  66. }
  67. /*
  68. * __fls: find last bit set.
  69. * @word: The word to search
  70. *
  71. * Undefined if no zero exists, so code should check against ~0UL first.
  72. */
  73. static inline unsigned long __fls(unsigned long word)
  74. {
  75. __asm__("bsrq %1,%0"
  76. :"=r" (word)
  77. :"rm" (word));
  78. return word;
  79. }
  80. #ifdef __KERNEL__
  81. #include <asm-generic/bitops/sched.h>
  82. /**
  83. * ffs - find first bit set
  84. * @x: the word to search
  85. *
  86. * This is defined the same way as
  87. * the libc and compiler builtin ffs routines, therefore
  88. * differs in spirit from the above ffz (man ffs).
  89. */
  90. static inline int ffs(int x)
  91. {
  92. int r;
  93. __asm__("bsfl %1,%0\n\t"
  94. "cmovzl %2,%0"
  95. : "=r" (r) : "rm" (x), "r" (-1));
  96. return r+1;
  97. }
  98. /**
  99. * fls64 - find last bit set in 64 bit word
  100. * @x: the word to search
  101. *
  102. * This is defined the same way as fls.
  103. */
  104. static inline int fls64(__u64 x)
  105. {
  106. if (x == 0)
  107. return 0;
  108. return __fls(x) + 1;
  109. }
  110. /**
  111. * fls - find last bit set
  112. * @x: the word to search
  113. *
  114. * This is defined the same way as ffs.
  115. */
  116. static inline int fls(int x)
  117. {
  118. int r;
  119. __asm__("bsrl %1,%0\n\t"
  120. "cmovzl %2,%0"
  121. : "=&r" (r) : "rm" (x), "rm" (-1));
  122. return r+1;
  123. }
  124. #define ARCH_HAS_FAST_MULTIPLIER 1
  125. #include <asm-generic/bitops/hweight.h>
  126. #endif /* __KERNEL__ */
  127. #ifdef __KERNEL__
  128. #include <asm-generic/bitops/ext2-non-atomic.h>
  129. #define ext2_set_bit_atomic(lock, nr, addr) \
  130. test_and_set_bit((nr), (unsigned long *)(addr))
  131. #define ext2_clear_bit_atomic(lock, nr, addr) \
  132. test_and_clear_bit((nr), (unsigned long *)(addr))
  133. #include <asm-generic/bitops/minix.h>
  134. #endif /* __KERNEL__ */
  135. #endif /* _X86_64_BITOPS_H */