bitmap.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2013-2015 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. */
  20. #include <limits.h>
  21. #include <string.h>
  22. #include <kern/bitmap.h>
  23. #include <kern/bitmap_i.h>
  24. int
  25. bitmap_cmp(const unsigned long *a, const unsigned long *b, int nr_bits)
  26. {
  27. unsigned long last_a, last_b, mask;
  28. int n, rv;
  29. n = BITMAP_LONGS(nr_bits) - 1;
  30. if (n != 0) {
  31. rv = memcmp(a, b, n * sizeof(unsigned long));
  32. if (rv != 0) {
  33. return rv;
  34. }
  35. nr_bits -= n * LONG_BIT;
  36. }
  37. last_a = a[n];
  38. last_b = b[n];
  39. if (nr_bits != LONG_BIT) {
  40. mask = (1UL << nr_bits) - 1;
  41. last_a &= mask;
  42. last_b &= mask;
  43. }
  44. if (last_a == last_b) {
  45. return 0;
  46. } else if (last_a < last_b) {
  47. return -1;
  48. } else {
  49. return 1;
  50. }
  51. }
  52. static inline unsigned long
  53. bitmap_find_next_compute_complement(unsigned long word, int nr_bits)
  54. {
  55. if (nr_bits < LONG_BIT) {
  56. word |= (((unsigned long)-1) << nr_bits);
  57. }
  58. return ~word;
  59. }
  60. int
  61. bitmap_find_next_bit(const unsigned long *bm, int nr_bits, int bit,
  62. int complement)
  63. {
  64. const unsigned long *start, *end;
  65. unsigned long word;
  66. start = bm;
  67. end = bm + BITMAP_LONGS(nr_bits);
  68. if (bit >= LONG_BIT) {
  69. bitmap_lookup(&bm, &bit);
  70. nr_bits -= ((bm - start) * LONG_BIT);
  71. }
  72. word = *bm;
  73. if (complement) {
  74. word = bitmap_find_next_compute_complement(word, nr_bits);
  75. }
  76. if (bit < LONG_BIT) {
  77. word &= ~(bitmap_mask(bit) - 1);
  78. }
  79. for (;;) {
  80. bit = __builtin_ffsl(word);
  81. if (bit != 0) {
  82. return ((bm - start) * LONG_BIT) + bit - 1;
  83. }
  84. bm++;
  85. if (bm >= end) {
  86. return -1;
  87. }
  88. nr_bits -= LONG_BIT;
  89. word = *bm;
  90. if (complement) {
  91. word = bitmap_find_next_compute_complement(word, nr_bits);
  92. }
  93. }
  94. }