Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5 | #include "src/bit-vector.h" |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/base/bits.h" |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8 | |
| 9 | namespace v8 { |
| 10 | namespace internal { |
| 11 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12 | #ifdef DEBUG |
| 13 | void BitVector::Print() { |
| 14 | bool first = true; |
| 15 | PrintF("{"); |
| 16 | for (int i = 0; i < length(); i++) { |
| 17 | if (Contains(i)) { |
| 18 | if (!first) PrintF(","); |
| 19 | first = false; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 20 | PrintF("%d", i); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 21 | } |
| 22 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 23 | PrintF("}\n"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 24 | } |
| 25 | #endif |
| 26 | |
| 27 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 28 | void BitVector::Iterator::Advance() { |
| 29 | current_++; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 30 | uintptr_t val = current_value_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 31 | while (val == 0) { |
| 32 | current_index_++; |
| 33 | if (Done()) return; |
| 34 | val = target_->data_[current_index_]; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 35 | current_ = current_index_ << kDataBitShift; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 36 | } |
| 37 | val = SkipZeroBytes(val); |
| 38 | val = SkipZeroBits(val); |
| 39 | current_value_ = val >> 1; |
| 40 | } |
| 41 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 42 | |
| 43 | int BitVector::Count() const { |
| 44 | int count = 0; |
| 45 | for (int i = 0; i < data_length_; i++) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 46 | uintptr_t data = data_[i]; |
| 47 | if (sizeof(data) == 8) { |
| 48 | count += base::bits::CountPopulation64(data); |
| 49 | } else { |
| 50 | count += base::bits::CountPopulation32(static_cast<uint32_t>(data)); |
| 51 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 52 | } |
| 53 | return count; |
| 54 | } |
| 55 | |
| 56 | } // namespace internal |
| 57 | } // namespace v8 |