|
| 1 | +(set-logic QF_BV) |
| 2 | + |
| 3 | +; From https://rise4fun.com/z3/tutorialcontent/guide |
| 4 | + |
| 5 | +; Basic Bitvector Arithmetic |
| 6 | +(define-fun b01 () Bool (= (bvadd #x07 #x03) #x0a)) ; addition |
| 7 | +(define-fun b02 () Bool (= (bvsub #x07 #x03) #x04)) ; subtraction |
| 8 | +(define-fun b03 () Bool (= (bvneg #x07) #xf9)) ; unary minus |
| 9 | +(define-fun b04 () Bool (= (bvmul #x07 #x03) #x15)) ; multiplication |
| 10 | +(define-fun b05 () Bool (= (bvurem #x07 #x03) #x01)) ; unsigned remainder |
| 11 | +(define-fun b06 () Bool (= (bvsrem #x07 #x03) #x01)) ; signed remainder |
| 12 | +(define-fun b07 () Bool (= (bvsmod #x07 #x03) #x01)) ; signed modulo |
| 13 | +(define-fun b08 () Bool (= (bvshl #x07 #x03) #x38)) ; shift left |
| 14 | +(define-fun b09 () Bool (= (bvlshr #xf0 #x03) #x1e)) ; unsigned (logical) shift right |
| 15 | +(define-fun b10 () Bool (= (bvashr #xf0 #x03) #xfe)) ; signed (arithmetical) shift right#x0a |
| 16 | + |
| 17 | +; Bitwise Operations |
| 18 | + |
| 19 | +(define-fun w1 () Bool (= (bvor #x6 #x3) #x7)) ; bitwise or |
| 20 | +(define-fun w2 () Bool (= (bvand #x6 #x3) #x2)) ; bitwise and |
| 21 | +(define-fun w3 () Bool (= (bvnot #x6) #x9)) ; bitwise not |
| 22 | +(define-fun w4 () Bool (= (bvnand #x6 #x3) #xd)) ; bitwise nand |
| 23 | +(define-fun w5 () Bool (= (bvnor #x6 #x3) #x8)) ; bitwise nor |
| 24 | +(define-fun w6 () Bool (= (bvxnor #x6 #x3) #xa)) ; bitwise xnor |
| 25 | + |
| 26 | +; We can prove a bitwise version of deMorgan's law |
| 27 | + |
| 28 | +(declare-const x (_ BitVec 64)) |
| 29 | +(declare-const y (_ BitVec 64)) |
| 30 | +(define-fun d01 () Bool (= (bvand (bvnot x) (bvnot y)) (bvnot (bvor x y)))) |
| 31 | + |
| 32 | +; There is a fast way to check that fixed size numbers are powers of two |
| 33 | + |
| 34 | +(define-fun is-power-of-two ((x (_ BitVec 4))) Bool |
| 35 | + (= #x0 (bvand x (bvsub x #x1)))) |
| 36 | +(declare-const a (_ BitVec 4)) |
| 37 | +(define-fun power-test () Bool |
| 38 | + (= (is-power-of-two a) |
| 39 | + (or (= a #x0) |
| 40 | + (= a #x1) |
| 41 | + (= a #x2) |
| 42 | + (= a #x4) |
| 43 | + (= a #x8)))) |
| 44 | + |
| 45 | +; Predicates over Bitvectors |
| 46 | + |
| 47 | +(define-fun p1 () Bool (= (bvule #x0a #xf0) true)) ; unsigned less or equal |
| 48 | +(define-fun p2 () Bool (= (bvult #x0a #xf0) true)) ; unsigned less than |
| 49 | +(define-fun p3 () Bool (= (bvuge #x0a #xf0) false)) ; unsigned greater or equal |
| 50 | +(define-fun p4 () Bool (= (bvugt #x0a #xf0) false)) ; unsigned greater than |
| 51 | +(define-fun p5 () Bool (= (bvsle #x0a #xf0) false)) ; signed less or equal |
| 52 | +(define-fun p6 () Bool (= (bvslt #x0a #xf0) false)) ; signed less than |
| 53 | +(define-fun p7 () Bool (= (bvsge #x0a #xf0) true)) ; signed greater or equal |
| 54 | +(define-fun p8 () Bool (= (bvsgt #x0a #xf0) true)) ; signed greater than |
| 55 | + |
| 56 | +; all must be true |
| 57 | + |
| 58 | +(assert (not (and |
| 59 | + b01 b02 b03 b04 b05 b06 b07 b08 b09 b10 |
| 60 | + d01 |
| 61 | + power-test |
| 62 | + p1 p2 p3 p4 p5 p6 p7 p8))) |
| 63 | + |
| 64 | +(check-sat) |
0 commit comments