diff --git a/src/Init/Data/BitVec/Lemmas.lean b/src/Init/Data/BitVec/Lemmas.lean index 3d4a7d3445d36..9f554fa1c89a6 100644 --- a/src/Init/Data/BitVec/Lemmas.lean +++ b/src/Init/Data/BitVec/Lemmas.lean @@ -857,6 +857,33 @@ theorem sshiftRight_add {x : BitVec w} {m n : Nat} : @[simp] theorem sshiftRight_eq' (x : BitVec w) : x.sshiftRight' y = x.sshiftRight y.toNat := rfl +/-! ### udiv -/ + +theorem udiv_eq {x y : BitVec n} : x.udiv y = BitVec.ofNat n (x.toNat / y.toNat) := by + have h : x.toNat / y.toNat < 2 ^ n := by + exact Nat.lt_of_le_of_lt (Nat.div_le_self ..) (by omega) + simp [udiv, bv_toNat, h, Nat.mod_eq_of_lt] + +@[simp, bv_toNat] +theorem toNat_udiv {x y : BitVec n} : (x.udiv y).toNat = x.toNat / y.toNat := by + simp only [udiv_eq] + by_cases h : y = 0 + · simp [h] + · rw [toNat_ofNat, Nat.mod_eq_of_lt] + exact Nat.lt_of_le_of_lt (Nat.div_le_self ..) (by omega) + +/-! ### umod -/ + +theorem umod_eq {x y : BitVec n} : + x.umod y = BitVec.ofNat n (x.toNat % y.toNat) := by + have h : x.toNat % y.toNat < 2 ^ n := by + apply Nat.lt_of_le_of_lt (Nat.mod_le _ _) x.isLt + simp [umod, bv_toNat, Nat.mod_eq_of_lt h] + +@[simp, bv_toNat] +theorem toNat_umod {x y : BitVec n} : + (x.umod y).toNat = x.toNat % y.toNat := by rfl + /-! ### signExtend -/ /-- Equation theorem for `Int.sub` when both arguments are `Int.ofNat` -/