3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-06-10 16:13:26 +00:00

hashlib: redo interface for flexibility

This commit is contained in:
Emil J. Tywoniak 2024-10-01 15:12:03 +02:00
parent 7a362f1f74
commit d071489ab1
35 changed files with 542 additions and 386 deletions

View file

@ -74,10 +74,8 @@ struct DriveBitWire
return offset < other.offset;
}
unsigned int hash() const
{
return mkhash_add(wire->name.hash(), offset);
}
Hasher hash_acc(Hasher h) const;
operator SigBit() const
{
@ -107,10 +105,8 @@ struct DriveBitPort
return offset < other.offset;
}
unsigned int hash() const
{
return mkhash_add(mkhash(cell->name.hash(), port.hash()), offset);
}
Hasher hash_acc(Hasher h) const;
};
@ -133,10 +129,7 @@ struct DriveBitMarker
return offset < other.offset;
}
unsigned int hash() const
{
return mkhash_add(marker, offset);
}
Hasher hash_acc(Hasher h) const;
};
@ -171,10 +164,7 @@ public:
return multiple_ == other.multiple_;
}
unsigned int hash() const
{
return multiple_.hash();
}
Hasher hash_acc(Hasher h) const;
};
struct DriveBit
@ -362,35 +352,7 @@ public:
return *this;
}
unsigned int hash() const
{
unsigned int inner = 0;
switch (type_)
{
case DriveType::NONE:
inner = 0;
break;
case DriveType::CONSTANT:
inner = constant_;
break;
case DriveType::WIRE:
inner = wire_.hash();
break;
case DriveType::PORT:
inner = port_.hash();
break;
case DriveType::MARKER:
inner = marker_.hash();
break;
case DriveType::MULTIPLE:
inner = multiple_.hash();
break;
default:
log_abort();
break;
}
return mkhash((unsigned int)type_, inner);
}
Hasher hash_acc(Hasher h) const;
bool operator==(const DriveBit &other) const
{
@ -511,10 +473,7 @@ struct DriveChunkWire
return offset < other.offset;
}
unsigned int hash() const
{
return mkhash_add(mkhash(wire->name.hash(), width), offset);
}
Hasher hash_acc(Hasher h) const;
explicit operator SigChunk() const
{
@ -572,10 +531,7 @@ struct DriveChunkPort
return offset < other.offset;
}
unsigned int hash() const
{
return mkhash_add(mkhash(mkhash(cell->name.hash(), port.hash()), width), offset);
}
Hasher hash_acc(Hasher h) const;
};
@ -616,10 +572,7 @@ struct DriveChunkMarker
return offset < other.offset;
}
unsigned int hash() const
{
return mkhash_add(mkhash(marker, width), offset);
}
Hasher hash_acc(Hasher h) const;
};
struct DriveChunkMultiple
@ -659,10 +612,7 @@ public:
return false; // TODO implement, canonicalize order
}
unsigned int hash() const
{
return mkhash(width_, multiple_.hash());
}
Hasher hash_acc(Hasher h) const;
};
struct DriveChunk
@ -913,6 +863,7 @@ public:
bool try_append(DriveBit const &bit);
bool try_append(DriveChunk const &chunk);
<<<<<<< HEAD
unsigned int hash() const
{
unsigned int inner = 0;
@ -942,6 +893,9 @@ public:
}
return mkhash((unsigned int)type_, inner);
}
=======
Hasher hash_acc(Hasher h) const;
>>>>>>> 898d04260 (hashlib: redo interface for flexibility)
bool operator==(const DriveChunk &other) const
{
@ -1144,17 +1098,19 @@ public:
DriveSpec &operator=(DriveBitMarker const &bit) { return *this = DriveBit(bit); }
DriveSpec &operator=(DriveBitMultiple const &bit) { return *this = DriveBit(bit); }
unsigned int hash() const {
if (hash_ != 0) return hash_;
void updhash() const {
DriveSpec *that = (DriveSpec*)this;
pack();
hash_ = hash_ops<std::vector<DriveChunk>>().hash(chunks_);
hash_ |= (hash_ == 0);
return hash_;
that->hash_ = run_hash(chunks_);
that->hash_ |= (that->hash_ == 0);
}
Hasher hash_acc(Hasher h) const;
bool operator==(DriveSpec const &other) const {
if (size() != other.size() || hash() != other.hash())
updhash();
other.updhash();
if (size() != other.size() || hash_ != other.hash_)
return false;
return chunks() == other.chunks();
}
@ -1187,7 +1143,8 @@ private:
bool operator==(const DriveBitId &other) const { return id == other.id; }
bool operator!=(const DriveBitId &other) const { return id != other.id; }
bool operator<(const DriveBitId &other) const { return id < other.id; }
unsigned int hash() const { return id; }
// unsigned int hash() const { return id; }
Hasher hash_acc(Hasher h) const;
};
// Essentially a dict<DriveBitId, pool<DriveBitId>> but using less memory
// and fewer allocations
@ -1333,6 +1290,133 @@ private:
}
};
inline Hasher DriveBitWire::hash_acc(Hasher h) const
{
h.acc(wire->name);
h.acc(offset);
return h;
}
inline Hasher DriveBitPort::hash_acc(Hasher h) const
{
h.acc(cell->name);
h.acc(port);
h.acc(offset);
return h;
}
inline Hasher DriveBitMarker::hash_acc(Hasher h) const
{
h.acc(marker);
h.acc(offset);
return h;
}
inline Hasher DriveBitMultiple::hash_acc(Hasher h) const
{
h.acc(multiple_);
return h;
}
inline Hasher DriveBit::hash_acc(Hasher h) const
{
switch (type_) {
case DriveType::NONE:
h.acc(0);
break;
case DriveType::CONSTANT:
h.acc(constant_);
break;
case DriveType::WIRE:
h.acc(wire_);
break;
case DriveType::PORT:
h.acc(port_);
break;
case DriveType::MARKER:
h.acc(marker_);
break;
case DriveType::MULTIPLE:
h.acc(multiple_);
break;
}
h.acc(type_);
return h;
}
inline Hasher DriveChunkWire::hash_acc(Hasher h) const
{
h.acc(wire->name);
h.acc(width);
h.acc(offset);
return h;
}
inline Hasher DriveChunkPort::hash_acc(Hasher h) const
{
h.acc(cell->name);
h.acc(port);
h.acc(width);
h.acc(offset);
return h;
}
inline Hasher DriveChunkMarker::hash_acc(Hasher h) const
{
h.acc(marker);
h.acc(width);
h.acc(offset);
return h;
}
inline Hasher DriveChunkMultiple::hash_acc(Hasher h) const
{
h.acc(width_);
h.acc(multiple_);
return h;
}
inline Hasher DriveChunk::hash_acc(Hasher h) const
{
switch (type_) {
case DriveType::NONE:
h.acc(0);
break;
case DriveType::CONSTANT:
h.acc(constant_);
break;
case DriveType::WIRE:
h.acc(wire_);
break;
case DriveType::PORT:
h.acc(port_);
break;
case DriveType::MARKER:
h.acc(marker_);
break;
case DriveType::MULTIPLE:
h.acc(multiple_);
break;
}
h.acc(type_);
return h;
}
inline Hasher DriveSpec::hash_acc(Hasher h) const
{
if (hash_ == 0)
updhash();
h.acc(hash_);
return h;
}
inline Hasher DriverMap::DriveBitId::hash_acc(Hasher h) const
{
h.acc(id);
return h;
}
YOSYS_NAMESPACE_END
#endif