All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cvc5-cvc5-1.2.0.src.theory.bv.rewrites-elimination Maven / Gradle / Ivy

; -- Operator Elimination Rules --

(define-rule bv-ugt-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvugt x y)
  (bvult y x))
(define-rule bv-uge-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvuge x y)
  (bvule y x))
(define-rule bv-sgt-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvsgt x y)
  (bvslt y x))
(define-rule bv-sge-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvsge x y)
  (bvsle y x))
(define-rule bv-slt-eliminate
  ((x ?BitVec) (y ?BitVec))
  (def (pad (bvshl (@bv 1 (@bvsize x)) (@bv (- (@bvsize x) 1) (@bvsize x)))))
  (bvslt x y)
  (bvult
    (bvadd x pad)
    (bvadd y pad)
  ))
(define-rule bv-sle-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvsle x y)
  (not (bvslt y x)))

(define-rule bv-redor-eliminate
  ((x ?BitVec))
  (bvredor x)
  (bvnot (bvcomp x (@bv 0 (@bvsize x)))))
(define-rule bv-redand-eliminate
  ((x ?BitVec))
  (bvredand x)
  (bvcomp x (bvnot (@bv 0 (@bvsize x)))))

(define-rule bv-ule-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvule x y)
  (not (bvult y x)))
(define-rule bv-comp-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvcomp x y)
  (ite (= x y) (@bv 1 1) (@bv 0 1)))
(define-cond-rule bv-repeat-eliminate-1
  ((x ?BitVec) (n Int))
  (> n 1)
  (repeat n x)
  (concat x (repeat (- n 1) x)))
(define-cond-rule bv-repeat-eliminate-2
  ((x ?BitVec) (n Int))
  (= n 1)
  (repeat n x)
  x)

(define-cond-rule bv-rotate-left-eliminate-1
  ((x ?BitVec) (amount Int))
  (def (n (@bvsize x)) (a (mod amount n)))
  (not (= a 0))
  (rotate_left amount x)
  (concat
    (extract (- n (+ 1 a)) 0 x)
    (extract (- n 1) (- n a) x)))
(define-cond-rule bv-rotate-left-eliminate-2
  ((x ?BitVec) (amount Int))
  (= (mod amount (@bvsize x)) 0)
  (rotate_left amount x)
  x)
(define-cond-rule bv-rotate-right-eliminate-1
  ((x ?BitVec) (amount Int))
  (def (n (@bvsize x)) (a (mod amount n)))
  (not (= a 0))
  (rotate_right amount x)
  (concat
    (extract (- a 1) 0 x)
    (extract (- n 1) a x)))
(define-cond-rule bv-rotate-right-eliminate-2
  ((x ?BitVec) (amount Int))
  (= (mod amount (@bvsize x)) 0)
  (rotate_right amount x)
  x)

(define-rule bv-nand-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvnand x y)
  (bvnot (bvand x y)))
(define-rule bv-nor-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvnor x y)
  (bvnot (bvor x y)))
(define-rule bv-xnor-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvxnor x y)
  (bvnot (bvxor x y)))

(define-rule bv-sdiv-eliminate
  ((x ?BitVec) (y ?BitVec))
  (def
    (n (@bvsize x))
    (xLt0 (= (extract (- n 1) (- n 1) x) (@bv 1 1)))
    (yLt0 (= (extract (- n 1) (- n 1) y) (@bv 1 1)))
    (rUdiv (bvudiv (ite xLt0 (bvneg x) x) (ite yLt0 (bvneg y) y)))
  )
  (bvsdiv x y)
  (ite (xor xLt0 yLt0) (bvneg rUdiv) rUdiv))
(define-rule bv-sdiv-eliminate-fewer-bitwise-ops
  ((x ?BitVec) (y ?BitVec))
  (def
    (n (@bvsize x))
    (xLt0 (bvuge x (concat (@bv 1 1) (@bv 0 (- n 1)))))
    (yLt0 (bvuge y (concat (@bv 1 1) (@bv 0 (- n 1)))))
    (rUdiv (bvudiv (ite xLt0 (bvneg x) x) (ite yLt0 (bvneg y) y)))
  )
  (bvsdiv x y)
  (ite (xor xLt0 yLt0) (bvneg rUdiv) rUdiv))

(define-rule bv-zero-extend-eliminate
  ((x ?BitVec) (n Int))
  (zero_extend n x)
  (concat (@bv 0 n) x))
(define-rule bv-sign-extend-eliminate
  ((x ?BitVec) (n Int))
  (def (s (@bvsize x)))
  (sign_extend n x)
  (concat (repeat n (extract (- s 1) (- s 1) x)) x))

(define-rule bv-uaddo-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvuaddo x y)
  (= (extract (- (@bvsize x) 1) (- (@bvsize x) 1)
      (bvadd (concat (@bv 0 1) x) (concat (@bv 0 1) y)))
    (@bv 1 1)
  ))
