* fix #16542
This commit is contained in:
flywind 2021-01-02 07:32:37 -06:00 • committed by GitHub
commit d8b1ffc857
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 3 deletions

View file

@ -195,10 +195,29 @@ else:
## Efficient hashing of integers.
hashWangYi1(uint64(ord(x)))
when defined(js):
proc asBigInt(x: float): int64 =
# result is a `BigInt` type in js, but we cheat the type system
# and say it is a `int64` type.
# TODO refactor it using bigInt once jsBigInt is ready, pending pr #1640
asm """
const buffer = new ArrayBuffer(8);
const floatBuffer = new Float64Array(buffer);
const uintBuffer = new BigUint64Array(buffer);
floatBuffer[0] = `x`;
`result` = uintBuffer[0];"""
proc hash*(x: float): Hash {.inline.} =
## Efficient hashing of floats.
var y = x + 0.0 # for denormalization
result = hash(cast[ptr Hash](addr(y))[])
let y = x + 0.0 # for denormalization
when nimvm:
# workaround a JS VM bug: bug #16547
result = hashWangYi1(cast[int64](float64(y)))
else:
when not defined(js):
result = hashWangYi1(cast[Hash](y))
else:
result = hashWangYi1(asBigInt(y))
# Forward declarations before methods that hash containers. This allows
# containers to contain other containers