module System.Random.SplitMix (
SMGen,
nextWord64,
nextInt,
nextDouble,
splitSMGen,
mkSMGen,
initSMGen,
newSMGen,
seedSMGen,
seedSMGen',
unseedSMGen,
) where
import Data.Bits (popCount, shiftL, shiftR, xor, (.|.))
import Data.IORef (IORef, atomicModifyIORef, newIORef)
import Data.Time.Clock.POSIX (getPOSIXTime)
import Data.Word (Word64, Word32)
import System.CPUTime (getCPUTime, cpuTimePrecision)
import System.IO.Unsafe (unsafePerformIO)
import qualified System.Random as R
data SMGen = SMGen
{ _seed :: !Word64
, _gamma :: !Word64
}
deriving Show
nextWord64 :: SMGen -> (Word64, SMGen)
nextWord64 (SMGen seed gamma) = (mix64 seed', SMGen seed' gamma)
where
seed' = seed + gamma
nextInt :: SMGen -> (Int, SMGen)
nextInt g = case nextWord64 g of
(w64, g') -> (fromIntegral w64, g')
nextDouble :: SMGen -> (Double, SMGen)
nextDouble g = case nextWord64 g of
(w64, g') -> (fromIntegral (w64 `shiftR` 11) * doubleUlp, g')
splitSMGen :: SMGen -> (SMGen, SMGen)
splitSMGen (SMGen seed gamma) =
(SMGen seed'' gamma, SMGen (mix64 seed') (mixGamma seed''))
where
seed' = seed + gamma
seed'' = seed' + gamma
goldenGamma :: Word64
goldenGamma = 0x9e3779b97f4a7c15
doubleUlp :: Double
doubleUlp = 1.0 / fromIntegral (1 `shiftL` 53 :: Word64)
mix64 :: Word64 -> Word64
mix64 z0 =
let z1 = shiftXorMultiply 33 0xc4ceb9fe1a85ec53 z0
z2 = shiftXorMultiply 33 0xff51afd7ed558ccd z1
z3 = shiftXor 33 z2
in z3
mix64variant13 :: Word64 -> Word64
mix64variant13 z0 =
let z1 = shiftXorMultiply 30 0xbf58476d1ce4e5b9 z0
z2 = shiftXorMultiply 27 0x94d049bb133111eb z1
z3 = shiftXor 31 z2
in z3
mixGamma :: Word64 -> Word64
mixGamma z0 =
let z1 = mix64variant13 z0 .|. 1
n = popCount (z1 `xor` (z1 `shiftR` 1))
in if n >= 24
then z1 `xor` 0xaaaaaaaaaaaaaaaa
else z1
shiftXor :: Int -> Word64 -> Word64
shiftXor n w = w `xor` (w `shiftR` n)
shiftXorMultiply :: Int -> Word64 -> Word64 -> Word64
shiftXorMultiply n k w = shiftXor n w * k
seedSMGen
:: Word64
-> Word64
-> SMGen
seedSMGen seed gamma = SMGen seed (gamma .|. 1)
seedSMGen' :: (Word64, Word64) -> SMGen
seedSMGen' = uncurry seedSMGen
unseedSMGen :: SMGen -> (Word64, Word64)
unseedSMGen (SMGen seed gamma) = (seed, gamma)
mkSMGen :: Word64 -> SMGen
mkSMGen s = SMGen (mix64 s) (mixGamma (s + goldenGamma))
initSMGen :: IO SMGen
initSMGen = fmap mkSMGen mkSeedTime
newSMGen :: IO SMGen
newSMGen = atomicModifyIORef theSMGen splitSMGen
theSMGen :: IORef SMGen
theSMGen = unsafePerformIO $ initSMGen >>= newIORef
mkSeedTime :: IO Word64
mkSeedTime = do
now <- getPOSIXTime
cpu <- getCPUTime
let lo = truncate now :: Word32
hi = fromIntegral (cpu `div` cpuTimePrecision) :: Word32
return $ fromIntegral hi `shiftL` 32 .|. fromIntegral lo
instance R.RandomGen SMGen where
next = nextInt
split = splitSMGen