Basic operations on bitvectors #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
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
specific version of vector.append
Equations
Shift operations #
shl x i
is the bitvector obtained by left-shifting x
i
times and padding with ff
.
If x.length < i
then this will return the all-ff
s bitvector.
Equations
- x.shl i = bitvec.cong _ ((vector.drop i x).append (vector.replicate (linear_order.min n i) bool.ff))
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
- x.fill_shr i fill = bitvec.cong _ ((vector.replicate (linear_order.min n i) fill).append (vector.take (n - i) x))
signed shift right
Equations
- x.sshr i = vector.head x ::ᵥ bitvec.fill_shr (vector.tail x) i (vector.head x)
- _x.sshr _x_1 = vector.nil
Bitwise operations #
bitwise not
Equations
bitwise and
Equations
bitwise xor
Equations
Arithmetic operators #
Add with carry (no overflow)
Equations
- bitvec.has_zero = {zero := bitvec.zero n}
Equations
- bitvec.has_one = {one := bitvec.one n}
Equations
- bitvec.has_add = {add := bitvec.add n}
Equations
- bitvec.has_sub = {sub := bitvec.sub n}
Equations
- bitvec.has_neg = {neg := bitvec.neg n}
Equations
- bitvec.has_mul = {mul := bitvec.mul n}
Comparison operators #
unsigned less-than proposition
Instances for bitvec.ult
unsigned greater-than proposition
Instances for bitvec.ugt
sborrow x y
returns tt
iff x < y
as two's complement integers
Equations
- x.sborrow y = bitvec.sborrow._match_1 n x y (vector.head x, vector.head y)
- _x.sborrow _x_1 = bool.ff
- bitvec.sborrow._match_1 n x y (bool.tt, bool.tt) = bitvec.uborrow (vector.tail x) (vector.tail y)
- bitvec.sborrow._match_1 n x y (bool.tt, bool.ff) = bool.tt
- bitvec.sborrow._match_1 n x y (bool.ff, bool.tt) = bool.ff
- bitvec.sborrow._match_1 n x y (bool.ff, bool.ff) = bitvec.uborrow (vector.tail x) (vector.tail y)
Create a bitvector from a nat
Equations
- bitvec.of_nat n.succ x = vector.append (bitvec.of_nat n (x / 2)) (decidable.to_bool (x % 2 = 1) ::ᵥ vector.nil)
- bitvec.of_nat 0 x = vector.nil
Create a bitvector in the two's complement representation from an int
Equations
- bitvec.of_int n -[1+ m] = bool.tt ::ᵥ (bitvec.of_nat n m).not
- bitvec.of_int n (int.of_nat m) = bool.ff ::ᵥ bitvec.of_nat n m
add_lsb r b
is r + r + 1
if b
is tt
and r + r
otherwise.
Equations
- bitvec.add_lsb r b = r + r + cond b 1 0
Return the natural number encoded by the input bitvector
Equations
Return the integer encoded by the input bitvector
Equations
- v.to_int = cond (vector.head v) -[1+ (bitvec.not (vector.tail v)).to_nat] (int.of_nat (bitvec.to_nat (vector.tail v)))
- _x.to_int = 0
Miscellaneous instances #
Equations
- bitvec.has_repr n = {repr := repr n}