summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorc-blake <c-blake@users.noreply.github.com>2020-11-13 08:04:40 -0500
committerGitHub <noreply@github.com>2020-11-13 14:04:40 +0100
commita9bd4c4e80a73fde73ae4a976d1ee4d65a4a3a66 (patch)
treef83d00b7210d937b257ca773052ea7fecd59f0d1 /lib
parent797cb2e67b084f28474cc5d4251e57275c1e8f5f (diff)
downloadNim-a9bd4c4e80a73fde73ae4a976d1ee4d65a4a3a66.tar.gz
Alternate to https://github.com/nim-lang/Nim/pull/15915 (#15937)
* Alternate PR to https://github.com/nim-lang/Nim/pull/15915 to
resolve the problem mentioned there (`hash() == 0`) as well as
to close https://github.com/nim-lang/Nim/issues/15624

* Address https://github.com/nim-lang/Nim/pull/15937#discussion_r522759669
{ though this was only a move from 2 copies to 3 copies. ;-) }
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/hashes.nim9
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim
index 1024ce26f..3191d4030 100644
--- a/lib/pure/hashes.nim
+++ b/lib/pure/hashes.nim
@@ -113,8 +113,12 @@ proc hashWangYi1*(x: int64|uint64|Hash): Hash {.inline.} =
   const P0  = 0xa0761d6478bd642f'u64
   const P1  = 0xe7037ed1a0b428db'u64
   const P58 = 0xeb44accab455d165'u64 xor 8'u64
+  template h(x): untyped = hiXorLo(hiXorLo(P0, uint64(x) xor P1), P58)
   when nimvm:
-    cast[Hash](hiXorLo(hiXorLo(P0, uint64(x) xor P1), P58))
+    when defined(js): # Nim int64<->JS Number & VM match => JS gets 32-bit hash
+      result = cast[Hash](h(x)) and cast[Hash](0xFFFFFFFF)
+    else:
+      result = cast[Hash](h(x))
   else:
     when defined(js):
       asm """
@@ -132,8 +136,9 @@ proc hashWangYi1*(x: int64|uint64|Hash): Hash {.inline.} =
           var res   = hi_xor_lo_js(hi_xor_lo_js(P0, BigInt(`x`) ^ P1), P58);
           `result`  = Number(res & ((BigInt(1) << BigInt(53)) - BigInt(1)));
         }"""
+      result = result and cast[Hash](0xFFFFFFFF)
     else:
-      cast[Hash](hiXorLo(hiXorLo(P0, uint64(x) xor P1), P58))
+      result = cast[Hash](h(x))
 
 proc hashData*(data: pointer, size: int): Hash =
   ## Hashes an array of bytes of size `size`.