Skip to content

Commit a62410e

Browse files
committed
feat: add sdiv_eq, smod_eq to allow sdiv/smod bitblasting in terms of udiv/umod
1 parent 5dea30f commit a62410e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/Init/Data/BitVec/Lemmas.lean

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,34 @@ theorem umod_eq {x y : BitVec n} :
13121312
theorem toNat_umod {x y : BitVec n} :
13131313
(x.umod y).toNat = x.toNat % y.toNat := rfl
13141314

1315+
/-! ### sdiv -/
1316+
1317+
/-- Equation theorem for `sdiv` in terms of `udiv` -/
1318+
theorem sdiv_eq (x y : BitVec w) : x.sdiv y =
1319+
match x.msb, y.msb with
1320+
| false, false => udiv x y
1321+
| false, true => - (x.udiv (- y))
1322+
| true, false => - ((- x).udiv y)
1323+
| true, true => (- x).udiv (- y) := by
1324+
rw [BitVec.sdiv]
1325+
rcases x.msb <;> rcases y.msb <;> simp [BitVec.sdiv]
1326+
1327+
/-! ### smod -/
1328+
1329+
/-- Equation theorem for `smod` in terms of `umod` -/
1330+
theorem smod_eq (x y : BitVec w) : x.smod y =
1331+
match x.msb, y.msb with
1332+
| false, false => x.umod y
1333+
| false, true =>
1334+
let u := x.umod (- y)
1335+
(if u = 0#w then u else u + y)
1336+
| true, false =>
1337+
let u := umod (.neg x) y
1338+
(if u = 0#w then u else y - u)
1339+
| true, true => - ((- x).umod (- y)) := by
1340+
simp [BitVec.smod]
1341+
rcases x.msb <;> rcases y.msb <;> simp
1342+
13151343
/-! ### signExtend -/
13161344

13171345
/-- Equation theorem for `Int.sub` when both arguments are `Int.ofNat` -/

0 commit comments

Comments
 (0)