Copyright | (C) 2013-2016 University of Twente 2016-2017 Myrtle Software Ltd |
---|---|
License | BSD2 (see the file LICENSE) |
Maintainer | Christiaan Baaij <christiaan.baaij@gmail.com> |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
Extensions |
|
Synopsis
- class KnownNat (BitSize a) => BitPack a where
- bitCoerce :: (BitPack a, BitPack b, BitSize a ~ BitSize b) => a -> b
- bitCoerceMap :: forall a b. (BitPack a, BitPack b, BitSize a ~ BitSize b) => (a -> a) -> b -> b
- boolToBV :: KnownNat n => Bool -> BitVector (n + 1)
- boolToBit :: Bool -> Bit
- bitToBool :: Bit -> Bool
- packXWith :: KnownNat n => (a -> BitVector n) -> a -> BitVector n
- class GBitPack f where
- type GFieldSize f :: Nat
- type GConstructorCount f :: Nat
- gPackFields :: Int -> f a -> (Int, BitVector (GFieldSize f))
- gUnpack :: Int -> Int -> BitVector (GFieldSize f) -> f a
Documentation
class KnownNat (BitSize a) => BitPack a where Source #
Convert to and from a BitVector
Nothing
type BitSize a :: Nat Source #
Number of Bit
s needed to represents elements
of type a
Can be derived using Generics
:
import Clash.Prelude import GHC.Generics data MyProductType = MyProductType { a :: Int, b :: Bool } deriving (Generic, BitPack)
type BitSize a = CLog 2 (GConstructorCount (Rep a)) + GFieldSize (Rep a)
pack :: a -> BitVector (BitSize a) Source #
Convert element of type a
to a BitVector
>>>
pack (-5 :: Signed 6)
11_1011
default pack :: (Generic a, GBitPack (Rep a), KnownNat (BitSize a), KnownNat constrSize, KnownNat fieldSize, constrSize ~ CLog 2 (GConstructorCount (Rep a)), fieldSize ~ GFieldSize (Rep a), (constrSize + fieldSize) ~ BitSize a) => a -> BitVector (BitSize a) Source #
unpack :: BitVector (BitSize a) -> a Source #
Convert a BitVector
to an element of type a
>>>
pack (-5 :: Signed 6)
11_1011>>>
let x = pack (-5 :: Signed 6)
>>>
unpack x :: Unsigned 6
59>>>
pack (59 :: Unsigned 6)
11_1011
Instances
BitPack Bool Source # | |
BitPack Double Source # | |
BitPack Float Source # | |
BitPack Int Source # | |
BitPack Int8 Source # | |
BitPack Int16 Source # | |
BitPack Int32 Source # | |
BitPack Int64 Source # | |
BitPack Word Source # | |
BitPack Word8 Source # | |
BitPack Word16 Source # | |
BitPack Word32 Source # | |
BitPack Word64 Source # | |
BitPack () Source # | |
BitPack CUShort Source # | |
BitPack Half Source # | |
BitPack Bit Source # | |
BitPack a => BitPack (Maybe a) Source # | |
BitPack a => BitPack (Complex a) Source # | |
BitPack a => BitPack (Down a) Source # | |
KnownNat n => BitPack (BitVector n) Source # | |
(KnownNat n, 1 <= n) => BitPack (Index n) Source # | |
KnownNat n => BitPack (Unsigned n) Source # | |
KnownNat n => BitPack (Signed n) Source # | |
(BitPack a, BitPack b) => BitPack (Either a b) Source # | |
(BitPack a, BitPack b) => BitPack (a, b) Source # | |
(KnownNat n, BitPack a) => BitPack (Vec n a) Source # | |
(KnownNat d, BitPack a) => BitPack (RTree d a) Source # | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3), KnownNat (BitSize (a2, a3))) => BitPack (a1, a2, a3) Source # | |
BitPack (rep (int + frac)) => BitPack (Fixed rep int frac) Source # | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4), KnownNat (BitSize (a2, a3, a4))) => BitPack (a1, a2, a3, a4) Source # | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4, a5), KnownNat (BitSize (a2, a3, a4, a5))) => BitPack (a1, a2, a3, a4, a5) Source # | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4, a5, a6), KnownNat (BitSize (a2, a3, a4, a5, a6))) => BitPack (a1, a2, a3, a4, a5, a6) Source # | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4, a5, a6, a7), KnownNat (BitSize (a2, a3, a4, a5, a6, a7))) => BitPack (a1, a2, a3, a4, a5, a6, a7) Source # | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4, a5, a6, a7, a8), KnownNat (BitSize (a2, a3, a4, a5, a6, a7, a8))) => BitPack (a1, a2, a3, a4, a5, a6, a7, a8) Source # | |
Defined in Clash.Class.BitPack | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4, a5, a6, a7, a8, a9), KnownNat (BitSize (a2, a3, a4, a5, a6, a7, a8, a9))) => BitPack (a1, a2, a3, a4, a5, a6, a7, a8, a9) Source # | |
Defined in Clash.Class.BitPack | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4, a5, a6, a7, a8, a9, a10), KnownNat (BitSize (a2, a3, a4, a5, a6, a7, a8, a9, a10))) => BitPack (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) Source # | |
Defined in Clash.Class.BitPack | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4, a5, a6, a7, a8, a9, a10, a11), KnownNat (BitSize (a2, a3, a4, a5, a6, a7, a8, a9, a10, a11))) => BitPack (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) Source # | |
Defined in Clash.Class.BitPack | |
(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12), KnownNat (BitSize (a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12))) => BitPack (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) Source # | |
Defined in Clash.Class.BitPack |
bitCoerce :: (BitPack a, BitPack b, BitSize a ~ BitSize b) => a -> b Source #
Coerce a value from one type to another through its bit representation.
>>>
pack (-5 :: Signed 6)
11_1011>>>
bitCoerce (-5 :: Signed 6) :: Unsigned 6
59>>>
pack (59 :: Unsigned 6)
11_1011
bitCoerceMap :: forall a b. (BitPack a, BitPack b, BitSize a ~ BitSize b) => (a -> a) -> b -> b Source #
Map a value by first coercing to another type through its bit representation.
>>>
pack (-5 :: Signed 32)
1111_1111_1111_1111_1111_1111_1111_1011>>>
bitCoerceMap @(Vec 4 (BitVector 8)) (replace 1 0) (-5 :: Signed 32)
-16711685>>>
pack (-16711685 :: Signed 32)
1111_1111_0000_0000_1111_1111_1111_1011
Internals
class GBitPack f where Source #
type GFieldSize f :: Nat Source #
Size of fields. If multiple constructors exist, this is the maximum of the sum of each of the constructors fields.
type GConstructorCount f :: Nat Source #
Number of constructors this type has. Indirectly indicates how many bits are needed to represent the constructor.
:: Int | Current constructor |
-> f a | Data to pack |
-> (Int, BitVector (GFieldSize f)) | (Constructor number, Packed fields) |
Pack fields of a type. Caller should pack and prepend the constructor bits.
:: Int | Construct with constructor n |
-> Int | Current constructor |
-> BitVector (GFieldSize f) | BitVector containing fields |
-> f a | Unpacked result |
Unpack whole type.
Instances
GBitPack (U1 :: Type -> Type) Source # | |
Defined in Clash.Class.BitPack type GFieldSize U1 :: Nat Source # type GConstructorCount U1 :: Nat Source # | |
BitPack c => GBitPack (K1 i c :: Type -> Type) Source # | |
Defined in Clash.Class.BitPack type GFieldSize (K1 i c) :: Nat Source # type GConstructorCount (K1 i c) :: Nat Source # | |
(KnownNat (GFieldSize g), KnownNat (GFieldSize f), KnownNat (GConstructorCount f), GBitPack f, GBitPack g) => GBitPack (f :+: g) Source # | |
Defined in Clash.Class.BitPack type GFieldSize (f :+: g) :: Nat Source # type GConstructorCount (f :+: g) :: Nat Source # | |
(KnownNat (GFieldSize g), KnownNat (GFieldSize f), GBitPack f, GBitPack g) => GBitPack (f :*: g) Source # | |
Defined in Clash.Class.BitPack type GFieldSize (f :*: g) :: Nat Source # type GConstructorCount (f :*: g) :: Nat Source # | |
GBitPack a => GBitPack (M1 m d a) Source # | |
Defined in Clash.Class.BitPack type GFieldSize (M1 m d a) :: Nat Source # type GConstructorCount (M1 m d a) :: Nat Source # |