bitops.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  3. * Written by David Howells (dhowells@redhat.com)
  4. * Copyright (C) 2008 IBM Corporation
  5. * Written by Rusty Russell <rusty@rustcorp.com.au>
  6. * (Inspired by David Howell's find_next_bit implementation)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include "bitops.h"
  14. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  15. /*
  16. * Find the next set bit in a memory region.
  17. */
  18. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  19. unsigned long offset)
  20. {
  21. const unsigned long *p = addr + BITOP_WORD(offset);
  22. unsigned long result = offset & ~(BITS_PER_LONG-1);
  23. unsigned long tmp;
  24. if (offset >= size) {
  25. return size;
  26. }
  27. size -= result;
  28. offset %= BITS_PER_LONG;
  29. if (offset) {
  30. tmp = *(p++);
  31. tmp &= (~0UL << offset);
  32. if (size < BITS_PER_LONG) {
  33. goto found_first;
  34. }
  35. if (tmp) {
  36. goto found_middle;
  37. }
  38. size -= BITS_PER_LONG;
  39. result += BITS_PER_LONG;
  40. }
  41. while (size & ~(BITS_PER_LONG-1)) {
  42. if ((tmp = *(p++))) {
  43. goto found_middle;
  44. }
  45. result += BITS_PER_LONG;
  46. size -= BITS_PER_LONG;
  47. }
  48. if (!size) {
  49. return result;
  50. }
  51. tmp = *p;
  52. found_first:
  53. tmp &= (~0UL >> (BITS_PER_LONG - size));
  54. if (tmp == 0UL) { /* Are any bits set? */
  55. return result + size; /* Nope. */
  56. }
  57. found_middle:
  58. return result + bitops_ffsl(tmp);
  59. }
  60. /*
  61. * This implementation of find_{first,next}_zero_bit was stolen from
  62. * Linus' asm-alpha/bitops.h.
  63. */
  64. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  65. unsigned long offset)
  66. {
  67. const unsigned long *p = addr + BITOP_WORD(offset);
  68. unsigned long result = offset & ~(BITS_PER_LONG-1);
  69. unsigned long tmp;
  70. if (offset >= size) {
  71. return size;
  72. }
  73. size -= result;
  74. offset %= BITS_PER_LONG;
  75. if (offset) {
  76. tmp = *(p++);
  77. tmp |= ~0UL >> (BITS_PER_LONG - offset);
  78. if (size < BITS_PER_LONG) {
  79. goto found_first;
  80. }
  81. if (~tmp) {
  82. goto found_middle;
  83. }
  84. size -= BITS_PER_LONG;
  85. result += BITS_PER_LONG;
  86. }
  87. while (size & ~(BITS_PER_LONG-1)) {
  88. if (~(tmp = *(p++))) {
  89. goto found_middle;
  90. }
  91. result += BITS_PER_LONG;
  92. size -= BITS_PER_LONG;
  93. }
  94. if (!size) {
  95. return result;
  96. }
  97. tmp = *p;
  98. found_first:
  99. tmp |= ~0UL << size;
  100. if (tmp == ~0UL) { /* Are any bits zero? */
  101. return result + size; /* Nope. */
  102. }
  103. found_middle:
  104. return result + ffz(tmp);
  105. }
  106. unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
  107. {
  108. unsigned long words;
  109. unsigned long tmp;
  110. /* Start at final word. */
  111. words = size / BITS_PER_LONG;
  112. /* Partial final word? */
  113. if (size & (BITS_PER_LONG-1)) {
  114. tmp = (addr[words] & (~0UL >> (BITS_PER_LONG
  115. - (size & (BITS_PER_LONG-1)))));
  116. if (tmp) {
  117. goto found;
  118. }
  119. }
  120. while (words) {
  121. tmp = addr[--words];
  122. if (tmp) {
  123. found:
  124. return words * BITS_PER_LONG + bitops_flsl(tmp);
  125. }
  126. }
  127. /* Not found */
  128. return size;
  129. }