]> Cypherpunks repositories - gostls13.git/commitdiff
bytes/hash: initialize all 64 bits of hash seed
authorKeith Randall <khr@google.com>
Mon, 21 Oct 2019 21:10:35 +0000 (14:10 -0700)
committerKeith Randall <khr@golang.org>
Mon, 21 Oct 2019 22:01:12 +0000 (22:01 +0000)
Fixes #34925

Change-Id: Iadf12ca47a69b62c3f48d732b430cc85cf62a91c
Reviewed-on: https://go-review.googlesource.com/c/go/+/202577
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
doc/go1.14.html
src/bytes/hash/hash.go
src/bytes/hash/hash_test.go

index 79efb2b0bae34df9023f441d04a1fd51511e42cd..ddaf73d0a5f08f94b7858fd8564f87f538214a4f 100644 (file)
@@ -180,6 +180,19 @@ TODO
 
 </dl><!-- mime -->
 
+<dl id="math"><dt><a href="/pkg/math/">math</a></dt>
+  <dd>
+    <p><!-- CL 127458 -->
+      The new <a href="/pkg/math/#Fma"><code>Fma</code></a> function
+      computes <code>x*y+z</code> in floating point with no
+      intermediate rounding of the <code>x*y</code>
+      computation. Several architectures implement this computation
+      using dedicated hardware instructions for additional
+      performance.
+    </p>
+
+</dl><!-- math -->
+
 <dl id="plugin"><dt><a href="/pkg/plugin/">plugin</a></dt>
   <dd>
     <p><!-- CL 191617 -->
index 0e44e37ae7368eb3fcd9cf515af42bbc387fc91a..cc78b229019e879bb6e2943baa47654c6261de70 100644 (file)
@@ -130,7 +130,9 @@ func MakeSeed(s uint64) Seed {
 // New returns a new Hash object. Different hash objects allocated by
 // this function will very likely have different seeds.
 func New() *Hash {
-       seed := Seed{s: uint64(runtime_fastrand())}
+       s1 := uint64(runtime_fastrand())
+       s2 := uint64(runtime_fastrand())
+       seed := Seed{s: s1<<32 + s2}
        return &Hash{
                seed:  seed,
                state: seed,
index 311f451be2e02fc33e45dc09edba859664cccf5e..f36d5068310fc7c4e2d09901c3b3d3d46bc2455e 100644 (file)
@@ -61,6 +61,20 @@ func TestHashBytesVsString(t *testing.T) {
        }
 }
 
+func TestHashHighBytes(t *testing.T) {
+       // See issue 34925.
+       const N = 10
+       m := map[uint64]struct{}{}
+       for i := 0; i < N; i++ {
+               h := hash.New()
+               h.AddString("foo")
+               m[h.Hash()>>32] = struct{}{}
+       }
+       if len(m) < N/2 {
+               t.Errorf("from %d seeds, wanted at least %d different hashes; got %d", N, N/2, len(m))
+       }
+}
+
 // Make sure a Hash implements the hash.Hash and hash.Hash64 interfaces.
 var _ basehash.Hash = &hash.Hash{}
 var _ basehash.Hash64 = &hash.Hash{}