Skip to content

Instantly share code, notes, and snippets.

@igrep
Last active July 23, 2019 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrep/77b28131ff2d32fffc709d0723a0930b to your computer and use it in GitHub Desktop.
Save igrep/77b28131ff2d32fffc709d0723a0930b to your computer and use it in GitHub Desktop.
import Data.Bits
import Data.Word
import Data.STRef
import Control.Monad.ST
import Data.Function
import Test.Hspec
import Test.Hspec.QuickCheck
main :: IO ()
main = hspec $ do
prop "tempering32Modified behaves like tempering32" $ \wd ->
tempering32Modified wd `shouldBe` tempering32 wd
prop "tempering32Modified2 behaves like tempering32" $ \wd ->
tempering32Modified2 wd `shouldBe` tempering32 wd
tempering32 :: Word32 -> Word32
tempering32 x = runST $ do res <- newSTRef (x :: Word32)
res' <- readSTRef res
modifySTRef res (xor (res' `shiftR` 11))
res' <- readSTRef res
modifySTRef res (xor ((res' `shiftL` 7) .&. (0x9d2c5680 :: Word32)))
res' <- readSTRef res
modifySTRef res (xor ((res' `shiftL` 15) .&. (0xefc60000 :: Word32)))
res' <- readSTRef res
modifySTRef res (xor (res' `shiftR` 18))
readSTRef res
tempering32Modified :: Word32 -> Word32
tempering32Modified x = runST $ do
res <- newSTRef (x :: Word32)
modifySTRef res $ \v -> xor (v `shiftR` 11) v
modifySTRef res $ \v -> xor ((v `shiftL` 7) .&. (0x9d2c5680 :: Word32)) v
modifySTRef res $ \v -> xor ((v `shiftL` 15) .&. (0xefc60000 :: Word32)) v
modifySTRef res $ \v -> xor (v `shiftR` 18) v
readSTRef res
tempering32Modified2 :: Word32 -> Word32
tempering32Modified2 x =
x
& (\v -> xor (v `shiftR` 11) v)
& (\v -> xor ((v `shiftL` 7) .&. (0x9d2c5680 :: Word32)) v)
& (\v -> xor ((v `shiftL` 15) .&. (0xefc60000 :: Word32)) v)
& (\v -> xor (v `shiftR` 18) v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment