Skip to content

Commit

Permalink
hashlib: hash_eat -> hash_into
Browse files Browse the repository at this point in the history
  • Loading branch information
widlarizer committed Dec 18, 2024
1 parent 0a525f3 commit b9b9515
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 95 deletions.
10 changes: 5 additions & 5 deletions docs/source/yosys_internals/hashing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ Making a type hashable
Let's first take a look at the external interface on a simplified level.
Generally, to get the hash for ``T obj``, you would call the utility function
``run_hash<T>(const T& obj)``, corresponding to ``hash_top_ops<T>::hash(obj)``,
the default implementation of which is ``hash_ops<T>::hash_eat(Hasher(), obj)``.
the default implementation of which is ``hash_ops<T>::hash_into(Hasher(), obj)``.
``Hasher`` is the class actually implementing the hash function, hiding its
initialized internal state, and passing it out on ``hash_t yield()`` with
perhaps some finalization steps.

``hash_ops<T>`` is the star of the show. By default it pulls the ``Hasher h``
through a ``Hasher T::hash_eat(Hasher h)`` method. That's the method you have to
through a ``Hasher T::hash_into(Hasher h)`` method. That's the method you have to
implement to make a record (class or struct) type easily hashable with Yosys
hashlib associative data structures.

Expand All @@ -113,10 +113,10 @@ treats pointers the same as integers, so it doesn't dereference pointers. Since
many RTLIL data structures like ``RTLIL::Wire`` carry their own unique index
``Hasher::hash_t hashidx_;``, there are specializations for ``hash_ops<Wire*>``
and others in ``kernel/hashlib.h`` that actually dereference the pointers and
call ``hash_eat`` on the instances pointed to.
call ``hash_into`` on the instances pointed to.

``hash_ops<T>`` is also specialized for simple compound types like
``std::pair<U>`` by calling hash_eat in sequence on its members. For flexible
``std::pair<U>`` by calling hash_into in sequence on its members. For flexible
size containers like ``std::vector<U>`` the size of the container is hashed
first. That is also how implementing hashing for a custom record data type
should be - unless there is strong reason to do otherwise, call ``h.eat(m)`` on
Expand All @@ -143,7 +143,7 @@ based on the existance and value of `YS_HASHING_VERSION`.
return mkhash(a, b);
}
#elif YS_HASHING_VERSION == 1
Hasher T::hash_eat(Hasher h) const {
Hasher T::hash_into(Hasher h) const {
h.eat(a);
h.eat(b);
return h;
Expand Down
2 changes: 1 addition & 1 deletion frontends/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ namespace AST
{
// for dict<> and pool<>
unsigned int hashidx_;
Hasher hash_eat(Hasher h) const { h.eat(hashidx_); return h; }
Hasher hash_into(Hasher h) const { h.eat(hashidx_); return h; }

// this nodes type
AstNodeType type;
Expand Down
2 changes: 1 addition & 1 deletion kernel/bitpattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct BitPatternPool
return false;
return bitdata == other.bitdata;
}
Hasher hash_eat(Hasher h) const {
Hasher hash_into(Hasher h) const {
if (!cached_hash)
cached_hash = run_hash(bitdata);
h.eat(cached_hash);
Expand Down
4 changes: 2 additions & 2 deletions kernel/cellaigs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool AigNode::operator==(const AigNode &other) const
return true;
}

Hasher AigNode::hash_eat(Hasher h) const
Hasher AigNode::hash_into(Hasher h) const
{
h.eat(portname);
h.eat(portbit);
Expand All @@ -54,7 +54,7 @@ bool Aig::operator==(const Aig &other) const
return name == other.name;
}

Hasher Aig::hash_eat(Hasher h) const
Hasher Aig::hash_into(Hasher h) const
{
h.eat(name);
return h;
Expand Down
4 changes: 2 additions & 2 deletions kernel/cellaigs.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct AigNode

AigNode();
bool operator==(const AigNode &other) const;
Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;
};

struct Aig
Expand All @@ -44,7 +44,7 @@ struct Aig
Aig(Cell *cell);

bool operator==(const Aig &other) const;
Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;
};

YOSYS_NAMESPACE_END
Expand Down
48 changes: 24 additions & 24 deletions kernel/drivertools.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct DriveBitWire
return offset < other.offset;
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;


operator SigBit() const
Expand Down Expand Up @@ -105,7 +105,7 @@ struct DriveBitPort
return offset < other.offset;
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;

};

Expand All @@ -129,7 +129,7 @@ struct DriveBitMarker
return offset < other.offset;
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;

};

Expand Down Expand Up @@ -164,7 +164,7 @@ struct DriveBitMultiple
return multiple_ == other.multiple_;
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;
};

struct DriveBit
Expand Down Expand Up @@ -352,7 +352,7 @@ struct DriveBit
return *this;
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;

bool operator==(const DriveBit &other) const
{
Expand Down Expand Up @@ -473,7 +473,7 @@ struct DriveChunkWire
return offset < other.offset;
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;

explicit operator SigChunk() const
{
Expand Down Expand Up @@ -531,7 +531,7 @@ struct DriveChunkPort
return offset < other.offset;
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;
};


Expand Down Expand Up @@ -572,7 +572,7 @@ struct DriveChunkMarker
return offset < other.offset;
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;
};

