JS: make hash float support IE/Safari (#16872)
This commit is contained in:
parent
206ad43cdc
commit
a2855b66ae
2 changed files with 14 additions and 5 deletions
|
|
@ -141,13 +141,18 @@ when defined(js):
|
||||||
res = hiXorLoJs(hiXorLoJs(P0, x xor P1), P58)
|
res = hiXorLoJs(hiXorLoJs(P0, x xor P1), P58)
|
||||||
cast[Hash](toNumber(wrapToInt(res, 32)))
|
cast[Hash](toNumber(wrapToInt(res, 32)))
|
||||||
|
|
||||||
template asBigInt(num: float): JsBigInt =
|
template toBits(num: float): JsBigInt =
|
||||||
let
|
let
|
||||||
x = newArrayBuffer(8)
|
x = newArrayBuffer(8)
|
||||||
y = newFloat64Array(x)
|
y = newFloat64Array(x)
|
||||||
z = newBigUint64Array(x)
|
if hasBigUint64Array():
|
||||||
y[0] = num
|
let z = newBigUint64Array(x)
|
||||||
z[0]
|
y[0] = num
|
||||||
|
z[0]
|
||||||
|
else:
|
||||||
|
let z = newUint32Array(x)
|
||||||
|
y[0] = num
|
||||||
|
big(z[0]) + big(z[1]) shl big(32)
|
||||||
|
|
||||||
proc hashWangYi1*(x: int64|uint64|Hash): Hash {.inline.} =
|
proc hashWangYi1*(x: int64|uint64|Hash): Hash {.inline.} =
|
||||||
## Wang Yi's hash_v1 for 64-bit ints (see https://github.com/rurban/smhasher for
|
## Wang Yi's hash_v1 for 64-bit ints (see https://github.com/rurban/smhasher for
|
||||||
|
|
@ -237,7 +242,7 @@ proc hash*(x: float): Hash {.inline.} =
|
||||||
when not defined(js):
|
when not defined(js):
|
||||||
result = hashWangYi1(cast[Hash](y))
|
result = hashWangYi1(cast[Hash](y))
|
||||||
else:
|
else:
|
||||||
result = hashWangYiJS(asBigInt(y))
|
result = hashWangYiJS(toBits(y))
|
||||||
|
|
||||||
# Forward declarations before methods that hash containers. This allows
|
# Forward declarations before methods that hash containers. This allows
|
||||||
# containers to contain other containers
|
# containers to contain other containers
|
||||||
|
|
|
||||||
|
|
@ -16,5 +16,9 @@ when defined(js):
|
||||||
func `[]`*(arr: BigUint64Array, i: int): JsBigInt {.importjs: "#[#]".}
|
func `[]`*(arr: BigUint64Array, i: int): JsBigInt {.importjs: "#[#]".}
|
||||||
func `[]=`*(arr: Float64Array, i: int, v: float) {.importjs: "#[#] = #".}
|
func `[]=`*(arr: Float64Array, i: int, v: float) {.importjs: "#[#] = #".}
|
||||||
|
|
||||||
|
|
||||||
proc hasJsBigInt*(): bool =
|
proc hasJsBigInt*(): bool =
|
||||||
asm """`result` = typeof BigInt != 'undefined'"""
|
asm """`result` = typeof BigInt != 'undefined'"""
|
||||||
|
|
||||||
|
proc hasBigUint64Array*(): bool =
|
||||||
|
asm """`result` = typeof BigUint64Array != 'undefined'"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue