blob: 198b24273c2c2cf032cbf90e9d5bb998d540792b [file] [log] [blame]
Leon Clarke4515c472010-02-03 11:58:03 +00001// Copyright 2010 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Leon Clarke4515c472010-02-03 11:58:03 +00004
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005#include "src/bit-vector.h"
Leon Clarke4515c472010-02-03 11:58:03 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/base/bits.h"
8#include "src/scopes.h"
Leon Clarke4515c472010-02-03 11:58:03 +00009
10namespace v8 {
11namespace internal {
12
Steve Block6ded16b2010-05-10 14:33:55 +010013#ifdef DEBUG
14void BitVector::Print() {
15 bool first = true;
16 PrintF("{");
17 for (int i = 0; i < length(); i++) {
18 if (Contains(i)) {
19 if (!first) PrintF(",");
20 first = false;
Ben Murdochf87a2032010-10-22 12:50:53 +010021 PrintF("%d", i);
Steve Block6ded16b2010-05-10 14:33:55 +010022 }
23 }
24 PrintF("}");
25}
26#endif
27
28
Ben Murdochb0fe1622011-05-05 13:52:32 +010029void BitVector::Iterator::Advance() {
30 current_++;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040031 uintptr_t val = current_value_;
Ben Murdochb0fe1622011-05-05 13:52:32 +010032 while (val == 0) {
33 current_index_++;
34 if (Done()) return;
35 val = target_->data_[current_index_];
Emily Bernierd0a1eb72015-03-24 16:35:39 -040036 current_ = current_index_ << kDataBitShift;
Ben Murdochb0fe1622011-05-05 13:52:32 +010037 }
38 val = SkipZeroBytes(val);
39 val = SkipZeroBits(val);
40 current_value_ = val >> 1;
41}
42
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043
44int BitVector::Count() const {
45 int count = 0;
46 for (int i = 0; i < data_length_; i++) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040047 uintptr_t data = data_[i];
48 if (sizeof(data) == 8) {
49 count += base::bits::CountPopulation64(data);
50 } else {
51 count += base::bits::CountPopulation32(static_cast<uint32_t>(data));
52 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 }
54 return count;
55}
56
57} // namespace internal
58} // namespace v8