From 2ec12fe06f02deb31aec5dd29595c19359623098 Mon Sep 17 00:00:00 2001 From: Felix Riedel Date: Fri, 25 Dec 2020 16:19:18 +0000 Subject: [PATCH] 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. --- src/core/value.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/core/value.c b/src/core/value.c index 1be7966c..79ce6f32 100644 --- a/src/core/value.c +++ b/src/core/value.c @@ -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);