diff --git a/src/CLaSH/Prelude/BlockRam/File.hs b/src/CLaSH/Prelude/BlockRam/File.hs index 746db53..484b2ca 100644 --- a/src/CLaSH/Prelude/BlockRam/File.hs +++ b/src/CLaSH/Prelude/BlockRam/File.hs @@ -98,7 +98,7 @@ import Data.Maybe (listToMaybe) import GHC.TypeLits (KnownNat) import Numeric (readInt) -import CLaSH.Promoted.Nat (SNat (..), pow2SNat, snatToInteger) +import CLaSH.Promoted.Nat (SNat (..), pow2SNat, snatToNum) import CLaSH.Sized.BitVector (BitVector) import CLaSH.Signal (Signal) import CLaSH.Signal.Explicit (Signal', SClock, register', systemClock) @@ -285,7 +285,7 @@ blockRamFile# :: KnownNat m -- clock cycle blockRamFile# clk sz file wr rd en din = register' clk (errorX "blockRamFile#: intial value undefined") dout where - szI = fromInteger $ snatToInteger sz + szI = snatToNum sz dout = runST $ do mem <- unsafeIOToST (initMem file) arr <- newListArray (0,szI-1) mem diff --git a/src/CLaSH/Prelude/RAM.hs b/src/CLaSH/Prelude/RAM.hs index bdf7482..ef7a222 100644 --- a/src/CLaSH/Prelude/RAM.hs +++ b/src/CLaSH/Prelude/RAM.hs @@ -43,7 +43,7 @@ import Data.Array.MArray.Safe (newListArray,readArray,writeArray) import Data.Array.ST.Safe (STArray) import GHC.TypeLits (KnownNat) -import CLaSH.Promoted.Nat (SNat (..), snatToInteger, pow2SNat) +import CLaSH.Promoted.Nat (SNat (..), snatToNum, pow2SNat) import CLaSH.Signal (Signal) import CLaSH.Signal.Bundle (bundle) import CLaSH.Signal.Explicit (Signal', SClock, systemClock, unsafeSynchronizer) @@ -146,7 +146,7 @@ asyncRam# :: SClock wclk -- ^ 'Clock' to which to synchronise the write -> Signal' rclk a -- ^ Value of the @RAM@ at address @r@ asyncRam# wclk rclk sz wr rd en din = unsafeSynchronizer wclk rclk dout where - szI = fromInteger $ snatToInteger sz + szI = snatToNum sz rd' = unsafeSynchronizer rclk wclk rd dout = runST $ do arr <- newListArray (0,szI-1) (replicate szI (errorX "asyncRam#: initial value undefined")) diff --git a/src/CLaSH/Prelude/ROM/File.hs b/src/CLaSH/Prelude/ROM/File.hs index d18a289..a690b1b 100644 --- a/src/CLaSH/Prelude/ROM/File.hs +++ b/src/CLaSH/Prelude/ROM/File.hs @@ -93,7 +93,7 @@ import GHC.TypeLits (KnownNat) import System.IO.Unsafe (unsafePerformIO) import CLaSH.Prelude.BlockRam.File (initMem) -import CLaSH.Promoted.Nat (SNat (..), pow2SNat, snatToInteger) +import CLaSH.Promoted.Nat (SNat (..), pow2SNat, snatToNum) import CLaSH.Sized.BitVector (BitVector) import CLaSH.Signal (Signal) import CLaSH.Signal.Explicit (Signal', SClock, register', systemClock) @@ -166,7 +166,7 @@ asyncRomFile sz file = asyncRomFile# sz file . fromEnum -- > where -- > mem = unsafePerformIO (initMem file) -- > content = listArray (0,szI-1) mem --- > szI = fromInteger (snatToInteger sz) +-- > szI = snatToNum sz -- -- We write: -- @@ -174,7 +174,7 @@ asyncRomFile sz file = asyncRomFile# sz file . fromEnum -- > where -- > mem = unsafePerformIO (initMem file) -- > content = listArray (0,szI-1) mem --- > szI = fromInteger (snatToInteger sz) +-- > szI = snatToNum sz -- -- Where instead of returning the BitVector defined by @(content ! rd)@, we -- return the function (thunk) @(content !)@. @@ -232,7 +232,7 @@ asyncRomFile# sz file = (content !) -- Leave "(content !)" eta-reduced, see where -- Note [Eta-reduction and unsafePerformIO initMem] mem = unsafePerformIO (initMem file) content = listArray (0,szI-1) mem - szI = fromInteger (snatToInteger sz) + szI = snatToNum sz {-# INLINE romFile #-} -- | A ROM with a synchronous read port, with space for @n@ elements @@ -379,4 +379,4 @@ romFile# clk sz file rd = register' clk (errorX "romFile#: initial value undefin where mem = unsafePerformIO (initMem file) content = listArray (0,szI-1) mem - szI = fromInteger (snatToInteger sz) + szI = snatToNum sz diff --git a/src/CLaSH/Promoted/Nat.hs b/src/CLaSH/Promoted/Nat.hs index cf395a9..4f3bf66 100644 --- a/src/CLaSH/Promoted/Nat.hs +++ b/src/CLaSH/Promoted/Nat.hs @@ -24,7 +24,7 @@ module CLaSH.Promoted.Nat , withSNat , snat -- ** Conversion - , snatToInteger + , snatToInteger, snatToNum -- ** Arithmetic , addSNat, mulSNat, powSNat -- *** Partial @@ -104,6 +104,11 @@ withSNat f = f SNat snatToInteger :: SNat n -> Integer snatToInteger p@SNat = natVal p +-- | Reify the type-level 'Nat' @n@ to it's term-level 'Num'ber. +snatToNum :: Num a => SNat n -> a +snatToNum p@SNat = fromInteger (natVal p) +{-# INLINE snatToNum #-} + -- | Unary representation of a type-level natural -- -- __NB__: Not synthesisable diff --git a/src/CLaSH/Signal/Explicit.hs b/src/CLaSH/Signal/Explicit.hs index 7ff553b..047e7e5 100644 --- a/src/CLaSH/Signal/Explicit.hs +++ b/src/CLaSH/Signal/Explicit.hs @@ -37,7 +37,7 @@ where import Data.Maybe (isJust, fromJust) import GHC.TypeLits (KnownNat, KnownSymbol) -import CLaSH.Promoted.Nat (SNat (..), snatToInteger) +import CLaSH.Promoted.Nat (SNat (..), snatToNum) import CLaSH.Promoted.Symbol (SSymbol (..)) import CLaSH.Signal.Internal (Signal' (..), Clock (..), SClock (..), register#, regEn#) @@ -243,8 +243,8 @@ unsafeSynchronizer :: SClock clk1 -- ^ 'Clock' of the incoming signal -> Signal' clk2 a unsafeSynchronizer (SClock _ period1) (SClock _ period2) s = s' where - t1 = fromInteger (snatToInteger period1) - t2 = fromInteger (snatToInteger period2) + t1 = snatToNum period1 + t2 = snatToNum period2 s' | t1 < t2 = compress t2 t1 s | t1 > t2 = oversample t1 t2 s | otherwise = same s diff --git a/src/CLaSH/Sized/Internal/BitVector.hs b/src/CLaSH/Sized/Internal/BitVector.hs index 77a4500..3285cf8 100644 --- a/src/CLaSH/Sized/Internal/BitVector.hs +++ b/src/CLaSH/Sized/Internal/BitVector.hs @@ -122,7 +122,7 @@ import Test.QuickCheck.Arbitrary (Arbitrary (..), CoArbitrary (..), import CLaSH.Class.Num (ExtendingNum (..), SaturatingNum (..), SaturationMode (..)) import CLaSH.Class.Resize (Resize (..)) -import CLaSH.Promoted.Nat (SNat, snatToInteger) +import CLaSH.Promoted.Nat (SNat, snatToInteger, snatToNum) import CLaSH.XException (ShowX (..), showsPrecXWith) import {-# SOURCE #-} qualified CLaSH.Sized.Vector as V @@ -452,7 +452,7 @@ slice# :: BitVector (m + 1 + i) -> SNat m -> SNat n -> BitVector (m + 1 - n) slice# (BV i) m n = BV (shiftR (i .&. mask) n') where m' = snatToInteger m - n' = fromInteger (snatToInteger n) + n' = snatToNum n mask = 2 ^ (m' + 1) - 1 diff --git a/src/CLaSH/Sized/RTree.hs b/src/CLaSH/Sized/RTree.hs index 1311ff8..25c8f23 100644 --- a/src/CLaSH/Sized/RTree.hs +++ b/src/CLaSH/Sized/RTree.hs @@ -68,7 +68,7 @@ import Prelude hiding ((++), (!!)) import Test.QuickCheck (Arbitrary (..), CoArbitrary (..)) import CLaSH.Class.BitPack (BitPack (..)) -import CLaSH.Promoted.Nat (SNat (..), UNat (..), pow2SNat, snatToInteger, +import CLaSH.Promoted.Nat (SNat (..), UNat (..), pow2SNat, snatToNum, subSNat, toUNat) import CLaSH.Promoted.Nat.Literals (d1) import CLaSH.Sized.Index (Index) @@ -371,7 +371,7 @@ tmap f = tdfold (Proxy @(MapTree b)) (LR . f) (\_ l r -> BR l r) tindices :: forall d . KnownNat d => RTree d (Index (2^d)) tindices = tdfold (Proxy @(MapTree (Index (2^d)))) LR - (\s@SNat l r -> BR l (tmap (+(fromInteger (snatToInteger (pow2SNat s)))) r)) + (\s@SNat l r -> BR l (tmap (+(snatToNum (pow2SNat s))) r)) (treplicate SNat 0) data V2TTree (a :: *) (f :: TyFun Nat *) :: *