Skip to content

Commit

Permalink
Move DriveSpec implementation to drivertools.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
RCoeurjoly committed May 23, 2024
1 parent 9dd2829 commit 589fc40
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 99 deletions.
181 changes: 181 additions & 0 deletions kernel/drivertools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,187 @@ void DriveSpec::compute_width()
width_ += chunk.size();
}


inline bool DriveSpec::packed() const
{
return bits_.empty();
}

DriveSpec::DriveSpec() {}

DriveSpec::DriveSpec(DriveChunk const &chunk)
{
*this = chunk;
}

DriveSpec::DriveSpec(DriveChunkWire const &chunk)
{
*this = chunk;
}

DriveSpec::DriveSpec(DriveChunkPort const &chunk)
{
*this = chunk;
}

DriveSpec::DriveSpec(DriveChunkMarker const &chunk)
{
*this = chunk;
}

DriveSpec::DriveSpec(DriveChunkMultiple const &chunk)
{
*this = chunk;
}

DriveSpec::DriveSpec(DriveBit const &bit)
{
*this = bit;
}

DriveSpec::DriveSpec(DriveBitWire const &bit)
{
*this = bit;
}

DriveSpec::DriveSpec(DriveBitPort const &bit)
{
*this = bit;
}

DriveSpec::DriveSpec(DriveBitMarker const &bit)
{
*this = bit;
}

DriveSpec::DriveSpec(DriveBitMultiple const &bit)
{
*this = bit;
}

DriveSpec::DriveSpec(std::vector<DriveChunk> const &chunks) : chunks_(chunks)
{
compute_width();
}

DriveSpec::DriveSpec(std::vector<DriveBit> const &bits)
{
for (auto const &bit : bits)
append(bit);
}

std::vector<DriveChunk> const &DriveSpec::chunks() const
{
pack();
return chunks_;
}

std::vector<DriveBit> const &DriveSpec::bits() const
{
unpack();
return bits_;
}

int DriveSpec::size() const
{
return width_;
}

DriveBit &DriveSpec::operator[](int index)
{
log_assert(index >= 0 && index < size());
unpack();
return bits_[index];
}

const DriveBit &DriveSpec::operator[](int index) const
{
log_assert(index >= 0 && index < size());
unpack();
return bits_[index];
}

void DriveSpec::clear()
{
chunks_.clear();
bits_.clear();
width_ = 0;
}

DriveSpec &DriveSpec::operator=(DriveChunk const &chunk)
{
chunks_.clear();
bits_.clear();
append(chunk);
return *this;
}

DriveSpec &DriveSpec::operator=(DriveChunkWire const &chunk)
{
return *this = DriveChunk(chunk);
}

DriveSpec &DriveSpec::operator=(DriveChunkPort const &chunk)
{
return *this = DriveChunk(chunk);
}

DriveSpec &DriveSpec::operator=(DriveChunkMarker const &chunk)
{
return *this = DriveChunk(chunk);
}

DriveSpec &DriveSpec::operator=(DriveChunkMultiple const &chunk)
{
return *this = DriveChunk(chunk);
}

DriveSpec &DriveSpec::operator=(DriveBit const &bit)
{
chunks_.clear();
bits_.clear();
append(bit);
return *this;
}

DriveSpec &DriveSpec::operator=(DriveBitWire const &bit)
{
return *this = DriveBit(bit);
}

DriveSpec &DriveSpec::operator=(DriveBitPort const &bit)
{
return *this = DriveBit(bit);
}

DriveSpec &DriveSpec::operator=(DriveBitMarker const &bit)
{
return *this = DriveBit(bit);
}

DriveSpec &DriveSpec::operator=(DriveBitMultiple const &bit)
{
return *this = DriveBit(bit);
}

unsigned int DriveSpec::hash() const
{
if (hash_ != 0)
return hash_;

pack();
hash_ = hash_ops<std::vector<DriveChunk>>().hash(chunks_);
hash_ |= (hash_ == 0);
return hash_;
}

bool DriveSpec::operator==(DriveSpec const &other) const
{
if (size() != other.size() || hash() != other.hash())
return false;
return chunks() == other.chunks();
}

