clash-prelude-1.8.4: Clash: a functional hardware description language - Prelude library

Copyright | (C) 2013-2016 University of Twente 2016-2017 Myrtle Software Ltd 2021-2025 QBayLogic B.V. 2022 Google Inc.
License | BSD2 (see the file LICENSE)
Maintainer | QBayLogic B.V. <devops@qbaylogic.com>
Safe Haskell | Trustworthy
Language | Haskell2010

Extensions

- Cpp
- UndecidableInstances
- MonoLocalBinds
- TemplateHaskell
- TemplateHaskellQuotes
- ScopedTypeVariables
- BangPatterns
- TypeFamilies
- ViewPatterns
- DataKinds
- InstanceSigs
- StandaloneDeriving
- DeriveDataTypeable
- DeriveFunctor
- DeriveTraversable
- DeriveFoldable
- DeriveGeneric
- DefaultSignatures
- DeriveLift
- DerivingStrategies
- FlexibleContexts
- MagicHash
- KindSignatures
- PostfixOperators
- TupleSections
- TypeOperators
- ExplicitNamespaces
- ExplicitForAll
- BinaryLiterals
- TypeApplications

Clash.Class.BitPack.Internal

Description

Synopsis

- class KnownNat (BitSize a) => BitPack a where
  - type BitSize a :: Nat
  - pack :: a -> BitVector (BitSize a)
  - unpack :: BitVector (BitSize a) -> a
- packXWith :: forall (n :: Nat) a. KnownNat n => (a -> BitVector n) -> a -> BitVector n
- isLike :: BitPack a => a -> a -> Bool
- bitCoerce :: (BitPack a, BitPack b, BitSize a ~ BitSize b) => a -> b
- bitCoerceMap :: (BitPack a, BitPack b, BitSize a ~ BitSize b) => (a -> a) -> b -> b
- packFloat# :: Float -> BitVector 32
- unpackFloat# :: BitVector 32 -> Float
- packDouble# :: Double -> BitVector 64
- unpackDouble# :: BitVector 64 -> Double
- packChar# :: Char -> BitVector 21
- unpackChar# :: BitVector 21 -> Char
- class GBitPack (f :: Type -> Type) where
  - type GFieldSize (f :: Type -> Type) :: Nat
  - type GConstructorCount (f :: Type -> Type) :: Nat
  - gPackFields :: Int -> f a -> (Int, BitVector (GFieldSize f))
  - gUnpack :: Int -> Int -> BitVector (GFieldSize f) -> f a
- boolToBV :: forall (n :: Nat). KnownNat n => Bool -> BitVector (n + 1)
- boolToBit :: Bool -> Bit
- bitToBool :: Bit -> Bool

# Documentation

    >>> :m -Prelude
    >>> :set -XDataKinds
    >>> import Clash.Prelude

class KnownNat (BitSize a) => BitPack a where Source #

Convert data to/from a `BitVector`. This allows functions to be defined on the underlying representation of data, while exposing a nicer API using `pack` / `unpack` at the boundaries. For example:

        f :: forall a b. (BitPack a, BitPack b) => a -> b
        f = unpack . go . pack
         where
          go :: BitVector (BitSize a) -> BitVector (BitSize b)
          go = _ -- A function on the underlying bit vector

A type should only implement this class if it has a statically known size, as otherwise it is not possible to determine how many bits are needed to represent values. This means that types such as `[a]` cannot have `BitPack` instances, as even if `a` has a statically known size, the length of the list cannot be known in advance.

It is not possible to give data a custom bit representation by providing a `BitPack` instance. A `BitPack` instance allows no creativity and should always accurately reflect the bit representation of the data in HDL. You should always `derive (`Generic`, BitPack)` unless you use a custom data representation, in which case you should use `deriveBitPack`. Custom encodings can be created with Clash.Annotations.BitRepresentation and Clash.Annotations.BitRepresentation.Deriving.

If the `BitPack` instance does not accurately match the bit representation of the data in HDL, Clash designs will exhibit incorrect behavior in various places.