struct DriveChunkMultiple
Expand Down Expand Up @@ -612,7 +612,7 @@ struct DriveChunkMultiple
return false; // TODO implement, canonicalize order
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;
};

struct DriveChunk
Expand Down Expand Up @@ -863,7 +863,7 @@ struct DriveChunk
bool try_append(DriveBit const &bit);
bool try_append(DriveChunk const &chunk);

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;

bool operator==(const DriveChunk &other) const
{
Expand Down Expand Up @@ -1073,7 +1073,7 @@ struct DriveSpec
that->hash_ |= (that->hash_ == 0);
}

Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;

bool operator==(DriveSpec const &other) const {
updhash();
Expand Down Expand Up @@ -1112,7 +1112,7 @@ struct DriverMap
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; }
Hasher hash_eat(Hasher h) const;
Hasher hash_into(Hasher h) const;
};
// Essentially a dict<DriveBitId, pool<DriveBitId>> but using less memory
// and fewer allocations
Expand Down Expand Up @@ -1258,35 +1258,35 @@ struct DriverMap
}
};

inline Hasher DriveBitWire::hash_eat(Hasher h) const
inline Hasher DriveBitWire::hash_into(Hasher h) const
{
h.eat(wire->name);
h.eat(offset);
return h;
}

inline Hasher DriveBitPort::hash_eat(Hasher h) const
inline Hasher DriveBitPort::hash_into(Hasher h) const
{
h.eat(cell->name);
h.eat(port);
h.eat(offset);
return h;
}

inline Hasher DriveBitMarker::hash_eat(Hasher h) const
inline Hasher DriveBitMarker::hash_into(Hasher h) const
{
h.eat(marker);
h.eat(offset);
return h;
}

inline Hasher DriveBitMultiple::hash_eat(Hasher h) const
inline Hasher DriveBitMultiple::hash_into(Hasher h) const
{
h.eat(multiple_);
return h;
}

inline Hasher DriveBit::hash_eat(Hasher h) const
inline Hasher DriveBit::hash_into(Hasher h) const
{
switch (type_) {
case DriveType::NONE:
Expand All @@ -1312,15 +1312,15 @@ inline Hasher DriveBit::hash_eat(Hasher h) const
return h;
}

inline Hasher DriveChunkWire::hash_eat(Hasher h) const
inline Hasher DriveChunkWire::hash_into(Hasher h) const
{
h.eat(wire->name);
h.eat(width);
h.eat(offset);
return h;
}

inline Hasher DriveChunkPort::hash_eat(Hasher h) const
inline Hasher DriveChunkPort::hash_into(Hasher h) const
{
h.eat(cell->name);
h.eat(port);
Expand All @@ -1329,22 +1329,22 @@ inline Hasher DriveChunkPort::hash_eat(Hasher h) const
return h;
}

inline Hasher DriveChunkMarker::hash_eat(Hasher h) const
inline Hasher DriveChunkMarker::hash_into(Hasher h) const
{
h.eat(marker);
h.eat(width);
h.eat(offset);
return h;
}

inline Hasher DriveChunkMultiple::hash_eat(Hasher h) const
inline Hasher DriveChunkMultiple::hash_into(Hasher h) const
{
h.eat(width_);
h.eat(multiple_);
return h;
}

inline Hasher DriveChunk::hash_eat(Hasher h) const
inline Hasher DriveChunk::hash_into(Hasher h) const
{
switch (type_) {
case DriveType::NONE:
Expand All @@ -1370,7 +1370,7 @@ inline Hasher DriveChunk::hash_eat(Hasher h) const
return h;
}

inline Hasher DriveSpec::hash_eat(Hasher h) const
inline Hasher DriveSpec::hash_into(Hasher h) const
{
if (hash_ == 0)
updhash();
Expand All @@ -1379,7 +1379,7 @@ inline Hasher DriveSpec::hash_eat(Hasher h) const
return h;
}

inline Hasher DriverMap::DriveBitId::hash_eat(Hasher h) const
inline Hasher DriverMap::DriveBitId::hash_into(Hasher h) const
{
h.eat(id);
return h;
Expand Down
4 changes: 2 additions & 2 deletions kernel/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ namespace Functional {
// returns the data width of a bitvector sort, errors out for other sorts
int data_width() const { return std::get<1>(_v).second; }
bool operator==(Sort const& other) const { return _v == other._v; }
Hasher hash_eat(Hasher h) const { h.eat(_v); return h; }
Hasher hash_into(Hasher h) const { h.eat(_v); return h; }
};
class IR;
class Factory;
Expand Down Expand Up @@ -225,7 +225,7 @@ namespace Functional {
const RTLIL::Const &as_const() const { return std::get<RTLIL::Const>(_extra); }
std::pair<IdString, IdString> as_idstring_pair() const { return std::get<std::pair<IdString, IdString>>(_extra); }
int as_int() const { return std::get<int>(_extra); }
Hasher hash_eat(Hasher h) const {
Hasher hash_into(Hasher h) const {
h.eat((unsigned int) _fn);
h.eat(_extra);
return h;
Expand Down
Loading

0 comments on commit b9b9515

Please sign in to comment.