Basic operations on bitvectors #
This is a work-in-progress, and contains additions to other theories.
This file was moved to mathlib from core Lean in the switch to Lean 3.20.0c. It is not fully in compliance with mathlib style standards.
Create a zero bitvector
Equations
Create a bitvector of length n
whose n-1
st entry is 1 and other entries are 0.
Equations
- Bitvec.one x = match x with | 0 => Vector.nil | Nat.succ n => Vector.append (Vector.replicate n false) (true ::ᵥ Vector.nil)
Create a bitvector from another with a provably equal length.
Equations
- Bitvec.cong h x = match x with | { val := x, property := p } => { val := x, property := (_ : List.length x = b) }
Shift operations #
shl x i
is the bitvector obtained by left-shifting x
i
times and padding with false
.
If x.length < i
then this will return the all-false
s bitvector.
Equations
- Bitvec.shl x i = Bitvec.cong (_ : n - i + min n i = n) (Vector.append (Vector.drop i x) (Vector.replicate (min n i) false))
fill_shr x i fill
is the bitvector obtained by right-shifting x
i
times and then
padding with fill : Bool
. If x.length < i
then this will return the constant fill
bitvector.
Equations
- Bitvec.fillShr x i fill = Bitvec.cong (_ : min n i + min (n - i) n = n) (Vector.append (Vector.replicate (min n i) fill) (Vector.take (n - i) x))
unsigned shift right
Equations
- Bitvec.ushr x i = Bitvec.fillShr x i false
signed shift right
Equations
- Bitvec.sshr x x = match x, x, x with | 0, x, x_1 => Vector.nil | Nat.succ n, x, i => Vector.head x ::ᵥ Bitvec.fillShr (Vector.tail x) i (Vector.head x)
Bitwise operations #
bitwise not
Equations
- Bitvec.not bv = Vector.map not bv
bitwise and
Equations
- Bitvec.and = Vector.map₂ and
bitwise xor
Equations
- Bitvec.xor = Vector.map₂ xor
Arithmetic operators #
neg x
is the two's complement of x
.
Equations
- Bitvec.neg x = let f := fun y c => (y || c, xor y c); (Vector.mapAccumr f x false).snd
Add with carry (no overflow)
Equations
- Bitvec.adc x y c = let f := fun x y c => (Bitvec.carry x y c, Bitvec.xor3 x y c); match Vector.mapAccumr₂ f x y c with | (c, z) => c ::ᵥ z
The sum of two bitvectors
Equations
- Bitvec.add x y = Vector.tail (Bitvec.adc x y false)
Subtract with borrow
Equations
- Bitvec.sbb x y b = let f := fun x y c => (Bitvec.carry (!x) y c, Bitvec.xor3 x y c); Vector.mapAccumr₂ f x y b
The difference of two bitvectors
Equations
- Bitvec.sub x y = (Bitvec.sbb x y false).snd
Equations
- Bitvec.instZeroBitvec = { zero := Bitvec.zero n }
Equations
- Bitvec.instOneBitvec = { one := Bitvec.one n }
The product of two bitvectors
Equations
- Bitvec.mul x y = let f := fun r b => bif b then r + r + y else r + r; List.foldl f 0 (Vector.toList x)
Comparison operators #
uborrow x y
returns true
iff the "subtract with borrow" operation on x
, y
and false
required a borrow.
Equations
- Bitvec.uborrow x y = (Bitvec.sbb x y false).fst
unsigned less-than proposition
Equations
- Bitvec.Ult x y = (Bitvec.uborrow x y = true)
unsigned greater-than proposition
Equations
- Bitvec.Ugt x y = Bitvec.Ult y x
unsigned less-than-or-equal-to proposition
Equations
- Bitvec.Ule x y = ¬Bitvec.Ult y x
unsigned greater-than-or-equal-to proposition
Equations
- Bitvec.Uge x y = Bitvec.Ule y x
signed less-than proposition
Equations
- Bitvec.Slt x y = (Bitvec.sborrow x y = true)
signed greater-than proposition
Equations
- Bitvec.Sgt x y = Bitvec.Slt y x
signed less-than-or-equal-to proposition
Equations
- Bitvec.Sle x y = ¬Bitvec.Slt y x
signed greater-than-or-equal-to proposition
Equations
- Bitvec.Sge x y = Bitvec.Sle y x
Conversion to nat
and int
#
Create a bitvector from a nat
Equations
- Bitvec.ofNat 0 x = Vector.nil
- Bitvec.ofNat (Nat.succ n) x = Vector.append (Bitvec.ofNat n (x / 2)) (decide (x % 2 = 1) ::ᵥ Vector.nil)
Create a bitvector in the two's complement representation from an int
Equations
- Bitvec.ofInt x x = match x, x with | n, Int.ofNat m => false ::ᵥ Bitvec.ofNat n m | n, Int.negSucc m => true ::ᵥ Bitvec.not (Bitvec.ofNat n m)
add_lsb r b
is r + r + 1
if b
is true
and r + r
otherwise.
Equations
- Bitvec.addLsb r b = r + r + bif b then 1 else 0
Return the natural number encoded by the input bitvector
Equations
Miscellaneous instances #
Equations
- Bitvec.instReprBitvec n = { reprPrec := fun b x => Std.Format.text (Bitvec.repr b) }
Equations
- instDecidableUlt = decEq (Bitvec.uborrow x y) true
Equations
- instDecidableUgt = decEq (Bitvec.uborrow y x) true