Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousleo committed Apr 2, 2020
1 parent 9cca62a commit eea2cd2
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions System/Random.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
Expand All @@ -9,7 +8,6 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GHCForeignImportPrim #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UnliftedFFITypes #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
Expand Down Expand Up @@ -195,7 +193,6 @@ import Foreign.Ptr (plusPtr)
import Foreign.Storable (peekByteOff, pokeByteOff)
import GHC.Exts
import GHC.ForeignPtr
-- import GHC.Integer.Logarithms (integerLog2#)
import System.IO.Unsafe (unsafePerformIO)
import qualified System.Random.SplitMix as SM
import GHC.Word
Expand Down Expand Up @@ -1108,16 +1105,16 @@ uniformIntegerM (l, h) gen
boundedIntegerM :: (MonadRandom g m) => Integer -> g -> m Integer
boundedIntegerM s gen = go
where
words = integerWordSize s
n = integerWordSize s
-- We renamed 'L' from the paper to 'k' here because 'L' is not a valid
-- variable name in Haskell and 'l' is already used in the algorithm.
k = WORD_SIZE_IN_BITS * words
k = WORD_SIZE_IN_BITS * n
twoToK = (1::Integer) `shiftL` k
modTwoToKMask = twoToK - 1

t = (twoToK - s) `mod` s
go = do
x <- uniformIntegerWords words gen
x <- uniformIntegerWords n gen
let m = x * s
-- m .&. modTwoToKMask == m `mod` twoToK
let l = m .&. modTwoToKMask
Expand All @@ -1129,28 +1126,22 @@ boundedIntegerM s gen = go
-- | @integerWordSize i@ returns that least @w@ such that
-- @i <= WORD_SIZE_IN_BITS^w@.
integerWordSize :: Integer -> Int
integerWordSize x = go x 0
integerWordSize = go 0
where
go i acc
go acc i
| i == 0 = acc
| otherwise = go (i `shiftR` WORD_SIZE_IN_BITS) (acc + 1)
{-
integerWordSize :: Integer -> Int
integerWordSize i = I# (q# +# (r# ># 0#))
where
!(# q#, r# #) = integerLog2# i `quotRemInt#` WORD_SIZE_IN_BITS#
-}
| otherwise = go (acc + 1) (i `shiftR` WORD_SIZE_IN_BITS)

-- | @uniformIntegerWords w@ is a uniformly random 'Integer' in the range
-- @[0, WORD_SIZE_IN_BITS^w)@.
-- | @uniformIntegerWords n@ is a uniformly random 'Integer' in the range
-- @[0, WORD_SIZE_IN_BITS^n)@.
uniformIntegerWords :: (MonadRandom g m) => Int -> g -> m Integer
uniformIntegerWords words gen = go 0 0
uniformIntegerWords n gen = go 0 n
where
go i acc
| i == words = return acc
go acc i
| i == 0 = return acc
| otherwise = do
(w :: Word) <- uniform gen
go (i + 1) ((acc `shiftL` WORD_SIZE_IN_BITS) .|. (fromIntegral w))
go ((acc `shiftL` WORD_SIZE_IN_BITS) .|. (fromIntegral w)) (i - 1)

-- | This only works for unsigned integrals
unsignedBitmaskWithRejectionRM ::
Expand Down

0 comments on commit eea2cd2

Please sign in to comment.