From 70decdb703f5d053da3dbe349e9a7328a14aa1f2 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 10 Aug 2020 10:43:12 +0200 Subject: Updated hash funcs. --- examples/benchmark.c | 11 +++++------ stc/cmap.h | 22 +++++----------------- stc/crandom.h | 5 +++-- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/examples/benchmark.c b/examples/benchmark.c index 52aa8353..30fb48af 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -15,11 +15,10 @@ // Visual Studio: compile with -TP to force C++: cl -TP -EHsc -O2 benchmark.c static inline uint32_t fibfast_hash(const void* data, size_t len) { - const uint64_t key = *(const uint64_t *) data; - return (uint32_t) (key * 11400714819323198485llu); + return (*(const uint64_t *) data) * 11400714819323198485llu; } -declare_cmap(ii, int64_t, int64_t, c_default_destroy, c_default_equals, fibfast_hash); // c_fibonacci_hash64); +declare_cmap(ii, int64_t, int64_t, c_default_destroy, c_default_equals, fibfast_hash); // c_default_hash32); KHASH_MAP_INIT_INT64(ii, uint64_t) @@ -143,7 +142,7 @@ int main(int argc, char* argv[]) printf("\nRandom keys are in range [0, 2^%d), seed = %zu:\n", rr, seed); printf("\nUnordered maps: %zu repeats of Insert random key + try to remove a random key:\n", N1); MAP_TEST1(CMAP, ii) - MAP_TEST1(KMAP, ii) + //MAP_TEST1(KMAP, ii) #ifdef __cplusplus MAP_TEST1(UMAP, ii) MAP_TEST1(BMAP, ii) @@ -153,7 +152,7 @@ int main(int argc, char* argv[]) printf("\nUnordered maps: Insert %zu index keys, then remove them in same order:\n", N2); MAP_TEST2(CMAP, ii) - MAP_TEST2(KMAP, ii) + //MAP_TEST2(KMAP, ii) #ifdef __cplusplus MAP_TEST2(UMAP, ii) MAP_TEST2(BMAP, ii) @@ -163,7 +162,7 @@ int main(int argc, char* argv[]) printf("\nUnordered maps: Insert %zu random keys, then remove them in same order:\n", N3); MAP_TEST3(CMAP, ii) - MAP_TEST3(KMAP, ii) + //MAP_TEST3(KMAP, ii) #ifdef __cplusplus MAP_TEST3(UMAP, ii) MAP_TEST3(BMAP, ii) diff --git a/stc/cmap.h b/stc/cmap.h index 08986f29..dfb845a2 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -379,32 +379,20 @@ ctype##_##tag##_next(ctype##_##tag##_iter_t* it) { \ while (++it->item != it->end && *++it->_hx == 0) ; \ } - -/* One-byte-at-a-time hash based on Murmur's mix */ -STC_API uint32_t c_default_hash(const void *data, size_t len) { - const volatile uint8_t *key = (const uint8_t *) data; - uint32_t x = UINT32_C(0xc613fc15); - while (len--) { - x ^= *key++; - x *= UINT32_C(0x5bd1e995); - x ^= x >> 15; - } +STC_API uint32_t c_default_hash(const void *data, size_t len) { + const volatile uint16_t *key = (const uint16_t *) data; + uint64_t x = 0xc613fc15u; + while (len -= 2) x = ((*key++ + x) * 2654435769u) >> 13; return x; } /* https://programmingpraxis.com/2018/06/19/fibonacci-hash */ /* https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/ */ -STC_API uint32_t c_fibonacci_hash32(const void* data, size_t len) { +STC_API uint32_t c_default_hash32(const void* data, size_t len) { const volatile uint32_t *key = (const uint32_t *) data; uint32_t x = *key++ * 2654435769u; while (len -= 4) x ^= *key++ * 2654435769u; return x; } -STC_API uint32_t c_fibonacci_hash64(const void* data, size_t len) { - const volatile uint64_t *key = (const uint64_t *) data; - uint64_t x = *key++ * 11400714819323198485ull; - while (len -= 8) x ^= *key++ * 11400714819323198485ull; - return (uint32_t) x; // (x >> 13); -} #else #define implement_CHASH(tag, ctype, Key, Value, valueDestroy, keyEqualsRaw, keyHashRaw, \ diff --git a/stc/crandom.h b/stc/crandom.h index 96a07fc4..1e3b2107 100644 --- a/stc/crandom.h +++ b/stc/crandom.h @@ -97,8 +97,9 @@ STC_INLINE crandom_distrib_i64_t crandom_uniform_i64_init(int64_t low, int64_t h STC_INLINE int64_t crandom_uniform_i64(crandom_eng64_t* rng, crandom_distrib_i64_t dist) { #if defined(__SIZEOF_INT128__) return dist.offset + (int64_t) (((__uint128_t) crandom_i64(rng) * dist.range) >> 64); -#elif defined(_MSC_VER) - int64_t hi; _mul128(crandom_i64(rng) >> 1, dist.range << 1, &hi); return dist.offset + hi; + +#elif defined(_MSC_VER) && defined(_WIN64) + int64_t hi; _umul128(crandom_i64(rng), dist.range, &hi); return dist.offset + hi; #else return dist.offset + crandom_i64(rng) % dist.range; // slower #endif -- cgit v1.2.3