Clash provides some generic functions on packable types in the prelude, such as indexing into packable stuctures (see Clash.Class.BitPack.BitIndex) and bitwise reduction of packable data (see Clash.Class.BitPack.BitReduction).

Minimal complete definition

Nothing

Associated Types

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)

Methods

pack :: a -> BitVector (BitSize a) Source #

Convert element of type `a` to a `BitVector`

    >>> pack (-5 :: Signed 6)
    0b11_1011

default pack :: forall (constrSize :: Nat) (fieldSize :: Nat). (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)
    0b11_1011
    >>> let x = pack (-5 :: Signed 6)
    >>> unpack x :: Unsigned 6
    59
    >>> pack (59 :: Unsigned 6)
    0b11_1011

default unpack :: forall (constrSize :: Nat) (fieldSize :: Nat). (Generic a, GBitPack (Rep a), KnownNat constrSize, KnownNat fieldSize, constrSize ~ CLog 2 (GConstructorCount (Rep a)), fieldSize ~ GFieldSize (Rep a), (constrSize + fieldSize) ~ BitSize a) => BitVector (BitSize a) -> a Source #

#### Instances

Instances details

BitPack Bit Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Bit |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Bit = 1

Methods

pack :: Bit -> BitVector (BitSize Bit) Source #

unpack :: BitVector (BitSize Bit) -> Bit Source #

BitPack CUShort Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize CUShort |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize CUShort = 16

Methods

pack :: CUShort -> BitVector (BitSize CUShort) Source #

unpack :: BitVector (BitSize CUShort) -> CUShort Source #

BitPack Int16 Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Int16 |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Int16 = 16

Methods

pack :: Int16 -> BitVector (BitSize Int16) Source #

unpack :: BitVector (BitSize Int16) -> Int16 Source #

BitPack Int32 Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Int32 |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Int32 = 32

Methods

pack :: Int32 -> BitVector (BitSize Int32) Source #

unpack :: BitVector (BitSize Int32) -> Int32 Source #

BitPack Int64 Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Int64 |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Int64 = 64

Methods

pack :: Int64 -> BitVector (BitSize Int64) Source #

unpack :: BitVector (BitSize Int64) -> Int64 Source #

BitPack Int8 Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Int8 |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Int8 = 8

Methods

pack :: Int8 -> BitVector (BitSize Int8) Source #

unpack :: BitVector (BitSize Int8) -> Int8 Source #

BitPack Word16 Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Word16 |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Word16 = 16

Methods

pack :: Word16 -> BitVector (BitSize Word16) Source #

unpack :: BitVector (BitSize Word16) -> Word16 Source #

BitPack Word32 Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Word32 |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Word32 = 32

Methods

pack :: Word32 -> BitVector (BitSize Word32) Source #

unpack :: BitVector (BitSize Word32) -> Word32 Source #

BitPack Word64 Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Word64 |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Word64 = 64

Methods

pack :: Word64 -> BitVector (BitSize Word64) Source #

unpack :: BitVector (BitSize Word64) -> Word64 Source #

BitPack Word8 Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Word8 |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Word8 = 8

Methods

pack :: Word8 -> BitVector (BitSize Word8) Source #

unpack :: BitVector (BitSize Word8) -> Word8 Source #

BitPack Ordering Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Ordering |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Ordering = CLog 2 (GConstructorCount (Rep Ordering)) + GFieldSize (Rep Ordering)

Methods

pack :: Ordering -> BitVector (BitSize Ordering) Source #

unpack :: BitVector (BitSize Ordering) -> Ordering Source #

BitPack Half Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Half |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Half = 16

Methods

pack :: Half -> BitVector (BitSize Half) Source #

unpack :: BitVector (BitSize Half) -> Half Source #

BitPack () Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize () |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize () = 0

Methods

pack :: () -> BitVector (BitSize ()) Source #

unpack :: BitVector (BitSize ()) -> () Source #

BitPack Bool Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Bool |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Bool = 1

Methods

pack :: Bool -> BitVector (BitSize Bool) Source #

unpack :: BitVector (BitSize Bool) -> Bool Source #

BitPack Char Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Char |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Char = 21

