bitops_32.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef _I386_BITOPS_H
  2. #define _I386_BITOPS_H
  3. /*
  4. * Copyright 1992, Linus Torvalds.
  5. */
  6. /**
  7. * find_first_zero_bit - find the first zero bit in a memory region
  8. * @addr: The address to start the search at
  9. * @size: The maximum size to search
  10. *
  11. * Returns the bit number of the first zero bit, not the number of the byte
  12. * containing a bit.
  13. */
  14. static inline int find_first_zero_bit(const unsigned long *addr, unsigned size)
  15. {
  16. int d0, d1, d2;
  17. int res;
  18. if (!size)
  19. return 0;
  20. /* This looks at memory. Mark it volatile to tell gcc not to move it around */
  21. __asm__ __volatile__(
  22. "movl $-1,%%eax\n\t"
  23. "xorl %%edx,%%edx\n\t"
  24. "repe; scasl\n\t"
  25. "je 1f\n\t"
  26. "xorl -4(%%edi),%%eax\n\t"
  27. "subl $4,%%edi\n\t"
  28. "bsfl %%eax,%%edx\n"
  29. "1:\tsubl %%ebx,%%edi\n\t"
  30. "shll $3,%%edi\n\t"
  31. "addl %%edi,%%edx"
  32. :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
  33. :"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory");
  34. return res;
  35. }
  36. /**
  37. * find_next_zero_bit - find the first zero bit in a memory region
  38. * @addr: The address to base the search on
  39. * @offset: The bit number to start searching at
  40. * @size: The maximum size to search
  41. */
  42. int find_next_zero_bit(const unsigned long *addr, int size, int offset);
  43. /**
  44. * __ffs - find first bit in word.
  45. * @word: The word to search
  46. *
  47. * Undefined if no bit exists, so code should check against 0 first.
  48. */
  49. static inline unsigned long __ffs(unsigned long word)
  50. {
  51. __asm__("bsfl %1,%0"
  52. :"=r" (word)
  53. :"rm" (word));
  54. return word;
  55. }
  56. /**
  57. * find_first_bit - find the first set bit in a memory region
  58. * @addr: The address to start the search at
  59. * @size: The maximum size to search
  60. *
  61. * Returns the bit number of the first set bit, not the number of the byte
  62. * containing a bit.
  63. */
  64. static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)
  65. {
  66. unsigned x = 0;
  67. while (x < size) {
  68. unsigned long val = *addr++;
  69. if (val)
  70. return __ffs(val) + x;
  71. x += (sizeof(*addr)<<3);
  72. }
  73. return x;
  74. }
  75. /**
  76. * find_next_bit - find the first set bit in a memory region
  77. * @addr: The address to base the search on
  78. * @offset: The bit number to start searching at
  79. * @size: The maximum size to search
  80. */
  81. int find_next_bit(const unsigned long *addr, int size, int offset);
  82. /**
  83. * ffz - find first zero in word.
  84. * @word: The word to search
  85. *
  86. * Undefined if no zero exists, so code should check against ~0UL first.
  87. */
  88. static inline unsigned long ffz(unsigned long word)
  89. {
  90. __asm__("bsfl %1,%0"
  91. :"=r" (word)
  92. :"r" (~word));
  93. return word;
  94. }
  95. #ifdef __KERNEL__
  96. #include <asm-generic/bitops/sched.h>
  97. /**
  98. * ffs - find first bit set
  99. * @x: the word to search
  100. *
  101. * This is defined the same way as
  102. * the libc and compiler builtin ffs routines, therefore
  103. * differs in spirit from the above ffz() (man ffs).
  104. */
  105. static inline int ffs(int x)
  106. {
  107. int r;
  108. __asm__("bsfl %1,%0\n\t"
  109. "jnz 1f\n\t"
  110. "movl $-1,%0\n"
  111. "1:" : "=r" (r) : "rm" (x));
  112. return r+1;
  113. }
  114. /**
  115. * fls - find last bit set
  116. * @x: the word to search
  117. *
  118. * This is defined the same way as ffs().
  119. */
  120. static inline int fls(int x)
  121. {
  122. int r;
  123. __asm__("bsrl %1,%0\n\t"
  124. "jnz 1f\n\t"
  125. "movl $-1,%0\n"
  126. "1:" : "=r" (r) : "rm" (x));
  127. return r+1;
  128. }
  129. #include <asm-generic/bitops/hweight.h>
  130. #endif /* __KERNEL__ */
  131. #include <asm-generic/bitops/fls64.h>
  132. #ifdef __KERNEL__
  133. #include <asm-generic/bitops/ext2-non-atomic.h>
  134. #define ext2_set_bit_atomic(lock, nr, addr) \
  135. test_and_set_bit((nr), (unsigned long *)addr)
  136. #define ext2_clear_bit_atomic(lock, nr, addr) \
  137. test_and_clear_bit((nr), (unsigned long *)addr)
  138. #include <asm-generic/bitops/minix.h>
  139. #endif /* __KERNEL__ */
  140. #endif /* _I386_BITOPS_H */