-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add randomly generated values for undefined bits in FFI
- Loading branch information
Showing
6 changed files
with
147 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
-- SPDX-FileCopyrightText: 2024 Google LLC | ||
-- | ||
-- SPDX-License-Identifier: Apache-2.0 | ||
{-# LANGUAGE CPP #-} | ||
|
||
module VexRiscv.Random where | ||
|
||
import Clash.Prelude | ||
|
||
#if __GLASGOW_HASKELL__ <= 902 | ||
import Numeric.Natural | ||
#endif | ||
|
||
import Clash.Sized.Internal.BitVector | ||
import Data.Bifunctor (bimap) | ||
import GHC.IO (unsafePerformIO) | ||
import System.Random | ||
|
||
-- | Unsafe version of 'makeDefinedRandom' with a NOINLINE pragma to avoid | ||
-- compiler optimizations. | ||
unsafeMakeDefinedRandom :: DefinedRandom a => a -> a | ||
unsafeMakeDefinedRandom = unsafePerformIO . makeDefinedRandom | ||
{-# NOINLINE unsafeMakeDefinedRandom #-} | ||
|
||
-- | A class for types that can be (partially) undefined whose undefined values | ||
-- can be replaced with defined random values. | ||
class DefinedRandom a where | ||
makeDefinedRandom :: a -> IO a | ||
|
||
instance DefinedRandom Bool where | ||
makeDefinedRandom b | ||
| hasUndefined b = randomIO | ||
| otherwise = pure b | ||
|
||
instance DefinedRandom Bit where | ||
makeDefinedRandom b | ||
| hasUndefined b = Bit 0 <$> randomRIO (0, 1) | ||
| otherwise = pure b | ||
|
||
instance KnownNat n => DefinedRandom (BitVector n) where | ||
makeDefinedRandom :: KnownNat n => BitVector n -> IO (BitVector n) | ||
makeDefinedRandom (ensureSpine -> BV mask dat) = do | ||
let maxVal = natToNum @(2^n - 1) | ||
randomInt <- genNatural (0, fromIntegral maxVal) | ||
pure $ BV 0 (((maxVal `xor` mask) .&. dat) .|. (mask .&. randomInt)) | ||
|
||
-- | Generate a random natural number in the given inclusive range. | ||
genNatural :: (Natural, Natural) -> IO Natural | ||
genNatural = fmap fromInteger . randomRIO . bimap toInteger toInteger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-- SPDX-FileCopyrightText: 2024 Google LLC | ||
-- | ||
-- SPDX-License-Identifier: Apache-2.0 | ||
module Tests.VexRiscv.Random where | ||
|
||
import Clash.Prelude | ||
|
||
import Clash.Hedgehog.Sized.BitVector | ||
import Hedgehog | ||
import Test.Tasty | ||
import Test.Tasty.Hedgehog | ||
import VexRiscv.Random | ||
|
||
import qualified Hedgehog.Gen as Gen | ||
import qualified Hedgehog.Range as Range | ||
|
||
tests :: TestTree | ||
tests = testGroup "VexRiscv.Random" | ||
[ testProperty "genNatural" prop_genNatural | ||
, testProperty "makeDefinedRandomBitVector" prop_makeDefinedRandomBitVector | ||
, testProperty "makeDefinedRandomBit" prop_makeDefinedRandomBit | ||
] | ||
|
||
prop_genNatural :: Property | ||
prop_genNatural = property $ do | ||
lo <- forAll $ Gen.integral (Range.linear 0 (shiftL 1 1024)) | ||
hi <- forAll $ Gen.integral (Range.linear lo (shiftL 1 1024)) | ||
n <- evalIO $ genNatural (lo, hi) | ||
assert ((n >= lo) && (n <= hi)) | ||
|
||
prop_makeDefinedRandomBitVector :: Property | ||
prop_makeDefinedRandomBitVector = property $ do | ||
someBv <- forAll $ (genSomeBitVector @_ @0) (Range.linear 0 1024) genBitVector | ||
case someBv of | ||
SomeBitVector SNat bv -> do | ||
definedBv <- evalIO $ makeDefinedRandom bv | ||
assert (not $ hasUndefined definedBv) | ||
assert (definedBv <= maxBound) | ||
|
||
prop_makeDefinedRandomBit :: Property | ||
prop_makeDefinedRandomBit = property $ do | ||
b <- forAll $ genBit | ||
definedB <- evalIO $ makeDefinedRandom b | ||
assert (not $ hasUndefined definedB) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters