mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-13 23:43:40 +00:00
runtime: seed fastrand() with hardware randomness
This commit is contained in:
@@ -42,8 +42,8 @@ func TestBinarySize(t *testing.T) {
|
|||||||
tests := []sizeTest{
|
tests := []sizeTest{
|
||||||
// microcontrollers
|
// microcontrollers
|
||||||
{"hifive1b", "examples/echo", 4484, 280, 0, 2252},
|
{"hifive1b", "examples/echo", 4484, 280, 0, 2252},
|
||||||
{"microbit", "examples/serial", 2732, 388, 8, 2256},
|
{"microbit", "examples/serial", 2808, 388, 8, 2256},
|
||||||
{"wioterminal", "examples/pininterrupt", 6016, 1484, 116, 6816},
|
{"wioterminal", "examples/pininterrupt", 6064, 1484, 116, 6816},
|
||||||
|
|
||||||
// TODO: also check wasm. Right now this is difficult, because
|
// TODO: also check wasm. Right now this is difficult, because
|
||||||
// wasm binaries are run through wasm-opt and therefore the
|
// wasm binaries are run through wasm-opt and therefore the
|
||||||
|
|||||||
@@ -23,7 +23,13 @@ func fastrand() uint32 {
|
|||||||
return xorshift32State
|
return xorshift32State
|
||||||
}
|
}
|
||||||
|
|
||||||
var xorshift32State uint32 = 1
|
func init() {
|
||||||
|
r, _ := hardwareRand()
|
||||||
|
xorshift64State = uint64(r | 1) // protect against 0
|
||||||
|
xorshift32State = uint32(xorshift64State)
|
||||||
|
}
|
||||||
|
|
||||||
|
var xorshift32State uint32
|
||||||
|
|
||||||
func xorshift32(x uint32) uint32 {
|
func xorshift32(x uint32) uint32 {
|
||||||
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
|
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
|
||||||
@@ -43,7 +49,7 @@ func fastrand64() uint64 {
|
|||||||
return xorshift64State
|
return xorshift64State
|
||||||
}
|
}
|
||||||
|
|
||||||
var xorshift64State uint64 = 1
|
var xorshift64State uint64
|
||||||
|
|
||||||
// 64-bit xorshift multiply rng from http://vigna.di.unimi.it/ftp/papers/xorshift.pdf
|
// 64-bit xorshift multiply rng from http://vigna.di.unimi.it/ftp/papers/xorshift.pdf
|
||||||
func xorshiftMult64(x uint64) uint64 {
|
func xorshiftMult64(x uint64) uint64 {
|
||||||
|
|||||||
Reference in New Issue
Block a user