Skip to content

Commit d7ff964

Browse files
committed
Switch to FxHasher hash function in int64hash and bitmix
1 parent b71523e commit d7ff964

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/support/hashing.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@ uint32_t int32hash(uint32_t a)
2525
return a;
2626
}
2727

28+
// FxHasher
2829
uint64_t int64hash(uint64_t key)
2930
{
30-
key = (~key) + (key << 21); // key = (key << 21) - key - 1;
31-
key = key ^ (key >> 24);
32-
key = (key + (key << 3)) + (key << 8); // key * 265
33-
key = key ^ (key >> 14);
34-
key = (key + (key << 2)) + (key << 4); // key * 21
35-
key = key ^ (key >> 28);
36-
key = key + (key << 31);
37-
return key;
31+
return key * 0x517cc1b727220a95;
3832
}
3933

4034
uint32_t int64to32hash(uint64_t key)

src/support/hashing.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,25 @@ JL_DLLEXPORT uint64_t memhash_seed(const char *buf, size_t n, uint32_t seed) JL_
2525
JL_DLLEXPORT uint32_t memhash32(const char *buf, size_t n) JL_NOTSAFEPOINT;
2626
JL_DLLEXPORT uint32_t memhash32_seed(const char *buf, size_t n, uint32_t seed) JL_NOTSAFEPOINT;
2727

28+
// FxHasher
2829
#ifdef _P64
29-
STATIC_INLINE uint64_t bitmix(uint64_t a, uint64_t b) JL_NOTSAFEPOINT
30+
STATIC_INLINE uint64_t bitmix(uint64_t h, uint64_t a) JL_NOTSAFEPOINT
3031
{
31-
return int64hash(a^bswap_64(b));
32+
h = (h << 5) | (h >> (sizeof(h) * 8 - 5)); // rotate 5 bits to the left
33+
h ^= a;
34+
h *= 0x517cc1b727220a95;
35+
return h;
3236
}
3337
#else
34-
STATIC_INLINE uint32_t bitmix(uint32_t a, uint32_t b) JL_NOTSAFEPOINT
38+
STATIC_INLINE uint32_t bitmix(uint32_t h, uint32_t a) JL_NOTSAFEPOINT
3539
{
36-
return int64to32hash((((uint64_t)a) << 32) | (uint64_t)b);
40+
h = (h << 5) | (h >> (sizeof(h) * 8 - 5)); // rotate 5 bits to the left
41+
h ^= a;
42+
h *= 0x9e3779b9;
43+
return h;
3744
}
3845
#endif
39-
#define bitmix(a, b) (bitmix)((uintptr_t)(a), (uintptr_t)(b))
46+
#define bitmix(h, a) (bitmix)((uintptr_t)(h), (uintptr_t)(a))
4047

4148
#ifdef __cplusplus
4249
}

0 commit comments

Comments
 (0)