void DriverMap::DriveBitGraph::add_edge(DriveBitId src, DriveBitId dst)
{
if (first_edges.emplace(src, dst).first->second == dst)
Expand Down
145 changes: 46 additions & 99 deletions kernel/drivertools.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,119 +360,66 @@ struct DriveChunk
struct DriveSpec
{
private:
int width_ = 0;
mutable std::vector<DriveChunk> chunks_;
mutable std::vector<DriveBit> bits_;
mutable unsigned int hash_ = 0;
public:

inline bool packed() const {
return bits_.empty();
}

DriveSpec() {}

DriveSpec(DriveChunk const &chunk) { *this = chunk; }
DriveSpec(DriveChunkWire const &chunk) { *this = chunk; }
DriveSpec(DriveChunkPort const &chunk) { *this = chunk; }
DriveSpec(DriveChunkMarker const &chunk) { *this = chunk; }
DriveSpec(DriveChunkMultiple const &chunk) { *this = chunk; }
int width_ = 0;
mutable std::vector<DriveChunk> chunks_;
mutable std::vector<DriveBit> bits_;
mutable unsigned int hash_ = 0;

DriveSpec(DriveBit const &bit) { *this = bit; }
DriveSpec(DriveBitWire const &bit) { *this = bit; }
DriveSpec(DriveBitPort const &bit) { *this = bit; }
DriveSpec(DriveBitMarker const &bit) { *this = bit; }
DriveSpec(DriveBitMultiple const &bit) { *this = bit; }

DriveSpec(std::vector<DriveChunk> const &chunks) : chunks_(chunks) { compute_width(); }

DriveSpec(std::vector<DriveBit> const &bits)
{
for (auto const &bit : bits)
append(bit);
}

std::vector<DriveChunk> const &chunks() const { pack(); return chunks_; }
std::vector<DriveBit> const &bits() const { unpack(); return bits_; }

int size() const { return width_; }

void append(DriveBit const &bit);

void append(DriveChunk const &chunk);

void pack() const;

void unpack() const;

DriveBit &operator[](int index)
{
log_assert(index >= 0 && index < size());
unpack();
return bits_[index];
}
public:
inline bool packed() const;

DriveSpec();
DriveSpec(DriveChunk const &chunk);
DriveSpec(DriveChunkWire const &chunk);
DriveSpec(DriveChunkPort const &chunk);
DriveSpec(DriveChunkMarker const &chunk);
DriveSpec(DriveChunkMultiple const &chunk);

DriveSpec(DriveBit const &bit);
DriveSpec(DriveBitWire const &bit);
DriveSpec(DriveBitPort const &bit);
DriveSpec(DriveBitMarker const &bit);
DriveSpec(DriveBitMultiple const &bit);

DriveSpec(std::vector<DriveChunk> const &chunks);
DriveSpec(std::vector<DriveBit> const &bits);

std::vector<DriveChunk> const &chunks() const;
std::vector<DriveBit> const &bits() const;

const DriveBit &operator[](int index) const
{
log_assert(index >= 0 && index < size());
unpack();
return bits_[index];
}
int size() const;

void clear()
{
chunks_.clear();
bits_.clear();
width_ = 0;
}
void append(DriveBit const &bit);
void append(DriveChunk const &chunk);

DriveSpec &operator=(DriveChunk const &chunk)
{
chunks_.clear();
bits_.clear();
append(chunk);
return *this;
}
void pack() const;
void unpack() const;

DriveSpec &operator=(DriveChunkWire const &chunk) { return *this = DriveChunk(chunk); }
DriveSpec &operator=(DriveChunkPort const &chunk) { return *this = DriveChunk(chunk); }
DriveSpec &operator=(DriveChunkMarker const &chunk) { return *this = DriveChunk(chunk); }
DriveSpec &operator=(DriveChunkMultiple const &chunk) { return *this = DriveChunk(chunk); }
DriveBit &operator[](int index);
const DriveBit &operator[](int index) const;

DriveSpec &operator=(DriveBit const &bit)
{
chunks_.clear();
bits_.clear();
append(bit);
return *this;
}
void clear();

DriveSpec &operator=(DriveBitWire const &bit) { return *this = DriveBit(bit); }
DriveSpec &operator=(DriveBitPort const &bit) { return *this = DriveBit(bit); }
DriveSpec &operator=(DriveBitMarker const &bit) { return *this = DriveBit(bit); }
DriveSpec &operator=(DriveBitMultiple const &bit) { return *this = DriveBit(bit); }
DriveSpec &operator=(DriveChunk const &chunk);
DriveSpec &operator=(DriveChunkWire const &chunk);
DriveSpec &operator=(DriveChunkPort const &chunk);
DriveSpec &operator=(DriveChunkMarker const &chunk);
DriveSpec &operator=(DriveChunkMultiple const &chunk);

unsigned int hash() const {
if (hash_ != 0) return hash_;
DriveSpec &operator=(DriveBit const &bit);
DriveSpec &operator=(DriveBitWire const &bit);
DriveSpec &operator=(DriveBitPort const &bit);
DriveSpec &operator=(DriveBitMarker const &bit);
DriveSpec &operator=(DriveBitMultiple const &bit);

pack();
hash_ = hash_ops<std::vector<DriveChunk>>().hash(chunks_);
hash_ |= (hash_ == 0);
return hash_;
}
unsigned int hash() const;

bool operator==(DriveSpec const &other) const {
if (size() != other.size() || hash() != other.hash())
return false;
return chunks() == other.chunks();
}
bool operator==(DriveSpec const &other) const;

private:
void compute_width();
void compute_width();
};



struct DriverMap
{
CellTypes celltypes;
Expand Down

0 comments on commit 589fc40

Please sign in to comment.