-
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
5 changed files
with
110 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
-- SPDX-FileCopyrightText: 2024 Google LLC | ||
-- | ||
-- SPDX-License-Identifier: Apache-2.0 | ||
module VexRiscv.Random where | ||
|
||
import Clash.Prelude | ||
import Clash.Sized.Internal.BitVector | ||
import System.Random | ||
|
||
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@(Bit 0 _) = pure b | ||
makeDefinedRandom _ = do | ||
d <- randomRIO (0, 1) | ||
pure (Bit 1 d) | ||
|
||
instance KnownNat n => DefinedRandom (BitVector n) where | ||
makeDefinedRandom :: KnownNat n => BitVector n -> IO (BitVector n) | ||
makeDefinedRandom (BV mask dat) = do | ||
let | ||
(BV _ maxVal) = (maxBound :: BitVector n) | ||
(BV maxMask _) = deepErrorX "" :: BitVector n | ||
randomInt <- genNatural (0, fromIntegral maxVal) | ||
|
||
pure $ BV 0 ((dat .&. (maxMask `xor` mask)) .|. (randomInt .&. mask)) | ||
|
||
genNatural :: (Natural, Natural) -> IO Natural | ||
genNatural (lo, hi) | ||
| lo > hi = error "genNatural: lower bound > upper bound" | ||
| otherwise = (lo +) <$> go (hi - lo) | ||
where | ||
intMax = fromIntegral (maxBound :: Int) | ||
intBits = finiteBitSize (0 :: Int) | ||
|
||
go :: Natural -> IO Natural | ||
go h | ||
| h <= intMax = fmap fromIntegral (randomRIO (0, fromIntegral h) :: IO Int) | ||
| otherwise = do | ||
x <- fmap fromIntegral (randomRIO (0, maxBound) :: IO Int) | ||
y <- go (shiftR h intBits) | ||
pure (x + shiftL y intBits) |
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,27 @@ | ||
-- SPDX-FileCopyrightText: 2024 Google LLC | ||
-- | ||
-- SPDX-License-Identifier: Apache-2.0 | ||
module Tests.VexRiscv.Random where | ||
|
||
import Clash.Prelude | ||
|
||
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 | ||
] | ||
|
||
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)) |
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