@@ -139,11 +139,11 @@ def adc (x y : BitVec w) : Bool → Bool × BitVec w :=
139
139
iunfoldr fun (i : Fin w) c => adcb (x.getLsbD i) (y.getLsbD i) c
140
140
141
141
theorem getLsbD_add_add_bool {i : Nat} (i_lt : i < w) (x y : BitVec w) (c : Bool) :
142
- getLsbD (x + y + zeroExtend w (ofBool c)) i =
142
+ getLsbD (x + y + setWidth w (ofBool c)) i =
143
143
Bool.xor (getLsbD x i) (Bool.xor (getLsbD y i) (carry i x y c)) := by
144
144
let ⟨x, x_lt⟩ := x
145
145
let ⟨y, y_lt⟩ := y
146
- simp only [getLsbD, toNat_add, toNat_zeroExtend , i_lt, toNat_ofFin, toNat_ofBool,
146
+ simp only [getLsbD, toNat_add, toNat_setWidth , i_lt, toNat_ofFin, toNat_ofBool,
147
147
Nat.mod_add_mod, Nat.add_mod_mod]
148
148
apply Eq.trans
149
149
rw [← Nat.div_add_mod x (2 ^i), ← Nat.div_add_mod y (2 ^i)]
@@ -165,11 +165,11 @@ theorem getLsbD_add {i : Nat} (i_lt : i < w) (x y : BitVec w) :
165
165
simpa using getLsbD_add_add_bool i_lt x y false
166
166
167
167
theorem adc_spec (x y : BitVec w) (c : Bool) :
168
- adc x y c = (carry w x y c, x + y + zeroExtend w (ofBool c)) := by
168
+ adc x y c = (carry w x y c, x + y + setWidth w (ofBool c)) := by
169
169
simp only [adc]
170
170
apply iunfoldr_replace
171
171
(fun i => carry i x y c)
172
- (x + y + zeroExtend w (ofBool c))
172
+ (x + y + setWidth w (ofBool c))
173
173
c
174
174
case init =>
175
175
simp [carry, Nat.mod_one]
@@ -306,12 +306,12 @@ theorem mulRec_succ_eq (x y : BitVec w) (s : Nat) :
306
306
Recurrence lemma: truncating to `i+1` bits and then zero extending to `w`
307
307
equals truncating upto `i` bits `[0..i-1]`, and then adding the `i`th bit of `x`.
308
308
-/
309
- theorem zeroExtend_truncate_succ_eq_zeroExtend_truncate_add_twoPow (x : BitVec w) (i : Nat) :
310
- zeroExtend w (x.truncate (i + 1 )) =
311
- zeroExtend w (x.truncate i) + (x &&& twoPow w i) := by
309
+ theorem setWidth_setWidth_succ_eq_setWidth_setWidth_add_twoPow (x : BitVec w) (i : Nat) :
310
+ setWidth w (x.setWidth (i + 1 )) =
311
+ setWidth w (x.setWidth i) + (x &&& twoPow w i) := by
312
312
rw [add_eq_or_of_and_eq_zero]
313
313
· ext k
314
- simp only [getLsbD_zeroExtend , Fin.is_lt, decide_True, Bool.true_and, getLsbD_or, getLsbD_and]
314
+ simp only [getLsbD_setWidth , Fin.is_lt, decide_True, Bool.true_and, getLsbD_or, getLsbD_and]
315
315
by_cases hik : i = k
316
316
· subst hik
317
317
simp
@@ -322,41 +322,49 @@ theorem zeroExtend_truncate_succ_eq_zeroExtend_truncate_add_twoPow (x : BitVec w
322
322
· have hik'' : ¬ (k < i) := by omega
323
323
simp [hik', hik'']
324
324
· ext k
325
- simp only [and_twoPow, getLsbD_and, getLsbD_zeroExtend , Fin.is_lt, decide_True, Bool.true_and,
325
+ simp only [and_twoPow, getLsbD_and, getLsbD_setWidth , Fin.is_lt, decide_True, Bool.true_and,
326
326
getLsbD_zero, and_eq_false_imp, and_eq_true, decide_eq_true_eq, and_imp]
327
327
by_cases hi : x.getLsbD i <;> simp [hi] <;> omega
328
328
329
+ @[deprecated setWidth_setWidth_succ_eq_setWidth_setWidth_add_twoPow (since := "2024-09-18" ),
330
+ inherit_doc setWidth_setWidth_succ_eq_setWidth_setWidth_add_twoPow]
331
+ abbrev zeroExtend_truncate_succ_eq_zeroExtend_truncate_add_twoPow :=
332
+ @setWidth_setWidth_succ_eq_setWidth_setWidth_add_twoPow
333
+
329
334
/--
330
335
Recurrence lemma: multiplying `x` with the first `s` bits of `y` is the
331
336
same as truncating `y` to `s` bits, then zero extending to the original length,
332
337
and performing the multplication. -/
333
- theorem mulRec_eq_mul_signExtend_truncate (x y : BitVec w) (s : Nat) :
334
- mulRec x y s = x * ((y.truncate (s + 1 )).zeroExtend w) := by
338
+ theorem mulRec_eq_mul_signExtend_setWidth (x y : BitVec w) (s : Nat) :
339
+ mulRec x y s = x * ((y.setWidth (s + 1 )).setWidth w) := by
335
340
induction s
336
341
case zero =>
337
342
simp only [mulRec_zero_eq, ofNat_eq_ofNat, Nat.reduceAdd]
338
343
by_cases y.getLsbD 0
339
344
case pos hy =>
340
- simp only [hy, ↓reduceIte, truncate, zeroExtend_one_eq_ofBool_getLsb_zero ,
345
+ simp only [hy, ↓reduceIte, setWidth_one_eq_ofBool_getLsb_zero ,
341
346
ofBool_true, ofNat_eq_ofNat]
342
- rw [zeroExtend_ofNat_one_eq_ofNat_one_of_lt (by omega)]
347
+ rw [setWidth_ofNat_one_eq_ofNat_one_of_lt (by omega)]
343
348
simp
344
349
case neg hy =>
345
- simp [hy, zeroExtend_one_eq_ofBool_getLsb_zero ]
350
+ simp [hy, setWidth_one_eq_ofBool_getLsb_zero ]
346
351
case succ s' hs =>
347
352
rw [mulRec_succ_eq, hs]
348
353
have heq :
349
354
(if y.getLsbD (s' + 1 ) = true then x <<< (s' + 1 ) else 0 ) =
350
355
(x * (y &&& (BitVec.twoPow w (s' + 1 )))) := by
351
356
simp only [ofNat_eq_ofNat, and_twoPow]
352
357
by_cases hy : y.getLsbD (s' + 1 ) <;> simp [hy]
353
- rw [heq, ← BitVec.mul_add, ← zeroExtend_truncate_succ_eq_zeroExtend_truncate_add_twoPow]
358
+ rw [heq, ← BitVec.mul_add, ← setWidth_setWidth_succ_eq_setWidth_setWidth_add_twoPow]
359
+
360
+ @[deprecated mulRec_eq_mul_signExtend_setWidth (since := "2024-09-18" ),
361
+ inherit_doc mulRec_eq_mul_signExtend_setWidth]
362
+ abbrev mulRec_eq_mul_signExtend_truncate := @mulRec_eq_mul_signExtend_setWidth
354
363
355
364
theorem getLsbD_mul (x y : BitVec w) (i : Nat) :
356
365
(x * y).getLsbD i = (mulRec x y w).getLsbD i := by
357
- simp only [mulRec_eq_mul_signExtend_truncate]
358
- rw [truncate, ← truncate_eq_zeroExtend, ← truncate_eq_zeroExtend,
359
- truncate_truncate_of_le]
366
+ simp only [mulRec_eq_mul_signExtend_setWidth]
367
+ rw [setWidth_setWidth_of_le]
360
368
· simp
361
369
· omega
362
370
@@ -402,22 +410,22 @@ theorem shiftLeft_or_of_and_eq_zero {x : BitVec w₁} {y z : BitVec w₂}
402
410
`shiftLeftRec x y n` shifts `x` to the left by the first `n` bits of `y`.
403
411
-/
404
412
theorem shiftLeftRec_eq {x : BitVec w₁} {y : BitVec w₂} {n : Nat} :
405
- shiftLeftRec x y n = x <<< (y.truncate (n + 1 )).zeroExtend w₂ := by
413
+ shiftLeftRec x y n = x <<< (y.setWidth (n + 1 )).setWidth w₂ := by
406
414
induction n generalizing x y
407
415
case zero =>
408
416
ext i
409
- simp only [shiftLeftRec_zero, twoPow_zero, Nat.reduceAdd, truncate_one ,
410
- and_one_eq_zeroExtend_ofBool_getLsbD ]
417
+ simp only [shiftLeftRec_zero, twoPow_zero, Nat.reduceAdd, setWidth_one ,
418
+ and_one_eq_setWidth_ofBool_getLsbD ]
411
419
case succ n ih =>
412
420
simp only [shiftLeftRec_succ, and_twoPow]
413
421
rw [ih]
414
422
by_cases h : y.getLsbD (n + 1 )
415
423
· simp only [h, ↓reduceIte]
416
- rw [zeroExtend_truncate_succ_eq_zeroExtend_truncate_or_twoPow_of_getLsbD_true h,
424
+ rw [setWidth_setWidth_succ_eq_setWidth_setWidth_or_twoPow_of_getLsbD_true h,
417
425
shiftLeft_or_of_and_eq_zero]
418
426
simp [and_twoPow]
419
427
· simp only [h, false_eq_true, ↓reduceIte, shiftLeft_zero']
420
- rw [zeroExtend_truncate_succ_eq_zeroExtend_truncate_of_getLsbD_false (i := n + 1 )]
428
+ rw [setWidth_setWidth_succ_eq_setWidth_setWidth_of_getLsbD_false (i := n + 1 )]
421
429
simp [h]
422
430
423
431
/--
@@ -466,18 +474,18 @@ theorem sshiftRight'_or_of_and_eq_zero {x : BitVec w₁} {y z : BitVec w₂}
466
474
toNat_add_of_and_eq_zero h, sshiftRight_add]
467
475
468
476
theorem sshiftRightRec_eq (x : BitVec w₁) (y : BitVec w₂) (n : Nat) :
469
- sshiftRightRec x y n = x.sshiftRight' ((y.truncate (n + 1 )).zeroExtend w₂) := by
477
+ sshiftRightRec x y n = x.sshiftRight' ((y.setWidth (n + 1 )).setWidth w₂) := by
470
478
induction n generalizing x y
471
479
case zero =>
472
480
ext i
473
- simp [twoPow_zero, Nat.reduceAdd, and_one_eq_zeroExtend_ofBool_getLsbD, truncate_one ]
481
+ simp [twoPow_zero, Nat.reduceAdd, and_one_eq_setWidth_ofBool_getLsbD, setWidth_one ]
474
482
case succ n ih =>
475
483
simp only [sshiftRightRec_succ_eq, and_twoPow, ih]
476
484
by_cases h : y.getLsbD (n + 1 )
477
- · rw [zeroExtend_truncate_succ_eq_zeroExtend_truncate_or_twoPow_of_getLsbD_true h,
485
+ · rw [setWidth_setWidth_succ_eq_setWidth_setWidth_or_twoPow_of_getLsbD_true h,
478
486
sshiftRight'_or_of_and_eq_zero (by simp [and_twoPow]), h]
479
487
simp
480
- · rw [zeroExtend_truncate_succ_eq_zeroExtend_truncate_of_getLsbD_false (i := n + 1 )
488
+ · rw [setWidth_setWidth_succ_eq_setWidth_setWidth_of_getLsbD_false (i := n + 1 )
481
489
(by simp [h])]
482
490
simp [h]
483
491
@@ -529,20 +537,20 @@ theorem ushiftRight'_or_of_and_eq_zero {x : BitVec w₁} {y z : BitVec w₂}
529
537
simp [← add_eq_or_of_and_eq_zero _ _ h, toNat_add_of_and_eq_zero h, shiftRight_add]
530
538
531
539
theorem ushiftRightRec_eq (x : BitVec w₁) (y : BitVec w₂) (n : Nat) :
532
- ushiftRightRec x y n = x >>> (y.truncate (n + 1 )).zeroExtend w₂ := by
540
+ ushiftRightRec x y n = x >>> (y.setWidth (n + 1 )).setWidth w₂ := by
533
541
induction n generalizing x y
534
542
case zero =>
535
543
ext i
536
544
simp only [ushiftRightRec_zero, twoPow_zero, Nat.reduceAdd,
537
- and_one_eq_zeroExtend_ofBool_getLsbD, truncate_one ]
545
+ and_one_eq_setWidth_ofBool_getLsbD, setWidth_one ]
538
546
case succ n ih =>
539
547
simp only [ushiftRightRec_succ, and_twoPow]
540
548
rw [ih]
541
549
by_cases h : y.getLsbD (n + 1 ) <;> simp only [h, ↓reduceIte]
542
- · rw [zeroExtend_truncate_succ_eq_zeroExtend_truncate_or_twoPow_of_getLsbD_true h,
550
+ · rw [setWidth_setWidth_succ_eq_setWidth_setWidth_or_twoPow_of_getLsbD_true h,
543
551
ushiftRight'_or_of_and_eq_zero]
544
552
simp [and_twoPow]
545
- · simp [zeroExtend_truncate_succ_eq_zeroExtend_truncate_of_getLsbD_false , h]
553
+ · simp [setWidth_setWidth_succ_eq_setWidth_setWidth_of_getLsbD_false , h]
546
554
547
555
/--
548
556
Show that `x >>> y` can be written in terms of `ushiftRightRec`.
0 commit comments