mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-03 18:47:47 +00:00
runtime: add fastrand
This is needed for hash/maphash support.
This commit is contained in:
committed by
Ron Evans
parent
0791603c86
commit
4c28f1b875
@@ -0,0 +1,22 @@
|
||||
package runtime
|
||||
|
||||
// This file implements various core algorithms used in the runtime package and
|
||||
// standard library.
|
||||
|
||||
// This function is used by hash/maphash.
|
||||
func fastrand() uint32 {
|
||||
xorshift32State = xorshift32(xorshift32State)
|
||||
return xorshift32State
|
||||
}
|
||||
|
||||
var xorshift32State uint32 = 1
|
||||
|
||||
func xorshift32(x uint32) uint32 {
|
||||
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
|
||||
// Improved sequence based on
|
||||
// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf
|
||||
x ^= x << 7
|
||||
x ^= x >> 1
|
||||
x ^= x << 9
|
||||
return x
|
||||
}
|
||||
Reference in New Issue
Block a user