src/share/vm/utilities/bitMap.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/bitMap.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,553 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "memory/allocation.inline.hpp"
    1.30 +#include "utilities/bitMap.inline.hpp"
    1.31 +#include "utilities/copy.hpp"
    1.32 +#ifdef TARGET_OS_FAMILY_linux
    1.33 +# include "os_linux.inline.hpp"
    1.34 +#endif
    1.35 +#ifdef TARGET_OS_FAMILY_solaris
    1.36 +# include "os_solaris.inline.hpp"
    1.37 +#endif
    1.38 +#ifdef TARGET_OS_FAMILY_windows
    1.39 +# include "os_windows.inline.hpp"
    1.40 +#endif
    1.41 +#ifdef TARGET_OS_FAMILY_aix
    1.42 +# include "os_aix.inline.hpp"
    1.43 +#endif
    1.44 +#ifdef TARGET_OS_FAMILY_bsd
    1.45 +# include "os_bsd.inline.hpp"
    1.46 +#endif
    1.47 +
    1.48 +
    1.49 +BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :
    1.50 +  _map(map), _size(size_in_bits), _map_allocator(false)
    1.51 +{
    1.52 +  assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
    1.53 +  assert(size_in_bits >= 0, "just checking");
    1.54 +}
    1.55 +
    1.56 +
    1.57 +BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
    1.58 +  _map(NULL), _size(0), _map_allocator(false)
    1.59 +{
    1.60 +  assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
    1.61 +  resize(size_in_bits, in_resource_area);
    1.62 +}
    1.63 +
    1.64 +void BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
    1.65 +  assert(size_in_bits >= 0, "just checking");
    1.66 +  idx_t old_size_in_words = size_in_words();
    1.67 +  bm_word_t* old_map = map();
    1.68 +
    1.69 +  _size = size_in_bits;
    1.70 +  idx_t new_size_in_words = size_in_words();
    1.71 +  if (in_resource_area) {
    1.72 +    _map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words);
    1.73 +  } else {
    1.74 +    if (old_map != NULL) {
    1.75 +      _map_allocator.free();
    1.76 +    }
    1.77 +    _map = _map_allocator.allocate(new_size_in_words);
    1.78 +  }
    1.79 +  Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
    1.80 +                       MIN2(old_size_in_words, new_size_in_words));
    1.81 +  if (new_size_in_words > old_size_in_words) {
    1.82 +    clear_range_of_words(old_size_in_words, size_in_words());
    1.83 +  }
    1.84 +}
    1.85 +
    1.86 +void BitMap::set_range_within_word(idx_t beg, idx_t end) {
    1.87 +  // With a valid range (beg <= end), this test ensures that end != 0, as
    1.88 +  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
    1.89 +  if (beg != end) {
    1.90 +    bm_word_t mask = inverted_bit_mask_for_range(beg, end);
    1.91 +    *word_addr(beg) |= ~mask;
    1.92 +  }
    1.93 +}
    1.94 +
    1.95 +void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
    1.96 +  // With a valid range (beg <= end), this test ensures that end != 0, as
    1.97 +  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
    1.98 +  if (beg != end) {
    1.99 +    bm_word_t mask = inverted_bit_mask_for_range(beg, end);
   1.100 +    *word_addr(beg) &= mask;
   1.101 +  }
   1.102 +}
   1.103 +
   1.104 +void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
   1.105 +  assert(value == 0 || value == 1, "0 for clear, 1 for set");
   1.106 +  // With a valid range (beg <= end), this test ensures that end != 0, as
   1.107 +  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
   1.108 +  if (beg != end) {
   1.109 +    intptr_t* pw  = (intptr_t*)word_addr(beg);
   1.110 +    intptr_t  w   = *pw;
   1.111 +    intptr_t  mr  = (intptr_t)inverted_bit_mask_for_range(beg, end);
   1.112 +    intptr_t  nw  = value ? (w | ~mr) : (w & mr);
   1.113 +    while (true) {
   1.114 +      intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
   1.115 +      if (res == w) break;
   1.116 +      w  = res;
   1.117 +      nw = value ? (w | ~mr) : (w & mr);
   1.118 +    }
   1.119 +  }
   1.120 +}
   1.121 +
   1.122 +void BitMap::set_range(idx_t beg, idx_t end) {
   1.123 +  verify_range(beg, end);
   1.124 +
   1.125 +  idx_t beg_full_word = word_index_round_up(beg);
   1.126 +  idx_t end_full_word = word_index(end);
   1.127 +
   1.128 +  if (beg_full_word < end_full_word) {
   1.129 +    // The range includes at least one full word.
   1.130 +    set_range_within_word(beg, bit_index(beg_full_word));
   1.131 +    set_range_of_words(beg_full_word, end_full_word);
   1.132 +    set_range_within_word(bit_index(end_full_word), end);
   1.133 +  } else {
   1.134 +    // The range spans at most 2 partial words.
   1.135 +    idx_t boundary = MIN2(bit_index(beg_full_word), end);
   1.136 +    set_range_within_word(beg, boundary);
   1.137 +    set_range_within_word(boundary, end);
   1.138 +  }
   1.139 +}
   1.140 +
   1.141 +void BitMap::clear_range(idx_t beg, idx_t end) {
   1.142 +  verify_range(beg, end);
   1.143 +
   1.144 +  idx_t beg_full_word = word_index_round_up(beg);
   1.145 +  idx_t end_full_word = word_index(end);
   1.146 +
   1.147 +  if (beg_full_word < end_full_word) {
   1.148 +    // The range includes at least one full word.
   1.149 +    clear_range_within_word(beg, bit_index(beg_full_word));
   1.150 +    clear_range_of_words(beg_full_word, end_full_word);
   1.151 +    clear_range_within_word(bit_index(end_full_word), end);
   1.152 +  } else {
   1.153 +    // The range spans at most 2 partial words.
   1.154 +    idx_t boundary = MIN2(bit_index(beg_full_word), end);
   1.155 +    clear_range_within_word(beg, boundary);
   1.156 +    clear_range_within_word(boundary, end);
   1.157 +  }
   1.158 +}
   1.159 +
   1.160 +void BitMap::set_large_range(idx_t beg, idx_t end) {
   1.161 +  verify_range(beg, end);
   1.162 +
   1.163 +  idx_t beg_full_word = word_index_round_up(beg);
   1.164 +  idx_t end_full_word = word_index(end);
   1.165 +
   1.166 +  assert(end_full_word - beg_full_word >= 32,
   1.167 +         "the range must include at least 32 bytes");
   1.168 +
   1.169 +  // The range includes at least one full word.
   1.170 +  set_range_within_word(beg, bit_index(beg_full_word));
   1.171 +  set_large_range_of_words(beg_full_word, end_full_word);
   1.172 +  set_range_within_word(bit_index(end_full_word), end);
   1.173 +}
   1.174 +
   1.175 +void BitMap::clear_large_range(idx_t beg, idx_t end) {
   1.176 +  verify_range(beg, end);
   1.177 +
   1.178 +  idx_t beg_full_word = word_index_round_up(beg);
   1.179 +  idx_t end_full_word = word_index(end);
   1.180 +
   1.181 +  assert(end_full_word - beg_full_word >= 32,
   1.182 +         "the range must include at least 32 bytes");
   1.183 +
   1.184 +  // The range includes at least one full word.
   1.185 +  clear_range_within_word(beg, bit_index(beg_full_word));
   1.186 +  clear_large_range_of_words(beg_full_word, end_full_word);
   1.187 +  clear_range_within_word(bit_index(end_full_word), end);
   1.188 +}
   1.189 +
   1.190 +void BitMap::at_put(idx_t offset, bool value) {
   1.191 +  if (value) {
   1.192 +    set_bit(offset);
   1.193 +  } else {
   1.194 +    clear_bit(offset);
   1.195 +  }
   1.196 +}
   1.197 +
   1.198 +// Return true to indicate that this thread changed
   1.199 +// the bit, false to indicate that someone else did.
   1.200 +// In either case, the requested bit is in the
   1.201 +// requested state some time during the period that
   1.202 +// this thread is executing this call. More importantly,
   1.203 +// if no other thread is executing an action to
   1.204 +// change the requested bit to a state other than
   1.205 +// the one that this thread is trying to set it to,
   1.206 +// then the the bit is in the expected state
   1.207 +// at exit from this method. However, rather than
   1.208 +// make such a strong assertion here, based on
   1.209 +// assuming such constrained use (which though true
   1.210 +// today, could change in the future to service some
   1.211 +// funky parallel algorithm), we encourage callers
   1.212 +// to do such verification, as and when appropriate.
   1.213 +bool BitMap::par_at_put(idx_t bit, bool value) {
   1.214 +  return value ? par_set_bit(bit) : par_clear_bit(bit);
   1.215 +}
   1.216 +
   1.217 +void BitMap::at_put_grow(idx_t offset, bool value) {
   1.218 +  if (offset >= size()) {
   1.219 +    resize(2 * MAX2(size(), offset));
   1.220 +  }
   1.221 +  at_put(offset, value);
   1.222 +}
   1.223 +
   1.224 +void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
   1.225 +  if (value) {
   1.226 +    set_range(start_offset, end_offset);
   1.227 +  } else {
   1.228 +    clear_range(start_offset, end_offset);
   1.229 +  }
   1.230 +}
   1.231 +
   1.232 +void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
   1.233 +  verify_range(beg, end);
   1.234 +
   1.235 +  idx_t beg_full_word = word_index_round_up(beg);
   1.236 +  idx_t end_full_word = word_index(end);
   1.237 +
   1.238 +  if (beg_full_word < end_full_word) {
   1.239 +    // The range includes at least one full word.
   1.240 +    par_put_range_within_word(beg, bit_index(beg_full_word), value);
   1.241 +    if (value) {
   1.242 +      set_range_of_words(beg_full_word, end_full_word);
   1.243 +    } else {
   1.244 +      clear_range_of_words(beg_full_word, end_full_word);
   1.245 +    }
   1.246 +    par_put_range_within_word(bit_index(end_full_word), end, value);
   1.247 +  } else {
   1.248 +    // The range spans at most 2 partial words.
   1.249 +    idx_t boundary = MIN2(bit_index(beg_full_word), end);
   1.250 +    par_put_range_within_word(beg, boundary, value);
   1.251 +    par_put_range_within_word(boundary, end, value);
   1.252 +  }
   1.253 +
   1.254 +}
   1.255 +
   1.256 +void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
   1.257 +  if (value) {
   1.258 +    set_large_range(beg, end);
   1.259 +  } else {
   1.260 +    clear_large_range(beg, end);
   1.261 +  }
   1.262 +}
   1.263 +
   1.264 +void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
   1.265 +  verify_range(beg, end);
   1.266 +
   1.267 +  idx_t beg_full_word = word_index_round_up(beg);
   1.268 +  idx_t end_full_word = word_index(end);
   1.269 +
   1.270 +  assert(end_full_word - beg_full_word >= 32,
   1.271 +         "the range must include at least 32 bytes");
   1.272 +
   1.273 +  // The range includes at least one full word.
   1.274 +  par_put_range_within_word(beg, bit_index(beg_full_word), value);
   1.275 +  if (value) {
   1.276 +    set_large_range_of_words(beg_full_word, end_full_word);
   1.277 +  } else {
   1.278 +    clear_large_range_of_words(beg_full_word, end_full_word);
   1.279 +  }
   1.280 +  par_put_range_within_word(bit_index(end_full_word), end, value);
   1.281 +}
   1.282 +
   1.283 +bool BitMap::contains(const BitMap other) const {
   1.284 +  assert(size() == other.size(), "must have same size");
   1.285 +  bm_word_t* dest_map = map();
   1.286 +  bm_word_t* other_map = other.map();
   1.287 +  idx_t size = size_in_words();
   1.288 +  for (idx_t index = 0; index < size_in_words(); index++) {
   1.289 +    bm_word_t word_union = dest_map[index] | other_map[index];
   1.290 +    // If this has more bits set than dest_map[index], then other is not a
   1.291 +    // subset.
   1.292 +    if (word_union != dest_map[index]) return false;
   1.293 +  }
   1.294 +  return true;
   1.295 +}
   1.296 +
   1.297 +bool BitMap::intersects(const BitMap other) const {
   1.298 +  assert(size() == other.size(), "must have same size");
   1.299 +  bm_word_t* dest_map = map();
   1.300 +  bm_word_t* other_map = other.map();
   1.301 +  idx_t size = size_in_words();
   1.302 +  for (idx_t index = 0; index < size_in_words(); index++) {
   1.303 +    if ((dest_map[index] & other_map[index]) != 0) return true;
   1.304 +  }
   1.305 +  // Otherwise, no intersection.
   1.306 +  return false;
   1.307 +}
   1.308 +
   1.309 +void BitMap::set_union(BitMap other) {
   1.310 +  assert(size() == other.size(), "must have same size");
   1.311 +  bm_word_t* dest_map = map();
   1.312 +  bm_word_t* other_map = other.map();
   1.313 +  idx_t size = size_in_words();
   1.314 +  for (idx_t index = 0; index < size_in_words(); index++) {
   1.315 +    dest_map[index] = dest_map[index] | other_map[index];
   1.316 +  }
   1.317 +}
   1.318 +
   1.319 +
   1.320 +void BitMap::set_difference(BitMap other) {
   1.321 +  assert(size() == other.size(), "must have same size");
   1.322 +  bm_word_t* dest_map = map();
   1.323 +  bm_word_t* other_map = other.map();
   1.324 +  idx_t size = size_in_words();
   1.325 +  for (idx_t index = 0; index < size_in_words(); index++) {
   1.326 +    dest_map[index] = dest_map[index] & ~(other_map[index]);
   1.327 +  }
   1.328 +}
   1.329 +
   1.330 +
   1.331 +void BitMap::set_intersection(BitMap other) {
   1.332 +  assert(size() == other.size(), "must have same size");
   1.333 +  bm_word_t* dest_map = map();
   1.334 +  bm_word_t* other_map = other.map();
   1.335 +  idx_t size = size_in_words();
   1.336 +  for (idx_t index = 0; index < size; index++) {
   1.337 +    dest_map[index]  = dest_map[index] & other_map[index];
   1.338 +  }
   1.339 +}
   1.340 +
   1.341 +
   1.342 +void BitMap::set_intersection_at_offset(BitMap other, idx_t offset) {
   1.343 +  assert(other.size() >= offset, "offset not in range");
   1.344 +  assert(other.size() - offset >= size(), "other not large enough");
   1.345 +  // XXX Ideally, we would remove this restriction.
   1.346 +  guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0,
   1.347 +            "Only handle aligned cases so far.");
   1.348 +  bm_word_t* dest_map = map();
   1.349 +  bm_word_t* other_map = other.map();
   1.350 +  idx_t offset_word_ind = word_index(offset);
   1.351 +  idx_t size = size_in_words();
   1.352 +  for (idx_t index = 0; index < size; index++) {
   1.353 +    dest_map[index] = dest_map[index] & other_map[offset_word_ind + index];
   1.354 +  }
   1.355 +}
   1.356 +
   1.357 +bool BitMap::set_union_with_result(BitMap other) {
   1.358 +  assert(size() == other.size(), "must have same size");
   1.359 +  bool changed = false;
   1.360 +  bm_word_t* dest_map = map();
   1.361 +  bm_word_t* other_map = other.map();
   1.362 +  idx_t size = size_in_words();
   1.363 +  for (idx_t index = 0; index < size; index++) {
   1.364 +    idx_t temp = map(index) | other_map[index];
   1.365 +    changed = changed || (temp != map(index));
   1.366 +    map()[index] = temp;
   1.367 +  }
   1.368 +  return changed;
   1.369 +}
   1.370 +
   1.371 +
   1.372 +bool BitMap::set_difference_with_result(BitMap other) {
   1.373 +  assert(size() == other.size(), "must have same size");
   1.374 +  bool changed = false;
   1.375 +  bm_word_t* dest_map = map();
   1.376 +  bm_word_t* other_map = other.map();
   1.377 +  idx_t size = size_in_words();
   1.378 +  for (idx_t index = 0; index < size; index++) {
   1.379 +    bm_word_t temp = dest_map[index] & ~(other_map[index]);
   1.380 +    changed = changed || (temp != dest_map[index]);
   1.381 +    dest_map[index] = temp;
   1.382 +  }
   1.383 +  return changed;
   1.384 +}
   1.385 +
   1.386 +
   1.387 +bool BitMap::set_intersection_with_result(BitMap other) {
   1.388 +  assert(size() == other.size(), "must have same size");
   1.389 +  bool changed = false;
   1.390 +  bm_word_t* dest_map = map();
   1.391 +  bm_word_t* other_map = other.map();
   1.392 +  idx_t size = size_in_words();
   1.393 +  for (idx_t index = 0; index < size; index++) {
   1.394 +    bm_word_t orig = dest_map[index];
   1.395 +    bm_word_t temp = orig & other_map[index];
   1.396 +    changed = changed || (temp != orig);
   1.397 +    dest_map[index]  = temp;
   1.398 +  }
   1.399 +  return changed;
   1.400 +}
   1.401 +
   1.402 +
   1.403 +void BitMap::set_from(BitMap other) {
   1.404 +  assert(size() == other.size(), "must have same size");
   1.405 +  bm_word_t* dest_map = map();
   1.406 +  bm_word_t* other_map = other.map();
   1.407 +  idx_t size = size_in_words();
   1.408 +  for (idx_t index = 0; index < size; index++) {
   1.409 +    dest_map[index] = other_map[index];
   1.410 +  }
   1.411 +}
   1.412 +
   1.413 +
   1.414 +bool BitMap::is_same(BitMap other) {
   1.415 +  assert(size() == other.size(), "must have same size");
   1.416 +  bm_word_t* dest_map = map();
   1.417 +  bm_word_t* other_map = other.map();
   1.418 +  idx_t size = size_in_words();
   1.419 +  for (idx_t index = 0; index < size; index++) {
   1.420 +    if (dest_map[index] != other_map[index]) return false;
   1.421 +  }
   1.422 +  return true;
   1.423 +}
   1.424 +
   1.425 +bool BitMap::is_full() const {
   1.426 +  bm_word_t* word = map();
   1.427 +  idx_t rest = size();
   1.428 +  for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
   1.429 +    if (*word != (bm_word_t) AllBits) return false;
   1.430 +    word++;
   1.431 +  }
   1.432 +  return rest == 0 || (*word | ~right_n_bits((int)rest)) == (bm_word_t) AllBits;
   1.433 +}
   1.434 +
   1.435 +
   1.436 +bool BitMap::is_empty() const {
   1.437 +  bm_word_t* word = map();
   1.438 +  idx_t rest = size();
   1.439 +  for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
   1.440 +    if (*word != (bm_word_t) NoBits) return false;
   1.441 +    word++;
   1.442 +  }
   1.443 +  return rest == 0 || (*word & right_n_bits((int)rest)) == (bm_word_t) NoBits;
   1.444 +}
   1.445 +
   1.446 +void BitMap::clear_large() {
   1.447 +  clear_large_range_of_words(0, size_in_words());
   1.448 +}
   1.449 +
   1.450 +// Note that if the closure itself modifies the bitmap
   1.451 +// then modifications in and to the left of the _bit_ being
   1.452 +// currently sampled will not be seen. Note also that the
   1.453 +// interval [leftOffset, rightOffset) is right open.
   1.454 +bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
   1.455 +  verify_range(leftOffset, rightOffset);
   1.456 +
   1.457 +  idx_t startIndex = word_index(leftOffset);
   1.458 +  idx_t endIndex   = MIN2(word_index(rightOffset) + 1, size_in_words());
   1.459 +  for (idx_t index = startIndex, offset = leftOffset;
   1.460 +       offset < rightOffset && index < endIndex;
   1.461 +       offset = (++index) << LogBitsPerWord) {
   1.462 +    idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
   1.463 +    for (; offset < rightOffset && rest != (bm_word_t)NoBits; offset++) {
   1.464 +      if (rest & 1) {
   1.465 +        if (!blk->do_bit(offset)) return false;
   1.466 +        //  resample at each closure application
   1.467 +        // (see, for instance, CMS bug 4525989)
   1.468 +        rest = map(index) >> (offset & (BitsPerWord -1));
   1.469 +      }
   1.470 +      rest = rest >> 1;
   1.471 +    }
   1.472 +  }
   1.473 +  return true;
   1.474 +}
   1.475 +
   1.476 +BitMap::idx_t* BitMap::_pop_count_table = NULL;
   1.477 +
   1.478 +void BitMap::init_pop_count_table() {
   1.479 +  if (_pop_count_table == NULL) {
   1.480 +    BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal);
   1.481 +    for (uint i = 0; i < 256; i++) {
   1.482 +      table[i] = num_set_bits(i);
   1.483 +    }
   1.484 +
   1.485 +    intptr_t res = Atomic::cmpxchg_ptr((intptr_t)  table,
   1.486 +                                       (intptr_t*) &_pop_count_table,
   1.487 +                                       (intptr_t)  NULL_WORD);
   1.488 +    if (res != NULL_WORD) {
   1.489 +      guarantee( _pop_count_table == (void*) res, "invariant" );
   1.490 +      FREE_C_HEAP_ARRAY(bm_word_t, table, mtInternal);
   1.491 +    }
   1.492 +  }
   1.493 +}
   1.494 +
   1.495 +BitMap::idx_t BitMap::num_set_bits(bm_word_t w) {
   1.496 +  idx_t bits = 0;
   1.497 +
   1.498 +  while (w != 0) {
   1.499 +    while ((w & 1) == 0) {
   1.500 +      w >>= 1;
   1.501 +    }
   1.502 +    bits++;
   1.503 +    w >>= 1;
   1.504 +  }
   1.505 +  return bits;
   1.506 +}
   1.507 +
   1.508 +BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) {
   1.509 +  assert(_pop_count_table != NULL, "precondition");
   1.510 +  return _pop_count_table[c];
   1.511 +}
   1.512 +
   1.513 +BitMap::idx_t BitMap::count_one_bits() const {
   1.514 +  init_pop_count_table(); // If necessary.
   1.515 +  idx_t sum = 0;
   1.516 +  typedef unsigned char uchar;
   1.517 +  for (idx_t i = 0; i < size_in_words(); i++) {
   1.518 +    bm_word_t w = map()[i];
   1.519 +    for (size_t j = 0; j < sizeof(bm_word_t); j++) {
   1.520 +      sum += num_set_bits_from_table(uchar(w & 255));
   1.521 +      w >>= 8;
   1.522 +    }
   1.523 +  }
   1.524 +  return sum;
   1.525 +}
   1.526 +
   1.527 +void BitMap::print_on_error(outputStream* st, const char* prefix) const {
   1.528 +  st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")",
   1.529 +      prefix, p2i(map()), p2i((char*)map() + (size() >> LogBitsPerByte)));
   1.530 +}
   1.531 +
   1.532 +#ifndef PRODUCT
   1.533 +
   1.534 +void BitMap::print_on(outputStream* st) const {
   1.535 +  tty->print("Bitmap(" SIZE_FORMAT "):", size());
   1.536 +  for (idx_t index = 0; index < size(); index++) {
   1.537 +    tty->print("%c", at(index) ? '1' : '0');
   1.538 +  }
   1.539 +  tty->cr();
   1.540 +}
   1.541 +
   1.542 +#endif
   1.543 +
   1.544 +
   1.545 +BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
   1.546 +  : _bits_per_slot(bits_per_slot)
   1.547 +  , _map(map, size_in_slots * bits_per_slot)
   1.548 +{
   1.549 +}
   1.550 +
   1.551 +
   1.552 +BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
   1.553 +  : _bits_per_slot(bits_per_slot)
   1.554 +  , _map(size_in_slots * bits_per_slot)
   1.555 +{
   1.556 +}

mercurial