Methods

pack :: Char -> BitVector (BitSize Char) Source #

unpack :: BitVector (BitSize Char) -> Char Source #

BitPack Double Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Double |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Double = 64

Methods

pack :: Double -> BitVector (BitSize Double) Source #

unpack :: BitVector (BitSize Double) -> Double Source #

BitPack Float Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Float |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Float = 32

Methods

pack :: Float -> BitVector (BitSize Float) Source #

unpack :: BitVector (BitSize Float) -> Float Source #

BitPack Int Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Int |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Int = 64

Methods

pack :: Int -> BitVector (BitSize Int) Source #

unpack :: BitVector (BitSize Int) -> Int Source #

BitPack Word Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize Word |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize Word = 64

Methods

pack :: Word -> BitVector (BitSize Word) Source #

unpack :: BitVector (BitSize Word) -> Word Source #

BitPack a => BitPack (Complex a) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (Complex a) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (Complex a) = CLog 2 (GConstructorCount (Rep (Complex a))) + GFieldSize (Rep (Complex a))

Methods

pack :: Complex a -> BitVector (BitSize (Complex a)) Source #

unpack :: BitVector (BitSize (Complex a)) -> Complex a Source #

BitPack a => BitPack (Erroring a) Source # |

Instance details

Defined in Clash.Num.Erroring

Associated Types

type BitSize (Erroring a) |
Instance details Defined in Clash.Num.Erroring type BitSize (Erroring a) = BitSize a

Methods

pack :: Erroring a -> BitVector (BitSize (Erroring a)) Source #

unpack :: BitVector (BitSize (Erroring a)) -> Erroring a Source #

(BitPack a, KnownNat (BitSize a + 1)) => BitPack (Overflowing a) Source # |

Instance details

Defined in Clash.Num.Overflowing

Associated Types

type BitSize (Overflowing a) |
Instance details Defined in Clash.Num.Overflowing type BitSize (Overflowing a) = BitSize a + 1

Methods

pack :: Overflowing a -> BitVector (BitSize (Overflowing a)) Source #

unpack :: BitVector (BitSize (Overflowing a)) -> Overflowing a Source #

BitPack a => BitPack (Saturating a) Source # |

Instance details

Defined in Clash.Num.Saturating

Associated Types

type BitSize (Saturating a) |
Instance details Defined in Clash.Num.Saturating type BitSize (Saturating a) = BitSize a

Methods

pack :: Saturating a -> BitVector (BitSize (Saturating a)) Source #

unpack :: BitVector (BitSize (Saturating a)) -> Saturating a Source #

BitPack a => BitPack (Wrapping a) Source # |

Instance details

Defined in Clash.Num.Wrapping

Associated Types

type BitSize (Wrapping a) |
Instance details Defined in Clash.Num.Wrapping type BitSize (Wrapping a) = BitSize a

Methods

pack :: Wrapping a -> BitVector (BitSize (Wrapping a)) Source #

unpack :: BitVector (BitSize (Wrapping a)) -> Wrapping a Source #

BitPack a => BitPack (Zeroing a) Source # |

Instance details

Defined in Clash.Num.Zeroing

Associated Types

type BitSize (Zeroing a) |
Instance details Defined in Clash.Num.Zeroing type BitSize (Zeroing a) = BitSize a

Methods

pack :: Zeroing a -> BitVector (BitSize (Zeroing a)) Source #

unpack :: BitVector (BitSize (Zeroing a)) -> Zeroing a Source #

KnownNat n => BitPack (BitVector n) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (BitVector n) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (BitVector n) = n

Methods

pack :: BitVector n -> BitVector (BitSize (BitVector n)) Source #

unpack :: BitVector (BitSize (BitVector n)) -> BitVector n Source #

(KnownNat n, 1 <= n) => BitPack (Index n) Source # |

Instance details

Defined in Clash.Sized.Internal.Index

Associated Types

type BitSize (Index n) |
Instance details Defined in Clash.Sized.Internal.Index type BitSize (Index n) = CLog 2 n

Methods

pack :: Index n -> BitVector (BitSize (Index n)) Source #

unpack :: BitVector (BitSize (Index n)) -> Index n Source #

KnownNat n => BitPack (Signed n) Source # |

Instance details

Defined in Clash.Sized.Internal.Signed

Associated Types

type BitSize (Signed n) |
Instance details Defined in Clash.Sized.Internal.Signed type BitSize (Signed n) = n

Methods

pack :: Signed n -> BitVector (BitSize (Signed n)) Source #

unpack :: BitVector (BitSize (Signed n)) -> Signed n Source #

KnownNat n => BitPack (Unsigned n) Source # |

Instance details

Defined in Clash.Sized.Internal.Unsigned

Associated Types

type BitSize (Unsigned n) |
Instance details Defined in Clash.Sized.Internal.Unsigned type BitSize (Unsigned n) = n

Methods

pack :: Unsigned n -> BitVector (BitSize (Unsigned n)) Source #

unpack :: BitVector (BitSize (Unsigned n)) -> Unsigned n Source #

BitPack a => BitPack (Identity a) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (Identity a) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (Identity a) = CLog 2 (GConstructorCount (Rep (Identity a))) + GFieldSize (Rep (Identity a))

Methods

pack :: Identity a -> BitVector (BitSize (Identity a)) Source #

unpack :: BitVector (BitSize (Identity a)) -> Identity a Source #

BitPack a => BitPack (Down a) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (Down a) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (Down a) = CLog 2 (GConstructorCount (Rep (Down a))) + GFieldSize (Rep (Down a))

Methods

pack :: Down a -> BitVector (BitSize (Down a)) Source #

unpack :: BitVector (BitSize (Down a)) -> Down a Source #

BitPack a => BitPack (Maybe a) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (Maybe a) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (Maybe a) = CLog 2 (GConstructorCount (Rep (Maybe a))) + GFieldSize (Rep (Maybe a))

Methods

pack :: Maybe a -> BitVector (BitSize (Maybe a)) Source #

unpack :: BitVector (BitSize (Maybe a)) -> Maybe a Source #

(KnownNat d, BitPack a) => BitPack (RTree d a) Source # |

Instance details

Defined in Clash.Sized.RTree

Associated Types

type BitSize (RTree d a) |
Instance details Defined in Clash.Sized.RTree type BitSize (RTree d a) = (2 ^ d) * BitSize a

Methods

pack :: RTree d a -> BitVector (BitSize (RTree d a)) Source #

unpack :: BitVector (BitSize (RTree d a)) -> RTree d a Source #

(KnownNat n, BitPack a) => BitPack (Vec n a) Source # |

Instance details

Defined in Clash.Sized.Vector

Associated Types

type BitSize (Vec n a) |
Instance details Defined in Clash.Sized.Vector type BitSize (Vec n a) = n * BitSize a

Methods

pack :: Vec n a -> BitVector (BitSize (Vec n a)) Source #

unpack :: BitVector (BitSize (Vec n a)) -> Vec n a Source #

(BitPack a, BitPack b) => BitPack (Either a b) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (Either a b) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (Either a b) = CLog 2 (GConstructorCount (Rep (Either a b))) + GFieldSize (Rep (Either a b))

Methods

pack :: Either a b -> BitVector (BitSize (Either a b)) Source #

unpack :: BitVector (BitSize (Either a b)) -> Either a b Source #

(BitPack a, BitPack b) => BitPack (a, b) Source # | NB: The documentation only shows instances up to 3-tuples. By default, instances up to and including 12-tuples will exist. If the flag `large-tuples` is set instances up to the GHC imposed limit will exist. The GHC imposed limit is either 62 or 64 depending on the GHC version.

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a, b) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a, b) = BitSize a + BitSize b

Methods

pack :: (a, b) -> BitVector (BitSize (a, b)) Source #

unpack :: BitVector (BitSize (a, b)) -> (a, b) Source #

(BitPack (rep (int + frac)), KnownNat (BitSize (rep (int + frac)))) => BitPack (Fixed rep int frac) Source # |

Instance details

