From e01b56fbd5f644232cc0dc27ab4aa6e2e616d0c5 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 19 Mar 2023 03:47:51 -0700 Subject: [PATCH] Scripting: Fix scalar hashing on different union layouts, e.g. big endian --- src/script/types.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/script/types.c b/src/script/types.c index b63fed4a5..ce19d75f6 100644 --- a/src/script/types.c +++ b/src/script/types.c @@ -347,24 +347,6 @@ static uint32_t _hashString(const struct mScriptValue* val) { return hash32(buffer, size, 0); } -uint32_t _hashScalar(const struct mScriptValue* val) { - // From https://stackoverflow.com/questions/664014/what-integer-hash-function-are-good-that-accepts-an-integer-hash-key - uint32_t x = 0; - switch (val->type->base) { - case mSCRIPT_TYPE_SINT: - x = val->value.s32; - break; - case mSCRIPT_TYPE_UINT: - default: - x = val->value.u32; - break; - } - x = ((x >> 16) ^ x) * 0x45D9F3B; - x = ((x >> 16) ^ x) * 0x45D9F3B; - x = (x >> 16) ^ x; - return x; -} - #define AS(NAME, TYPE) \ bool _as ## NAME(const struct mScriptValue* input, mSCRIPT_TYPE_C_ ## TYPE * T) { \ switch (input->type->base) { \ @@ -463,6 +445,16 @@ bool _castScalar(const struct mScriptValue* input, const struct mScriptType* typ return true; } +uint32_t _hashScalar(const struct mScriptValue* val) { + // From https://stackoverflow.com/questions/664014/what-integer-hash-function-are-good-that-accepts-an-integer-hash-key + uint32_t x = 0; + _asUInt32(val, &x); + x = ((x >> 16) ^ x) * 0x45D9F3B; + x = ((x >> 16) ^ x) * 0x45D9F3B; + x = (x >> 16) ^ x; + return x; +} + uint32_t _valHash(const void* val, size_t len, uint32_t seed) { UNUSED(len); const struct mScriptValue* value = val;