Skip to content

Commit 0cd5136

Browse files
committed
add comments
1 parent 5cb7e07 commit 0cd5136

1 file changed

Lines changed: 9 additions & 23 deletions

File tree

DatapathVerification/CSA.lean

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,37 @@ import Blase
66

77
namespace CSA
88

9+
-- The result of a carry-save adder consists of a partial sum `s` and carry bits `t`.
910
structure CSAResult (w : ℕ) where
1011
s : BitVec w
1112
t : BitVec w
1213

14+
-- The carry-save adder splits the sum into a partial sum `s` and
15+
-- carry bits `t`, such that the original sum is recovered by
16+
-- adding `s` to the carries shifted left by 1 (i.e., t * 2).
1317
def carrySave (w : ℕ) (a b c : BitVec w) : CSAResult w :=
1418
let s := a ^^^ b ^^^ c
1519
let t := (a &&& b ||| a &&& c ||| b &&& c)
1620
⟨s, t⟩
1721

1822
#eval carrySave 4 5 5 5
1923

24+
-- a + b + c = CSA(a, b, c)
2025
theorem carrySaveAdder (w : ℕ) (a b c : BitVec w) :
2126
let ⟨s, t⟩ := carrySave w a b c
2227
a + b + c = s + t <<< 1 := by
2328
simp [carrySave]
2429
bv_automata_classic
2530

26-
-- a + b + c + d = CSA(a, b, c) + CSA(CSA(a, b, c), d)
31+
-- a + b + c + d = CSA(CSA(a, b, c), d)
2732
theorem carrySaveAdder4Input (w : ℕ) (a b c d : BitVec w) :
2833
let ⟨s1, t1⟩ := carrySave w a b c
2934
let ⟨s2, t2⟩ := carrySave w s1 (t1 <<< 1) d
3035
a + b + c + d = s2 + t2 <<< 1 := by
3136
simp [carrySave]
3237
bv_automata_classic
3338

34-
-- a*b = (a[0] * b) + (2 * a[1] * b) + (4 * a[2] * b) + (8 * a[3] * b)
39+
-- CSA(CSA(p1, p2, p3), p4), with p1, p2, p3, p4 being the partial products of a 4-bit multiplication.
3540
def mul4 (a b : BitVec 4) : BitVec 4 :=
3641
-- partial products
3742
let p0 : BitVec 4 := (BitVec.ofBool a[0]).zeroExtend 4 * b
@@ -45,6 +50,7 @@ def mul4 (a b : BitVec 4) : BitVec 4 :=
4550

4651
#eval mul4 4 3
4752

53+
-- a*b = (a[0] * b) + (2 * a[1] * b) + (4 * a[2] * b) + (8 * a[3] * b)
4854
@[simp]
4955
theorem mul4_partial_products (a b : BitVec 4) :
5056
let p0 : BitVec 4 := (BitVec.ofBool a[0]).zeroExtend 4 * b
@@ -54,30 +60,10 @@ theorem mul4_partial_products (a b : BitVec 4) :
5460
a * b = p0 + p1 + p2 + p3 := by
5561
bv_decide
5662

63+
-- a * b = CSA(CSA(p0, p1, p2), p3)
5764
theorem mul4_correct (a b : BitVec 4) : a * b = mul4 a b := by
5865
rw [mul4_partial_products]
5966
simp only [mul4, carrySave]
6067
bv_decide
6168

62-
def csaChain (a : Fin (n + 2) → BitVec w)
63-
: (i : ℕ) → (i ≤ n) → BitVec w × BitVec w
64-
| 0, _ => (a ⟨0, by omega⟩, a ⟨1, by omega⟩)
65-
| i + 1, h =>
66-
let (sum_prev, carry_prev) := csaChain a i (by omega)
67-
let ⟨s, t⟩ := carrySave w sum_prev carry_prev (a ⟨i + 2, by omega⟩)
68-
(s, t <<< 1)
69-
70-
def finSum (a : Fin n → BitVec w) : BitVec w :=
71-
Finset.univ.sum a
72-
73-
theorem csaChain_correct (a : Fin (n + 2) → BitVec w)
74-
(h : n > 0) :
75-
let (carry, sum) := csaChain a n (le_refl n)
76-
carry + sum = finSum a := by
77-
induction n with
78-
| zero => omega
79-
| succ k ih =>
80-
sorry
81-
82-
8369
end CSA

0 commit comments

Comments
 (0)