{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds   #-}
module Benchmark.CSPRG where

import Control.Monad
import GHC.TypeLits

import Raaz.Core
import Benchmark.Types
import Buffer
import Implementation


-- | Number of blocks.
nblocks :: BlockCount Prim
nblocks :: BlockCount Prim
nblocks = forall src dest. (LengthUnit src, LengthUnit dest) => src -> dest
atLeast BYTES Int
nBytes

allocAndRun  :: (BufferPtr -> IO ()) -> IO ()
allocAndRun :: (BufferPtr -> IO ()) -> IO ()
allocAndRun  = forall l (ptr :: * -> *) something b.
(LengthUnit l, Pointer ptr) =>
l -> (ptr something -> IO b) -> IO b
allocaBuffer (BlockCount Prim
nblocks forall a. Semigroup a => a -> a -> a
<> BlockCount Prim
additionalBlocks)

bench :: KnownNat BufferAlignment => RaazBench
bench :: KnownNat BufferAlignment => RaazBench
bench = (String
name, (Int64 -> IO ()) -> Benchmarkable
toBenchmarkable forall a b. (a -> b) -> a -> b
$ Int -> IO ()
action forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral)
  where action :: Int -> IO ()
action Int
count = (BufferPtr -> IO ()) -> IO ()
allocAndRun forall a b. (a -> b) -> a -> b
$ Int -> BufferPtr -> IO ()
doit Int
count
        doit :: Int -> BufferPtr -> IO ()
doit Int
count BufferPtr
ptr = forall mem a. Memory mem => (mem -> IO a) -> IO a
withMemory forall a b. (a -> b) -> a -> b
$ \ Internals
mem -> forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ Int
count (BufferPtr -> BlockCount Prim -> Internals -> IO ()
randomBlocks BufferPtr
ptr BlockCount Prim
nblocks Internals
mem)