Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bitvector.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Planned, auditors: [Sergei], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8#include <cassert>
9#include <cstdint>
10#include <cstring> // For memset (optional)
11#include <vector>
12
15
21class BitVector {
22 public:
23 BitVector(size_t num_bits)
24 : num_bits_(num_bits)
25 , data_((num_bits + 63) / 64, 0)
26 {}
27
28 BB_INLINE void set(size_t index, bool value) noexcept
29 {
31 const size_t word = index >> 6;
32 const size_t bit = index & 63;
33
34 // from bit twiddling hacks
35 // http://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching
36 const uint64_t mask = static_cast<uint64_t>(1) << bit;
37 const uint64_t f = -static_cast<uint64_t>(value);
38 const uint64_t limb = data_[word];
39 data_[word] = (limb & ~mask) | (f & mask);
40 }
41
42 BB_INLINE bool get(size_t index) const noexcept
43 {
45 const uint64_t word = index >> 6;
46 const uint64_t bit = index & 63;
47 return ((data_[static_cast<size_t>(word)] >> bit) & 1) == 1;
48 }
49
50 void clear()
51 {
52 // std::fill(data_.begin(), data_.end(), 0);
53 // or use raw memset for faster clearing
54 std::memset(data_.data(), 0, data_.size() * sizeof(uint64_t));
55 }
56
57 size_t size() const { return num_bits_; }
58
59 // Optional: access raw pointer for performance
60 uint64_t* raw_data() { return data_.data(); }
61 const uint64_t* raw_data() const { return data_.data(); }
62
63 private:
64 size_t num_bits_;
65 std::vector<uint64_t> data_;
66};
#define BB_ASSERT_DEBUG(expression,...)
Definition assert.hpp:55
Custom class to handle packed vectors of bits.
Definition bitvector.hpp:21
BB_INLINE bool get(size_t index) const noexcept
Definition bitvector.hpp:42
const uint64_t * raw_data() const
Definition bitvector.hpp:61
BB_INLINE void set(size_t index, bool value) noexcept
Definition bitvector.hpp:28
BitVector(size_t num_bits)
Definition bitvector.hpp:23
size_t num_bits_
Definition bitvector.hpp:64
void clear()
Definition bitvector.hpp:50
size_t size() const
Definition bitvector.hpp:57
uint64_t * raw_data()
Definition bitvector.hpp:60
std::vector< uint64_t > data_
Definition bitvector.hpp:65
#define BB_INLINE
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13