(define-rule bv-saddo-eliminate
  ((x ?BitVec) (y ?BitVec))
  (def
    (n (@bvsize x))
    (xS (extract (- n 1) (- n 1) x))
    (yS (extract (- n 1) (- n 1) y))
    (aS (extract (- n 1) (- n 1) (bvadd x y)))
  )
  (bvsaddo x y)
  (or
    (and (and (= xS (@bv 0 1)) (= yS (@bv 0 1))) (= aS (@bv 1 1)))
    (and (and (= xS (@bv 1 1)) (= yS (@bv 1 1))) (= aS (@bv 0 1)))
  ))
(define-rule bv-sdivo-eliminate
  ((x ?BitVec) (y ?BitVec))
  (bvsdivo x y)
  (and
    (= x (concat (@bv 1 1) (@bv 0 (- (@bvsize x) 1))))
    (= y (bvnot (@bv 0 (@bvsize y))))
  ))
(define-rule bv-smod-eliminate
  ((x ?BitVec) (y ?BitVec))
  (def
    (n (@bvsize x))
    (xLt0 (= (extract (- n 1) (- n 1) x) (@bv 1 1)))
    (yLt0 (= (extract (- n 1) (- n 1) y) (@bv 1 1)))
    (xAbs (ite xLt0 (bvneg x) x))
    (yAbs (ite yLt0 (bvneg y) y))
    (u (bvurem xAbs yAbs))
  )
  (bvsmod x y)
  (ite (= u (@bv 0 n))
    u
    (ite (and (not xLt0) (not yLt0))
      u
      (ite (and xLt0 (not yLt0))
        (bvadd (bvneg u) y)
        (ite (and (not xLt0) yLt0)
          (bvadd u y)
          (bvneg u))))))
(define-rule bv-smod-eliminate-fewer-bitwise-ops
  ((x ?BitVec) (y ?BitVec))
  (def
    (n (@bvsize x))
    (xLt0 (bvuge x (concat (@bv 1 1) (@bv 0 (- n 1)))))
    (yLt0 (bvuge y (concat (@bv 1 1) (@bv 0 (- n 1)))))
    (xAbs (ite xLt0 (bvneg x) x))
    (yAbs (ite yLt0 (bvneg y) y))
    (u (bvurem xAbs yAbs))
  )
  (bvsmod x y)
  (ite (= u (@bv 0 n))
    u
    (ite (and (not xLt0) (not yLt0))
      u
      (ite (and xLt0 (not yLt0))
        (bvadd (bvneg u) y)
        (ite (and (not xLt0) yLt0)
          (bvadd u y)
          (bvneg u))))))

(define-rule bv-srem-eliminate
  ((x ?BitVec) (y ?BitVec))
  (def
    (n (@bvsize x))
    (xLt0 (extract (- n 1) (- n 1) x))
    (yLt0 (extract (- n 1) (- n 1) y))
    (xAbs (bvite xLt0 (bvneg x) x))
    (yAbs (bvite yLt0 (bvneg y) y))
    (u (bvurem xAbs yAbs))
  )
  (bvsrem x y)
  (bvite xLt0 (bvneg u) u))
(define-rule bv-srem-eliminate-fewer-bitwise-ops
  ((x ?BitVec) (y ?BitVec))
  (def
    (n (@bvsize x))
    (xLt0 (bvuge x (concat (@bv 1 1) (@bv 0 (- n 1)))))
    (yLt0 (bvuge y (concat (@bv 1 1) (@bv 0 (- n 1)))))
    (xAbs (ite xLt0 (bvneg x) x))
    (yAbs (ite yLt0 (bvneg y) y))
    (u (bvurem xAbs yAbs))
  )
  (bvsrem x y)
  (ite xLt0 (bvneg u) u))

(define-rule bv-usubo-eliminate
  ((x ?BitVec) (y ?BitVec))
  (def
    (n (@bvsize x))
    (s (bvsub (zero_extend 1 x) (zero_extend 1 y)))
  )
  (bvusubo x y)
  (= (extract n n s) (@bv 1 1)))
; Overflow occurs when
; 1. (N - P) = P
; 2. (P - N) = N
(define-rule bv-ssubo-eliminate
  ((x ?BitVec) (y ?BitVec))
  (def
    (n (@bvsize x))
    (xLt0 (= (extract (- n 1) (- n 1) x) (@bv 1 1)))
    (yLt0 (= (extract (- n 1) (- n 1) y) (@bv 1 1)))
    (s (bvsub x y))
    (sLt0 (= (extract (- n 1) (- n 1) s) (@bv 1 1)))
  )
  (bvssubo x y)
  (or
    (and xLt0 (not yLt0) (not sLt0))
    (and (not xLt0) yLt0 sLt0)))




© 2015 - 2025 Weber Informatics LLC | Privacy Policy