Refactor hash function implementation in preparation of tabularization

parent 86ef2a16
...@@ -77,26 +77,32 @@ fh_assert_lengths(void) ...@@ -77,26 +77,32 @@ fh_assert_lengths(void)
#endif #endif
} }
#define FH_H_SHA256(d, p, l) sha256(d, p, l)
#define FH_H_XXH32(d, p, l) do { (d) = XXH32(p, l, 0); } while(0)
#define FH_H_XXH3_64(d, p, l) do { (d) = XXH3_64bits(p, l); } while(0)
#define FH_H_XXH3_128(d, p, l) do { \
XXH128_hash_t h = XXH3_128bits(p, l); \
memcpy(d, &h, sizeof d); \
} while (0);
void void
fh(uint8_t fht, union fh *fhh, const void *p, size_t l) fh(uint8_t fht, union fh *fhh, const void *p, size_t l)
{ {
//lint --e{779,506} == NULL comparison with constant //lint --e{779,506} == NULL comparison with constant
switch (fht) { switch (fht) {
case FH_SHA256: case FH_SHA256:
sha256(fhh->sha256, p, l); FH_H_SHA256(fhh->sha256, p, l);
break; break;
#ifdef HAVE_XXHASH_H #ifdef HAVE_XXHASH_H
case FH_XXH32: case FH_XXH32:
fhh->xxh32 = XXH32(p, l, 0); FH_H_XXH32(fhh->xxh32, p, l);
break; break;
case FH_XXH3_64: case FH_XXH3_64:
fhh->xxh64 = XXH3_64bits(p, l); FH_H_XXH3_64(fhh->xxh64, p, l);
break; break;
case FH_XXH3_128: { case FH_XXH3_128:
XXH128_hash_t h = XXH3_128bits(p, l); FH_H_XXH3_128(fhh->xxh128, p, l);
memcpy(fhh->xxh128, &h, sizeof fhh->xxh128);
break; break;
}
#else #else
case FH_XXH32: case FH_XXH32:
case FH_XXH3_64: case FH_XXH3_64:
...@@ -108,23 +114,32 @@ fh(uint8_t fht, union fh *fhh, const void *p, size_t l) ...@@ -108,23 +114,32 @@ fh(uint8_t fht, union fh *fhh, const void *p, size_t l)
} }
} }
static inline int
xxh3_128_cmp(const uint8_t v[16], const void *p, size_t l)
{
XXH128_hash_t h = XXH3_128bits(p, l);
return (memcmp(v, &h, sizeof h));
}
#define FH_C_SHA256(v, p, l) sha256cmp(v, p, l)
#define FH_C_XXH32(v, p, l) ((v) != XXH32(p, l, 0))
#define FH_C_XXH3_64(v, p, l) ((v) != XXH3_64bits(p, l))
#define FH_C_XXH3_128(v, p, l) xxh3_128_cmp(v, p, l)
int int
fhcmp(uint8_t fht, const union fh *fhh, const void *p, size_t l) fhcmp(uint8_t fht, const union fh *fhh, const void *p, size_t l)
{ {
//lint --e{779,506} == NULL comparison with constant //lint --e{779,506} == NULL comparison with constant
switch (fht) { switch (fht) {
case FH_SHA256: case FH_SHA256:
return (sha256cmp(fhh->sha256, p, l)); return (FH_C_SHA256(fhh->sha256, p, l));
#ifdef HAVE_XXHASH_H #ifdef HAVE_XXHASH_H
case FH_XXH32: case FH_XXH32:
return (fhh->xxh32 != XXH32(p, l, 0)); return (FH_C_XXH32(fhh->xxh32, p, l));
case FH_XXH3_64: case FH_XXH3_64:
return (fhh->xxh64 != XXH3_64bits(p, l)); return (FH_C_XXH3_64(fhh->xxh64, p, l));
case FH_XXH3_128: case FH_XXH3_128:
{ return (FH_C_XXH3_128(fhh->xxh128, p, l));
XXH128_hash_t h = XXH3_128bits(p, l);
return (memcmp(fhh->xxh128, &h, sizeof fhh->xxh128));
}
#else #else
case FH_XXH32: case FH_XXH32:
case FH_XXH3_64: case FH_XXH3_64:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment