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
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 # |