1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Improve hashing of numbers

Using an integer hash (https://stackoverflow.com/a/12996028/60617) on
the number casted to int32 combined with lower bits of the number.
This commit is contained in:
Felix Riedel 2020-12-25 16:19:18 +00:00
parent c76e0ae685
commit 2ec12fe06f

View File

@ -260,6 +260,16 @@ int32_t janet_hash(Janet x) {
break;
case JANET_STRUCT:
hash = janet_struct_hash(janet_unwrap_struct(x));
break;
case JANET_NUMBER:
hash = (int32_t)janet_unwrap_number(x);
hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
hash = (hash >> 16) ^ hash;
uint32_t lo = (uint32_t)(janet_u64(x) & 0xFFFFFFFF);
hash ^= lo + 0x9e3779b9 + (hash<<6) + (hash>>2);
break;
case JANET_ABSTRACT: {
JanetAbstract xx = janet_unwrap_abstract(x);