hashlib: fixes from jix

This commit is contained in:
Emil J. Tywoniak 2024-11-20 12:11:37 +01:00
parent ae5b715107
commit 80ec052878
2 changed files with 9 additions and 16 deletions

View File

@ -1067,10 +1067,11 @@ public:
DriveSpec &operator=(DriveBitMultiple const &bit) { return *this = DriveBit(bit); } DriveSpec &operator=(DriveBitMultiple const &bit) { return *this = DriveBit(bit); }
void updhash() const { void updhash() const {
DriveSpec *that = (DriveSpec*)this; if (hash_ != 0)
return;
pack(); pack();
that->hash_ = run_hash(chunks_); hash_ = run_hash(chunks_);
that->hash_ |= (that->hash_ == 0); hash_ |= (hash_ == 0);
} }
Hasher hash_into(Hasher h) const; Hasher hash_into(Hasher h) const;
@ -1372,9 +1373,7 @@ inline Hasher DriveChunk::hash_into(Hasher h) const
inline Hasher DriveSpec::hash_into(Hasher h) const inline Hasher DriveSpec::hash_into(Hasher h) const
{ {
if (hash_ == 0) updhash();
updhash();
h.eat(hash_); h.eat(hash_);
return h; return h;
} }

View File

@ -116,7 +116,7 @@ private:
return; return;
} }
void hash64(uint64_t i) { void hash64(uint64_t i) {
state = djb2_xor((uint32_t)(i % (1ULL << 32ULL)), state); state = djb2_xor((uint32_t)(i & 0xFFFFFFFFULL), state);
state = djb2_xor((uint32_t)(i >> 32ULL), state); state = djb2_xor((uint32_t)(i >> 32ULL), state);
state = mkhash_xorshift(fudge ^ state); state = mkhash_xorshift(fudge ^ state);
return; return;
@ -163,10 +163,7 @@ struct hash_ops {
return a == b; return a == b;
} }
static inline Hasher hash_into(const T &a, Hasher h) { static inline Hasher hash_into(const T &a, Hasher h) {
if constexpr (std::is_same_v<T, bool>) { if constexpr (std::is_integral_v<T>) {
h.hash32(a ? 1 : 0);
return h;
} else if constexpr (std::is_integral_v<T>) {
static_assert(sizeof(T) <= sizeof(uint64_t)); static_assert(sizeof(T) <= sizeof(uint64_t));
if (sizeof(T) == sizeof(uint64_t)) if (sizeof(T) == sizeof(uint64_t))
h.hash64(a); h.hash64(a);
@ -221,7 +218,7 @@ template<typename T> struct hash_ops<std::vector<T>> {
return a == b; return a == b;
} }
static inline Hasher hash_into(std::vector<T> a, Hasher h) { static inline Hasher hash_into(std::vector<T> a, Hasher h) {
h.eat(a.size()); h.eat((uint32_t)a.size());
for (auto k : a) for (auto k : a)
h.eat(k); h.eat(k);
return h; return h;
@ -241,10 +238,7 @@ template<typename T, size_t N> struct hash_ops<std::array<T, N>> {
struct hash_cstr_ops { struct hash_cstr_ops {
static inline bool cmp(const char *a, const char *b) { static inline bool cmp(const char *a, const char *b) {
for (int i = 0; a[i] || b[i]; i++) return strcmp(a, b) == 0;
if (a[i] != b[i])
return false;
return true;
} }
static inline Hasher hash_into(const char *a, Hasher h) { static inline Hasher hash_into(const char *a, Hasher h) {
while (*a) while (*a)