Skip to content

Commit

Permalink
feat: do not use toNat for division (#703)
Browse files Browse the repository at this point in the history
... also clean up some proofs.
  • Loading branch information
tobiasgrosser authored Oct 14, 2024
1 parent b52c067 commit 01ee836
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions SSA/Projects/InstCombine/LLVM/Semantics.lean
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ Division by zero is undefined behavior.
-/
@[simp_llvm]
def udiv? {w : Nat} (x y : BitVec w) : IntW w :=
if y = 0
then none
else pure <| BitVec.ofInt w (x.toNat / y.toNat)
if y = 0 then
none
else
pure <| x / y

structure ExactFlag where
exact : Bool := false
Expand Down Expand Up @@ -214,9 +215,10 @@ at width 2, -4 / -1 is considered overflow!
-- is no magnitude to overflow.
@[simp_llvm]
def sdiv? {w : Nat} (x y : BitVec w) : IntW w :=
if y == 0 || (w != 1 && x == (BitVec.intMin w) && y == -1)
then .none
else pure (BitVec.sdiv x y)
if y == 0 || (w != 1 && x == (BitVec.intMin w) && y == -1) then
none
else
pure (x.sdiv y)

theorem sdiv?_denom_zero_eq_none {w : Nat} (x : BitVec w) :
LLVM.sdiv? x 0 = none := by
Expand Down Expand Up @@ -253,9 +255,10 @@ Taking the remainder of a division by zero is undefined behavior.
-/
@[simp_llvm]
def urem? {w : Nat} (x y : BitVec w) : IntW w :=
if y = 0
then none
else pure <| BitVec.ofNat w (x.toNat % y.toNat)
if y = 0 then
none
else
pure <| x % y

@[simp_llvm_option]
def urem {w : Nat} (x y : IntW w) : IntW w := do
Expand Down

0 comments on commit 01ee836

Please sign in to comment.