src/runtime: improve float/complex hashing

This allows positive and negative zero to hash to the same value,
as required by Go.

This is not perfect, but the best I could do without
revamping all the hash funtions to take a seed.

Fixes #2356
This commit is contained in:
Damian Gryski
2021-12-07 19:03:18 -08:00
committed by Ayke
parent 9734f349a3
commit 1903cf23c9
3 changed files with 77 additions and 3 deletions
+45 -1
View File
@@ -1,6 +1,8 @@
package main
import "sort"
import (
"sort"
)
var testmap1 = map[string]int{"data": 3}
var testmap2 = map[string]int{
@@ -109,6 +111,48 @@ func main() {
squares = make(map[int]int, 20)
testBigMap(squares, 40)
println("tested growing of a map")
floatcmplx()
}
func floatcmplx() {
var zero float64
var negz float64 = -zero
// test that zero and negative zero hash to the same thing
m := make(map[float64]int)
m[zero]++
m[negz]++
println(m[negz])
cmap := make(map[complex128]int)
var c complex128
c = complex(zero, zero)
cmap[c]++
c = complex(negz, negz)
cmap[c]++
c = complex(0, 0)
println(cmap[c])
c = complex(1, negz)
cmap[c]++
c = complex(1, zero)
cmap[c]++
println(cmap[c])
c = complex(negz, 2)
cmap[c]++
c = complex(zero, 2)
cmap[c]++
println(cmap[c])
}
func readMap(m map[string]int, key string) {
+4
View File
@@ -72,3 +72,7 @@ structMap[{"tau", 3.14}]: 0
structMap[{"tau", 6.28}]: 0
tested preallocated map
tested growing of a map
2
2
2
2