make test green

This commit is contained in:
Andreas Rumpf 2016-05-30 17:40:06 +02:00
commit 3cedf3e887
2 changed files with 40 additions and 22 deletions

View file

@ -12,21 +12,30 @@
## | http://xoroshiro.di.unimi.it/xoroshiro128plus.c ## | http://xoroshiro.di.unimi.it/xoroshiro128plus.c
include "system/inclrtl" include "system/inclrtl"
{.push debugger:off .} # the user does not want to trace a part {.push debugger:off.}
# of the standard library!
# XXX Expose RandomGenState # XXX Expose RandomGenState
when defined(JS):
type ui = uint32
else:
type ui = uint64
type type
RandomGenState = object RandomGenState = object
a0, a1: uint64 a0, a1: ui
when defined(JS):
var state = RandomGenState(
a0: 0x69B4C98Cu32,
a1: 0xFED1DD30u32) # global for backwards compatibility
else:
# racy for multi-threading but good enough for now: # racy for multi-threading but good enough for now:
var state = RandomGenState( var state = RandomGenState(
a0: 0x69B4C98CB8530805u64, a0: 0x69B4C98CB8530805u64,
a1: 0xFED1DD3004688D67CAu64) # global for backwards compatibility a1: 0xFED1DD3004688D67CAu64) # global for backwards compatibility
proc rotl(x: uint64, k: uint64): uint64 = proc rotl(x, k: ui): ui =
result = (x shl k) or (x shr (64u64 - k)) result = (x shl k) or (x shr (ui(64) - k))
proc next(s: var RandomGenState): uint64 = proc next(s: var RandomGenState): uint64 =
let s0 = s.a0 let s0 = s.a0
@ -40,13 +49,16 @@ proc skipRandomNumbers(s: var RandomGenState) =
## This is the jump function for the generator. It is equivalent ## This is the jump function for the generator. It is equivalent
## to 2^64 calls to next(); it can be used to generate 2^64 ## to 2^64 calls to next(); it can be used to generate 2^64
## non-overlapping subsequences for parallel computations. ## non-overlapping subsequences for parallel computations.
when defined(JS):
const helper = [0xbeac0467u32, 0xd86b048bu32]
else:
const helper = [0xbeac0467eba5facbu64, 0xd86b048b86aa9922u64] const helper = [0xbeac0467eba5facbu64, 0xd86b048b86aa9922u64]
var var
s0 = 0u64 s0 = ui 0
s1 = 0u64 s1 = ui 0
for i in 0..high(helper): for i in 0..high(helper):
for b in 0..< 64: for b in 0..< 64:
if (helper[i] and (1u64 shl uint64(b))) != 0: if (helper[i] and (ui(1) shl ui(b))) != 0:
s0 = s0 xor s.a0 s0 = s0 xor s.a0
s1 = s1 xor s.a1 s1 = s1 xor s.a1
discard next(s) discard next(s)
@ -66,6 +78,9 @@ proc random*(max: float): float {.benign.} =
## which initializes the random number generator with a "random" ## which initializes the random number generator with a "random"
## number, i.e. a tickcount. ## number, i.e. a tickcount.
let x = next(state) let x = next(state)
when defined(JS):
result = (float(x) / float(high(uint32))) * max
else:
let u = (0x3FFu64 shl 52u64) or (x shr 12u64) let u = (0x3FFu64 shl 52u64) or (x shr 12u64)
result = (cast[float](u) - 1.0) * max result = (cast[float](u) - 1.0) * max
@ -79,16 +94,19 @@ proc random*[T](a: openArray[T]): T =
proc randomize*(seed: int) {.benign.} = proc randomize*(seed: int) {.benign.} =
## Initializes the random number generator with a specific seed. ## Initializes the random number generator with a specific seed.
state.a0 = uint64(seed shr 16) state.a0 = ui(seed shr 16)
state.a1 = uint64(seed and 0xffff) state.a1 = ui(seed and 0xffff)
when not defined(nimscript): when not defined(nimscript):
import times import times
proc randomize*() {.benign.} = proc randomize*() {.benign.} =
## Initializes the random number generator with a "random" ## Initializes the random number generator with a "random"
## number, i.e. a tickcount. Note: Does nothing for the JavaScript target, ## number, i.e. a tickcount. Note: Does not work for NimScript.
## as JavaScript does not support this. Nor does it work for NimScript. when defined(JS):
proc getMil(t: Time): int {.importcpp: "getTime", nodecl.}
randomize(getMil times.getTime())
else:
randomize(int times.getTime()) randomize(int times.getTime())
{.pop.} {.pop.}

View file

@ -1,5 +1,5 @@
import nake import nake
import httpclient, zip/zipfiles, times, math, sequtils import httpclient, zip/zipfiles, times, random, sequtils
nakeImports nakeImports
randomize() randomize()
@ -145,7 +145,7 @@ task "download", "download game assets":
echo "Extracted the libs dir. Copy the ones you need to this dir." echo "Extracted the libs dir. Copy the ones you need to this dir."
task "zip-lib", "zip up the libs dir": task "zip-lib", "zip up the libs dir":
var z: TZipArchive var z: ZipArchive
if not z.open("libs-" & getDateStr() & ".zip", fmReadWrite): if not z.open("libs-" & getDateStr() & ".zip", fmReadWrite):
quit "Could not open zip" quit "Could not open zip"
for file in walkDirRec("libs", {pcFile, pcDir}): for file in walkDirRec("libs", {pcFile, pcDir}):