From fdcca7c1650f277791a0c183f11642acef5c26d6 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/hashlib.h | 9 ++++++++- kernel/yosys.cc | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/kernel/driver.cc b/kernel/driver.cc index 65f090993..c5e0e48b8 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 @@ -276,6 +277,8 @@ int main(int argc, char **argv) options.add_options("developer") ("X,trace", "enable tracing of core data structure changes. for debugging") ("M,randomize-pointers", "will slightly randomize allocated pointer addresses. for debugging") + ("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>(), "") @@ -427,6 +430,10 @@ int main(int argc, char **argv) if (result.count("infile")) { frontend_files = result["infile"].as>(); } + 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/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/yosys.cc b/kernel/yosys.cc index 774c7f37d..9f44c829c 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -92,6 +92,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;