find_bit.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* bit search implementation
  2. *
  3. * Copied from lib/find_bit.c to tools/lib/find_bit.c
  4. *
  5. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  6. * Written by David Howells (dhowells@redhat.com)
  7. *
  8. * Copyright (C) 2008 IBM Corporation
  9. * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
  10. * (Inspired by David Howell's find_next_bit implementation)
  11. *
  12. * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
  13. * size and improve performance, 2015.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/bitops.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/kernel.h>
  23. #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
  24. !defined(find_next_and_bit)
  25. /*
  26. * This is a common helper function for find_next_bit, find_next_zero_bit, and
  27. * find_next_and_bit. The differences are:
  28. * - The "invert" argument, which is XORed with each fetched word before
  29. * searching it for one bits.
  30. * - The optional "addr2", which is anded with "addr1" if present.
  31. */
  32. static inline unsigned long _find_next_bit(const unsigned long *addr1,
  33. const unsigned long *addr2, unsigned long nbits,
  34. unsigned long start, unsigned long invert)
  35. {
  36. unsigned long tmp;
  37. if (unlikely(start >= nbits))
  38. return nbits;
  39. tmp = addr1[start / BITS_PER_LONG];
  40. if (addr2)
  41. tmp &= addr2[start / BITS_PER_LONG];
  42. tmp ^= invert;
  43. /* Handle 1st word. */
  44. tmp &= BITMAP_FIRST_WORD_MASK(start);
  45. start = round_down(start, BITS_PER_LONG);
  46. while (!tmp) {
  47. start += BITS_PER_LONG;
  48. if (start >= nbits)
  49. return nbits;
  50. tmp = addr1[start / BITS_PER_LONG];
  51. if (addr2)
  52. tmp &= addr2[start / BITS_PER_LONG];
  53. tmp ^= invert;
  54. }
  55. return min(start + __ffs(tmp), nbits);
  56. }
  57. #endif
  58. #ifndef find_next_bit
  59. /*
  60. * Find the next set bit in a memory region.
  61. */
  62. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  63. unsigned long offset)
  64. {
  65. return _find_next_bit(addr, NULL, size, offset, 0UL);
  66. }
  67. #endif
  68. #ifndef find_first_bit
  69. /*
  70. * Find the first set bit in a memory region.
  71. */
  72. unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
  73. {
  74. unsigned long idx;
  75. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  76. if (addr[idx])
  77. return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
  78. }
  79. return size;
  80. }
  81. #endif
  82. #ifndef find_first_zero_bit
  83. /*
  84. * Find the first cleared bit in a memory region.
  85. */
  86. unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
  87. {
  88. unsigned long idx;
  89. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  90. if (addr[idx] != ~0UL)
  91. return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
  92. }
  93. return size;
  94. }
  95. #endif
  96. #ifndef find_next_zero_bit
  97. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  98. unsigned long offset)
  99. {
  100. return _find_next_bit(addr, NULL, size, offset, ~0UL);
  101. }
  102. #endif
  103. #ifndef find_next_and_bit
  104. unsigned long find_next_and_bit(const unsigned long *addr1,
  105. const unsigned long *addr2, unsigned long size,
  106. unsigned long offset)
  107. {
  108. return _find_next_bit(addr1, addr2, size, offset, 0UL);
  109. }
  110. #endif