bitops_64.c 2.8 KB

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