From 4c28f1b87500adfe44228270d795df86dfea77f6 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 2 Mar 2022 18:14:17 +0100 Subject: [PATCH] runtime: add fastrand This is needed for hash/maphash support. --- src/runtime/algorithm.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/runtime/algorithm.go diff --git a/src/runtime/algorithm.go b/src/runtime/algorithm.go new file mode 100644 index 00000000..8b443d48 --- /dev/null +++ b/src/runtime/algorithm.go @@ -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 +}