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 #
Std.BitVec.sgt
: Signed greater-than comparison of bitvectorsStd.BitVec.sge
: Signed greater-equals comparison of bitvectorsStd.BitVec.ugt
: Unsigned greater-than comparison of bitvectorsStd.BitVec.uge
: Unsigned greater-equals comparison of bitvectors
Constants #
The bitvector representing 1
.
That is, the bitvector with least-significant bit 1
and all other bits 0
Equations
- Std.BitVec.one w = 1
Instances For
Bitwise operations #
Arithmetic operators #
Add with carry (no overflow)
See also Std.BitVec.adc
, which stores the carry bit separately.
Equations
- Std.BitVec.adc' x y c = let a := Std.BitVec.adc x y c; Std.BitVec.cons a.1 a.2
Instances For
Subtract with borrow
Equations
- Std.BitVec.sbb x y b = let y := y + { toFin := ↑(Bool.toNat b) }; (decide (x < y), x - y)
Instances For
Comparison operators #
Signed greater than or equal to for bitvectors.
Equations
- Std.BitVec.uge x y = Std.BitVec.ule y x
Instances For
Signed greater than or equal to for bitvectors.
Equations
- Std.BitVec.sge x y = Std.BitVec.sle y x
Instances For
Conversion to nat
and int
#
addLsb r b
is r + r + 1
if b
is true
and r + r
otherwise.
Equations
- Std.BitVec.addLsb r b = Nat.bit b r
Instances For
Return the i
-th least significant bit, where i
is a statically known in-bounds index
Equations
- Std.BitVec.getLsb' x i = Std.BitVec.getLsb x ↑i
Instances For
Return the i
-th most significant bit, where i
is a statically known in-bounds index
Equations
- Std.BitVec.getMsb' x i = Std.BitVec.getMsb x ↑i
Instances For
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
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
Equations
- Std.BitVec.instSMulNatBitVec = { smul := fun (x : ℕ) (y : Std.BitVec w) => { toFin := x • y.toFin } }
Equations
- Std.BitVec.instSMulIntBitVec = { smul := fun (x : ℤ) (y : Std.BitVec w) => { toFin := x • y.toFin } }
Equations
- Std.BitVec.instPowBitVecNat = { pow := fun (x : Std.BitVec w) (n : ℕ) => { toFin := x.toFin ^ n } }
Equations
- Std.BitVec.instNatCastBitVec = { natCast := Std.BitVec.ofNat w }
Equations
- Std.BitVec.instIntCastBitVec = { intCast := Std.BitVec.ofInt w }