Defined in Clash.Sized.Fixed

Associated Types

type BitSize (Fixed rep int frac) |
Instance details Defined in Clash.Sized.Fixed type BitSize (Fixed rep int frac) = BitSize (rep (int + frac))

Methods

pack :: Fixed rep int frac -> BitVector (BitSize (Fixed rep int frac)) Source #

unpack :: BitVector (BitSize (Fixed rep int frac)) -> Fixed rep int frac Source #

BitPack a => BitPack (Const a b) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (Const a b) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (Const a b) = CLog 2 (GConstructorCount (Rep (Const a b))) + GFieldSize (Rep (Const a b))

Methods

pack :: Const a b -> BitVector (BitSize (Const a b)) Source #

unpack :: BitVector (BitSize (Const a b)) -> Const a b Source #

(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3), KnownNat (BitSize (a2, a3))) => BitPack (a1, a2, a3) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3) = BitSize a1 + BitSize (a2, a3)

Methods

pack :: (a1, a2, a3) -> BitVector (BitSize (a1, a2, a3)) Source #

unpack :: BitVector (BitSize (a1, a2, a3)) -> (a1, a2, a3) Source #

(BitPack (f a), BitPack (g a)) => BitPack (Product f g a) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (Product f g a) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (Product f g a) = CLog 2 (GConstructorCount (Rep (Product f g a))) + GFieldSize (Rep (Product f g a))

Methods

pack :: Product f g a -> BitVector (BitSize (Product f g a)) Source #

unpack :: BitVector (BitSize (Product f g a)) -> Product f g a Source #

(BitPack (f a), BitPack (g a)) => BitPack (Sum f g a) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (Sum f g a) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (Sum f g a) = CLog 2 (GConstructorCount (Rep (Sum f g a))) + GFieldSize (Rep (Sum f g a))

Methods

pack :: Sum f g a -> BitVector (BitSize (Sum f g a)) Source #

unpack :: BitVector (BitSize (Sum f g a)) -> Sum f g a Source #

(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4), KnownNat (BitSize (a2, a3, a4))) => BitPack (a1, a2, a3, a4) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3, a4) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3, a4) = BitSize a1 + BitSize (a2, a3, a4)

Methods

pack :: (a1, a2, a3, a4) -> BitVector (BitSize (a1, a2, a3, a4)) Source #

unpack :: BitVector (BitSize (a1, a2, a3, a4)) -> (a1, a2, a3, a4) Source #

BitPack (f (g a)) => BitPack (Compose f g a) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (Compose f g a) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (Compose f g a) = CLog 2 (GConstructorCount (Rep (Compose f g a))) + GFieldSize (Rep (Compose f g a))

Methods

pack :: Compose f g a -> BitVector (BitSize (Compose f g a)) Source #

unpack :: BitVector (BitSize (Compose f g a)) -> Compose f g a Source #

(BitPack a1, KnownNat (BitSize a1), BitPack (a2, a3, a4, a5), KnownNat (BitSize (a2, a3, a4, a5))) => BitPack (a1, a2, a3, a4, a5) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3, a4, a5) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3, a4, a5) = BitSize a1 + BitSize (a2, a3, a4, a5)

Methods

pack :: (a1, a2, a3, a4, a5) -> BitVector (BitSize (a1, a2, a3, a4, a5)) Source #

unpack :: BitVector (BitSize (a1, a2, a3, a4, a5)) -> (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 # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3, a4, a5, a6) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3, a4, a5, a6) = BitSize a1 + BitSize (a2, a3, a4, a5, a6)

Methods

pack :: (a1, a2, a3, a4, a5, a6) -> BitVector (BitSize (a1, a2, a3, a4, a5, a6)) Source #

unpack :: BitVector (BitSize (a1, a2, a3, a4, a5, a6)) -> (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 # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3, a4, a5, a6, a7) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3, a4, a5, a6, a7) = BitSize a1 + BitSize (a2, a3, a4, a5, a6, a7)

Methods

pack :: (a1, a2, a3, a4, a5, a6, a7) -> BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7)) Source #

