hashlib: declare YS_HASHING_VERSION = 1

This commit is contained in:
Emil J. Tywoniak 2024-11-19 20:01:41 +01:00
parent 1df8a3e64b
commit 0a525f38c2
2 changed files with 16 additions and 12 deletions

View File

@ -131,23 +131,25 @@ Previously, the interface to implement hashing on custom types was just
``unsigned int T::hash() const``. This meant hashes for members were computed ``unsigned int T::hash() const``. This meant hashes for members were computed
independently and then ad-hoc combined with the hash function with some xorshift independently and then ad-hoc combined with the hash function with some xorshift
operations thrown in to mix bits together somewhat. A plugin can stay compatible operations thrown in to mix bits together somewhat. A plugin can stay compatible
with both versions prior and after the break by implementing the aforementioned with both versions prior and after the break by implementing both interfaces
current interface and redirecting the legacy one: based on the existance and value of `YS_HASHING_VERSION`.
``void Hasher::eat(const T& t)`` hashes ``t`` into its internal state by also
redirecting to ``hash_ops<T>``
.. code-block:: cpp .. code-block:: cpp
:caption: Example hash compatibility wrapper :caption: Example hash compatibility wrapper
:name: hash_plugin_compat :name: hash_plugin_compat
inline unsigned int T::hash() const { #ifndef YS_HASHING_VERSION
Hasher h; unsigned int T::hash() const {
return (unsigned int)hash_eat(h).yield(); return mkhash(a, b);
} }
#elif YS_HASHING_VERSION == 1
Hasher T::hash_eat(Hasher h) const {
h.eat(a);
h.eat(b);
return h;
}
#else
#error "Unsupported hashing interface"
#endif
To get hashes for Yosys types, you can temporarily use the templated deprecated
``mkhash`` function until the majority of your plugin's users switch to a newer
version and live with the warnings, or set up a custom ``#ifdef``-based solution
if you really need to.
Feel free to contact Yosys maintainers with related issues. Feel free to contact Yosys maintainers with related issues.

View File

@ -20,6 +20,8 @@
#include <type_traits> #include <type_traits>
#include <stdint.h> #include <stdint.h>
#define YS_HASHING_VERSION 1
namespace hashlib { namespace hashlib {
/** /**