]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: make fastrand to generate 32bit values
authorSokolov Yura <funny.falcon@gmail.com>
Sun, 12 Feb 2017 10:18:22 +0000 (13:18 +0300)
committerJosh Bleecher Snyder <josharian@gmail.com>
Mon, 13 Feb 2017 20:22:02 +0000 (20:22 +0000)
Extend period of fastrand from (1<<31)-1 to (1<<32)-1 by
choosing other polynom and reacting on high bit before shift.

Polynomial is taken at https://users.ece.cmu.edu/~koopman/lfsr/index.html
from 32.dat.gz . It is referred as F7711115 cause this list of
polynomials is for LFSR with shift to right (and fastrand uses shift to
left). (old polynomial is referred in 31.dat.gz as 7BB88888).

There were couple of places with conversation of fastrand to int, which
leads to negative values on 32bit platforms. They are fixed.

Change-Id: Ibee518a3f9103e0aea220ada494b3aec77babb72
Reviewed-on: https://go-review.googlesource.com/36875
Run-TryBot: Minux Ma <minux@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Minux Ma <minux@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/runtime/malloc.go
src/runtime/select.go
src/runtime/stubs.go

index 11c49f665737249ef271369ad5409db5780bb74d..25ae261bb2f7b16db1d60f18cc56e0f1d1786bdb 100644 (file)
@@ -877,7 +877,7 @@ func nextSampleNoFP() int32 {
                rate = 0x3fffffff
        }
        if rate != 0 {
-               return int32(int(fastrand()) % (2 * rate))
+               return int32(fastrand() % uint32(2*rate))
        }
        return 0
 }
index 0d846b1470032c3941d3d6d7a4502bfcbb3a2a0e..4a744a19678953a0cb30b77c779f328cc6adbb8b 100644 (file)
@@ -270,7 +270,7 @@ func selectgoImpl(sel *hselect) (uintptr, uint16) {
        pollslice := slice{unsafe.Pointer(sel.pollorder), int(sel.ncase), int(sel.ncase)}
        pollorder := *(*[]uint16)(unsafe.Pointer(&pollslice))
        for i := 1; i < int(sel.ncase); i++ {
-               j := int(fastrand()) % (i + 1)
+               j := fastrand() % uint32(i+1)
                pollorder[i] = pollorder[j]
                pollorder[j] = uint16(i)
        }
index 101c8cfd10884737709572f4220451a38168f72e..e839c59d55f29b3fbcd0c8417c4fb8af4bff2c4a 100644 (file)
@@ -97,8 +97,8 @@ var hashLoad = loadFactor
 func fastrand() uint32 {
        mp := getg().m
        fr := mp.fastrand
-       fr <<= 1
-       fr ^= uint32(int32(fr)>>31) & 0x88888eef
+       mx := uint32(int32(fr)>>31) & 0xa8888eef
+       fr = fr<<1 ^ mx
        mp.fastrand = fr
        return fr
 }