unpack :: BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7)) -> (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 # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3, a4, a5, a6, a7, a8) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3, a4, a5, a6, a7, a8) = BitSize a1 + BitSize (a2, a3, a4, a5, a6, a7, a8)

Methods

pack :: (a1, a2, a3, a4, a5, a6, a7, a8) -> BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8)) Source #

unpack :: BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8)) -> (a1, a2, a3, a4, a5, a6, a7, a8) Source #

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

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9) = BitSize a1 + BitSize (a2, a3, a4, a5, a6, a7, a8, a9)

Methods

pack :: (a1, a2, a3, a4, a5, a6, a7, a8, a9) -> BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9)) Source #

unpack :: BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9)) -> (a1, a2, a3, a4, a5, a6, a7, a8, a9) Source #

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

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) = BitSize a1 + BitSize (a2, a3, a4, a5, a6, a7, a8, a9, a10)

Methods

pack :: (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) -> BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)) Source #

unpack :: BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)) -> (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) Source #

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

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) = BitSize a1 + BitSize (a2, a3, a4, a5, a6, a7, a8, a9, a10, a11)

Methods

pack :: (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) -> BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11)) Source #

unpack :: BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11)) -> (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) Source #

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

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) |
Instance details Defined in Clash.Class.BitPack.Internal type BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) = BitSize a1 + BitSize (a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)

Methods

pack :: (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) -> BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)) Source #

unpack :: BitVector (BitSize (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)) -> (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) Source #

packXWith :: forall (n :: Nat) a. KnownNat n => (a -> BitVector n) -> a -> BitVector n Source #

isLike :: BitPack a => a -> a -> Bool Source #

Pack both arguments to a `BitVector` and use `isLike#` to compare them. This is a more lentiant comparison than `(==)`, behaving more like (but not necessarily exactly the same as) `std_match` in VHDL or `casez` in Verilog.

Unlike `(==)`, isLike is not symmetric. The reason for this is that a defined bit is said to be like an undefined bit, but not vice-versa:

    >>> isLike (12 :: Signed 8) undefined
    True
    >>> isLike undefined (12 :: Signed 8)
    False

However, it is still trivially reflexive and transitive:

    >>> :set -XTemplateHaskell
    >>> let x1 = $(bLit "0010")
    >>> let x2 = $(bLit "0.10")
    >>> let x3 = $(bLit "0.1.")
    >>> isLike x1 x1
    True
    >>> isLike x1 x2
    True
    >>> isLike x2 x3
    True
    >>> isLike x1 x3
    True

NB: Not synthesizable

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)
    0b11_1011
    >>> bitCoerce (-5 :: Signed 6) :: Unsigned 6
    59
    >>> pack (59 :: Unsigned 6)
    0b11_1011

bitCoerceMap :: (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)
    0b1111_1111_1111_1111_1111_1111_1111_1011
    >>> bitCoerceMap @(Vec 4 (BitVector 8)) (replace 1 0) (-5 :: Signed 32)
    -16711685
    >>> pack (-16711685 :: Signed 32)
    0b1111_1111_0000_0000_1111_1111_1111_1011

packFloat# :: Float -> BitVector 32 Source #

unpackFloat# :: BitVector 32 -> Float Source #

packDouble# :: Double -> BitVector 64 Source #

unpackDouble# :: BitVector 64 -> Double Source #

packChar# :: Char -> BitVector 21 Source #

unpackChar# :: BitVector 21 -> Char Source #

class GBitPack (f :: Type -> Type) where Source #

Associated Types

type GFieldSize (f :: Type -> Type) :: 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 :: Type -> Type) :: Nat Source #

Number of constructors this type has. Indirectly indicates how many bits are needed to represent the constructor.

Methods

gPackFields Source #

Arguments

:: 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.

gUnpack Source #

Arguments

:: Int | Construct with constructor n
-> Int | Current constructor
-> BitVector (GFieldSize f) | BitVector containing fields
-> f a | Unpacked result

Unpack whole type.

#### Instances

Instances details

