From 734e94dae8348eacab9e144567c202ad97eb7113 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 13:59:48 +0800 Subject: [PATCH 01/16] fix #16542 --- lib/pure/hashes.nim | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 667f27e95d2ef..4bbd2d76a6f68 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -198,7 +198,20 @@ else: proc hash*(x: float): Hash {.inline.} = ## Efficient hashing of floats. var y = x + 0.0 # for denormalization - result = hash(cast[ptr Hash](addr(y))[]) + when nimvm: + result = hash(cast[Hash](y)) + else: + when not defined(js): + result = hash(cast[Hash](y)) + else: + var res: Hash + asm """const buffer = new ArrayBuffer(8); + const floatBuffer = new Float64Array(buffer); + const uintBuffer = new BigUint64Array(buffer); + floatBuffer[0] = `y`; + `res` = uintBuffer[0]; + """ + result = hash(res) # Forward declarations before methods that hash containers. This allows # containers to contain other containers From ab687c17e255257b3374c6c82cd1f3278d4643b6 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 15:00:00 +0800 Subject: [PATCH 02/16] fix --- lib/pure/hashes.nim | 24 ++++++++++++++++-------- tests/stdlib/thashes.nim | 12 ++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 4bbd2d76a6f68..ff8125ad19fc0 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -199,19 +199,27 @@ proc hash*(x: float): Hash {.inline.} = ## Efficient hashing of floats. var y = x + 0.0 # for denormalization when nimvm: - result = hash(cast[Hash](y)) + when defined(js): + # workaround a JS VM bug + # TODO see issue #16547 + result = hashWangYi1(cast[int64](y)) + else: + result = hashWangYi1(cast[Hash](y)) else: when not defined(js): - result = hash(cast[Hash](y)) + result = hashWangYi1(cast[Hash](y)) else: var res: Hash asm """const buffer = new ArrayBuffer(8); - const floatBuffer = new Float64Array(buffer); - const uintBuffer = new BigUint64Array(buffer); - floatBuffer[0] = `y`; - `res` = uintBuffer[0]; - """ - result = hash(res) + const floatBuffer = new Float64Array(buffer); + const uintBuffer = new BigUint64Array(buffer); + floatBuffer[0] = `y`; + `res` = uintBuffer[0];""" + + # res is a `BigInt` type, but we cheat the type system + # and say it is a `Hash` type. + # TODO refactor it using bigint once jsBigint is ready + result = hashWangYi1(res) # Forward declarations before methods that hash containers. This allows # containers to contain other containers diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index 520b27e263186..11015b93b5a4f 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -75,3 +75,15 @@ block largeSize: # longer than 4 characters doAssert hash(xx) == hash(ssl, 0, 4) doAssert hash(xx, 0, 3) == hash(xxl, 0, 3) doAssert hash(xx, 0, 3) == hash(ssl, 0, 3) + +proc main() = + block: + doAssert hash(0.0) == hash(0) + doAssert hash(1.0) != 0 + doAssert hash(-1.0) != 0 + doAssert hash(1000.0) != 0 + doAssert hash(-1000.0) != 0 + + +static: main() +main() From d28e9ff8363dc3a0940a49dda17b9d1b60cd1f5e Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 15:02:41 +0800 Subject: [PATCH 03/16] minor --- lib/pure/hashes.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index ff8125ad19fc0..cb2e0e9efe38c 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -197,7 +197,7 @@ else: proc hash*(x: float): Hash {.inline.} = ## Efficient hashing of floats. - var y = x + 0.0 # for denormalization + let y = x + 0.0 # for denormalization when nimvm: when defined(js): # workaround a JS VM bug From f4cf8f81d4558f86fa88b83623486329f461c98e Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 15:17:01 +0800 Subject: [PATCH 04/16] more tests --- tests/stdlib/thashes.nim | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index 11015b93b5a4f..acd67817ba026 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -84,6 +84,17 @@ proc main() = doAssert hash(1000.0) != 0 doAssert hash(-1000.0) != 0 + when defined(js): + doAssert hash(0.345602) == 2035867618 + doAssert hash(234567.45) == -20468103 + doAssert hash(-9999.283456) == -43247422 + doAssert hash(84375674.0) == 707542256 + else: + doAssert hash(0.345602) == 387936373221941218 + doAssert hash(234567.45) == -8179139172229468551 + doAssert hash(-9999.283456) == 5876943921626224834 + doAssert hash(84375674.0) == 1964453089107524848 + static: main() main() From 04c2229e47725e539b739954df6b31655d97a988 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 15:21:01 +0800 Subject: [PATCH 05/16] minor --- tests/stdlib/thashes.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index acd67817ba026..10e414482a831 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -2,7 +2,7 @@ discard """ targets: "c cpp js" """ -import hashes +import std/hashes block hashes: block hashing: From 17335f61f2e581008e23690cef8aa9f97339f5b2 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 15:22:22 +0800 Subject: [PATCH 06/16] go --- lib/pure/hashes.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index cb2e0e9efe38c..89c2d5aa62602 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -218,7 +218,7 @@ proc hash*(x: float): Hash {.inline.} = # res is a `BigInt` type, but we cheat the type system # and say it is a `Hash` type. - # TODO refactor it using bigint once jsBigint is ready + # TODO refactor it using bigInt once jsBigInt is ready result = hashWangYi1(res) # Forward declarations before methods that hash containers. This allows From 2f2c0f1f22aca3f79386ddf3fbc211f61a21663a Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 15:29:49 +0800 Subject: [PATCH 07/16] done --- lib/pure/hashes.nim | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 89c2d5aa62602..d590faf16a2fd 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -200,8 +200,7 @@ proc hash*(x: float): Hash {.inline.} = let y = x + 0.0 # for denormalization when nimvm: when defined(js): - # workaround a JS VM bug - # TODO see issue #16547 + # workaround a JS VM bug: bug #16547 result = hashWangYi1(cast[int64](y)) else: result = hashWangYi1(cast[Hash](y)) @@ -218,7 +217,7 @@ proc hash*(x: float): Hash {.inline.} = # res is a `BigInt` type, but we cheat the type system # and say it is a `Hash` type. - # TODO refactor it using bigInt once jsBigInt is ready + # TODO refactor it using bigInt once jsBigInt is ready, pending pr #1640 result = hashWangYi1(res) # Forward declarations before methods that hash containers. This allows From 0b47e2027bbe02a09f0a65fa69b563840bb0a7b3 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 15:57:23 +0800 Subject: [PATCH 08/16] done --- lib/pure/hashes.nim | 24 +++++++++++++----------- tests/stdlib/thashes.nim | 9 +++++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index d590faf16a2fd..e3bf35c298c27 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -195,6 +195,18 @@ else: ## Efficient hashing of integers. hashWangYi1(uint64(ord(x))) +when defined(js): + proc asBigInt(x: float): Hash = + # 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. let y = x + 0.0 # for denormalization @@ -208,17 +220,7 @@ proc hash*(x: float): Hash {.inline.} = when not defined(js): result = hashWangYi1(cast[Hash](y)) else: - var res: Hash - asm """const buffer = new ArrayBuffer(8); - const floatBuffer = new Float64Array(buffer); - const uintBuffer = new BigUint64Array(buffer); - floatBuffer[0] = `y`; - `res` = uintBuffer[0];""" - - # res is a `BigInt` type, but we cheat the type system - # and say it is a `Hash` type. - # TODO refactor it using bigInt once jsBigInt is ready, pending pr #1640 - result = hashWangYi1(res) + result = hashWangYi1(asBigInt(y)) # Forward declarations before methods that hash containers. This allows # containers to contain other containers diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index 10e414482a831..5572ef36a8777 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -79,10 +79,11 @@ block largeSize: # longer than 4 characters proc main() = block: doAssert hash(0.0) == hash(0) - doAssert hash(1.0) != 0 - doAssert hash(-1.0) != 0 - doAssert hash(1000.0) != 0 - doAssert hash(-1000.0) != 0 + var s: seq[Hash] + for a in [0.0, 1.0, -1.0, 1000.0, -1000.0]: + let b = hash(a) + doAssert b notin s + s.add b when defined(js): doAssert hash(0.345602) == 2035867618 From 4766a56cb8019b5db41bbd644d8276f9a02e51e3 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 15:58:37 +0800 Subject: [PATCH 09/16] minor --- lib/pure/hashes.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index e3bf35c298c27..763aa347d378d 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -196,7 +196,7 @@ else: hashWangYi1(uint64(ord(x))) when defined(js): - proc asBigInt(x: float): Hash = + 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 From bd52ae6a1cec4dba5fbe04b7cde7af3a9e7e04ef Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 16:37:22 +0800 Subject: [PATCH 10/16] fix bug --- lib/pure/hashes.nim | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 763aa347d378d..da72f2fab01d0 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -211,11 +211,8 @@ proc hash*(x: float): Hash {.inline.} = ## Efficient hashing of floats. let y = x + 0.0 # for denormalization when nimvm: - when defined(js): - # workaround a JS VM bug: bug #16547 - result = hashWangYi1(cast[int64](y)) - else: - result = hashWangYi1(cast[Hash](y)) + # workaround a JS VM bug: bug #16547 + result = hashWangYi1(cast[int64](float64(y))) else: when not defined(js): result = hashWangYi1(cast[Hash](y)) From 406eb598c3a55c9e87658d654126367d30baafce Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 17:23:45 +0800 Subject: [PATCH 11/16] fix --- tests/stdlib/thashes.nim | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index 5572ef36a8777..cf6bd4071c5d6 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -85,16 +85,22 @@ proc main() = doAssert b notin s s.add b - when defined(js): - doAssert hash(0.345602) == 2035867618 - doAssert hash(234567.45) == -20468103 - doAssert hash(-9999.283456) == -43247422 - doAssert hash(84375674.0) == 707542256 + when sizeof(int) == 4: + when defined(js): + doAssert hash(0.345602) == 2035867618 + doAssert hash(234567.45) == -20468103 + doAssert hash(-9999.283456) == -43247422 + doAssert hash(84375674.0) == 707542256 + else: + doAssert hash(0.345602) == 387936373221941218 + doAssert hash(234567.45) == -8179139172229468551 + doAssert hash(-9999.283456) == 5876943921626224834 + doAssert hash(84375674.0) == 1964453089107524848 else: - doAssert hash(0.345602) == 387936373221941218 - doAssert hash(234567.45) == -8179139172229468551 - doAssert hash(-9999.283456) == 5876943921626224834 - doAssert hash(84375674.0) == 1964453089107524848 + doAssert hash(0.345602) != 0 + doAssert hash(234567.45) != 0 + doAssert hash(-9999.283456) != 0 + doAssert hash(84375674.0) != 0 static: main() From b8c5ce9bdec73a7277f3c179c54c0ce0506b2c39 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 17:27:46 +0800 Subject: [PATCH 12/16] fake --- tests/stdlib/thashes.nim | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index cf6bd4071c5d6..0b665403a358c 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -97,10 +97,21 @@ proc main() = doAssert hash(-9999.283456) == 5876943921626224834 doAssert hash(84375674.0) == 1964453089107524848 else: - doAssert hash(0.345602) != 0 - doAssert hash(234567.45) != 0 - doAssert hash(-9999.283456) != 0 - doAssert hash(84375674.0) != 0 + echo defined(js) + echo hash(0.345602) + echo hash(234567.45) + echo hash(-9999.283456) + echo hash(84375674.0) + when defined(js): + doAssert hash(0.345602) == 2035867618 + doAssert hash(234567.45) == -20468103 + doAssert hash(-9999.283456) == -43247422 + doAssert hash(84375674.0) == 707542256 + else: + doAssert hash(0.345602) == 387936373221941218 + doAssert hash(234567.45) == -8179139172229468551 + doAssert hash(-9999.283456) == 5876943921626224834 + doAssert hash(84375674.0) == 1964453089107524848 static: main() From 24852188cd3a9ab7a13cb67bec22eaaafa25f341 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 17:56:30 +0800 Subject: [PATCH 13/16] fake --- tests/stdlib/thashes.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index 0b665403a358c..71370d79bac9f 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -85,7 +85,7 @@ proc main() = doAssert b notin s s.add b - when sizeof(int) == 4: + when sizeof(int) == 8: when defined(js): doAssert hash(0.345602) == 2035867618 doAssert hash(234567.45) == -20468103 From 0148f0dc13f0b0abd7adb4aa2f21dd6d96cffb71 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 19:06:08 +0800 Subject: [PATCH 14/16] fake --- tests/stdlib/thashes.nim | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index 71370d79bac9f..73aac1d10ca6b 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -102,16 +102,11 @@ proc main() = echo hash(234567.45) echo hash(-9999.283456) echo hash(84375674.0) - when defined(js): - doAssert hash(0.345602) == 2035867618 - doAssert hash(234567.45) == -20468103 - doAssert hash(-9999.283456) == -43247422 - doAssert hash(84375674.0) == 707542256 - else: - doAssert hash(0.345602) == 387936373221941218 - doAssert hash(234567.45) == -8179139172229468551 - doAssert hash(-9999.283456) == 5876943921626224834 - doAssert hash(84375674.0) == 1964453089107524848 + + doAssert hash(0.345602) == 2035867618 + doAssert hash(234567.45) == -20468103 + doAssert hash(-9999.283456) == -43247422 + doAssert hash(84375674.0) == 707542256 static: main() From 41fdbb497594b9182c7d83edf4e2dfe9e526005d Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 19:37:39 +0800 Subject: [PATCH 15/16] fake --- tests/stdlib/thashes.nim | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index 73aac1d10ca6b..02ff6ce0f1211 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -77,15 +77,14 @@ block largeSize: # longer than 4 characters doAssert hash(xx, 0, 3) == hash(ssl, 0, 3) proc main() = - block: - doAssert hash(0.0) == hash(0) - var s: seq[Hash] - for a in [0.0, 1.0, -1.0, 1000.0, -1000.0]: - let b = hash(a) - doAssert b notin s - s.add b - when sizeof(int) == 8: + block: + doAssert hash(0.0) == hash(0) + var s: seq[Hash] + for a in [0.0, 1.0, -1.0, 1000.0, -1000.0]: + let b = hash(a) + doAssert b notin s + s.add b when defined(js): doAssert hash(0.345602) == 2035867618 doAssert hash(234567.45) == -20468103 @@ -97,12 +96,6 @@ proc main() = doAssert hash(-9999.283456) == 5876943921626224834 doAssert hash(84375674.0) == 1964453089107524848 else: - echo defined(js) - echo hash(0.345602) - echo hash(234567.45) - echo hash(-9999.283456) - echo hash(84375674.0) - doAssert hash(0.345602) == 2035867618 doAssert hash(234567.45) == -20468103 doAssert hash(-9999.283456) == -43247422 From ffcd4286650aa77d6d793c21d5f025cc01989b26 Mon Sep 17 00:00:00 2001 From: flywind Date: Sat, 2 Jan 2021 20:29:09 +0800 Subject: [PATCH 16/16] fix --- tests/stdlib/thashes.nim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim index 02ff6ce0f1211..9c928778489b9 100644 --- a/tests/stdlib/thashes.nim +++ b/tests/stdlib/thashes.nim @@ -77,9 +77,9 @@ block largeSize: # longer than 4 characters doAssert hash(xx, 0, 3) == hash(ssl, 0, 3) proc main() = + doAssert hash(0.0) == hash(0) when sizeof(int) == 8: block: - doAssert hash(0.0) == hash(0) var s: seq[Hash] for a in [0.0, 1.0, -1.0, 1000.0, -1000.0]: let b = hash(a) @@ -96,10 +96,10 @@ proc main() = doAssert hash(-9999.283456) == 5876943921626224834 doAssert hash(84375674.0) == 1964453089107524848 else: - doAssert hash(0.345602) == 2035867618 - doAssert hash(234567.45) == -20468103 - doAssert hash(-9999.283456) == -43247422 - doAssert hash(84375674.0) == 707542256 + doAssert hash(0.345602) != 0 + doAssert hash(234567.45) != 0 + doAssert hash(-9999.283456) != 0 + doAssert hash(84375674.0) != 0 static: main()