Skip to content

Commit 4c28f1b

Browse files
aykevldeadprogram
authored andcommitted
runtime: add fastrand
This is needed for hash/maphash support.
1 parent 0791603 commit 4c28f1b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/runtime/algorithm.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package runtime
2+
3+
// This file implements various core algorithms used in the runtime package and
4+
// standard library.
5+
6+
// This function is used by hash/maphash.
7+
func fastrand() uint32 {
8+
xorshift32State = xorshift32(xorshift32State)
9+
return xorshift32State
10+
}
11+
12+
var xorshift32State uint32 = 1
13+
14+
func xorshift32(x uint32) uint32 {
15+
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
16+
// Improved sequence based on
17+
// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf
18+
x ^= x << 7
19+
x ^= x >> 1
20+
x ^= x << 9
21+
return x
22+
}

0 commit comments

Comments
 (0)