Documentation

Sparkle.Data.BitPack

class Sparkle.Data.BitPack.BitPack (α : Type u) (n : Nat) :

BitPack type class: Proves a type α can be converted to/from a BitVec of width n.

This is essential for hardware synthesis, as all values must ultimately be representable as bit patterns.

Laws (not enforced, but expected):

  • toBitVec (fromBitVec bv) = bv (round-trip property)
  • fromBitVec (toBitVec x) = x (round-trip property)
Instances
    @[inline]
    def Sparkle.Data.BitPack.BitPack.pack {α : Type u} {n : Nat} [BitPack α n] (x : α) :

    Convert a value to its bit vector representation

    Equations
    Instances For
      @[inline]
      def Sparkle.Data.BitPack.BitPack.unpack {α : Type u} {n : Nat} [BitPack α n] (bv : BitVec n) :
      α

      Convert a bit vector to its value representation

      Equations
      Instances For

        BitPack instance for Bool (1 bit)

        Equations

        BitPack instance for BitVec n (identity mapping)

        Equations

        BitPack instance for Unit (0 bits)

        Equations

        BitPack instance for UInt8 (8 bits)

        Equations

        BitPack instance for UInt16 (16 bits)

        Equations

        BitPack instance for UInt32 (32 bits)

        Equations

        BitPack instance for UInt64 (64 bits)

        Equations
        instance Sparkle.Data.BitPack.instBitPackProdHAddNat {α β : Type u} {n m : Nat} [BitPack α n] [BitPack β m] :
        BitPack (α × β) (n + m)

        BitPack instance for pairs (concatenate bit vectors)

        Equations
        • One or more equations did not get rendered due to their size.
        def Sparkle.Data.BitPack.bitWidth (α : Type u) (n : Nat) [BitPack α n] :

        Helper to get the bit width of a type with BitPack instance

        Equations
        Instances For
          def Sparkle.Data.BitPack.testRoundTrip {α : Type u} {n : Nat} [BitPack α n] [BEq α] (x : α) :

          Test if a value round-trips correctly through BitPack

          Equations
          Instances For

            Example: RGB structure for demonstration

            Instances For
              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                Equations
                Instances For

                  BitPack instance for RGB (24 bits total)

                  Equations
                  • One or more equations did not get rendered due to their size.

                  Example RGB value

                  Equations
                  Instances For