@@ -162,6 +162,16 @@ theorem toNat_zero (n : Nat) : (0#n).toNat = 0 := by trivial
162
162
@[simp] theorem toNat_mod_cancel (x : BitVec n) : x.toNat % (2 ^n) = x.toNat :=
163
163
Nat.mod_eq_of_lt x.isLt
164
164
165
+ @[simp] theorem sub_toNat_mod_cancel {x : BitVec w} (h : ¬ x = 0 #w) :
166
+ (2 ^ w - x.toNat) % 2 ^ w = 2 ^ w - x.toNat := by
167
+ simp only [toNat_eq, toNat_ofNat, Nat.zero_mod] at h
168
+ rw [Nat.mod_eq_of_lt (by omega)]
169
+
170
+ @[simp] theorem sub_sub_toNat_cancel {x : BitVec w} :
171
+ 2 ^ w - (2 ^ w - x.toNat) = x.toNat := by
172
+ simp [Nat.sub_sub_eq_min, Nat.min_eq_right]
173
+ omega
174
+
165
175
private theorem lt_two_pow_of_le {x m n : Nat} (lt : x < 2 ^ m) (le : m ≤ n) : x < 2 ^ n :=
166
176
Nat.lt_of_lt_of_le lt (Nat.pow_le_pow_of_le_right (by trivial : 0 < 2 ) le)
167
177
@@ -300,8 +310,7 @@ theorem truncate_eq_zeroExtend {v : Nat} {x : BitVec w} :
300
310
301
311
@[simp, bv_toNat] theorem toNat_zeroExtend' {m n : Nat} (p : m ≤ n) (x : BitVec m) :
302
312
(zeroExtend' p x).toNat = x.toNat := by
303
- unfold zeroExtend'
304
- simp [p, x.isLt, Nat.mod_eq_of_lt]
313
+ simp [zeroExtend']
305
314
306
315
@[bv_toNat] theorem toNat_zeroExtend (i : Nat) (x : BitVec n) :
307
316
BitVec.toNat (zeroExtend i x) = x.toNat % 2 ^i := by
@@ -1264,6 +1273,19 @@ theorem neg_eq_not_add (x : BitVec w) : -x = ~~~x + 1 := by
1264
1273
have hx : x.toNat < 2 ^w := x.isLt
1265
1274
rw [Nat.sub_sub, Nat.add_comm 1 x.toNat, ← Nat.sub_sub, Nat.sub_add_cancel (by omega)]
1266
1275
1276
+ @[simp]
1277
+ theorem neg_neg {x : BitVec w} : - - x = x := by
1278
+ by_cases h : x = 0 #w
1279
+ · simp [h]
1280
+ · simp [bv_toNat, h]
1281
+
1282
+ theorem neg_ne_iff_ne_neg {x y : BitVec w} : -x ≠ y ↔ x ≠ -y := by
1283
+ constructor
1284
+ all_goals
1285
+ intro h h'
1286
+ subst h'
1287
+ simp at h
1288
+
1267
1289
/-! ### mul -/
1268
1290
1269
1291
theorem mul_def {n} {x y : BitVec n} : x * y = (ofFin <| x.toFin * y.toFin) := by rfl
0 commit comments