From 953508f6d208b17740f08cea673f2d4bacc292f2 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Tue, 1 Oct 2024 16:02:41 +0200 Subject: [PATCH] driver: add --hash-seed --- kernel/driver.cc | 7 +++++++ kernel/drivertools.h | 32 -------------------------------- kernel/hashlib.h | 9 ++++++++- kernel/scopeinfo.h | 7 +++++-- kernel/yosys.cc | 1 + 5 files changed, 21 insertions(+), 35 deletions(-) diff --git a/kernel/driver.cc b/kernel/driver.cc index 6565c472c..a3d85bd90 100644 --- a/kernel/driver.cc +++ b/kernel/driver.cc @@ -18,6 +18,7 @@ */ #include "kernel/yosys.h" +#include "kernel/hashlib.h" #include "libs/sha1/sha1.h" #include "libs/cxxopts/include/cxxopts.hpp" #include @@ -282,6 +283,8 @@ int main(int argc, char **argv) ("M,randomize-pointers", "will slightly randomize allocated pointer addresses. for debugging") ("autoidx", "start counting autoidx up from , similar effect to --hash-seed", cxxopts::value(), "") + ("hash-seed", "mix up hashing values with , for extreme optimization and testing", + cxxopts::value(), "") ("A,abort", "will call abort() at the end of the script. for debugging") ("x,experimental", "do not print warnings for the experimental ", cxxopts::value>(), "") @@ -437,6 +440,10 @@ int main(int argc, char **argv) int idx = result["autoidx"].as(); autoidx = idx; } + if (result.count("hash-seed")) { + int seed = result["hash-seed"].as(); + Hasher::set_fudge((Hasher::hash_t)seed); + } if (log_errfile == NULL) { log_files.push_back(stdout); diff --git a/kernel/drivertools.h b/kernel/drivertools.h index fdead5c36..0e7b872e4 100644 --- a/kernel/drivertools.h +++ b/kernel/drivertools.h @@ -863,39 +863,7 @@ public: bool try_append(DriveBit const &bit); bool try_append(DriveChunk const &chunk); -<<<<<<< HEAD - unsigned int hash() const - { - unsigned int inner = 0; - switch (type_) - { - case DriveType::NONE: - inner = 0; - break; - case DriveType::CONSTANT: - inner = constant_.hash(); - 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; ->>>>>>> 898d04260 (hashlib: redo interface for flexibility) bool operator==(const DriveChunk &other) const { diff --git a/kernel/hashlib.h b/kernel/hashlib.h index 21e4e155b..15c2c1afb 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -81,13 +81,20 @@ class Hasher { // traditionally 5381 is used as starting value for the djb2 hash state = 5381; } + static void set_fudge(uint32_t f) { + fudge = f; + } private: uint32_t state; + static uint32_t fudge; // The XOR version of DJB2 [[nodiscard]] static uint32_t mkhash(uint32_t a, uint32_t b) { - return ((a << 5) + a) ^ b; + uint32_t hash = ((a << 5) + a) ^ b; + if (fudge) + hash = fudge ^ mkhash_xorshift(hash); + return hash; } public: void hash32(uint32_t i) { diff --git a/kernel/scopeinfo.h b/kernel/scopeinfo.h index 5d2e6d4b1..fa550dab6 100644 --- a/kernel/scopeinfo.h +++ b/kernel/scopeinfo.h @@ -169,8 +169,11 @@ public: return !(*this == other); } - int hash() const { - return mkhash(scope_name.hash(), hash_ptr_ops::hash(target)); + Hasher hash_acc(Hasher h) const + { + h.acc(scope_name); + h.acc(target); + return h; } bool valid() const { diff --git a/kernel/yosys.cc b/kernel/yosys.cc index bdd7303aa..7cf60f068 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -93,6 +93,7 @@ std::set yosys_input_files, yosys_output_files; bool memhasher_active = false; uint32_t memhasher_rng = 123456; std::vector memhasher_store; +uint32_t Hasher::fudge = 0; std::string yosys_share_dirname; std::string yosys_abc_executable;