mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-03 18:47:47 +00:00
runtime: manually initialize xorshift state
This ensures:
1. The xorshift state is initialized during interp.
2. The xorshift state gets initialized to a real random number on
hardware that supports it at runtime.
This fixes a big binary size regression from the previous commit. It's
still not perfect: most programs increase binary size by a few bytes.
But it's not nearly as bad as before.
This commit is contained in:
committed by
Ron Evans
parent
811b13a9d3
commit
f4fd79f7be
@@ -23,13 +23,13 @@ func fastrand() uint32 {
|
||||
return xorshift32State
|
||||
}
|
||||
|
||||
func init() {
|
||||
func initRand() {
|
||||
r, _ := hardwareRand()
|
||||
xorshift64State = uint64(r | 1) // protect against 0
|
||||
xorshift32State = uint32(xorshift64State)
|
||||
}
|
||||
|
||||
var xorshift32State uint32
|
||||
var xorshift32State uint32 = 1
|
||||
|
||||
func xorshift32(x uint32) uint32 {
|
||||
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
|
||||
@@ -49,7 +49,7 @@ func fastrand64() uint64 {
|
||||
return xorshift64State
|
||||
}
|
||||
|
||||
var xorshift64State uint64
|
||||
var xorshift64State uint64 = 1
|
||||
|
||||
// 64-bit xorshift multiply rng from http://vigna.di.unimi.it/ftp/papers/xorshift.pdf
|
||||
func xorshiftMult64(x uint64) uint64 {
|
||||
|
||||
Reference in New Issue
Block a user