Skip to content

Commit

Permalink
hashlib: fixes from jix
Browse files Browse the repository at this point in the history
  • Loading branch information
widlarizer committed Dec 18, 2024
1 parent b9b9515 commit ed70038
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
11 changes: 5 additions & 6 deletions kernel/drivertools.h
Original file line number Diff line number Diff line change
Expand Up @@ -1067,10 +1067,11 @@ struct DriveSpec
DriveSpec &operator=(DriveBitMultiple const &bit) { return *this = DriveBit(bit); }

void updhash() const {
DriveSpec *that = (DriveSpec*)this;
if (hash_ != 0)
return;
pack();
that->hash_ = run_hash(chunks_);
that->hash_ |= (that->hash_ == 0);
hash_ = run_hash(chunks_);
hash_ |= (hash_ == 0);
}

Hasher hash_into(Hasher h) const;
Expand Down Expand Up @@ -1372,9 +1373,7 @@ inline Hasher DriveChunk::hash_into(Hasher h) const

inline Hasher DriveSpec::hash_into(Hasher h) const
{
if (hash_ == 0)
updhash();

updhash();
h.eat(hash_);
return h;
}
Expand Down
14 changes: 4 additions & 10 deletions kernel/hashlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class HasherDJB32 {
return;
}
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 = mkhash_xorshift(fudge ^ state);
return;
Expand Down Expand Up @@ -163,10 +163,7 @@ struct hash_ops {
return a == b;
}
static inline Hasher hash_into(const T &a, Hasher h) {
if constexpr (std::is_same_v<T, bool>) {
h.hash32(a ? 1 : 0);
return h;
} else if constexpr (std::is_integral_v<T>) {
if constexpr (std::is_integral_v<T>) {
static_assert(sizeof(T) <= sizeof(uint64_t));
if (sizeof(T) == sizeof(uint64_t))
h.hash64(a);
Expand Down Expand Up @@ -221,7 +218,7 @@ template<typename T> struct hash_ops<std::vector<T>> {
return a == b;
}
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)
h.eat(k);
return h;
Expand All @@ -241,10 +238,7 @@ template<typename T, size_t N> struct hash_ops<std::array<T, N>> {

struct hash_cstr_ops {
static inline bool cmp(const char *a, const char *b) {
for (int i = 0; a[i] || b[i]; i++)
if (a[i] != b[i])
return false;
return true;
return strcmp(a, b) == 0;
}
static inline Hasher hash_into(const char *a, Hasher h) {
while (*a)
Expand Down

0 comments on commit ed70038

Please sign in to comment.