bitops_64.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef CONFIG_GENERIC_FIND_FIRST_BIT
  2. #include <linux/bitops.h>
  3. #undef find_first_zero_bit
  4. #undef find_first_bit
  5. static inline long
  6. __find_first_zero_bit(const unsigned long * addr, unsigned long size)
  7. {
  8. long d0, d1, d2;
  9. long res;
  10. /*
  11. * We must test the size in words, not in bits, because
  12. * otherwise incoming sizes in the range -63..-1 will not run
  13. * any scasq instructions, and then the flags used by the je
  14. * instruction will have whatever random value was in place
  15. * before. Nobody should call us like that, but
  16. * find_next_zero_bit() does when offset and size are at the
  17. * same word and it fails to find a zero itself.
  18. */
  19. size += 63;
  20. size >>= 6;
  21. if (!size)
  22. return 0;
  23. asm volatile(
  24. " repe; scasq\n"
  25. " je 1f\n"
  26. " xorq -8(%%rdi),%%rax\n"
  27. " subq $8,%%rdi\n"
  28. " bsfq %%rax,%%rdx\n"
  29. "1: subq %[addr],%%rdi\n"
  30. " shlq $3,%%rdi\n"
  31. " addq %%rdi,%%rdx"
  32. :"=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2)
  33. :"0" (0ULL), "1" (size), "2" (addr), "3" (-1ULL),
  34. [addr] "S" (addr) : "memory");
  35. /*
  36. * Any register would do for [addr] above, but GCC tends to
  37. * prefer rbx over rsi, even though rsi is readily available
  38. * and doesn't have to be saved.
  39. */
  40. return res;
  41. }
  42. /**
  43. * find_first_zero_bit - find the first zero bit in a memory region
  44. * @addr: The address to start the search at
  45. * @size: The maximum size to search
  46. *
  47. * Returns the bit-number of the first zero bit, not the number of the byte
  48. * containing a bit.
  49. */
  50. long find_first_zero_bit(const unsigned long * addr, unsigned long size)
  51. {
  52. return __find_first_zero_bit (addr, size);
  53. }
  54. static inline long
  55. __find_first_bit(const unsigned long * addr, unsigned long size)
  56. {
  57. long d0, d1;
  58. long res;
  59. /*
  60. * We must test the size in words, not in bits, because
  61. * otherwise incoming sizes in the range -63..-1 will not run
  62. * any scasq instructions, and then the flags used by the jz
  63. * instruction will have whatever random value was in place
  64. * before. Nobody should call us like that, but
  65. * find_next_bit() does when offset and size are at the same
  66. * word and it fails to find a one itself.
  67. */
  68. size += 63;
  69. size >>= 6;
  70. if (!size)
  71. return 0;
  72. asm volatile(
  73. " repe; scasq\n"
  74. " jz 1f\n"
  75. " subq $8,%%rdi\n"
  76. " bsfq (%%rdi),%%rax\n"
  77. "1: subq %[addr],%%rdi\n"
  78. " shlq $3,%%rdi\n"
  79. " addq %%rdi,%%rax"
  80. :"=a" (res), "=&c" (d0), "=&D" (d1)
  81. :"0" (0ULL), "1" (size), "2" (addr),
  82. [addr] "r" (addr) : "memory");
  83. return res;
  84. }
  85. /**
  86. * find_first_bit - find the first set bit in a memory region
  87. * @addr: The address to start the search at
  88. * @size: The maximum size to search
  89. *
  90. * Returns the bit-number of the first set bit, not the number of the byte
  91. * containing a bit.
  92. */
  93. long find_first_bit(const unsigned long * addr, unsigned long size)
  94. {
  95. return __find_first_bit(addr,size);
  96. }
  97. #include <linux/module.h>
  98. EXPORT_SYMBOL(find_first_bit);
  99. EXPORT_SYMBOL(find_first_zero_bit);
  100. #endif