mirror of https://github.com/tinygo-org/tinygo.git
wasmstm32webassemblymicrocontrollerarmavrspiwasiadafruitarduinocircuitplayground-expressgpioi2cllvmmicrobitnrf51nrf52nrf52840samd21tinygo
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.3 KiB
60 lines
1.3 KiB
6 years ago
|
package main
|
||
|
|
||
|
var xorshift32State uint32 = 1
|
||
|
|
||
|
func xorshift32(x uint32) uint32 {
|
||
|
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
|
||
|
x ^= x << 13
|
||
|
x ^= x >> 17
|
||
|
x ^= x << 5
|
||
|
return x
|
||
|
}
|
||
|
|
||
|
func randuint32() uint32 {
|
||
|
xorshift32State = xorshift32(xorshift32State)
|
||
|
return xorshift32State
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
testNonPointerHeap()
|
||
|
}
|
||
|
|
||
|
var scalarSlices [4][]byte
|
||
|
var randSeeds [4]uint32
|
||
|
|
||
|
func testNonPointerHeap() {
|
||
|
// Allocate roughly 0.5MB of memory.
|
||
|
for i := 0; i < 1000; i++ {
|
||
|
// Pick a random index that the optimizer can't predict.
|
||
|
index := randuint32() % 4
|
||
|
|
||
|
// Check whether the contents of the previous allocation was correct.
|
||
|
rand := randSeeds[index]
|
||
|
for _, b := range scalarSlices[index] {
|
||
|
rand = xorshift32(rand)
|
||
|
if b != byte(rand) {
|
||
|
panic("memory was overwritten!")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Allocate a randomly-sized slice, randomly sliced to be smaller.
|
||
|
sliceLen := randuint32() % 1024
|
||
|
slice := make([]byte, sliceLen)
|
||
|
cutLen := randuint32() % 1024
|
||
|
if cutLen < sliceLen {
|
||
|
slice = slice[cutLen:]
|
||
|
}
|
||
|
scalarSlices[index] = slice
|
||
|
|
||
|
// Fill the slice with a pattern that looks random but is easily
|
||
|
// calculated and verified.
|
||
|
rand = randuint32() + 1
|
||
|
randSeeds[index] = rand
|
||
|
for i := 0; i < len(slice); i++ {
|
||
|
rand = xorshift32(rand)
|
||
|
slice[i] = byte(rand)
|
||
|
}
|
||
|
}
|
||
|
println("ok")
|
||
|
}
|