Bitvectors
(require bv) | package: bv |
This module is inspired by the bitvector interface exposed by Rosette. Functions with the same name will behave the same.
Bitvectors are constant-sized vectors of bits and the provided interface extends the one defined in the Rosette Bitvector Guide.
For example, all of the non-symbolic examples in Rosette’s Guide work as expected:
> (bv 4 (bitvector 7)) ; a bitvector literal of size 7 {bv #b0000100 7}
> (bv 4 7) ; a shorthand for the same literal {bv #b0000100 7}
> (bvslt (bv 4 7) (bv -1 7)) ; signed 7-bit < comparison of 4 and -1 #f
> (bvult (bv 4 7) (bv -1 7)) ; unsigned 7-bit < comparison of 4 and -1 #t
1 Constructing Bitvectors
To construct bitvectors you can use the function bv.
procedure
val : (and/c integer? (not/c term?) (not/c union?))
size :
(and/c (or/c bitvector? (and/c integer? positive?)) (not/c term?) (not/c union?))
procedure
(bitvector size) → bitvector?
size : (and/c integer? positive? (not/c term?) (not/c union?))
procedure
(bitvector? v) → boolean?
v : any/c
> (define bv6? (bitvector 6)) > (define bv7? (bitvector 7)) > (bitvector? bv6?) ; a concrete bitvector type #t
> (bitvector? integer?) ; not a bitvector type #f
> (bitvector? 3) ; not a type #f
2 Comparison Operators
procedure
x : (bitvector n) y : (bitvector n) (bvslt x y) → boolean? x : (bitvector n) y : (bitvector n) (bvult x y) → boolean? x : (bitvector n) y : (bitvector n) (bvsle x y) → boolean? x : (bitvector n) y : (bitvector n) (bvule x y) → boolean? x : (bitvector n) y : (bitvector n) (bvsgt x y) → boolean? x : (bitvector n) y : (bitvector n) (bvugt x y) → boolean? x : (bitvector n) y : (bitvector n) (bvsge x y) → boolean? x : (bitvector n) y : (bitvector n) (bvuge x y) → boolean? x : (bitvector n) y : (bitvector n)
3 Bitwise Operators
4 Arithmetic Operators
procedure
x : (bitvector n) (bvsub x ...+) → (bitvector n) x : (bitvector n) (bvmul x ...+) → (bitvector n) x : (bitvector n)
procedure
x : (bitvector n) y : (bitvector n) (bvudiv x y) → (bitvector n) x : (bitvector n) y : (bitvector n) (bvsrem x y) → (bitvector n) x : (bitvector n) y : (bitvector n) (bvurem x y) → (bitvector n) x : (bitvector n) y : (bitvector n) (bvsmod x y) → (bitvector n) x : (bitvector n) y : (bitvector n)
5 Conversion Operators
procedure
(sign-extend x t) → bv?
x : bv? t : (or/c bitvector? union?) (zero-extend x t) → bv? x : bv? t : (or/c bitvector? union?)
> (sign-extend (bv -3 4) (bitvector 6)) {bv #b111101 6}
> (zero-extend (bv -3 4) (bitvector 6)) {bv #b001101 6}
procedure
(bitvector->integer x) → integer?
x : bv? (bitvector->natural x) → integer? x : bv?
> (bitvector->integer (bv -1 4)) -1
> (bitvector->natural (bv -1 4)) 15
procedure
(integer->bitvector i t) → bv?
i : integer? t : (or/c bitvector? union?)
> (integer->bitvector 4 (bitvector 2)) {bv #b00 2}
> (integer->bitvector 15 (bitvector 4)) {bv #xf 4}
6 Additional Operators
procedure
(bool->bitvector b [t]) → bv?
b : any/c t : (or/c positive-integer? (bitvector n)) = (bitvector 1)
> (bool->bitvector #f 3) {bv #b000 3}
> (bool->bitvector "non-false-value") {bv #b1 1}
7 Extensions
The following operators are an extension to what Rosette provides.
procedure
n : exact-positive-integer?
procedure
(random-bv/limits n min max) → bv?
n : exact-positive-integer? min : exact-nonnegative-integer? max : exact-nonnegative-integer?