Scripting: Fix scalar hashing on different union layouts, e.g. big endian

This commit is contained in:
Vicki Pfau 2023-03-19 03:47:51 -07:00
parent 5b52f0f277
commit e01b56fbd5
1 changed files with 10 additions and 18 deletions

View File

@ -347,24 +347,6 @@ static uint32_t _hashString(const struct mScriptValue* val) {
return hash32(buffer, size, 0); 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) \ #define AS(NAME, TYPE) \
bool _as ## NAME(const struct mScriptValue* input, mSCRIPT_TYPE_C_ ## TYPE * T) { \ bool _as ## NAME(const struct mScriptValue* input, mSCRIPT_TYPE_C_ ## TYPE * T) { \
switch (input->type->base) { \ switch (input->type->base) { \
@ -463,6 +445,16 @@ bool _castScalar(const struct mScriptValue* input, const struct mScriptType* typ
return true; 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) { uint32_t _valHash(const void* val, size_t len, uint32_t seed) {
UNUSED(len); UNUSED(len);
const struct mScriptValue* value = val; const struct mScriptValue* value = val;