! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors alien.c-types kernel locals math math.ranges
-math.bitwise math.vectors math.vectors.simd random
+USING: accessors alien.c-types alien.data kernel locals math
+math.ranges math.bitwise math.vectors math.vectors.simd random
sequences specialized-arrays sequences.private classes.struct
combinators.short-circuit fry ;
+FROM: sequences => change-nth ;
SPECIALIZED-ARRAY: uint
SPECIALIZED-ARRAY: uint-4
IN: random.sfmt
M:: sfmt generate ( sfmt -- )
sfmt state>> :> state
sfmt uint-4-array>> :> array
- state n>> 2 - array nth state (>>r1)
- state n>> 1 - array nth state (>>r2)
+ state n>> 2 - array nth state r1<<
+ state n>> 1 - array nth state r2<<
state m>> :> m
state n>> :> n
state mask>> :> mask
mask state r1>> state r2>> formula :> r
r i array set-nth-unsafe
- state r2>> state (>>r1)
- r state (>>r2)
+ state r2>> state r1<<
+ r state r2<<
] each
! n m - 1 + n [a,b) [
mask state r1>> state r2>> formula :> r
r i array set-nth-unsafe
- state r2>> state (>>r1)
- r state (>>r2)
+ state r2>> state r1<<
+ r state r2<<
] each
- 0 state (>>index) ;
+ 0 state index<< ;
: period-certified? ( sfmt -- ? )
[ uint-4-array>> first ]
: <sfmt-array> ( sfmt -- uint-array uint-4-array )
state>>
- [ n>> 4 * 1 swap [a,b] >uint-array ] [ seed>> ] bi
+ [ n>> 4 * [1,b] uint >c-array ] [ seed>> ] bi
[
[
[ -30 shift ] [ ] bi bitxor
state-multiplier * 32 bits
] dip + 32 bits
] uint-array{ } accumulate-as nip
- dup underlying>> byte-array>uint-4-array ;
+ dup uint-4 cast-array ;
: <sfmt-state> ( seed n m mask parity -- sfmt )
sfmt-state <struct>
: <sfmt-19937> ( seed -- sfmt )
156 122
- uint-4{ HEX: dfffffef HEX: ddfecb7f HEX: bffaffff HEX: bffffff6 }
- uint-4{ HEX: 1 HEX: 0 HEX: 0 HEX: 13c9e684 }
+ uint-4{ 0xdfffffef 0xddfecb7f 0xbffaffff 0xbffffff6 }
+ uint-4{ 0x1 0x0 0x0 0x13c9e684 }
<sfmt> ; inline
: default-sfmt ( -- sfmt )