GBitPack (U1 :: Type -> Type) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type GFieldSize (U1 :: Type -> Type) |
Instance details Defined in Clash.Class.BitPack.Internal type GFieldSize (U1 :: Type -> Type) = 0
type GConstructorCount (U1 :: Type -> Type) |
Instance details Defined in Clash.Class.BitPack.Internal type GConstructorCount (U1 :: Type -> Type) = 1

Methods

gPackFields :: Int -> U1 a -> (Int, BitVector (GFieldSize (U1 :: Type -> Type))) Source #

gUnpack :: Int -> Int -> BitVector (GFieldSize (U1 :: Type -> Type)) -> U1 a Source #

(KnownNat (GFieldSize g), KnownNat (GFieldSize f), GBitPack f, GBitPack g) => GBitPack (f :*: g) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type GFieldSize (f :*: g) |
Instance details Defined in Clash.Class.BitPack.Internal type GFieldSize (f :*: g) = GFieldSize f + GFieldSize g
type GConstructorCount (f :*: g) |
Instance details Defined in Clash.Class.BitPack.Internal type GConstructorCount (f :*: g) = 1

Methods

gPackFields :: Int -> (f :*: g) a -> (Int, BitVector (GFieldSize (f :*: g))) Source #

gUnpack :: Int -> Int -> BitVector (GFieldSize (f :*: g)) -> (f :*: g) a Source #

(KnownNat (GFieldSize g), KnownNat (GFieldSize f), KnownNat (GConstructorCount f), GBitPack f, GBitPack g) => GBitPack (f :+: g) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type GFieldSize (f :+: g) |
Instance details Defined in Clash.Class.BitPack.Internal type GFieldSize (f :+: g) = Max (GFieldSize f) (GFieldSize g)
type GConstructorCount (f :+: g) |
Instance details Defined in Clash.Class.BitPack.Internal type GConstructorCount (f :+: g) = GConstructorCount f + GConstructorCount g

Methods

gPackFields :: Int -> (f :+: g) a -> (Int, BitVector (GFieldSize (f :+: g))) Source #

gUnpack :: Int -> Int -> BitVector (GFieldSize (f :+: g)) -> (f :+: g) a Source #

BitPack c => GBitPack (K1 i c :: Type -> Type) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type GFieldSize (K1 i c :: Type -> Type) |
Instance details Defined in Clash.Class.BitPack.Internal type GFieldSize (K1 i c :: Type -> Type) = BitSize c
type GConstructorCount (K1 i c :: Type -> Type) |
Instance details Defined in Clash.Class.BitPack.Internal type GConstructorCount (K1 i c :: Type -> Type) = 1

Methods

gPackFields :: Int -> K1 i c a -> (Int, BitVector (GFieldSize (K1 i c :: Type -> Type))) Source #

gUnpack :: Int -> Int -> BitVector (GFieldSize (K1 i c :: Type -> Type)) -> K1 i c a Source #

GBitPack a => GBitPack (M1 m d a) Source # |

Instance details

Defined in Clash.Class.BitPack.Internal

Associated Types

type GFieldSize (M1 m d a) |
Instance details Defined in Clash.Class.BitPack.Internal type GFieldSize (M1 m d a) = GFieldSize a
type GConstructorCount (M1 m d a) |
Instance details Defined in Clash.Class.BitPack.Internal type GConstructorCount (M1 m d a) = GConstructorCount a

Methods

gPackFields :: Int -> M1 m d a a0 -> (Int, BitVector (GFieldSize (M1 m d a))) Source #

gUnpack :: Int -> Int -> BitVector (GFieldSize (M1 m d a)) -> M1 m d a a0 Source #

boolToBV :: forall (n :: Nat). KnownNat n => Bool -> BitVector (n + 1) Source #

Zero-extend a `Bool`ean value to a `BitVector` of the appropriate size.

    >>> boolToBV True :: BitVector 6
    0b00_0001
    >>> boolToBV False :: BitVector 6
    0b00_0000

boolToBit :: Bool -> Bit Source #

Convert a Bool to a Bit

bitToBool :: Bit -> Bool Source #

Convert a Bit to a Bool