2
0

bitops.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "qemu/osdep.h"
  14. #include "qemu/bitops.h"
  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 + BIT_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 >= 4*BITS_PER_LONG) {
  42. unsigned long d1, d2, d3;
  43. tmp = *p;
  44. d1 = *(p+1);
  45. d2 = *(p+2);
  46. d3 = *(p+3);
  47. if (tmp) {
  48. goto found_middle;
  49. }
  50. if (d1 | d2 | d3) {
  51. break;
  52. }
  53. p += 4;
  54. result += 4*BITS_PER_LONG;
  55. size -= 4*BITS_PER_LONG;
  56. }
  57. while (size >= BITS_PER_LONG) {
  58. if ((tmp = *(p++))) {
  59. goto found_middle;
  60. }
  61. result += BITS_PER_LONG;
  62. size -= BITS_PER_LONG;
  63. }
  64. if (!size) {
  65. return result;
  66. }
  67. tmp = *p;
  68. found_first:
  69. tmp &= (~0UL >> (BITS_PER_LONG - size));
  70. if (tmp == 0UL) { /* Are any bits set? */
  71. return result + size; /* Nope. */
  72. }
  73. found_middle:
  74. return result + ctzl(tmp);
  75. }
  76. /*
  77. * This implementation of find_{first,next}_zero_bit was stolen from
  78. * Linus' asm-alpha/bitops.h.
  79. */
  80. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  81. unsigned long offset)
  82. {
  83. const unsigned long *p = addr + BIT_WORD(offset);
  84. unsigned long result = offset & ~(BITS_PER_LONG-1);
  85. unsigned long tmp;
  86. if (offset >= size) {
  87. return size;
  88. }
  89. size -= result;
  90. offset %= BITS_PER_LONG;
  91. if (offset) {
  92. tmp = *(p++);
  93. tmp |= ~0UL >> (BITS_PER_LONG - offset);
  94. if (size < BITS_PER_LONG) {
  95. goto found_first;
  96. }
  97. if (~tmp) {
  98. goto found_middle;
  99. }
  100. size -= BITS_PER_LONG;
  101. result += BITS_PER_LONG;
  102. }
  103. while (size & ~(BITS_PER_LONG-1)) {
  104. if (~(tmp = *(p++))) {
  105. goto found_middle;
  106. }
  107. result += BITS_PER_LONG;
  108. size -= BITS_PER_LONG;
  109. }
  110. if (!size) {
  111. return result;
  112. }
  113. tmp = *p;
  114. found_first:
  115. tmp |= ~0UL << size;
  116. if (tmp == ~0UL) { /* Are any bits zero? */
  117. return result + size; /* Nope. */
  118. }
  119. found_middle:
  120. return result + ctzl(~tmp);
  121. }
  122. unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
  123. {
  124. unsigned long words;
  125. unsigned long tmp;
  126. /* Start at final word. */
  127. words = size / BITS_PER_LONG;
  128. /* Partial final word? */
  129. if (size & (BITS_PER_LONG-1)) {
  130. tmp = (addr[words] & (~0UL >> (BITS_PER_LONG
  131. - (size & (BITS_PER_LONG-1)))));
  132. if (tmp) {
  133. goto found;
  134. }
  135. }
  136. while (words) {
  137. tmp = addr[--words];
  138. if (tmp) {
  139. found:
  140. return words * BITS_PER_LONG + BITS_PER_LONG - 1 - clzl(tmp);
  141. }
  142. }
  143. /* Not found */
  144. return size;
  145. }