Skip to content

Commit

Permalink
hash: Make basis of hash_words64() 32 bits.
Browse files Browse the repository at this point in the history
The basis of hash_words64() was 64 bits, even when the hash value is
32 bits, thus confusing the domain and the range of the function.
This patch fixes this by making the basis an uint32_t.

Suggested-by: Ben Pfaff <[email protected]>
Signed-off-by: Jarno Rajahalme <[email protected]>
  • Loading branch information
Jarno Rajahalme committed Jan 6, 2015
1 parent b2623fd commit 4ad07ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ hash_words__(const uint32_t p[], size_t n_words, uint32_t basis)
}

uint32_t
hash_words64__(const uint64_t p[], size_t n_words, uint64_t basis)
hash_words64__(const uint64_t p[], size_t n_words, uint32_t basis)
{
return hash_words64_inline(p, n_words, basis);
}
Expand Down
17 changes: 8 additions & 9 deletions lib/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ hash_words_inline(const uint32_t p[], size_t n_words, uint32_t basis)
}

static inline uint32_t
hash_words64_inline(const uint64_t p[], size_t n_words, uint64_t basis)
hash_words64_inline(const uint64_t p[], size_t n_words, uint32_t basis)
{
return hash_words_inline((uint32_t *)p, n_words * 2,
(uint32_t)basis ^ basis >> 32);
return hash_words_inline((uint32_t *)p, n_words * 2, basis);
}

static inline uint32_t hash_pointer(const void *p, uint32_t basis)
Expand Down Expand Up @@ -215,10 +214,10 @@ hash_words_inline(const uint32_t p_[], size_t n_words, uint32_t basis)
/* A simpler version for 64-bit data.
* 'n_words' is the count of 64-bit words, basis is 64 bits. */
static inline uint32_t
hash_words64_inline(const uint64_t p[], size_t n_words, uint64_t basis)
hash_words64_inline(const uint64_t p[], size_t n_words, uint32_t basis)
{
uint64_t hash1 = (uint32_t)basis;
uint64_t hash2 = basis >> 32;
uint64_t hash1 = basis;
uint64_t hash2 = 0;
uint64_t hash3 = n_words;
const uint64_t *endp = p + n_words;
const uint64_t *limit = endp - 3;
Expand Down Expand Up @@ -265,7 +264,7 @@ static inline uint32_t hash_pointer(const void *p, uint32_t basis)
#endif

uint32_t hash_words__(const uint32_t p[], size_t n_words, uint32_t basis);
uint32_t hash_words64__(const uint64_t p[], size_t n_words, uint64_t basis);
uint32_t hash_words64__(const uint64_t p[], size_t n_words, uint32_t basis);

/* Inline the larger hash functions only when 'n_words' is known to be
* compile-time constant. */
Expand All @@ -281,7 +280,7 @@ hash_words(const uint32_t p[], size_t n_words, uint32_t basis)
}

static inline uint32_t
hash_words64(const uint64_t p[], size_t n_words, uint64_t basis)
hash_words64(const uint64_t p[], size_t n_words, uint32_t basis)
{
if (__builtin_constant_p(n_words)) {
return hash_words64_inline(p, n_words, basis);
Expand All @@ -299,7 +298,7 @@ hash_words(const uint32_t p[], size_t n_words, uint32_t basis)
}

static inline uint32_t
hash_words64(const uint64_t p[], size_t n_words, uint64_t basis)
hash_words64(const uint64_t p[], size_t n_words, uint32_t basis)
{
return hash_words64__(p, n_words, basis);
}
Expand Down

0 comments on commit 4ad07ad

Please sign in to comment.