Documentation

Mathlib.Data.BitVec.Defs

Basic operations on bitvectors #

Std has defined bitvector of length w as Fin (2^w). Here we define a few more operations on these bitvectors

Main definitions #

Constants #

@[reducible, inline]
abbrev BitVec.one (w : ) :

The bitvector representing 1. That is, the bitvector with least-significant bit 1 and all other bits 0

Equations
Instances For

    Bitwise operations #

    Arithmetic operators #

    def BitVec.adc' {n : } (x : BitVec n) (y : BitVec n) (c : Bool) :
    BitVec (n + 1)

    Add with carry (no overflow)

    See also Std.BitVec.adc, which stores the carry bit separately.

    Equations
    Instances For
      def BitVec.sbb {n : } (x : BitVec n) (y : BitVec n) (b : Bool) :

      Subtract with borrow

      Equations
      • x.sbb y b = let y := y + { toFin := b.toNat }; (decide (x < y), x - y)
      Instances For

        Comparison operators #

        def BitVec.ugt {w : } (x : BitVec w) (y : BitVec w) :

        Unsigned greater than for bitvectors.

        Equations
        • x.ugt y = y.ult x
        Instances For
          def BitVec.uge {w : } (x : BitVec w) (y : BitVec w) :

          Signed greater than or equal to for bitvectors.

          Equations
          • x.uge y = y.ule x
          Instances For
            def BitVec.sgt {w : } (x : BitVec w) (y : BitVec w) :

            Signed greater than for bitvectors.

            Equations
            • x.sgt y = y.slt x
            Instances For
              def BitVec.sge {w : } (x : BitVec w) (y : BitVec w) :

              Signed greater than or equal to for bitvectors.

              Equations
              • x.sge y = y.sle x
              Instances For

                Conversion to nat and int #

                def BitVec.addLsb (r : ) (b : Bool) :

                addLsb r b is r + r + 1 if b is true and r + r otherwise.

                Equations
                Instances For
                  def BitVec.getLsb' {w : } (x : BitVec w) (i : Fin w) :

                  Return the i-th least significant bit, where i is a statically known in-bounds index

                  Equations
                  • x.getLsb' i = x.getLsb i
                  Instances For
                    def BitVec.getMsb' {w : } (x : BitVec w) (i : Fin w) :

                    Return the i-th most significant bit, where i is a statically known in-bounds index

                    Equations
                    • x.getMsb' i = x.getMsb i
                    Instances For
                      def BitVec.toLEList {w : } (x : BitVec w) :

                      Convert a bitvector to a little-endian list of Booleans. That is, the head of the list is the least significant bit

                      Equations
                      Instances For
                        def BitVec.toBEList {w : } (x : BitVec w) :

                        Convert a bitvector to a big-endian list of Booleans. That is, the head of the list is the most significant bit

                        Equations
                        Instances For
                          instance BitVec.instSMulNat {w : } :
                          Equations
                          • BitVec.instSMulNat = { smul := fun (x : ) (y : BitVec w) => { toFin := x y.toFin } }
                          instance BitVec.instSMulInt {w : } :
                          Equations
                          • BitVec.instSMulInt = { smul := fun (x : ) (y : BitVec w) => { toFin := x y.toFin } }
                          Equations
                          • BitVec.instPowNat_mathlib = { pow := fun (x : BitVec w) (n : ) => { toFin := x.toFin ^ n } }