find_bit.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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)
  24. /*
  25. * This is a common helper function for find_next_bit and
  26. * find_next_zero_bit. The difference is the "invert" argument, which
  27. * is XORed with each fetched word before searching it for one bits.
  28. */
  29. static unsigned long _find_next_bit(const unsigned long *addr,
  30. unsigned long nbits, unsigned long start, unsigned long invert)
  31. {
  32. unsigned long tmp;
  33. if (!nbits || start >= nbits)
  34. return nbits;
  35. tmp = addr[start / BITS_PER_LONG] ^ invert;
  36. /* Handle 1st word. */
  37. tmp &= BITMAP_FIRST_WORD_MASK(start);
  38. start = round_down(start, BITS_PER_LONG);
  39. while (!tmp) {
  40. start += BITS_PER_LONG;
  41. if (start >= nbits)
  42. return nbits;
  43. tmp = addr[start / BITS_PER_LONG] ^ invert;
  44. }
  45. return min(start + __ffs(tmp), nbits);
  46. }
  47. #endif
  48. #ifndef find_next_bit
  49. /*
  50. * Find the next set bit in a memory region.
  51. */
  52. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  53. unsigned long offset)
  54. {
  55. return _find_next_bit(addr, size, offset, 0UL);
  56. }
  57. #endif
  58. #ifndef find_first_bit
  59. /*
  60. * Find the first set bit in a memory region.
  61. */
  62. unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
  63. {
  64. unsigned long idx;
  65. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  66. if (addr[idx])
  67. return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
  68. }
  69. return size;
  70. }
  71. #endif
  72. #ifndef find_first_zero_bit
  73. /*
  74. * Find the first cleared bit in a memory region.
  75. */
  76. unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
  77. {
  78. unsigned long idx;
  79. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  80. if (addr[idx] != ~0UL)
  81. return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
  82. }
  83. return size;
  84. }
  85. #endif
  86. #ifndef find_next_zero_bit
  87. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  88. unsigned long offset)
  89. {
  90. return _find_next_bit(addr, size, offset, ~0UL);
  91. }
  92. #endif