{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
#if __GLASGOW_HASKELL__ >= 707
{-# LANGUAGE RoleAnnotations #-}
#endif
module System.Random.PCG.Single
(
Gen, GenIO, GenST
, create, createSystemRandom, initialize
, withSystemRandom, withFrozen
, Variate (..)
, advance, retract
, FrozenGen
, save, restore, seed, initFrozen
, uniformW8, uniformW16, uniformW32, uniformW64
, uniformI8, uniformI16, uniformI32, uniformI64
, uniformF, uniformD, uniformBool
, uniformRW8, uniformRW16, uniformRW32, uniformRW64
, uniformRI8, uniformRI16, uniformRI32, uniformRI64
, uniformRF, uniformRD, uniformRBool
, uniformBW8, uniformBW16, uniformBW32, uniformBW64
, uniformBI8, uniformBI16, uniformBI32, uniformBI64
, uniformBF, uniformBD, uniformBBool
) where
import Control.Applicative
import Control.Monad.Primitive
import Control.Monad.ST
import Data.Data
import Foreign
import GHC.Generics
import System.IO.Unsafe
import System.Random
import System.Random.PCG.Class
newtype FrozenGen = FrozenGen Word64
deriving (Show, Eq, Ord, Storable, Data, Typeable, Generic)
save :: PrimMonad m => Gen (PrimState m) -> m FrozenGen
save (Gen p) = unsafePrimToPrim (peek p)
{-# INLINE save #-}
restore :: PrimMonad m => FrozenGen -> m (Gen (PrimState m))
restore s = unsafePrimToPrim $ do
p <- malloc
poke p s
return (Gen p)
{-# INLINE restore #-}
initFrozen :: Word64 -> FrozenGen
initFrozen w = unsafeDupablePerformIO . alloca $ \p ->
pcg32s_srandom_r p w >> peek p
{-# INLINE initFrozen #-}
seed :: FrozenGen
seed = FrozenGen 0x4d595df4d0f33173
create :: PrimMonad m => m (Gen (PrimState m))
create = restore seed
newtype Gen s = Gen (Ptr FrozenGen)
deriving (Eq, Ord)
#if __GLASGOW_HASKELL__ >= 707
type role Gen representational
#endif
type GenIO = Gen RealWorld
type GenST = Gen
initialize :: PrimMonad m => Word64 -> m (Gen (PrimState m))
initialize a = unsafePrimToPrim $ do
p <- malloc
pcg32s_srandom_r p a
return (Gen p)
withSystemRandom :: (GenIO -> IO a) -> IO a
withSystemRandom f = sysRandom >>= initialize >>= f
withFrozen :: FrozenGen -> (forall s. Gen s -> ST s a) -> (a, FrozenGen)
withFrozen s f = runST $ restore s >>= \g -> liftA2 (,) (f g) (save g)
createSystemRandom :: IO GenIO
createSystemRandom = withSystemRandom return
advance :: PrimMonad m => Word64 -> Gen (PrimState m) -> m ()
advance u (Gen p) = unsafePrimToPrim $ pcg32s_advance_r p u
{-# INLINE advance #-}
retract :: PrimMonad m => Word64 -> Gen (PrimState m) -> m ()
retract u g = advance (-u) g
{-# INLINE retract #-}
foreign import ccall unsafe "pcg_oneseq_64_srandom_r"
pcg32s_srandom_r :: Ptr FrozenGen -> Word64 -> IO ()
foreign import ccall unsafe "pcg_oneseq_64_xsh_rs_32_random_r"
pcg32s_random_r :: Ptr FrozenGen -> IO Word32
foreign import ccall unsafe "pcg_oneseq_64_xsh_rs_32_boundedrand_r"
pcg32s_boundedrand_r :: Ptr FrozenGen -> Word32 -> IO Word32
foreign import ccall unsafe "pcg_oneseq_64_advance_r"
pcg32s_advance_r :: Ptr FrozenGen -> Word64 -> IO ()
instance (PrimMonad m, s ~ PrimState m) => Generator (Gen s) m where
uniform1 f (Gen p) = unsafePrimToPrim $
f <$> pcg32s_random_r p
{-# INLINE uniform1 #-}
uniform2 f (Gen p) = unsafePrimToPrim $
f <$> pcg32s_random_r p <*> pcg32s_random_r p
{-# INLINE uniform2 #-}
uniform1B f b (Gen p) = unsafePrimToPrim $ f <$> pcg32s_boundedrand_r p b
{-# INLINE uniform1B #-}
instance RandomGen FrozenGen where
next s = unsafeDupablePerformIO $ do
p <- malloc
poke p s
w1 <- pcg32s_random_r p
w2 <- pcg32s_random_r p
s' <- peek p
free p
return (wordsTo64Bit w1 w2, s')
{-# INLINE next #-}
split s = unsafeDupablePerformIO $ do
p <- malloc
poke p s
w1 <- pcg32s_random_r p
w2 <- pcg32s_random_r p
w3 <- pcg32s_random_r p
w4 <- pcg32s_random_r p
pcg32s_srandom_r p (wordsTo64Bit w1 w2)
s1 <- peek p
pcg32s_srandom_r p (wordsTo64Bit w3 w4)
s2 <- peek p
free p
return (s1,s2)